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);
369 * @rec_mutex: a #GRecMutex
371 * Locks @rec_mutex. If @rec_mutex is already locked by another
372 * thread, the current thread will block until @rec_mutex is
373 * unlocked by the other thread. If @rec_mutex is already locked
374 * by the current thread, the 'lock count' of @rec_mutex is increased.
375 * The mutex will only become available again when it is unlocked
376 * as many times as it has been locked.
381 g_rec_mutex_lock (GRecMutex *mutex)
383 pthread_mutex_lock (g_rec_mutex_get_impl (mutex));
387 * g_rec_mutex_unlock:
388 * @rec_mutex: a #GRecMutex
390 * Unlocks @rec_mutex. If another thread is blocked in a
391 * g_rec_mutex_lock() call for @rec_mutex, it will become unblocked
392 * and can lock @rec_mutex itself.
394 * Calling g_rec_mutex_unlock() on a recursive mutex that is not
395 * locked by the current thread leads to undefined behaviour.
400 g_rec_mutex_unlock (GRecMutex *rec_mutex)
402 pthread_mutex_unlock (rec_mutex->p);
406 * g_rec_mutex_trylock:
407 * @rec_mutex: a #GRecMutex
409 * Tries to lock @rec_mutex. If @rec_mutex is already locked
410 * by another thread, it immediately returns %FALSE. Otherwise
411 * it locks @rec_mutex and returns %TRUE.
413 * Returns: %TRUE if @rec_mutex could be locked
418 g_rec_mutex_trylock (GRecMutex *rec_mutex)
420 if (pthread_mutex_trylock (g_rec_mutex_get_impl (rec_mutex)) != 0)
428 static pthread_rwlock_t *
429 g_rw_lock_impl_new (void)
431 pthread_rwlock_t *rwlock;
434 rwlock = malloc (sizeof (pthread_rwlock_t));
435 if G_UNLIKELY (rwlock == NULL)
436 g_thread_abort (errno, "malloc");
438 if G_UNLIKELY ((status = pthread_rwlock_init (rwlock, NULL)) != 0)
439 g_thread_abort (status, "pthread_rwlock_init");
445 g_rw_lock_impl_free (pthread_rwlock_t *rwlock)
447 pthread_rwlock_destroy (rwlock);
451 static pthread_rwlock_t *
452 g_rw_lock_get_impl (GRWLock *lock)
454 pthread_rwlock_t *impl = g_atomic_pointer_get (&lock->p);
456 if G_UNLIKELY (impl == NULL)
458 impl = g_rw_lock_impl_new ();
459 if (!g_atomic_pointer_compare_and_exchange (&lock->p, NULL, impl))
460 g_rw_lock_impl_free (impl);
469 * @rw_lock: an uninitialized #GRWLock
471 * Initializes a #GRWLock so that it can be used.
473 * This function is useful to initialize a lock that has been
474 * allocated on the stack, or as part of a larger structure. It is not
475 * necessary to initialise a reader-writer lock that has been statically
486 * b = g_new (Blob, 1);
487 * g_rw_lock_init (&b->l);
490 * To undo the effect of g_rw_lock_init() when a lock is no longer
491 * needed, use g_rw_lock_clear().
493 * Calling g_rw_lock_init() on an already initialized #GRWLock leads
494 * to undefined behaviour.
499 g_rw_lock_init (GRWLock *rw_lock)
501 rw_lock->p = g_rw_lock_impl_new ();
506 * @rw_lock: an initialized #GRWLock
508 * Frees the resources allocated to a lock with g_rw_lock_init().
510 * This function should not be used with a #GRWLock that has been
511 * statically allocated.
513 * Calling g_rw_lock_clear() when any thread holds the lock
514 * leads to undefined behaviour.
519 g_rw_lock_clear (GRWLock *rw_lock)
521 g_rw_lock_impl_free (rw_lock->p);
525 * g_rw_lock_writer_lock:
526 * @rw_lock: a #GRWLock
528 * Obtain a write lock on @rw_lock. If any thread already holds
529 * a read or write lock on @rw_lock, the current thread will block
530 * until all other threads have dropped their locks on @rw_lock.
535 g_rw_lock_writer_lock (GRWLock *rw_lock)
537 pthread_rwlock_wrlock (g_rw_lock_get_impl (rw_lock));
541 * g_rw_lock_writer_trylock:
542 * @rw_lock: a #GRWLock
544 * Tries to obtain a write lock on @rw_lock. If any other thread holds
545 * a read or write lock on @rw_lock, it immediately returns %FALSE.
546 * Otherwise it locks @rw_lock and returns %TRUE.
548 * Returns: %TRUE if @rw_lock could be locked
553 g_rw_lock_writer_trylock (GRWLock *rw_lock)
555 if (pthread_rwlock_trywrlock (g_rw_lock_get_impl (rw_lock)) != 0)
562 * g_rw_lock_writer_unlock:
563 * @rw_lock: a #GRWLock
565 * Release a write lock on @rw_lock.
567 * Calling g_rw_lock_writer_unlock() on a lock that is not held
568 * by the current thread leads to undefined behaviour.
573 g_rw_lock_writer_unlock (GRWLock *rw_lock)
575 pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
579 * g_rw_lock_reader_lock:
580 * @rw_lock: a #GRWLock
582 * Obtain a read lock on @rw_lock. If another thread currently holds
583 * the write lock on @rw_lock or blocks waiting for it, the current
584 * thread will block. Read locks can be taken recursively.
586 * It is implementation-defined how many threads are allowed to
587 * hold read locks on the same lock simultaneously.
592 g_rw_lock_reader_lock (GRWLock *rw_lock)
594 pthread_rwlock_rdlock (g_rw_lock_get_impl (rw_lock));
598 * g_rw_lock_reader_trylock:
599 * @rw_lock: a #GRWLock
601 * Tries to obtain a read lock on @rw_lock and returns %TRUE if
602 * the read lock was successfully obtained. Otherwise it
605 * Returns: %TRUE if @rw_lock could be locked
610 g_rw_lock_reader_trylock (GRWLock *rw_lock)
612 if (pthread_rwlock_tryrdlock (g_rw_lock_get_impl (rw_lock)) != 0)
619 * g_rw_lock_reader_unlock:
620 * @rw_lock: a #GRWLock
622 * Release a read lock on @rw_lock.
624 * Calling g_rw_lock_reader_unlock() on a lock that is not held
625 * by the current thread leads to undefined behaviour.
630 g_rw_lock_reader_unlock (GRWLock *rw_lock)
632 pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
637 static pthread_cond_t *
638 g_cond_impl_new (void)
640 pthread_condattr_t attr;
641 pthread_cond_t *cond;
644 pthread_condattr_init (&attr);
645 #if defined (HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined (CLOCK_MONOTONIC)
646 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
649 cond = malloc (sizeof (pthread_cond_t));
650 if G_UNLIKELY (cond == NULL)
651 g_thread_abort (errno, "malloc");
653 if G_UNLIKELY ((status = pthread_cond_init (cond, &attr)) != 0)
654 g_thread_abort (status, "pthread_cond_init");
656 pthread_condattr_destroy (&attr);
662 g_cond_impl_free (pthread_cond_t *cond)
664 pthread_cond_destroy (cond);
668 static pthread_cond_t *
669 g_cond_get_impl (GCond *cond)
671 pthread_cond_t *impl = g_atomic_pointer_get (&cond->p);
673 if G_UNLIKELY (impl == NULL)
675 impl = g_cond_impl_new ();
676 if (!g_atomic_pointer_compare_and_exchange (&cond->p, NULL, impl))
677 g_cond_impl_free (impl);
686 * @cond: an uninitialized #GCond
688 * Initialises a #GCond so that it can be used.
690 * This function is useful to initialise a #GCond that has been
691 * allocated as part of a larger structure. It is not necessary to
692 * initialise a #GCond that has been statically allocated.
694 * To undo the effect of g_cond_init() when a #GCond is no longer
695 * needed, use g_cond_clear().
697 * Calling g_cond_init() on an already-initialised #GCond leads
698 * to undefined behaviour.
703 g_cond_init (GCond *cond)
705 cond->p = g_cond_impl_new ();
710 * @cond: an initialised #GCond
712 * Frees the resources allocated to a #GCond with g_cond_init().
714 * This function should not be used with a #GCond that has been
715 * statically allocated.
717 * Calling g_cond_clear() for a #GCond on which threads are
718 * blocking leads to undefined behaviour.
723 g_cond_clear (GCond *cond)
725 g_cond_impl_free (cond->p);
731 * @mutex: a #GMutex that is currently locked
733 * Atomically releases @mutex and waits until @cond is signalled.
734 * When this function returns, @mutex is locked again and owned by the
737 * When using condition variables, it is possible that a spurious wakeup
738 * may occur (ie: g_cond_wait() returns even though g_cond_signal() was
739 * not called). It's also possible that a stolen wakeup may occur.
740 * This is when g_cond_signal() is called, but another thread acquires
741 * @mutex before this thread and modifies the state of the program in
742 * such a way that when g_cond_wait() is able to return, the expected
743 * condition is no longer met.
745 * For this reason, g_cond_wait() must always be used in a loop. See
746 * the documentation for #GCond for a complete example.
749 g_cond_wait (GCond *cond,
754 if G_UNLIKELY ((status = pthread_cond_wait (g_cond_get_impl (cond), g_mutex_get_impl (mutex))) != 0)
755 g_thread_abort (status, "pthread_cond_wait");
762 * If threads are waiting for @cond, at least one of them is unblocked.
763 * If no threads are waiting for @cond, this function has no effect.
764 * It is good practice to hold the same lock as the waiting thread
765 * while calling this function, though not required.
768 g_cond_signal (GCond *cond)
772 if G_UNLIKELY ((status = pthread_cond_signal (g_cond_get_impl (cond))) != 0)
773 g_thread_abort (status, "pthread_cond_signal");
780 * If threads are waiting for @cond, all of them are unblocked.
781 * If no threads are waiting for @cond, this function has no effect.
782 * It is good practice to lock the same mutex as the waiting threads
783 * while calling this function, though not required.
786 g_cond_broadcast (GCond *cond)
790 if G_UNLIKELY ((status = pthread_cond_broadcast (g_cond_get_impl (cond))) != 0)
791 g_thread_abort (status, "pthread_cond_broadcast");
797 * @mutex: a #GMutex that is currently locked
798 * @end_time: the monotonic time to wait until
800 * Waits until either @cond is signalled or @end_time has passed.
802 * As with g_cond_wait() it is possible that a spurious or stolen wakeup
803 * could occur. For that reason, waiting on a condition variable should
804 * always be in a loop, based on an explicitly-checked predicate.
806 * %TRUE is returned if the condition variable was signalled (or in the
807 * case of a spurious wakeup). %FALSE is returned if @end_time has
810 * The following code shows how to correctly perform a timed wait on a
811 * condition variable (extended the example presented in the
812 * documentation for #GCond):
816 * pop_data_timed (void)
821 * g_mutex_lock (&data_mutex);
823 * end_time = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND;
824 * while (!current_data)
825 * if (!g_cond_wait_until (&data_cond, &data_mutex, end_time))
827 * // timeout has passed.
828 * g_mutex_unlock (&data_mutex);
832 * // there is data for us
833 * data = current_data;
834 * current_data = NULL;
836 * g_mutex_unlock (&data_mutex);
842 * Notice that the end time is calculated once, before entering the
843 * loop and reused. This is the motivation behind the use of absolute
844 * time on this API -- if a relative time of 5 seconds were passed
845 * directly to the call and a spurious wakeup occurred, the program would
846 * have to start over waiting again (which would lead to a total wait
847 * time of more than 5 seconds).
849 * Returns: %TRUE on a signal, %FALSE on a timeout
853 g_cond_wait_until (GCond *cond,
860 ts.tv_sec = end_time / 1000000;
861 ts.tv_nsec = (end_time % 1000000) * 1000;
863 if ((status = pthread_cond_timedwait (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
866 if G_UNLIKELY (status != ETIMEDOUT)
867 g_thread_abort (status, "pthread_cond_timedwait");
877 * The #GPrivate struct is an opaque data structure to represent a
878 * thread-local data key. It is approximately equivalent to the
879 * pthread_setspecific()/pthread_getspecific() APIs on POSIX and to
880 * TlsSetValue()/TlsGetValue() on Windows.
882 * If you don't already know why you might want this functionality,
883 * then you probably don't need it.
885 * #GPrivate is a very limited resource (as far as 128 per program,
886 * shared between all libraries). It is also not possible to destroy a
887 * #GPrivate after it has been used. As such, it is only ever acceptable
888 * to use #GPrivate in static scope, and even then sparingly so.
890 * See G_PRIVATE_INIT() for a couple of examples.
892 * The #GPrivate structure should be considered opaque. It should only
893 * be accessed via the <function>g_private_</function> functions.
898 * @notify: a #GDestroyNotify
900 * A macro to assist with the static initialisation of a #GPrivate.
902 * This macro is useful for the case that a #GDestroyNotify function
903 * should be associated the key. This is needed when the key will be
904 * used to point at memory that should be deallocated when the thread
907 * Additionally, the #GDestroyNotify will also be called on the previous
908 * value stored in the key when g_private_replace() is used.
910 * If no #GDestroyNotify is needed, then use of this macro is not
911 * required -- if the #GPrivate is declared in static scope then it will
912 * be properly initialised by default (ie: to all zeros). See the
916 * static GPrivate name_key = G_PRIVATE_INIT (g_free);
918 * // return value should not be freed
920 * get_local_name (void)
922 * return g_private_get (&name_key);
926 * set_local_name (const gchar *name)
928 * g_private_replace (&name_key, g_strdup (name));
932 * static GPrivate count_key; // no free function
935 * get_local_count (void)
937 * return GPOINTER_TO_INT (g_private_get (&count_key));
941 * set_local_count (gint count)
943 * g_private_set (&count_key, GINT_TO_POINTER (count));
950 static pthread_key_t *
951 g_private_impl_new (GDestroyNotify notify)
956 key = malloc (sizeof (pthread_key_t));
957 if G_UNLIKELY (key == NULL)
958 g_thread_abort (errno, "malloc");
959 status = pthread_key_create (key, notify);
960 if G_UNLIKELY (status != 0)
961 g_thread_abort (status, "pthread_key_create");
967 g_private_impl_free (pthread_key_t *key)
971 status = pthread_key_delete (*key);
972 if G_UNLIKELY (status != 0)
973 g_thread_abort (status, "pthread_key_delete");
977 static pthread_key_t *
978 g_private_get_impl (GPrivate *key)
980 pthread_key_t *impl = g_atomic_pointer_get (&key->p);
982 if G_UNLIKELY (impl == NULL)
984 impl = g_private_impl_new (key->notify);
985 if (!g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
987 g_private_impl_free (impl);
999 * Returns the current value of the thread local variable @key.
1001 * If the value has not yet been set in this thread, %NULL is returned.
1002 * Values are never copied between threads (when a new thread is
1003 * created, for example).
1005 * Returns: the thread-local value
1008 g_private_get (GPrivate *key)
1010 /* quote POSIX: No errors are returned from pthread_getspecific(). */
1011 return pthread_getspecific (*g_private_get_impl (key));
1017 * @value: the new value
1019 * Sets the thread local variable @key to have the value @value in the
1022 * This function differs from g_private_replace() in the following way:
1023 * the #GDestroyNotify for @key is not called on the old value.
1026 g_private_set (GPrivate *key,
1031 if G_UNLIKELY ((status = pthread_setspecific (*g_private_get_impl (key), value)) != 0)
1032 g_thread_abort (status, "pthread_setspecific");
1036 * g_private_replace:
1038 * @value: the new value
1040 * Sets the thread local variable @key to have the value @value in the
1043 * This function differs from g_private_set() in the following way: if
1044 * the previous value was non-%NULL then the #GDestroyNotify handler for
1045 * @key is run on it.
1050 g_private_replace (GPrivate *key,
1053 pthread_key_t *impl = g_private_get_impl (key);
1057 old = pthread_getspecific (*impl);
1058 if (old && key->notify)
1061 if G_UNLIKELY ((status = pthread_setspecific (*impl, value)) != 0)
1062 g_thread_abort (status, "pthread_setspecific");
1067 #define posix_check_err(err, name) G_STMT_START{ \
1068 int error = (err); \
1070 g_error ("file %s: line %d (%s): error '%s' during '%s'", \
1071 __FILE__, __LINE__, G_STRFUNC, \
1072 g_strerror (error), name); \
1075 #define posix_check_cmd(cmd) posix_check_err (cmd, #cmd)
1081 pthread_t system_thread;
1087 g_system_thread_free (GRealThread *thread)
1089 GThreadPosix *pt = (GThreadPosix *) thread;
1092 pthread_detach (pt->system_thread);
1094 g_mutex_clear (&pt->lock);
1096 g_slice_free (GThreadPosix, pt);
1100 g_system_thread_new (GThreadFunc thread_func,
1104 GThreadPosix *thread;
1105 pthread_attr_t attr;
1108 thread = g_slice_new0 (GThreadPosix);
1110 posix_check_cmd (pthread_attr_init (&attr));
1112 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
1115 #ifdef _SC_THREAD_STACK_MIN
1116 stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), stack_size);
1117 #endif /* _SC_THREAD_STACK_MIN */
1118 /* No error check here, because some systems can't do it and
1119 * we simply don't want threads to fail because of that. */
1120 pthread_attr_setstacksize (&attr, stack_size);
1122 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
1124 ret = pthread_create (&thread->system_thread, &attr, (void* (*)(void*))thread_func, thread);
1126 posix_check_cmd (pthread_attr_destroy (&attr));
1130 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
1131 "Error creating thread: %s", g_strerror (ret));
1132 g_slice_free (GThreadPosix, thread);
1136 posix_check_err (ret, "pthread_create");
1138 g_mutex_init (&thread->lock);
1140 return (GRealThread *) thread;
1146 * Causes the calling thread to voluntarily relinquish the CPU, so
1147 * that other threads can run.
1149 * This function is often used as a method to make busy wait less evil.
1152 g_thread_yield (void)
1158 g_system_thread_wait (GRealThread *thread)
1160 GThreadPosix *pt = (GThreadPosix *) thread;
1162 g_mutex_lock (&pt->lock);
1166 posix_check_cmd (pthread_join (pt->system_thread, NULL));
1170 g_mutex_unlock (&pt->lock);
1174 g_system_thread_exit (void)
1176 pthread_exit (NULL);
1180 g_system_thread_set_name (const gchar *name)
1182 #ifdef HAVE_SYS_PRCTL_H
1184 prctl (PR_SET_NAME, name, 0, 0, 0, 0);
1190 /* vim:set foldmethod=marker: */