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.
45 #include "gthreadprivate.h"
55 g_thread_abort (gint status,
56 const gchar *function)
58 fprintf (stderr, "GLib (gthread-posix.c): Unexpected error from C library during '%s': %s. Aborting.\n",
59 strerror (status), function);
67 * @mutex: an uninitialized #GMutex
69 * Initializes a #GMutex so that it can be used.
71 * This function is useful to initialize a mutex that has been
72 * allocated on the stack, or as part of a larger structure.
73 * It is not necessary to initialize a mutex that has been
74 * created with g_mutex_new(). Also see #G_MUTEX_INITIALIZER
75 * for an alternative way to initialize statically allocated mutexes.
85 * b = g_new (Blob, 1);
86 * g_mutex_init (&b->m);
90 * To undo the effect of g_mutex_init() when a mutex is no longer
91 * needed, use g_mutex_clear().
96 g_mutex_init (GMutex *mutex)
99 pthread_mutexattr_t *pattr = NULL;
100 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
101 pthread_mutexattr_t attr;
102 pthread_mutexattr_init (&attr);
103 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
107 if G_UNLIKELY ((status = pthread_mutex_init (&mutex->impl, pattr)) != 0)
108 g_thread_abort (status, "pthread_mutex_init");
110 #ifdef PTHREAD_ADAPTIVE_MUTEX_NP
111 pthread_mutexattr_destroy (&attr);
117 * @mutex: an initialized #GMutex
119 * Frees the resources allocated to a mutex with g_mutex_init().
121 * #GMutexes that have have been created with g_mutex_new() should
122 * be freed with g_mutex_free() instead.
127 g_mutex_clear (GMutex *mutex)
131 if G_UNLIKELY ((status = pthread_mutex_destroy (&mutex->impl)) != 0)
132 g_thread_abort (status, "pthread_mutex_destroy");
139 * Locks @mutex. If @mutex is already locked by another thread, the
140 * current thread will block until @mutex is unlocked by the other
143 * This function can be used even if g_thread_init() has not yet been
144 * called, and, in that case, will do nothing.
146 * <note>#GMutex is neither guaranteed to be recursive nor to be
147 * non-recursive, i.e. a thread could deadlock while calling
148 * g_mutex_lock(), if it already has locked @mutex. Use
149 * #GStaticRecMutex, if you need recursive mutexes.</note>
152 g_mutex_lock (GMutex *mutex)
156 if G_UNLIKELY ((status = pthread_mutex_lock (&mutex->impl)) != 0)
157 g_thread_abort (status, "pthread_mutex_lock");
164 * Unlocks @mutex. If another thread is blocked in a g_mutex_lock()
165 * call for @mutex, it will be woken and can lock @mutex itself.
167 * This function can be used even if g_thread_init() has not yet been
168 * called, and, in that case, will do nothing.
171 g_mutex_unlock (GMutex *mutex)
175 if G_UNLIKELY ((status = pthread_mutex_unlock (&mutex->impl)) != 0)
176 g_thread_abort (status, "pthread_mutex_lock");
183 * Tries to lock @mutex. If @mutex is already locked by another thread,
184 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
187 * This function can be used even if g_thread_init() has not yet been
188 * called, and, in that case, will immediately return %TRUE.
190 * <note>#GMutex is neither guaranteed to be recursive nor to be
191 * non-recursive, i.e. the return value of g_mutex_trylock() could be
192 * both %FALSE or %TRUE, if the current thread already has locked
193 * @mutex. Use #GStaticRecMutex, if you need recursive
196 * Returns: %TRUE, if @mutex could be locked
199 g_mutex_trylock (GMutex *mutex)
203 if G_LIKELY ((status = pthread_mutex_trylock (&mutex->impl)) == 0)
206 if G_UNLIKELY (status != EBUSY)
207 g_thread_abort (status, "pthread_mutex_trylock");
214 static pthread_mutex_t *
215 g_rec_mutex_impl_new (void)
217 pthread_mutexattr_t attr;
218 pthread_mutex_t *mutex;
220 mutex = g_slice_new (pthread_mutex_t);
221 pthread_mutexattr_init (&attr);
222 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
223 pthread_mutex_init (mutex, &attr);
224 pthread_mutexattr_destroy (&attr);
230 g_rec_mutex_impl_free (pthread_mutex_t *mutex)
232 pthread_mutex_destroy (mutex);
233 g_slice_free (pthread_mutex_t, mutex);
236 static pthread_mutex_t *
237 g_rec_mutex_get_impl (GRecMutex *mutex)
239 pthread_mutex_t *impl = mutex->impl;
241 if G_UNLIKELY (mutex->impl == NULL)
243 impl = g_rec_mutex_impl_new ();
244 if (!g_atomic_pointer_compare_and_exchange (&mutex->impl, NULL, impl))
245 g_rec_mutex_impl_free (impl);
253 g_rec_mutex_init (GRecMutex *mutex)
255 mutex->impl = g_rec_mutex_impl_new ();
259 g_rec_mutex_clear (GRecMutex *mutex)
262 g_rec_mutex_impl_free (mutex->impl);
266 g_rec_mutex_lock (GRecMutex *mutex)
268 pthread_mutex_lock (g_rec_mutex_get_impl (mutex));
272 g_rec_mutex_unlock (GRecMutex *mutex)
274 pthread_mutex_unlock (mutex->impl);
278 g_rec_mutex_trylock (GRecMutex *mutex)
280 if (pthread_mutex_trylock (g_rec_mutex_get_impl (mutex)) != 0)
289 g_rw_lock_init (GRWLock *lock)
291 pthread_rwlock_init (&lock->impl, NULL);
295 g_rw_lock_clear (GRWLock *lock)
297 pthread_rwlock_destroy (&lock->impl);
301 g_rw_lock_writer_lock (GRWLock *lock)
303 pthread_rwlock_wrlock (&lock->impl);
307 g_rw_lock_writer_trylock (GRWLock *lock)
309 if (pthread_rwlock_trywrlock (&lock->impl) != 0)
316 g_rw_lock_writer_unlock (GRWLock *lock)
318 pthread_rwlock_unlock (&lock->impl);
322 g_rw_lock_reader_lock (GRWLock *lock)
324 pthread_rwlock_rdlock (&lock->impl);
328 g_rw_lock_reader_trylock (GRWLock *lock)
330 if (pthread_rwlock_tryrdlock (&lock->impl) != 0)
337 g_rw_lock_reader_unlock (GRWLock *lock)
339 pthread_rwlock_unlock (&lock->impl);
346 * @cond: an uninitialized #GCond
348 * Initialized a #GCond so that it can be used.
350 * This function is useful to initialize a #GCond that has been
351 * allocated on the stack, or as part of a larger structure.
352 * It is not necessary to initialize a #GCond that has been
353 * created with g_cond_new(). Also see #G_COND_INITIALIZER
354 * for an alternative way to initialize statically allocated
360 g_cond_init (GCond *cond)
364 if G_UNLIKELY ((status = pthread_cond_init (&cond->impl, NULL)) != 0)
365 g_thread_abort (status, "pthread_cond_init");
370 * @cond: an initialized #GCond
372 * Frees the resources allocated ot a #GCond with g_cond_init().
374 * #GConds that have been created with g_cond_new() should
375 * be freed with g_cond_free() instead.
380 g_cond_clear (GCond *cond)
384 if G_UNLIKELY ((status = pthread_cond_destroy (&cond->impl)) != 0)
385 g_thread_abort (status, "pthread_cond_destroy");
391 * @mutex: a #GMutex that is currently locked
393 * Waits until this thread is woken up on @cond.
394 * The @mutex is unlocked before falling asleep
395 * and locked again before resuming.
397 * This function can be used even if g_thread_init() has not yet been
398 * called, and, in that case, will immediately return.
401 g_cond_wait (GCond *cond,
406 if G_UNLIKELY ((status = pthread_cond_wait (&cond->impl, &mutex->impl)) != 0)
407 g_thread_abort (status, "pthread_cond_wait");
414 * If threads are waiting for @cond, exactly one of them is woken up.
415 * It is good practice to hold the same lock as the waiting thread
416 * while calling this function, though not required.
418 * This function can be used even if g_thread_init() has not yet been
419 * called, and, in that case, will do nothing.
422 g_cond_signal (GCond *cond)
426 if G_UNLIKELY ((status = pthread_cond_signal (&cond->impl)) != 0)
427 g_thread_abort (status, "pthread_cond_signal");
434 * If threads are waiting for @cond, all of them are woken up.
435 * It is good practice to lock the same mutex as the waiting threads
436 * while calling this function, though not required.
438 * This function can be used even if g_thread_init() has not yet been
439 * called, and, in that case, will do nothing.
442 g_cond_broadcast (GCond *cond)
446 if G_UNLIKELY ((status = pthread_cond_broadcast (&cond->impl)) != 0)
447 g_thread_abort (status, "pthread_cond_broadcast");
453 * @mutex: a #GMutex that is currently locked
454 * @abs_time: a #GTimeVal, determining the final time
456 * Waits until this thread is woken up on @cond, but not longer than
457 * until the time specified by @abs_time. The @mutex is unlocked before
458 * falling asleep and locked again before resuming.
460 * If @abs_time is %NULL, g_cond_timed_wait() acts like g_cond_wait().
462 * This function can be used even if g_thread_init() has not yet been
463 * called, and, in that case, will immediately return %TRUE.
465 * To easily calculate @abs_time a combination of g_get_current_time()
466 * and g_time_val_add() can be used.
468 * Returns: %TRUE if @cond was signalled, or %FALSE on timeout
471 g_cond_timed_wait (GCond *cond,
475 struct timespec end_time;
478 if (abs_time == NULL)
480 g_cond_wait (cond, mutex);
484 end_time.tv_sec = abs_time->tv_sec;
485 end_time.tv_nsec = abs_time->tv_usec * 1000;
487 if ((status = pthread_cond_timedwait (&cond->impl, &mutex->impl, &end_time)) == 0)
490 if G_UNLIKELY (status != ETIMEDOUT)
491 g_thread_abort (status, "pthread_cond_timedwait");
499 * @mutex: a #GMutex that is currently locked
500 * @abs_time: the final time, in microseconds
502 * A variant of g_cond_timed_wait() that takes @abs_time
503 * as a #gint64 instead of a #GTimeVal.
504 * See g_cond_timed_wait() for details.
506 * Returns: %TRUE if @cond was signalled, or %FALSE on timeout
511 g_cond_timedwait (GCond *cond,
515 struct timespec end_time;
518 end_time.tv_sec = abs_time / 1000000;
519 end_time.tv_nsec = (abs_time % 1000000) * 1000;
521 if ((status = pthread_cond_timedwait (&cond->impl, &mutex->impl, &end_time)) == 0)
524 if G_UNLIKELY (status != ETIMEDOUT)
525 g_thread_abort (status, "pthread_cond_timedwait");
533 g_private_init (GPrivate *key,
534 GDestroyNotify notify)
536 pthread_key_create (&key->key, notify);
542 * @private_key: a #GPrivate
544 * Returns the pointer keyed to @private_key for the current thread. If
545 * g_private_set() hasn't been called for the current @private_key and
546 * thread yet, this pointer will be %NULL.
548 * This function can be used even if g_thread_init() has not yet been
549 * called, and, in that case, will return the value of @private_key
550 * casted to #gpointer. Note however, that private data set
551 * <emphasis>before</emphasis> g_thread_init() will
552 * <emphasis>not</emphasis> be retained <emphasis>after</emphasis> the
553 * call. Instead, %NULL will be returned in all threads directly after
554 * g_thread_init(), regardless of any g_private_set() calls issued
555 * before threading system initialization.
557 * Returns: the corresponding pointer
560 g_private_get (GPrivate *key)
563 return key->single_value;
565 /* quote POSIX: No errors are returned from pthread_getspecific(). */
566 return pthread_getspecific (key->key);
571 * @private_key: a #GPrivate
572 * @data: the new pointer
574 * Sets the pointer keyed to @private_key for the current thread.
576 * This function can be used even if g_thread_init() has not yet been
577 * called, and, in that case, will set @private_key to @data casted to
578 * #GPrivate*. See g_private_get() for resulting caveats.
581 g_private_set (GPrivate *key,
588 key->single_value = value;
592 if G_UNLIKELY ((status = pthread_setspecific (key->key, value)) != 0)
593 g_thread_abort (status, "pthread_setspecific");
599 #include "gthreadprivate.h"
604 #ifdef HAVE_SYS_TIME_H
605 # include <sys/time.h>
615 #define posix_check_err(err, name) G_STMT_START{ \
618 g_error ("file %s: line %d (%s): error '%s' during '%s'", \
619 __FILE__, __LINE__, G_STRFUNC, \
620 g_strerror (error), name); \
623 #define posix_check_cmd(cmd) posix_check_err (cmd, #cmd)
625 #define G_MUTEX_SIZE (sizeof (pthread_mutex_t))
628 g_system_thread_create (GThreadFunc thread_func,
638 g_return_if_fail (thread_func);
640 posix_check_cmd (pthread_attr_init (&attr));
642 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
645 #ifdef _SC_THREAD_STACK_MIN
646 stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), stack_size);
647 #endif /* _SC_THREAD_STACK_MIN */
648 /* No error check here, because some systems can't do it and
649 * we simply don't want threads to fail because of that. */
650 pthread_attr_setstacksize (&attr, stack_size);
652 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
654 posix_check_cmd (pthread_attr_setdetachstate (&attr,
655 joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
657 ret = pthread_create (thread, &attr, (void* (*)(void*))thread_func, arg);
659 posix_check_cmd (pthread_attr_destroy (&attr));
663 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
664 "Error creating thread: %s", g_strerror (ret));
668 posix_check_err (ret, "pthread_create");
674 * Gives way to other threads waiting to be scheduled.
676 * This function is often used as a method to make busy wait less evil.
677 * But in most cases you will encounter, there are better methods to do
678 * that. So in general you shouldn't use this function.
681 g_thread_yield (void)
687 g_system_thread_join (gpointer thread)
690 posix_check_cmd (pthread_join (*(pthread_t*)thread, &ignore));
694 g_system_thread_exit (void)
700 g_system_thread_self (gpointer thread)
702 *(pthread_t*)thread = pthread_self();
706 g_system_thread_equal (gpointer thread1,
709 return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0);
713 /* vim:set foldmethod=marker: */