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>
74 g_thread_abort (gint status,
75 const gchar *function)
77 fprintf (stderr, "GLib (gthread-posix.c): Unexpected error from C library during '%s': %s. Aborting.\n",
78 function, strerror (status));
84 static pthread_mutex_t *
85 g_mutex_impl_new (void)
87 pthread_mutexattr_t *pattr = NULL;
88 pthread_mutex_t *mutex;
91 mutex = malloc (sizeof (pthread_mutex_t));
92 if G_UNLIKELY (mutex == NULL)
93 g_thread_abort (errno, "malloc");
95 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
97 pthread_mutexattr_t attr;
98 pthread_mutexattr_init (&attr);
99 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
104 if G_UNLIKELY ((status = pthread_mutex_init (mutex, pattr)) != 0)
105 g_thread_abort (status, "pthread_mutex_init");
107 #ifdef PTHREAD_ADAPTIVE_MUTEX_NP
108 pthread_mutexattr_destroy (&attr);
115 g_mutex_impl_free (pthread_mutex_t *mutex)
117 pthread_mutex_destroy (mutex);
121 static pthread_mutex_t *
122 g_mutex_get_impl (GMutex *mutex)
124 pthread_mutex_t *impl = g_atomic_pointer_get (&mutex->p);
126 if G_UNLIKELY (impl == NULL)
128 impl = g_mutex_impl_new ();
129 if (!g_atomic_pointer_compare_and_exchange (&mutex->p, NULL, impl))
130 g_mutex_impl_free (impl);
140 * @mutex: an uninitialized #GMutex
142 * Initializes a #GMutex so that it can be used.
144 * This function is useful to initialize a mutex that has been
145 * allocated on the stack, or as part of a larger structure.
146 * It is not necessary to initialize a mutex that has been
147 * statically allocated.
157 * b = g_new (Blob, 1);
158 * g_mutex_init (&b->m);
161 * To undo the effect of g_mutex_init() when a mutex is no longer
162 * needed, use g_mutex_clear().
164 * Calling g_mutex_init() on an already initialized #GMutex leads
165 * to undefined behaviour.
170 g_mutex_init (GMutex *mutex)
172 mutex->p = g_mutex_impl_new ();
177 * @mutex: an initialized #GMutex
179 * Frees the resources allocated to a mutex with g_mutex_init().
181 * This function should not be used with a #GMutex that has been
182 * statically allocated.
184 * Calling g_mutex_clear() on a locked mutex leads to undefined
190 g_mutex_clear (GMutex *mutex)
192 g_mutex_impl_free (mutex->p);
200 * Locks @mutex. If @mutex is already locked by another thread, the
201 * current thread will block until @mutex is unlocked by the other
204 * <note>#GMutex is neither guaranteed to be recursive nor to be
205 * non-recursive. As such, calling g_mutex_lock() on a #GMutex that has
206 * already been locked by the same thread results in undefined behaviour
207 * (including but not limited to deadlocks).</note>
210 g_mutex_lock (GMutex *mutex)
214 if G_UNLIKELY ((status = pthread_mutex_lock (g_mutex_get_impl (mutex))) != 0)
215 g_thread_abort (status, "pthread_mutex_lock");
222 * Unlocks @mutex. If another thread is blocked in a g_mutex_lock()
223 * call for @mutex, it will become unblocked and can lock @mutex itself.
225 * Calling g_mutex_unlock() on a mutex that is not locked by the
226 * current thread leads to undefined behaviour.
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_unlock");
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 * <note>#GMutex is neither guaranteed to be recursive nor to be
246 * non-recursive. As such, calling g_mutex_lock() on a #GMutex that has
247 * already been locked by the same thread results in undefined behaviour
248 * (including but not limited to deadlocks or arbitrary return values).
251 * Returns: %TRUE if @mutex could be locked
254 g_mutex_trylock (GMutex *mutex)
258 if G_LIKELY ((status = pthread_mutex_trylock (g_mutex_get_impl (mutex))) == 0)
261 if G_UNLIKELY (status != EBUSY)
262 g_thread_abort (status, "pthread_mutex_trylock");
269 static pthread_mutex_t *
270 g_rec_mutex_impl_new (void)
272 pthread_mutexattr_t attr;
273 pthread_mutex_t *mutex;
275 mutex = g_slice_new (pthread_mutex_t);
276 pthread_mutexattr_init (&attr);
277 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
278 pthread_mutex_init (mutex, &attr);
279 pthread_mutexattr_destroy (&attr);
285 g_rec_mutex_impl_free (pthread_mutex_t *mutex)
287 pthread_mutex_destroy (mutex);
288 g_slice_free (pthread_mutex_t, mutex);
291 static pthread_mutex_t *
292 g_rec_mutex_get_impl (GRecMutex *rec_mutex)
294 pthread_mutex_t *impl = g_atomic_pointer_get (&rec_mutex->p);
296 if G_UNLIKELY (impl == NULL)
298 impl = g_rec_mutex_impl_new ();
299 if (!g_atomic_pointer_compare_and_exchange (&rec_mutex->p, NULL, impl))
300 g_rec_mutex_impl_free (impl);
309 * @rec_mutex: an uninitialized #GRecMutex
311 * Initializes a #GRecMutex so that it can be used.
313 * This function is useful to initialize a recursive mutex
314 * that has been allocated on the stack, or as part of a larger
317 * It is not necessary to initialise a recursive mutex that has been
318 * statically allocated.
328 * b = g_new (Blob, 1);
329 * g_rec_mutex_init (&b->m);
332 * Calling g_rec_mutex_init() on an already initialized #GRecMutex
333 * leads to undefined behaviour.
335 * To undo the effect of g_rec_mutex_init() when a recursive mutex
336 * is no longer needed, use g_rec_mutex_clear().
341 g_rec_mutex_init (GRecMutex *rec_mutex)
343 rec_mutex->p = g_rec_mutex_impl_new ();
348 * @rec_mutex: an initialized #GRecMutex
350 * Frees the resources allocated to a recursive mutex with
351 * g_rec_mutex_init().
353 * This function should not be used with a #GRecMutex that has been
354 * statically allocated.
356 * Calling g_rec_mutex_clear() on a locked recursive mutex leads
357 * to undefined behaviour.
362 g_rec_mutex_clear (GRecMutex *rec_mutex)
364 g_rec_mutex_impl_free (rec_mutex->p);
370 * @rec_mutex: a #GRecMutex
372 * Locks @rec_mutex. If @rec_mutex is already locked by another
373 * thread, the current thread will block until @rec_mutex is
374 * unlocked by the other thread. If @rec_mutex is already locked
375 * by the current thread, the 'lock count' of @rec_mutex is increased.
376 * The mutex will only become available again when it is unlocked
377 * as many times as it has been locked.
382 g_rec_mutex_lock (GRecMutex *mutex)
384 pthread_mutex_lock (g_rec_mutex_get_impl (mutex));
388 * g_rec_mutex_unlock:
389 * @rec_mutex: a #GRecMutex
391 * Unlocks @rec_mutex. If another thread is blocked in a
392 * g_rec_mutex_lock() call for @rec_mutex, it will become unblocked
393 * and can lock @rec_mutex itself.
395 * Calling g_rec_mutex_unlock() on a recursive mutex that is not
396 * locked by the current thread leads to undefined behaviour.
401 g_rec_mutex_unlock (GRecMutex *rec_mutex)
403 pthread_mutex_unlock (rec_mutex->p);
407 * g_rec_mutex_trylock:
408 * @rec_mutex: a #GRecMutex
410 * Tries to lock @rec_mutex. If @rec_mutex is already locked
411 * by another thread, it immediately returns %FALSE. Otherwise
412 * it locks @rec_mutex and returns %TRUE.
414 * Returns: %TRUE if @rec_mutex could be locked
419 g_rec_mutex_trylock (GRecMutex *rec_mutex)
421 if (pthread_mutex_trylock (g_rec_mutex_get_impl (rec_mutex)) != 0)
429 static pthread_rwlock_t *
430 g_rw_lock_impl_new (void)
432 pthread_rwlock_t *rwlock;
435 rwlock = malloc (sizeof (pthread_rwlock_t));
436 if G_UNLIKELY (rwlock == NULL)
437 g_thread_abort (errno, "malloc");
439 if G_UNLIKELY ((status = pthread_rwlock_init (rwlock, NULL)) != 0)
440 g_thread_abort (status, "pthread_rwlock_init");
446 g_rw_lock_impl_free (pthread_rwlock_t *rwlock)
448 pthread_rwlock_destroy (rwlock);
452 static pthread_rwlock_t *
453 g_rw_lock_get_impl (GRWLock *lock)
455 pthread_rwlock_t *impl = g_atomic_pointer_get (&lock->p);
457 if G_UNLIKELY (impl == NULL)
459 impl = g_rw_lock_impl_new ();
460 if (!g_atomic_pointer_compare_and_exchange (&lock->p, NULL, impl))
461 g_rw_lock_impl_free (impl);
470 * @rw_lock: an uninitialized #GRWLock
472 * Initializes a #GRWLock so that it can be used.
474 * This function is useful to initialize a lock that has been
475 * allocated on the stack, or as part of a larger structure. It is not
476 * necessary to initialise a reader-writer lock that has been statically
487 * b = g_new (Blob, 1);
488 * g_rw_lock_init (&b->l);
491 * To undo the effect of g_rw_lock_init() when a lock is no longer
492 * needed, use g_rw_lock_clear().
494 * Calling g_rw_lock_init() on an already initialized #GRWLock leads
495 * to undefined behaviour.
500 g_rw_lock_init (GRWLock *rw_lock)
502 rw_lock->p = g_rw_lock_impl_new ();
507 * @rw_lock: an initialized #GRWLock
509 * Frees the resources allocated to a lock with g_rw_lock_init().
511 * This function should not be used with a #GRWLock that has been
512 * statically allocated.
514 * Calling g_rw_lock_clear() when any thread holds the lock
515 * leads to undefined behaviour.
520 g_rw_lock_clear (GRWLock *rw_lock)
522 g_rw_lock_impl_free (rw_lock->p);
527 * g_rw_lock_writer_lock:
528 * @rw_lock: a #GRWLock
530 * Obtain a write lock on @rw_lock. If any thread already holds
531 * a read or write lock on @rw_lock, the current thread will block
532 * until all other threads have dropped their locks on @rw_lock.
537 g_rw_lock_writer_lock (GRWLock *rw_lock)
539 pthread_rwlock_wrlock (g_rw_lock_get_impl (rw_lock));
543 * g_rw_lock_writer_trylock:
544 * @rw_lock: a #GRWLock
546 * Tries to obtain a write lock on @rw_lock. If any other thread holds
547 * a read or write lock on @rw_lock, it immediately returns %FALSE.
548 * Otherwise it locks @rw_lock and returns %TRUE.
550 * Returns: %TRUE if @rw_lock could be locked
555 g_rw_lock_writer_trylock (GRWLock *rw_lock)
557 if (pthread_rwlock_trywrlock (g_rw_lock_get_impl (rw_lock)) != 0)
564 * g_rw_lock_writer_unlock:
565 * @rw_lock: a #GRWLock
567 * Release a write lock on @rw_lock.
569 * Calling g_rw_lock_writer_unlock() on a lock that is not held
570 * by the current thread leads to undefined behaviour.
575 g_rw_lock_writer_unlock (GRWLock *rw_lock)
577 pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
581 * g_rw_lock_reader_lock:
582 * @rw_lock: a #GRWLock
584 * Obtain a read lock on @rw_lock. If another thread currently holds
585 * the write lock on @rw_lock or blocks waiting for it, the current
586 * thread will block. Read locks can be taken recursively.
588 * It is implementation-defined how many threads are allowed to
589 * hold read locks on the same lock simultaneously.
594 g_rw_lock_reader_lock (GRWLock *rw_lock)
596 pthread_rwlock_rdlock (g_rw_lock_get_impl (rw_lock));
600 * g_rw_lock_reader_trylock:
601 * @rw_lock: a #GRWLock
603 * Tries to obtain a read lock on @rw_lock and returns %TRUE if
604 * the read lock was successfully obtained. Otherwise it
607 * Returns: %TRUE if @rw_lock could be locked
612 g_rw_lock_reader_trylock (GRWLock *rw_lock)
614 if (pthread_rwlock_tryrdlock (g_rw_lock_get_impl (rw_lock)) != 0)
621 * g_rw_lock_reader_unlock:
622 * @rw_lock: a #GRWLock
624 * Release a read lock on @rw_lock.
626 * Calling g_rw_lock_reader_unlock() on a lock that is not held
627 * by the current thread leads to undefined behaviour.
632 g_rw_lock_reader_unlock (GRWLock *rw_lock)
634 pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
639 static pthread_cond_t *
640 g_cond_impl_new (void)
642 pthread_condattr_t attr;
643 pthread_cond_t *cond;
646 pthread_condattr_init (&attr);
647 #if defined (HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined (CLOCK_MONOTONIC)
648 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
651 cond = malloc (sizeof (pthread_cond_t));
652 if G_UNLIKELY (cond == NULL)
653 g_thread_abort (errno, "malloc");
655 if G_UNLIKELY ((status = pthread_cond_init (cond, &attr)) != 0)
656 g_thread_abort (status, "pthread_cond_init");
658 pthread_condattr_destroy (&attr);
664 g_cond_impl_free (pthread_cond_t *cond)
666 pthread_cond_destroy (cond);
670 static pthread_cond_t *
671 g_cond_get_impl (GCond *cond)
673 pthread_cond_t *impl = g_atomic_pointer_get (&cond->p);
675 if G_UNLIKELY (impl == NULL)
677 impl = g_cond_impl_new ();
678 if (!g_atomic_pointer_compare_and_exchange (&cond->p, NULL, impl))
679 g_cond_impl_free (impl);
688 * @cond: an uninitialized #GCond
690 * Initialises a #GCond so that it can be used.
692 * This function is useful to initialise a #GCond that has been
693 * allocated as part of a larger structure. It is not necessary to
694 * initialise a #GCond that has been statically allocated.
696 * To undo the effect of g_cond_init() when a #GCond is no longer
697 * needed, use g_cond_clear().
699 * Calling g_cond_init() on an already-initialised #GCond leads
700 * to undefined behaviour.
705 g_cond_init (GCond *cond)
707 cond->p = g_cond_impl_new ();
712 * @cond: an initialised #GCond
714 * Frees the resources allocated to a #GCond with g_cond_init().
716 * This function should not be used with a #GCond that has been
717 * statically allocated.
719 * Calling g_cond_clear() for a #GCond on which threads are
720 * blocking leads to undefined behaviour.
725 g_cond_clear (GCond *cond)
727 g_cond_impl_free (cond->p);
734 * @mutex: a #GMutex that is currently locked
736 * Atomically releases @mutex and waits until @cond is signalled.
737 * When this function returns, @mutex is locked again and owned by the
740 * When using condition variables, it is possible that a spurious wakeup
741 * may occur (ie: g_cond_wait() returns even though g_cond_signal() was
742 * not called). It's also possible that a stolen wakeup may occur.
743 * This is when g_cond_signal() is called, but another thread acquires
744 * @mutex before this thread and modifies the state of the program in
745 * such a way that when g_cond_wait() is able to return, the expected
746 * condition is no longer met.
748 * For this reason, g_cond_wait() must always be used in a loop. See
749 * the documentation for #GCond for a complete example.
752 g_cond_wait (GCond *cond,
757 if G_UNLIKELY ((status = pthread_cond_wait (g_cond_get_impl (cond), g_mutex_get_impl (mutex))) != 0)
758 g_thread_abort (status, "pthread_cond_wait");
765 * If threads are waiting for @cond, at least one of them is unblocked.
766 * If no threads are waiting for @cond, this function has no effect.
767 * It is good practice to hold the same lock as the waiting thread
768 * while calling this function, though not required.
771 g_cond_signal (GCond *cond)
775 if G_UNLIKELY ((status = pthread_cond_signal (g_cond_get_impl (cond))) != 0)
776 g_thread_abort (status, "pthread_cond_signal");
783 * If threads are waiting for @cond, all of them are unblocked.
784 * If no threads are waiting for @cond, this function has no effect.
785 * It is good practice to lock the same mutex as the waiting threads
786 * while calling this function, though not required.
789 g_cond_broadcast (GCond *cond)
793 if G_UNLIKELY ((status = pthread_cond_broadcast (g_cond_get_impl (cond))) != 0)
794 g_thread_abort (status, "pthread_cond_broadcast");
800 * @mutex: a #GMutex that is currently locked
801 * @end_time: the monotonic time to wait until
803 * Waits until either @cond is signalled or @end_time has passed.
805 * As with g_cond_wait() it is possible that a spurious or stolen wakeup
806 * could occur. For that reason, waiting on a condition variable should
807 * always be in a loop, based on an explicitly-checked predicate.
809 * %TRUE is returned if the condition variable was signalled (or in the
810 * case of a spurious wakeup). %FALSE is returned if @end_time has
813 * The following code shows how to correctly perform a timed wait on a
814 * condition variable (extended the example presented in the
815 * documentation for #GCond):
819 * pop_data_timed (void)
824 * g_mutex_lock (&data_mutex);
826 * end_time = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND;
827 * while (!current_data)
828 * if (!g_cond_wait_until (&data_cond, &data_mutex, end_time))
830 * // timeout has passed.
831 * g_mutex_unlock (&data_mutex);
835 * // there is data for us
836 * data = current_data;
837 * current_data = NULL;
839 * g_mutex_unlock (&data_mutex);
845 * Notice that the end time is calculated once, before entering the
846 * loop and reused. This is the motivation behind the use of absolute
847 * time on this API -- if a relative time of 5 seconds were passed
848 * directly to the call and a spurious wakeup occurred, the program would
849 * have to start over waiting again (which would lead to a total wait
850 * time of more than 5 seconds).
852 * Returns: %TRUE on a signal, %FALSE on a timeout
856 g_cond_wait_until (GCond *cond,
863 ts.tv_sec = end_time / 1000000;
864 ts.tv_nsec = (end_time % 1000000) * 1000;
866 if ((status = pthread_cond_timedwait (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
869 if G_UNLIKELY (status != ETIMEDOUT)
870 g_thread_abort (status, "pthread_cond_timedwait");
880 * The #GPrivate struct is an opaque data structure to represent a
881 * thread-local data key. It is approximately equivalent to the
882 * pthread_setspecific()/pthread_getspecific() APIs on POSIX and to
883 * TlsSetValue()/TlsGetValue() on Windows.
885 * If you don't already know why you might want this functionality,
886 * then you probably don't need it.
888 * #GPrivate is a very limited resource (as far as 128 per program,
889 * shared between all libraries). It is also not possible to destroy a
890 * #GPrivate after it has been used. As such, it is only ever acceptable
891 * to use #GPrivate in static scope, and even then sparingly so.
893 * See G_PRIVATE_INIT() for a couple of examples.
895 * The #GPrivate structure should be considered opaque. It should only
896 * be accessed via the <function>g_private_</function> functions.
901 * @notify: a #GDestroyNotify
903 * A macro to assist with the static initialisation of a #GPrivate.
905 * This macro is useful for the case that a #GDestroyNotify function
906 * should be associated the key. This is needed when the key will be
907 * used to point at memory that should be deallocated when the thread
910 * Additionally, the #GDestroyNotify will also be called on the previous
911 * value stored in the key when g_private_replace() is used.
913 * If no #GDestroyNotify is needed, then use of this macro is not
914 * required -- if the #GPrivate is declared in static scope then it will
915 * be properly initialised by default (ie: to all zeros). See the
919 * static GPrivate name_key = G_PRIVATE_INIT (g_free);
921 * // return value should not be freed
923 * get_local_name (void)
925 * return g_private_get (&name_key);
929 * set_local_name (const gchar *name)
931 * g_private_replace (&name_key, g_strdup (name));
935 * static GPrivate count_key; // no free function
938 * get_local_count (void)
940 * return GPOINTER_TO_INT (g_private_get (&count_key));
944 * set_local_count (gint count)
946 * g_private_set (&count_key, GINT_TO_POINTER (count));
953 static pthread_key_t *
954 g_private_impl_new (GDestroyNotify notify)
959 key = malloc (sizeof (pthread_key_t));
960 if G_UNLIKELY (key == NULL)
961 g_thread_abort (errno, "malloc");
962 status = pthread_key_create (key, notify);
963 if G_UNLIKELY (status != 0)
964 g_thread_abort (status, "pthread_key_create");
970 g_private_impl_free (pthread_key_t *key)
974 status = pthread_key_delete (*key);
975 if G_UNLIKELY (status != 0)
976 g_thread_abort (status, "pthread_key_delete");
980 static pthread_key_t *
981 g_private_get_impl (GPrivate *key)
983 pthread_key_t *impl = g_atomic_pointer_get (&key->p);
985 if G_UNLIKELY (impl == NULL)
987 impl = g_private_impl_new (key->notify);
988 if (!g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
990 g_private_impl_free (impl);
1002 * Returns the current value of the thread local variable @key.
1004 * If the value has not yet been set in this thread, %NULL is returned.
1005 * Values are never copied between threads (when a new thread is
1006 * created, for example).
1008 * Returns: the thread-local value
1011 g_private_get (GPrivate *key)
1013 /* quote POSIX: No errors are returned from pthread_getspecific(). */
1014 return pthread_getspecific (*g_private_get_impl (key));
1020 * @value: the new value
1022 * Sets the thread local variable @key to have the value @value in the
1025 * This function differs from g_private_replace() in the following way:
1026 * the #GDestroyNotify for @key is not called on the old value.
1029 g_private_set (GPrivate *key,
1034 if G_UNLIKELY ((status = pthread_setspecific (*g_private_get_impl (key), value)) != 0)
1035 g_thread_abort (status, "pthread_setspecific");
1039 * g_private_replace:
1041 * @value: the new value
1043 * Sets the thread local variable @key to have the value @value in the
1046 * This function differs from g_private_set() in the following way: if
1047 * the previous value was non-%NULL then the #GDestroyNotify handler for
1048 * @key is run on it.
1053 g_private_replace (GPrivate *key,
1056 pthread_key_t *impl = g_private_get_impl (key);
1060 old = pthread_getspecific (*impl);
1061 if (old && key->notify)
1064 if G_UNLIKELY ((status = pthread_setspecific (*impl, value)) != 0)
1065 g_thread_abort (status, "pthread_setspecific");
1070 #define posix_check_err(err, name) G_STMT_START{ \
1071 int error = (err); \
1073 g_error ("file %s: line %d (%s): error '%s' during '%s'", \
1074 __FILE__, __LINE__, G_STRFUNC, \
1075 g_strerror (error), name); \
1078 #define posix_check_cmd(cmd) posix_check_err (cmd, #cmd)
1084 pthread_t system_thread;
1090 g_system_thread_free (GRealThread *thread)
1092 GThreadPosix *pt = (GThreadPosix *) thread;
1095 pthread_detach (pt->system_thread);
1097 g_mutex_clear (&pt->lock);
1099 g_slice_free (GThreadPosix, pt);
1103 g_system_thread_new (GThreadFunc thread_func,
1107 GThreadPosix *thread;
1108 pthread_attr_t attr;
1111 thread = g_slice_new0 (GThreadPosix);
1113 posix_check_cmd (pthread_attr_init (&attr));
1115 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
1118 #ifdef _SC_THREAD_STACK_MIN
1119 stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), stack_size);
1120 #endif /* _SC_THREAD_STACK_MIN */
1121 /* No error check here, because some systems can't do it and
1122 * we simply don't want threads to fail because of that. */
1123 pthread_attr_setstacksize (&attr, stack_size);
1125 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
1127 ret = pthread_create (&thread->system_thread, &attr, (void* (*)(void*))thread_func, thread);
1129 posix_check_cmd (pthread_attr_destroy (&attr));
1133 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
1134 "Error creating thread: %s", g_strerror (ret));
1135 g_slice_free (GThreadPosix, thread);
1139 posix_check_err (ret, "pthread_create");
1141 g_mutex_init (&thread->lock);
1143 return (GRealThread *) thread;
1149 * Causes the calling thread to voluntarily relinquish the CPU, so
1150 * that other threads can run.
1152 * This function is often used as a method to make busy wait less evil.
1155 g_thread_yield (void)
1161 g_system_thread_wait (GRealThread *thread)
1163 GThreadPosix *pt = (GThreadPosix *) thread;
1165 g_mutex_lock (&pt->lock);
1169 posix_check_cmd (pthread_join (pt->system_thread, NULL));
1173 g_mutex_unlock (&pt->lock);
1177 g_system_thread_exit (void)
1179 pthread_exit (NULL);
1183 g_system_thread_set_name (const gchar *name)
1185 #ifdef HAVE_SYS_PRCTL_H
1187 prctl (PR_SET_NAME, name, 0, 0, 0, 0);
1193 /* vim:set foldmethod=marker: */