1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: MT safety related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
26 * file for a list of people on the GLib Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GLib at ftp://ftp.gtk.org/pub/gtk/.
44 #include "gthreadinit.h"
46 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
47 # define g_system_thread_equal_simple(thread1, thread2) \
48 ((thread1).dummy_pointer == (thread2).dummy_pointer)
49 # define g_system_thread_assign(dest, src) \
50 ((dest).dummy_pointer = (src).dummy_pointer)
51 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
52 # define g_system_thread_equal_simple(thread1, thread2) \
53 (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
54 # define g_system_thread_assign(dest, src) \
55 (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
56 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
58 #define g_system_thread_equal(thread1, thread2) \
59 (g_thread_functions_for_glib_use.thread_equal ? \
60 g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
61 g_system_thread_equal_simple((thread1), (thread2)))
64 g_thread_error_quark (void)
68 quark = g_quark_from_static_string ("g_thread_error");
72 /* Keep this in sync with GRealThread in gmain.c! */
73 typedef struct _GRealThread GRealThread;
77 gpointer private_data;
79 GSystemThread system_thread;
82 typedef struct _GStaticPrivateNode GStaticPrivateNode;
83 struct _GStaticPrivateNode
86 GDestroyNotify destroy;
89 static void g_thread_cleanup (gpointer data);
90 static void g_thread_fail (void);
92 /* Global variables */
94 static GSystemThread zero_thread; /* This is initialized to all zero */
95 gboolean g_thread_use_default_impl = TRUE;
96 gboolean g_threads_got_initialized = FALSE;
98 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
101 GThreadFunctions g_thread_functions_for_glib_use = {
102 (GMutex*(*)())g_thread_fail, /* mutex_new */
103 NULL, /* mutex_lock */
104 NULL, /* mutex_trylock */
105 NULL, /* mutex_unlock */
106 NULL, /* mutex_free */
107 (GCond*(*)())g_thread_fail, /* cond_new */
108 NULL, /* cond_signal */
109 NULL, /* cond_broadcast */
110 NULL, /* cond_wait */
111 NULL, /* cond_timed_wait */
112 NULL, /* cond_free */
113 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
114 NULL, /* private_get */
115 NULL, /* private_set */
116 (void(*)(GThreadFunc, gpointer, gulong,
117 gboolean, gboolean, GThreadPriority,
118 gpointer, GError**))g_thread_fail, /* thread_create */
119 NULL, /* thread_yield */
120 NULL, /* thread_join */
121 NULL, /* thread_exit */
122 NULL, /* thread_set_priority */
123 NULL /* thread_self */
128 static GMutex *g_once_mutex = NULL;
129 static GCond *g_once_cond = NULL;
130 static GPrivate *g_thread_specific_private = NULL;
131 static GSList *g_thread_all_threads = NULL;
132 static GSList *g_thread_free_indeces = NULL;
134 G_LOCK_DEFINE_STATIC (g_thread);
136 #ifdef G_THREADS_ENABLED
137 /* This must be called only once, before any threads are created.
138 * It will only be called from g_thread_init() in -lgthread.
141 g_thread_init_glib (void)
143 /* We let the main thread (the one that calls g_thread_init) inherit
144 * the static_private data set before calling g_thread_init
146 GRealThread* main_thread = (GRealThread*) g_thread_self ();
148 g_once_mutex = g_mutex_new ();
149 g_once_cond = g_cond_new ();
151 _g_convert_thread_init ();
152 _g_rand_thread_init ();
153 _g_main_thread_init ();
154 _g_mem_thread_init ();
155 _g_messages_thread_init ();
156 _g_atomic_thread_init ();
158 g_threads_got_initialized = TRUE;
160 g_thread_specific_private = g_private_new (g_thread_cleanup);
161 g_private_set (g_thread_specific_private, main_thread);
162 G_THREAD_UF (thread_self, (&main_thread->system_thread));
164 _g_mem_thread_private_init ();
165 _g_messages_thread_private_init ();
168 #endif /* G_THREADS_ENABLED */
171 g_once_impl (GOnce *once,
175 g_mutex_lock (g_once_mutex);
177 while (once->status == G_ONCE_STATUS_PROGRESS)
178 g_cond_wait (g_once_cond, g_once_mutex);
180 if (once->status != G_ONCE_STATUS_READY)
182 once->status = G_ONCE_STATUS_PROGRESS;
183 g_mutex_unlock (g_once_mutex);
185 once->retval = func (arg);
187 g_mutex_lock (g_once_mutex);
188 once->status = G_ONCE_STATUS_READY;
189 g_cond_broadcast (g_once_cond);
192 g_mutex_unlock (g_once_mutex);
198 g_static_mutex_init (GStaticMutex *mutex)
200 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
202 g_return_if_fail (mutex);
208 g_static_mutex_get_mutex_impl (GMutex** mutex)
210 if (!g_thread_supported ())
213 g_assert (g_once_mutex);
215 g_mutex_lock (g_once_mutex);
219 GMutex *new_mutex = g_mutex_new ();
221 /* The following is a memory barrier to avoid the write
222 * to *new_mutex being reordered to after writing *mutex */
223 g_mutex_lock (new_mutex);
224 g_mutex_unlock (new_mutex);
229 g_mutex_unlock (g_once_mutex);
235 g_static_mutex_free (GStaticMutex* mutex)
237 GMutex **runtime_mutex;
239 g_return_if_fail (mutex);
241 /* The runtime_mutex is the first (or only) member of GStaticMutex,
242 * see both versions (of glibconfig.h) in configure.in */
243 runtime_mutex = ((GMutex**)mutex);
246 g_mutex_free (*runtime_mutex);
248 *runtime_mutex = NULL;
252 g_static_rec_mutex_init (GStaticRecMutex *mutex)
254 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
256 g_return_if_fail (mutex);
262 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
266 g_return_if_fail (mutex);
268 if (!g_thread_supported ())
271 G_THREAD_UF (thread_self, (&self));
273 if (g_system_thread_equal (self, mutex->owner))
278 g_static_mutex_lock (&mutex->mutex);
279 g_system_thread_assign (mutex->owner, self);
284 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
288 g_return_val_if_fail (mutex, FALSE);
290 if (!g_thread_supported ())
293 G_THREAD_UF (thread_self, (&self));
295 if (g_system_thread_equal (self, mutex->owner))
301 if (!g_static_mutex_trylock (&mutex->mutex))
304 g_system_thread_assign (mutex->owner, self);
310 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
312 g_return_if_fail (mutex);
314 if (!g_thread_supported ())
317 if (mutex->depth > 1)
322 g_system_thread_assign (mutex->owner, zero_thread);
323 g_static_mutex_unlock (&mutex->mutex);
327 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
331 g_return_if_fail (mutex);
333 if (!g_thread_supported ())
336 G_THREAD_UF (thread_self, (&self));
338 if (g_system_thread_equal (self, mutex->owner))
340 mutex->depth += depth;
343 g_static_mutex_lock (&mutex->mutex);
344 g_system_thread_assign (mutex->owner, self);
345 mutex->depth = depth;
349 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
353 g_return_val_if_fail (mutex, 0);
355 if (!g_thread_supported ())
358 depth = mutex->depth;
360 g_system_thread_assign (mutex->owner, zero_thread);
362 g_static_mutex_unlock (&mutex->mutex);
368 g_static_rec_mutex_free (GStaticRecMutex *mutex)
370 g_return_if_fail (mutex);
372 g_static_mutex_free (&mutex->mutex);
376 g_static_private_init (GStaticPrivate *private_key)
378 private_key->index = 0;
382 g_static_private_get (GStaticPrivate *private_key)
384 GRealThread *self = (GRealThread*) g_thread_self ();
387 array = self->private_data;
391 if (!private_key->index)
393 else if (private_key->index <= array->len)
394 return g_array_index (array, GStaticPrivateNode,
395 private_key->index - 1).data;
401 g_static_private_set (GStaticPrivate *private_key,
403 GDestroyNotify notify)
405 GRealThread *self = (GRealThread*) g_thread_self ();
407 static guint next_index = 0;
408 GStaticPrivateNode *node;
410 array = self->private_data;
413 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
414 self->private_data = array;
417 if (!private_key->index)
421 if (!private_key->index)
423 if (g_thread_free_indeces)
426 GPOINTER_TO_UINT (g_thread_free_indeces->data);
427 g_thread_free_indeces =
428 g_slist_delete_link (g_thread_free_indeces,
429 g_thread_free_indeces);
432 private_key->index = ++next_index;
438 if (private_key->index > array->len)
439 g_array_set_size (array, private_key->index);
441 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
444 gpointer ddata = node->data;
445 GDestroyNotify ddestroy = node->destroy;
448 node->destroy = notify;
455 node->destroy = notify;
460 g_static_private_free (GStaticPrivate *private_key)
462 guint index = private_key->index;
468 private_key->index = 0;
471 list = g_thread_all_threads;
474 GRealThread *thread = list->data;
475 GArray *array = thread->private_data;
478 if (array && index <= array->len)
480 GStaticPrivateNode *node = &g_array_index (array,
483 gpointer ddata = node->data;
484 GDestroyNotify ddestroy = node->destroy;
487 node->destroy = NULL;
497 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
498 GUINT_TO_POINTER (index));
503 g_thread_cleanup (gpointer data)
507 GRealThread* thread = data;
508 if (thread->private_data)
510 GArray* array = thread->private_data;
513 for (i = 0; i < array->len; i++ )
515 GStaticPrivateNode *node =
516 &g_array_index (array, GStaticPrivateNode, i);
518 node->destroy (node->data);
520 g_array_free (array, TRUE);
523 /* We only free the thread structure, if it isn't joinable. If
524 it is, the structure is freed in g_thread_join */
525 if (!thread->thread.joinable)
528 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
531 /* Just to make sure, this isn't used any more */
532 g_system_thread_assign (thread->system_thread, zero_thread);
541 g_error ("The thread system is not yet initialized.");
545 g_thread_create_proxy (gpointer data)
547 GRealThread* thread = data;
551 /* This has to happen before G_LOCK, as that might call g_thread_self */
552 g_private_set (g_thread_specific_private, data);
554 /* the lock makes sure, that thread->system_thread is written,
555 before thread->thread.func is called. See g_thread_create. */
559 thread->retval = thread->thread.func (thread->thread.data);
565 g_thread_create_full (GThreadFunc func,
570 GThreadPriority priority,
574 GError *local_error = NULL;
575 g_return_val_if_fail (func, NULL);
576 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
577 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
579 result = g_new (GRealThread, 1);
581 result->thread.joinable = joinable;
582 result->thread.priority = priority;
583 result->thread.func = func;
584 result->thread.data = data;
585 result->private_data = NULL;
587 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
588 stack_size, joinable, bound, priority,
589 &result->system_thread, &local_error));
590 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
595 g_propagate_error (error, local_error);
600 return (GThread*) result;
604 g_thread_exit (gpointer retval)
606 GRealThread* real = (GRealThread*) g_thread_self ();
607 real->retval = retval;
608 G_THREAD_CF (thread_exit, (void)0, ());
612 g_thread_join (GThread* thread)
614 GRealThread* real = (GRealThread*) thread;
617 g_return_val_if_fail (thread, NULL);
618 g_return_val_if_fail (thread->joinable, NULL);
619 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
622 G_THREAD_UF (thread_join, (&real->system_thread));
624 retval = real->retval;
627 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
630 /* Just to make sure, this isn't used any more */
631 thread->joinable = 0;
632 g_system_thread_assign (real->system_thread, zero_thread);
634 /* the thread structure for non-joinable threads is freed upon
635 thread end. We free the memory here. This will leave a loose end,
636 if a joinable thread is not joined. */
644 g_thread_set_priority (GThread* thread,
645 GThreadPriority priority)
647 GRealThread* real = (GRealThread*) thread;
649 g_return_if_fail (thread);
650 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
651 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
652 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
654 thread->priority = priority;
656 G_THREAD_CF (thread_set_priority, (void)0,
657 (&real->system_thread, priority));
663 GRealThread* thread = g_private_get (g_thread_specific_private);
667 /* If no thread data is available, provide and set one. This
668 can happen for the main thread and for threads, that are not
670 thread = g_new (GRealThread, 1);
671 thread->thread.joinable = FALSE; /* This is a save guess */
672 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
674 thread->thread.func = NULL;
675 thread->thread.data = NULL;
676 thread->private_data = NULL;
678 if (g_thread_supported ())
679 G_THREAD_UF (thread_self, (&thread->system_thread));
681 g_private_set (g_thread_specific_private, thread);
684 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
688 return (GThread*)thread;
692 g_static_rw_lock_init (GStaticRWLock* lock)
694 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
696 g_return_if_fail (lock);
702 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
705 *cond = g_cond_new ();
706 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
710 g_static_rw_lock_signal (GStaticRWLock* lock)
712 if (lock->want_to_write && lock->write_cond)
713 g_cond_signal (lock->write_cond);
714 else if (lock->want_to_read && lock->read_cond)
715 g_cond_broadcast (lock->read_cond);
719 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
721 g_return_if_fail (lock);
723 if (!g_threads_got_initialized)
726 g_static_mutex_lock (&lock->mutex);
727 lock->want_to_read++;
728 while (lock->have_writer || lock->want_to_write)
729 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
730 lock->want_to_read--;
731 lock->read_counter++;
732 g_static_mutex_unlock (&lock->mutex);
736 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
738 gboolean ret_val = FALSE;
740 g_return_val_if_fail (lock, FALSE);
742 if (!g_threads_got_initialized)
745 g_static_mutex_lock (&lock->mutex);
746 if (!lock->have_writer && !lock->want_to_write)
748 lock->read_counter++;
751 g_static_mutex_unlock (&lock->mutex);
756 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
758 g_return_if_fail (lock);
760 if (!g_threads_got_initialized)
763 g_static_mutex_lock (&lock->mutex);
764 lock->read_counter--;
765 if (lock->read_counter == 0)
766 g_static_rw_lock_signal (lock);
767 g_static_mutex_unlock (&lock->mutex);
771 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
773 g_return_if_fail (lock);
775 if (!g_threads_got_initialized)
778 g_static_mutex_lock (&lock->mutex);
779 lock->want_to_write++;
780 while (lock->have_writer || lock->read_counter)
781 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
782 lock->want_to_write--;
783 lock->have_writer = TRUE;
784 g_static_mutex_unlock (&lock->mutex);
788 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
790 gboolean ret_val = FALSE;
792 g_return_val_if_fail (lock, FALSE);
794 if (!g_threads_got_initialized)
797 g_static_mutex_lock (&lock->mutex);
798 if (!lock->have_writer && !lock->read_counter)
800 lock->have_writer = TRUE;
803 g_static_mutex_unlock (&lock->mutex);
808 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
810 g_return_if_fail (lock);
812 if (!g_threads_got_initialized)
815 g_static_mutex_lock (&lock->mutex);
816 lock->have_writer = FALSE;
817 g_static_rw_lock_signal (lock);
818 g_static_mutex_unlock (&lock->mutex);
822 g_static_rw_lock_free (GStaticRWLock* lock)
824 g_return_if_fail (lock);
828 g_cond_free (lock->read_cond);
829 lock->read_cond = NULL;
831 if (lock->write_cond)
833 g_cond_free (lock->write_cond);
834 lock->write_cond = NULL;
836 g_static_mutex_free (&lock->mutex);