1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: posix thread system implementation
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 /* The GMutex, GCond and GPrivate implementations in this file are some
31 * of the lowest-level code in GLib. All other parts of GLib (messages,
32 * memory, slices, etc) assume that they can freely use these facilities
33 * without risking recursion.
35 * As such, these functions are NOT permitted to call any other part of
38 * The thread manipulation functions (create, exit, join, etc.) have
39 * more freedom -- they can do as they please.
46 #include "gthreadprivate.h"
48 #include "gmessages.h"
49 #include "gstrfuncs.h"
57 #ifdef HAVE_SYS_TIME_H
58 # include <sys/time.h>
66 #ifdef HAVE_SYS_PRCTL_H
67 #include <sys/prctl.h>
71 g_thread_abort (gint status,
72 const gchar *function)
74 fprintf (stderr, "GLib (gthread-posix.c): Unexpected error from C library during '%s': %s. Aborting.\n",
75 strerror (status), function);
81 static pthread_mutex_t *
82 g_mutex_impl_new (void)
84 pthread_mutexattr_t *pattr = NULL;
85 pthread_mutex_t *mutex;
88 mutex = malloc (sizeof (pthread_mutex_t));
89 if G_UNLIKELY (mutex == NULL)
90 g_thread_abort (errno, "malloc");
92 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
93 pthread_mutexattr_t attr;
94 pthread_mutexattr_init (&attr);
95 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
99 if G_UNLIKELY ((status = pthread_mutex_init (mutex, pattr)) != 0)
100 g_thread_abort (status, "pthread_mutex_init");
102 #ifdef PTHREAD_ADAPTIVE_MUTEX_NP
103 pthread_mutexattr_destroy (&attr);
110 g_mutex_impl_free (pthread_mutex_t *mutex)
112 pthread_mutex_destroy (mutex);
116 static pthread_mutex_t *
117 g_mutex_get_impl (GMutex *mutex)
119 pthread_mutex_t *impl = mutex->p;
121 if G_UNLIKELY (impl == NULL)
123 impl = g_mutex_impl_new ();
124 if (!g_atomic_pointer_compare_and_exchange (&mutex->p, NULL, impl))
125 g_mutex_impl_free (impl);
135 * @mutex: an uninitialized #GMutex
137 * Initializes a #GMutex so that it can be used.
139 * This function is useful to initialize a mutex that has been
140 * allocated on the stack, or as part of a larger structure.
141 * It is not necessary to initialize a mutex that has been
142 * created that has been statically allocated.
152 * b = g_new (Blob, 1);
153 * g_mutex_init (&b->m);
156 * To undo the effect of g_mutex_init() when a mutex is no longer
157 * needed, use g_mutex_clear().
159 * Calling g_mutex_init() on an already initialized #GMutex leads
160 * to undefined behaviour.
165 g_mutex_init (GMutex *mutex)
167 mutex->p = g_mutex_impl_new ();
172 * @mutex: an initialized #GMutex
174 * Frees the resources allocated to a mutex with g_mutex_init().
176 * This function should not be used with a #GMutex that has been
177 * statically allocated.
179 * Calling g_mutex_clear() on a locked mutex leads to undefined
185 g_mutex_clear (GMutex *mutex)
187 g_mutex_impl_free (mutex->p);
194 * Locks @mutex. If @mutex is already locked by another thread, the
195 * current thread will block until @mutex is unlocked by the other
198 * This function can be used even if g_thread_init() has not yet been
199 * called, and, in that case, will do nothing.
201 * <note>#GMutex is neither guaranteed to be recursive nor to be
202 * non-recursive. As such, calling g_mutex_lock() on a #GMutex that has
203 * already been locked by the same thread results in undefined behaviour
204 * (including but not limited to deadlocks).</note>
207 g_mutex_lock (GMutex *mutex)
211 if G_UNLIKELY ((status = pthread_mutex_lock (g_mutex_get_impl (mutex))) != 0)
212 g_thread_abort (status, "pthread_mutex_lock");
219 * Unlocks @mutex. If another thread is blocked in a g_mutex_lock()
220 * call for @mutex, it will become unblocked and can lock @mutex itself.
222 * Calling g_mutex_unlock() on a mutex that is not locked by the
223 * current thread leads to undefined behaviour.
225 * This function can be used even if g_thread_init() has not yet been
226 * called, and, in that case, will do nothing.
229 g_mutex_unlock (GMutex *mutex)
233 if G_UNLIKELY ((status = pthread_mutex_unlock (g_mutex_get_impl (mutex))) != 0)
234 g_thread_abort (status, "pthread_mutex_lock");
241 * Tries to lock @mutex. If @mutex is already locked by another thread,
242 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
245 * This function can be used even if g_thread_init() has not yet been
246 * called, and, in that case, will immediately return %TRUE.
248 * <note>#GMutex is neither guaranteed to be recursive nor to be
249 * non-recursive. As such, calling g_mutex_lock() on a #GMutex that has
250 * already been locked by the same thread results in undefined behaviour
251 * (including but not limited to deadlocks or arbitrary return values).
254 * Returns: %TRUE if @mutex could be locked
257 g_mutex_trylock (GMutex *mutex)
261 if G_LIKELY ((status = pthread_mutex_trylock (g_mutex_get_impl (mutex))) == 0)
264 if G_UNLIKELY (status != EBUSY)
265 g_thread_abort (status, "pthread_mutex_trylock");
272 static pthread_mutex_t *
273 g_rec_mutex_impl_new (void)
275 pthread_mutexattr_t attr;
276 pthread_mutex_t *mutex;
278 mutex = g_slice_new (pthread_mutex_t);
279 pthread_mutexattr_init (&attr);
280 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
281 pthread_mutex_init (mutex, &attr);
282 pthread_mutexattr_destroy (&attr);
288 g_rec_mutex_impl_free (pthread_mutex_t *mutex)
290 pthread_mutex_destroy (mutex);
291 g_slice_free (pthread_mutex_t, mutex);
294 static pthread_mutex_t *
295 g_rec_mutex_get_impl (GRecMutex *rec_mutex)
297 pthread_mutex_t *impl = rec_mutex->p;
299 if G_UNLIKELY (impl == NULL)
301 impl = g_rec_mutex_impl_new ();
302 if (!g_atomic_pointer_compare_and_exchange (&rec_mutex->p, NULL, impl))
303 g_rec_mutex_impl_free (impl);
312 * @rec_mutex: an uninitialized #GRecMutex
314 * Initializes a #GRecMutex so that it can be used.
316 * This function is useful to initialize a recursive mutex
317 * that has been allocated on the stack, or as part of a larger
320 * It is not necessary to initialise a recursive mutex that has been
321 * statically allocated.
331 * b = g_new (Blob, 1);
332 * g_rec_mutex_init (&b->m);
335 * Calling g_rec_mutex_init() on an already initialized #GRecMutex
336 * leads to undefined behaviour.
338 * To undo the effect of g_rec_mutex_init() when a recursive mutex
339 * is no longer needed, use g_rec_mutex_clear().
344 g_rec_mutex_init (GRecMutex *rec_mutex)
346 rec_mutex->p = g_rec_mutex_impl_new ();
351 * @rec_mutex: an initialized #GRecMutex
353 * Frees the resources allocated to a recursive mutex with
354 * g_rec_mutex_init().
356 * This function should not be used with a #GRecMutex that has been
357 * statically allocated.
359 * Calling g_rec_mutex_clear() on a locked recursive mutex leads
360 * to undefined behaviour.
365 g_rec_mutex_clear (GRecMutex *rec_mutex)
367 g_rec_mutex_impl_free (rec_mutex->p);
372 * @rec_mutex: a #GRecMutex
374 * Locks @rec_mutex. If @rec_mutex is already locked by another
375 * thread, the current thread will block until @rec_mutex is
376 * unlocked by the other thread. If @rec_mutex is already locked
377 * by the current thread, the 'lock count' of @rec_mutex is increased.
378 * The mutex will only become available again when it is unlocked
379 * as many times as it has been locked.
384 g_rec_mutex_lock (GRecMutex *mutex)
386 pthread_mutex_lock (g_rec_mutex_get_impl (mutex));
390 * g_rec_mutex_unlock:
391 * @rec_mutex: a #RecGMutex
393 * Unlocks @rec_mutex. If another thread is blocked in a
394 * g_rec_mutex_lock() call for @rec_mutex, it will become unblocked
395 * and can lock @rec_mutex itself.
397 * Calling g_rec_mutex_unlock() on a recursive mutex that is not
398 * locked by the current thread leads to undefined behaviour.
403 g_rec_mutex_unlock (GRecMutex *rec_mutex)
405 pthread_mutex_unlock (rec_mutex->p);
409 * g_rec_mutex_trylock:
410 * @rec_mutex: a #GRecMutex
412 * Tries to lock @rec_mutex. If @rec_mutex is already locked
413 * by another thread, it immediately returns %FALSE. Otherwise
414 * it locks @rec_mutex and returns %TRUE.
416 * Returns: %TRUE if @rec_mutex could be locked
421 g_rec_mutex_trylock (GRecMutex *rec_mutex)
423 if (pthread_mutex_trylock (g_rec_mutex_get_impl (rec_mutex)) != 0)
431 static pthread_rwlock_t *
432 g_rw_lock_impl_new (void)
434 pthread_rwlock_t *rwlock;
437 rwlock = malloc (sizeof (pthread_rwlock_t));
438 if G_UNLIKELY (rwlock == NULL)
439 g_thread_abort (errno, "malloc");
441 if G_UNLIKELY ((status = pthread_rwlock_init (rwlock, NULL)) != 0)
442 g_thread_abort (status, "pthread_rwlock_init");
448 g_rw_lock_impl_free (pthread_rwlock_t *rwlock)
450 pthread_rwlock_destroy (rwlock);
454 static pthread_rwlock_t *
455 g_rw_lock_get_impl (GRWLock *lock)
457 pthread_rwlock_t *impl = lock->p;
459 if G_UNLIKELY (impl == NULL)
461 impl = g_rw_lock_impl_new ();
462 if (!g_atomic_pointer_compare_and_exchange (&lock->p, NULL, impl))
463 g_rw_lock_impl_free (impl);
472 * @rw_lock: an uninitialized #GRWLock
474 * Initializes a #GRWLock so that it can be used.
476 * This function is useful to initialize a lock that has been
477 * allocated on the stack, or as part of a larger structure. It is not
478 * necessary to initialise a reader-writer lock that has been statically
489 * b = g_new (Blob, 1);
490 * g_rw_lock_init (&b->l);
493 * To undo the effect of g_rw_lock_init() when a lock is no longer
494 * needed, use g_rw_lock_clear().
496 * Calling g_rw_lock_init() on an already initialized #GRWLock leads
497 * to undefined behaviour.
502 g_rw_lock_init (GRWLock *rw_lock)
504 rw_lock->p = g_rw_lock_impl_new ();
509 * @rw_lock: an initialized #GRWLock
511 * Frees the resources allocated to a lock with g_rw_lock_init().
513 * This function should not be used with a #GRWLock that has been
514 * statically allocated.
516 * Calling g_rw_lock_clear() when any thread holds the lock
517 * leads to undefined behaviour.
522 g_rw_lock_clear (GRWLock *rw_lock)
524 g_rw_lock_impl_free (rw_lock->p);
528 * g_rw_lock_writer_lock:
529 * @rw_lock: a #GRWLock
531 * Obtain a write lock on @rw_lock. If any thread already holds
532 * a read or write lock on @rw_lock, the current thread will block
533 * until all other threads have dropped their locks on @rw_lock.
538 g_rw_lock_writer_lock (GRWLock *rw_lock)
540 pthread_rwlock_wrlock (g_rw_lock_get_impl (rw_lock));
544 * g_rw_lock_writer_trylock:
545 * @rw_lock: a #GRWLock
547 * Tries to obtain a write lock on @rw_lock. If any other thread holds
548 * a read or write lock on @rw_lock, it immediately returns %FALSE.
549 * Otherwise it locks @rw_lock and returns %TRUE.
551 * Returns: %TRUE if @rw_lock could be locked
556 g_rw_lock_writer_trylock (GRWLock *rw_lock)
558 if (pthread_rwlock_trywrlock (g_rw_lock_get_impl (rw_lock)) != 0)
565 * g_rw_lock_writer_unlock:
566 * @rw_lock: a #GRWLock
568 * Release a write lock on @rw_lock.
570 * Calling g_rw_lock_writer_unlock() on a lock that is not held
571 * by the current thread leads to undefined behaviour.
576 g_rw_lock_writer_unlock (GRWLock *rw_lock)
578 pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
582 * g_rw_lock_reader_lock:
583 * @rw_lock: a #GRWLock
585 * Obtain a read lock on @rw_lock. If another thread currently holds
586 * the write lock on @rw_lock or blocks waiting for it, the current
587 * thread will block. Read locks can be taken recursively.
589 * It is implementation-defined how many threads are allowed to
590 * hold read locks on the same lock simultaneously.
595 g_rw_lock_reader_lock (GRWLock *rw_lock)
597 pthread_rwlock_rdlock (g_rw_lock_get_impl (rw_lock));
601 * g_rw_lock_reader_trylock:
602 * @rw_lock: a #GRWLock
604 * Tries to obtain a read lock on @rw_lock and returns %TRUE if
605 * the read lock was successfully obtained. Otherwise it
608 * Returns: %TRUE if @rw_lock could be locked
613 g_rw_lock_reader_trylock (GRWLock *rw_lock)
615 if (pthread_rwlock_tryrdlock (g_rw_lock_get_impl (rw_lock)) != 0)
622 * g_rw_lock_reader_unlock:
623 * @rw_lock: a #GRWLock
625 * Release a read lock on @rw_lock.
627 * Calling g_rw_lock_reader_unlock() on a lock that is not held
628 * by the current thread leads to undefined behaviour.
633 g_rw_lock_reader_unlock (GRWLock *rw_lock)
635 pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
640 static pthread_cond_t *
641 g_cond_impl_new (void)
643 pthread_cond_t *cond;
646 cond = malloc (sizeof (pthread_cond_t));
647 if G_UNLIKELY (cond == NULL)
648 g_thread_abort (errno, "malloc");
650 if G_UNLIKELY ((status = pthread_cond_init (cond, NULL)) != 0)
651 g_thread_abort (status, "pthread_cond_init");
657 g_cond_impl_free (pthread_cond_t *cond)
659 pthread_cond_destroy (cond);
663 static pthread_cond_t *
664 g_cond_get_impl (GCond *cond)
666 pthread_cond_t *impl = cond->p;
668 if G_UNLIKELY (impl == NULL)
670 impl = g_cond_impl_new ();
671 if (!g_atomic_pointer_compare_and_exchange (&cond->p, NULL, impl))
672 g_cond_impl_free (impl);
681 * @cond: an uninitialized #GCond
683 * Initialized a #GCond so that it can be used.
685 * This function is useful to initialize a #GCond that has been
686 * allocated on the stack, or as part of a larger structure.
687 * It is not necessary to initialize a #GCond that has been
688 * statically allocated.
690 * To undo the effect of g_cond_init() when a #GCond is no longer
691 * needed, use g_cond_clear().
693 * Calling g_cond_init() on an already initialized #GCond leads
694 * to undefined behaviour.
699 g_cond_init (GCond *cond)
701 cond->p = g_cond_impl_new ();
706 * @cond: an initialized #GCond
708 * Frees the resources allocated to a #GCond with g_cond_init().
710 * This function should not be used with a #GCond that has been
711 * statically allocated.
713 * Calling g_cond_clear() for a #GCond on which threads are
714 * blocking leads to undefined behaviour.
719 g_cond_clear (GCond *cond)
721 g_cond_impl_free (cond->p);
727 * @mutex: a #GMutex that is currently locked
729 * Waits until this thread is woken up on @cond. The @mutex is unlocked
730 * before falling asleep and locked again before resuming.
732 * This function can be used even if g_thread_init() has not yet been
733 * called, and, in that case, will immediately return.
736 g_cond_wait (GCond *cond,
741 if G_UNLIKELY ((status = pthread_cond_wait (g_cond_get_impl (cond), g_mutex_get_impl (mutex))) != 0)
742 g_thread_abort (status, "pthread_cond_wait");
749 * If threads are waiting for @cond, at least one of them is unblocked.
750 * If no threads are waiting for @cond, this function has no effect.
751 * It is good practice to hold the same lock as the waiting thread
752 * while calling this function, though not required.
754 * This function can be used even if g_thread_init() has not yet been
755 * called, and, in that case, will do nothing.
758 g_cond_signal (GCond *cond)
762 if G_UNLIKELY ((status = pthread_cond_signal (g_cond_get_impl (cond))) != 0)
763 g_thread_abort (status, "pthread_cond_signal");
770 * If threads are waiting for @cond, all of them are unblocked.
771 * If no threads are waiting for @cond, this function has no effect.
772 * It is good practice to lock the same mutex as the waiting threads
773 * while calling this function, though not required.
775 * This function can be used even if g_thread_init() has not yet been
776 * called, and, in that case, will do nothing.
779 g_cond_broadcast (GCond *cond)
783 if G_UNLIKELY ((status = pthread_cond_broadcast (g_cond_get_impl (cond))) != 0)
784 g_thread_abort (status, "pthread_cond_broadcast");
790 * @mutex: a #GMutex that is currently locked
791 * @abs_time: a #GTimeVal, determining the final time
793 * Waits until this thread is woken up on @cond, but not longer than
794 * until the time specified by @abs_time. The @mutex is unlocked before
795 * falling asleep and locked again before resuming.
797 * If @abs_time is %NULL, g_cond_timed_wait() acts like g_cond_wait().
799 * This function can be used even if g_thread_init() has not yet been
800 * called, and, in that case, will immediately return %TRUE.
802 * To easily calculate @abs_time a combination of g_get_current_time()
803 * and g_time_val_add() can be used.
805 * Returns: %TRUE if @cond was signalled, or %FALSE on timeout
808 g_cond_timed_wait (GCond *cond,
812 struct timespec end_time;
815 if (abs_time == NULL)
817 g_cond_wait (cond, mutex);
821 end_time.tv_sec = abs_time->tv_sec;
822 end_time.tv_nsec = abs_time->tv_usec * 1000;
824 if ((status = pthread_cond_timedwait (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &end_time)) == 0)
827 if G_UNLIKELY (status != ETIMEDOUT)
828 g_thread_abort (status, "pthread_cond_timedwait");
836 * @mutex: a #GMutex that is currently locked
837 * @abs_time: the final time, in microseconds
839 * A variant of g_cond_timed_wait() that takes @abs_time
840 * as a #gint64 instead of a #GTimeVal.
841 * See g_cond_timed_wait() for details.
843 * Returns: %TRUE if @cond was signalled, or %FALSE on timeout
848 g_cond_timedwait (GCond *cond,
852 struct timespec end_time;
855 end_time.tv_sec = abs_time / 1000000;
856 end_time.tv_nsec = (abs_time % 1000000) * 1000;
858 if ((status = pthread_cond_timedwait (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &end_time)) == 0)
861 if G_UNLIKELY (status != ETIMEDOUT)
862 g_thread_abort (status, "pthread_cond_timedwait");
872 * The #GPrivate struct is an opaque data structure to represent a
873 * thread-local data key. It is approximately equivalent to the
874 * pthread_setspecific()/pthread_getspecific() APIs on POSIX and to
875 * TlsSetValue()/TlsGetValue() on Windows.
877 * If you don't already know why you might want this functionality,
878 * then you probably don't need it.
880 * #GPrivate is a very limited resource (as far as 128 per program,
881 * shared between all libraries). It is also not possible to destroy a
882 * #GPrivate after it has been used. As such, it is only ever acceptable
883 * to use #GPrivate in static scope, and even then sparingly so.
885 * See G_PRIVATE_INIT() for a couple of examples.
887 * The #GPrivate structure should be considered opaque. It should only
888 * be accessed via the <function>g_private_</function> functions.
893 * @notify: a #GDestroyNotify
895 * A macro to assist with the static initialisation of a #GPrivate.
897 * This macro is useful for the case that a #GDestroyNotify function
898 * should be associated the key. This is needed when the key will be
899 * used to point at memory that should be deallocated when the thread
902 * Additionally, the #GDestroyNotify will also be called on the previous
903 * value stored in the key when g_private_replace() is used.
905 * If no #GDestroyNotify is needed, then use of this macro is not
906 * required -- if the #GPrivate is declared in static scope then it will
907 * be properly initialised by default (ie: to all zeros). See the
911 * static GPrivate name_key = G_PRIVATE_INIT (g_free);
913 * // return value should not be freed
915 * get_local_name (void)
917 * return g_private_get (&name_key);
921 * set_local_name (const gchar *name)
923 * g_private_replace (&name_key, g_strdup (name));
927 * static GPrivate count_key; // no free function
930 * get_local_count (void)
932 * return GPOINTER_TO_INT (g_private_get (&count_key));
936 * set_local_count (gint count)
938 * g_private_set (&count_key, GINT_TO_POINTER (count));
945 static pthread_key_t *
946 g_private_impl_new (GDestroyNotify notify)
951 key = malloc (sizeof (pthread_key_t));
952 if G_UNLIKELY (key == NULL)
953 g_thread_abort (errno, "malloc");
954 status = pthread_key_create (key, notify);
955 if G_UNLIKELY (status != 0)
956 g_thread_abort (status, "pthread_key_create");
962 g_private_impl_free (pthread_key_t *key)
966 status = pthread_key_delete (*key);
967 if G_UNLIKELY (status != 0)
968 g_thread_abort (status, "pthread_key_delete");
972 static pthread_key_t *
973 g_private_get_impl (GPrivate *key)
975 pthread_key_t *impl = key->p;
977 if G_UNLIKELY (impl == NULL)
979 impl = g_private_impl_new (key->notify);
980 if (!g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
982 g_private_impl_free (impl);
994 * Returns the current value of the thread local variable @key.
996 * If the value has not yet been set in this thread, %NULL is returned.
997 * Values are never copied between threads (when a new thread is
998 * created, for example).
1000 * Returns: the thread-local value
1003 g_private_get (GPrivate *key)
1005 /* quote POSIX: No errors are returned from pthread_getspecific(). */
1006 return pthread_getspecific (*g_private_get_impl (key));
1012 * @value: the new value
1014 * Sets the thread local variable @key to have the value @value in the
1017 * This function differs from g_private_replace() in the following way:
1018 * the #GDestroyNotify for @key is not called on the old value.
1021 g_private_set (GPrivate *key,
1026 if G_UNLIKELY ((status = pthread_setspecific (*g_private_get_impl (key), value)) != 0)
1027 g_thread_abort (status, "pthread_setspecific");
1031 * g_private_replace:
1033 * @value: the new value
1035 * Sets the thread local variable @key to have the value @value in the
1038 * This function differs from g_private_set() in the following way: if
1039 * the previous value was non-%NULL then the #GDestroyNotify handler for
1040 * @key is run on it.
1045 g_private_replace (GPrivate *key,
1048 pthread_key_t *impl = g_private_get_impl (key);
1052 old = pthread_getspecific (*impl);
1053 if (old && key->notify)
1056 if G_UNLIKELY ((status = pthread_setspecific (*impl, value)) != 0)
1057 g_thread_abort (status, "pthread_setspecific");
1062 #define posix_check_err(err, name) G_STMT_START{ \
1063 int error = (err); \
1065 g_error ("file %s: line %d (%s): error '%s' during '%s'", \
1066 __FILE__, __LINE__, G_STRFUNC, \
1067 g_strerror (error), name); \
1070 #define posix_check_cmd(cmd) posix_check_err (cmd, #cmd)
1076 pthread_t system_thread;
1082 g_system_thread_free (GRealThread *thread)
1084 GThreadPosix *pt = (GThreadPosix *) thread;
1087 pthread_detach (pt->system_thread);
1089 g_mutex_clear (&pt->lock);
1091 g_slice_free (GThreadPosix, pt);
1095 g_system_thread_new (GThreadFunc thread_func,
1099 GThreadPosix *thread;
1100 pthread_attr_t attr;
1103 thread = g_slice_new0 (GThreadPosix);
1105 posix_check_cmd (pthread_attr_init (&attr));
1107 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
1110 #ifdef _SC_THREAD_STACK_MIN
1111 stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), stack_size);
1112 #endif /* _SC_THREAD_STACK_MIN */
1113 /* No error check here, because some systems can't do it and
1114 * we simply don't want threads to fail because of that. */
1115 pthread_attr_setstacksize (&attr, stack_size);
1117 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
1119 ret = pthread_create (&thread->system_thread, &attr, (void* (*)(void*))thread_func, thread);
1121 posix_check_cmd (pthread_attr_destroy (&attr));
1125 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
1126 "Error creating thread: %s", g_strerror (ret));
1127 g_slice_free (GThreadPosix, thread);
1131 posix_check_err (ret, "pthread_create");
1133 g_mutex_init (&thread->lock);
1135 return (GRealThread *) thread;
1141 * Causes the calling thread to voluntarily relinquish the CPU, so
1142 * that other threads can run.
1144 * This function is often used as a method to make busy wait less evil.
1147 g_thread_yield (void)
1153 g_system_thread_wait (GRealThread *thread)
1155 GThreadPosix *pt = (GThreadPosix *) thread;
1157 g_mutex_lock (&pt->lock);
1161 posix_check_cmd (pthread_join (pt->system_thread, NULL));
1165 g_mutex_unlock (&pt->lock);
1169 g_system_thread_exit (void)
1171 pthread_exit (NULL);
1175 g_system_thread_set_name (const gchar *name)
1177 #ifdef HAVE_SYS_PRCTL_H
1178 prctl (PR_SET_NAME, name, 0, 0, 0, 0);
1183 /* vim:set foldmethod=marker: */