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"
47 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
48 # define g_system_thread_equal_simple(thread1, thread2) \
49 ((thread1).dummy_pointer == (thread2).dummy_pointer)
50 # define g_system_thread_assign(dest, src) \
51 ((dest).dummy_pointer = (src).dummy_pointer)
52 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
53 # define g_system_thread_equal_simple(thread1, thread2) \
54 (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
55 # define g_system_thread_assign(dest, src) \
56 (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
57 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
59 #define g_system_thread_equal(thread1, thread2) \
60 (g_thread_functions_for_glib_use.thread_equal ? \
61 g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
62 g_system_thread_equal_simple((thread1), (thread2)))
65 g_thread_error_quark (void)
69 quark = g_quark_from_static_string ("g_thread_error");
73 /* Keep this in sync with GRealThread in gmain.c! */
74 typedef struct _GRealThread GRealThread;
78 gpointer private_data;
81 GSystemThread system_thread;
84 typedef struct _GStaticPrivateNode GStaticPrivateNode;
85 struct _GStaticPrivateNode
88 GDestroyNotify destroy;
91 static void g_thread_cleanup (gpointer data);
92 static void g_thread_fail (void);
94 /* Global variables */
96 static GSystemThread zero_thread; /* This is initialized to all zero */
97 gboolean g_thread_use_default_impl = TRUE;
98 gboolean g_threads_got_initialized = FALSE;
100 GThreadFunctions g_thread_functions_for_glib_use = {
101 (GMutex*(*)())g_thread_fail, /* mutex_new */
102 NULL, /* mutex_lock */
103 NULL, /* mutex_trylock */
104 NULL, /* mutex_unlock */
105 NULL, /* mutex_free */
106 (GCond*(*)())g_thread_fail, /* cond_new */
107 NULL, /* cond_signal */
108 NULL, /* cond_broadcast */
109 NULL, /* cond_wait */
110 NULL, /* cond_timed_wait */
111 NULL, /* cond_free */
112 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
113 NULL, /* private_get */
114 NULL, /* private_set */
115 (void(*)(GThreadFunc, gpointer, gulong,
116 gboolean, gboolean, GThreadPriority,
117 gpointer, GError**))g_thread_fail, /* thread_create */
118 NULL, /* thread_yield */
119 NULL, /* thread_join */
120 NULL, /* thread_exit */
121 NULL, /* thread_set_priority */
122 NULL /* thread_self */
127 static GMutex *g_once_mutex = NULL;
128 static GCond *g_once_cond = NULL;
129 static GPrivate *g_thread_specific_private = NULL;
130 static GRealThread *g_thread_all_threads = NULL;
131 static GSList *g_thread_free_indeces = NULL;
133 G_LOCK_DEFINE_STATIC (g_thread);
135 #ifdef G_THREADS_ENABLED
136 /* This must be called only once, before any threads are created.
137 * It will only be called from g_thread_init() in -lgthread.
140 g_thread_init_glib (void)
142 /* We let the main thread (the one that calls g_thread_init) inherit
143 * the static_private data set before calling g_thread_init
145 GRealThread* main_thread = (GRealThread*) g_thread_self ();
147 /* mutex and cond creation works without g_threads_got_initialized */
148 g_once_mutex = g_mutex_new ();
149 g_once_cond = g_cond_new ();
151 /* we may only create mutex and cond in here */
152 _g_mem_thread_init_noprivate_nomessage ();
154 /* setup the basic threading system */
155 g_threads_got_initialized = TRUE;
156 g_thread_specific_private = g_private_new (g_thread_cleanup);
157 g_private_set (g_thread_specific_private, main_thread);
158 G_THREAD_UF (thread_self, (&main_thread->system_thread));
160 /* complete memory system initialization, g_private_*() works now */
161 _g_slice_thread_init_nomessage ();
163 /* accomplish log system initialization to enable messaging */
164 _g_messages_thread_init_nomessage ();
166 /* we may run full-fledged initializers from here */
167 _g_convert_thread_init ();
168 _g_rand_thread_init ();
169 _g_main_thread_init ();
170 _g_atomic_thread_init ();
171 _g_utils_thread_init ();
173 _g_win32_thread_init ();
176 #endif /* G_THREADS_ENABLED */
179 g_once_impl (GOnce *once,
183 g_mutex_lock (g_once_mutex);
185 while (once->status == G_ONCE_STATUS_PROGRESS)
186 g_cond_wait (g_once_cond, g_once_mutex);
188 if (once->status != G_ONCE_STATUS_READY)
190 once->status = G_ONCE_STATUS_PROGRESS;
191 g_mutex_unlock (g_once_mutex);
193 once->retval = func (arg);
195 g_mutex_lock (g_once_mutex);
196 once->status = G_ONCE_STATUS_READY;
197 g_cond_broadcast (g_once_cond);
200 g_mutex_unlock (g_once_mutex);
206 g_static_mutex_init (GStaticMutex *mutex)
208 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
210 g_return_if_fail (mutex);
216 g_static_mutex_get_mutex_impl (GMutex** mutex)
218 if (!g_thread_supported ())
221 g_assert (g_once_mutex);
223 g_mutex_lock (g_once_mutex);
227 GMutex *new_mutex = g_mutex_new ();
229 /* The following is a memory barrier to avoid the write
230 * to *new_mutex being reordered to after writing *mutex */
231 g_mutex_lock (new_mutex);
232 g_mutex_unlock (new_mutex);
237 g_mutex_unlock (g_once_mutex);
243 g_static_mutex_free (GStaticMutex* mutex)
245 GMutex **runtime_mutex;
247 g_return_if_fail (mutex);
249 /* The runtime_mutex is the first (or only) member of GStaticMutex,
250 * see both versions (of glibconfig.h) in configure.in */
251 runtime_mutex = ((GMutex**)mutex);
254 g_mutex_free (*runtime_mutex);
256 *runtime_mutex = NULL;
260 g_static_rec_mutex_init (GStaticRecMutex *mutex)
262 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
264 g_return_if_fail (mutex);
270 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
274 g_return_if_fail (mutex);
276 if (!g_thread_supported ())
279 G_THREAD_UF (thread_self, (&self));
281 if (g_system_thread_equal (self, mutex->owner))
286 g_static_mutex_lock (&mutex->mutex);
287 g_system_thread_assign (mutex->owner, self);
292 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
296 g_return_val_if_fail (mutex, FALSE);
298 if (!g_thread_supported ())
301 G_THREAD_UF (thread_self, (&self));
303 if (g_system_thread_equal (self, mutex->owner))
309 if (!g_static_mutex_trylock (&mutex->mutex))
312 g_system_thread_assign (mutex->owner, self);
318 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
320 g_return_if_fail (mutex);
322 if (!g_thread_supported ())
325 if (mutex->depth > 1)
330 g_system_thread_assign (mutex->owner, zero_thread);
331 g_static_mutex_unlock (&mutex->mutex);
335 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
339 g_return_if_fail (mutex);
341 if (!g_thread_supported ())
347 G_THREAD_UF (thread_self, (&self));
349 if (g_system_thread_equal (self, mutex->owner))
351 mutex->depth += depth;
354 g_static_mutex_lock (&mutex->mutex);
355 g_system_thread_assign (mutex->owner, self);
356 mutex->depth = depth;
360 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
364 g_return_val_if_fail (mutex, 0);
366 if (!g_thread_supported ())
369 depth = mutex->depth;
371 g_system_thread_assign (mutex->owner, zero_thread);
373 g_static_mutex_unlock (&mutex->mutex);
379 g_static_rec_mutex_free (GStaticRecMutex *mutex)
381 g_return_if_fail (mutex);
383 g_static_mutex_free (&mutex->mutex);
387 g_static_private_init (GStaticPrivate *private_key)
389 private_key->index = 0;
393 g_static_private_get (GStaticPrivate *private_key)
395 GRealThread *self = (GRealThread*) g_thread_self ();
398 array = self->private_data;
402 if (!private_key->index)
404 else if (private_key->index <= array->len)
405 return g_array_index (array, GStaticPrivateNode,
406 private_key->index - 1).data;
412 g_static_private_set (GStaticPrivate *private_key,
414 GDestroyNotify notify)
416 GRealThread *self = (GRealThread*) g_thread_self ();
418 static guint next_index = 0;
419 GStaticPrivateNode *node;
421 array = self->private_data;
424 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
425 self->private_data = array;
428 if (!private_key->index)
432 if (!private_key->index)
434 if (g_thread_free_indeces)
437 GPOINTER_TO_UINT (g_thread_free_indeces->data);
438 g_thread_free_indeces =
439 g_slist_delete_link (g_thread_free_indeces,
440 g_thread_free_indeces);
443 private_key->index = ++next_index;
449 if (private_key->index > array->len)
450 g_array_set_size (array, private_key->index);
452 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
455 gpointer ddata = node->data;
456 GDestroyNotify ddestroy = node->destroy;
459 node->destroy = notify;
466 node->destroy = notify;
471 g_static_private_free (GStaticPrivate *private_key)
473 guint index = private_key->index;
479 private_key->index = 0;
483 thread = g_thread_all_threads;
486 GArray *array = thread->private_data;
487 thread = thread->next;
489 if (array && index <= array->len)
491 GStaticPrivateNode *node = &g_array_index (array,
494 gpointer ddata = node->data;
495 GDestroyNotify ddestroy = node->destroy;
498 node->destroy = NULL;
508 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
509 GUINT_TO_POINTER (index));
514 g_thread_cleanup (gpointer data)
518 GRealThread* thread = data;
519 if (thread->private_data)
521 GArray* array = thread->private_data;
524 for (i = 0; i < array->len; i++ )
526 GStaticPrivateNode *node =
527 &g_array_index (array, GStaticPrivateNode, i);
529 node->destroy (node->data);
531 g_array_free (array, TRUE);
534 /* We only free the thread structure, if it isn't joinable. If
535 it is, the structure is freed in g_thread_join */
536 if (!thread->thread.joinable)
541 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
548 g_thread_all_threads = t->next;
554 /* Just to make sure, this isn't used any more */
555 g_system_thread_assign (thread->system_thread, zero_thread);
564 g_error ("The thread system is not yet initialized.");
568 g_thread_create_proxy (gpointer data)
570 GRealThread* thread = data;
574 /* This has to happen before G_LOCK, as that might call g_thread_self */
575 g_private_set (g_thread_specific_private, data);
577 /* the lock makes sure, that thread->system_thread is written,
578 before thread->thread.func is called. See g_thread_create. */
582 thread->retval = thread->thread.func (thread->thread.data);
588 g_thread_create_full (GThreadFunc func,
593 GThreadPriority priority,
597 GError *local_error = NULL;
598 g_return_val_if_fail (func, NULL);
599 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
600 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
602 result = g_new0 (GRealThread, 1);
604 result->thread.joinable = joinable;
605 result->thread.priority = priority;
606 result->thread.func = func;
607 result->thread.data = data;
608 result->private_data = NULL;
610 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
611 stack_size, joinable, bound, priority,
612 &result->system_thread, &local_error));
613 result->next = g_thread_all_threads;
614 g_thread_all_threads = result;
619 g_propagate_error (error, local_error);
624 return (GThread*) result;
628 g_thread_exit (gpointer retval)
630 GRealThread* real = (GRealThread*) g_thread_self ();
631 real->retval = retval;
632 G_THREAD_CF (thread_exit, (void)0, ());
636 g_thread_join (GThread* thread)
638 GRealThread* real = (GRealThread*) thread;
642 g_return_val_if_fail (thread, NULL);
643 g_return_val_if_fail (thread->joinable, NULL);
644 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
647 G_THREAD_UF (thread_join, (&real->system_thread));
649 retval = real->retval;
652 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
654 if (t == (GRealThread*) thread)
659 g_thread_all_threads = t->next;
665 /* Just to make sure, this isn't used any more */
666 thread->joinable = 0;
667 g_system_thread_assign (real->system_thread, zero_thread);
669 /* the thread structure for non-joinable threads is freed upon
670 thread end. We free the memory here. This will leave a loose end,
671 if a joinable thread is not joined. */
679 g_thread_set_priority (GThread* thread,
680 GThreadPriority priority)
682 GRealThread* real = (GRealThread*) thread;
684 g_return_if_fail (thread);
685 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
686 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
687 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
689 thread->priority = priority;
691 G_THREAD_CF (thread_set_priority, (void)0,
692 (&real->system_thread, priority));
698 GRealThread* thread = g_private_get (g_thread_specific_private);
702 /* If no thread data is available, provide and set one. This
703 can happen for the main thread and for threads, that are not
705 thread = g_new0 (GRealThread, 1);
706 thread->thread.joinable = FALSE; /* This is a save guess */
707 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
709 thread->thread.func = NULL;
710 thread->thread.data = NULL;
711 thread->private_data = NULL;
713 if (g_thread_supported ())
714 G_THREAD_UF (thread_self, (&thread->system_thread));
716 g_private_set (g_thread_specific_private, thread);
719 thread->next = g_thread_all_threads;
720 g_thread_all_threads = thread;
724 return (GThread*)thread;
728 g_static_rw_lock_init (GStaticRWLock* lock)
730 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
732 g_return_if_fail (lock);
738 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
741 *cond = g_cond_new ();
742 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
746 g_static_rw_lock_signal (GStaticRWLock* lock)
748 if (lock->want_to_write && lock->write_cond)
749 g_cond_signal (lock->write_cond);
750 else if (lock->want_to_read && lock->read_cond)
751 g_cond_broadcast (lock->read_cond);
755 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
757 g_return_if_fail (lock);
759 if (!g_threads_got_initialized)
762 g_static_mutex_lock (&lock->mutex);
763 lock->want_to_read++;
764 while (lock->have_writer || lock->want_to_write)
765 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
766 lock->want_to_read--;
767 lock->read_counter++;
768 g_static_mutex_unlock (&lock->mutex);
772 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
774 gboolean ret_val = FALSE;
776 g_return_val_if_fail (lock, FALSE);
778 if (!g_threads_got_initialized)
781 g_static_mutex_lock (&lock->mutex);
782 if (!lock->have_writer && !lock->want_to_write)
784 lock->read_counter++;
787 g_static_mutex_unlock (&lock->mutex);
792 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
794 g_return_if_fail (lock);
796 if (!g_threads_got_initialized)
799 g_static_mutex_lock (&lock->mutex);
800 lock->read_counter--;
801 if (lock->read_counter == 0)
802 g_static_rw_lock_signal (lock);
803 g_static_mutex_unlock (&lock->mutex);
807 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
809 g_return_if_fail (lock);
811 if (!g_threads_got_initialized)
814 g_static_mutex_lock (&lock->mutex);
815 lock->want_to_write++;
816 while (lock->have_writer || lock->read_counter)
817 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
818 lock->want_to_write--;
819 lock->have_writer = TRUE;
820 g_static_mutex_unlock (&lock->mutex);
824 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
826 gboolean ret_val = FALSE;
828 g_return_val_if_fail (lock, FALSE);
830 if (!g_threads_got_initialized)
833 g_static_mutex_lock (&lock->mutex);
834 if (!lock->have_writer && !lock->read_counter)
836 lock->have_writer = TRUE;
839 g_static_mutex_unlock (&lock->mutex);
844 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
846 g_return_if_fail (lock);
848 if (!g_threads_got_initialized)
851 g_static_mutex_lock (&lock->mutex);
852 lock->have_writer = FALSE;
853 g_static_rw_lock_signal (lock);
854 g_static_mutex_unlock (&lock->mutex);
858 g_static_rw_lock_free (GStaticRWLock* lock)
860 g_return_if_fail (lock);
864 g_cond_free (lock->read_cond);
865 lock->read_cond = NULL;
867 if (lock->write_cond)
869 g_cond_free (lock->write_cond);
870 lock->write_cond = NULL;
872 g_static_mutex_free (&lock->mutex);
877 * @thread_func: function to call for all GThread structures
878 * @user_data: second argument to @thread_func
880 * Call @thread_func on all existing #GThread structures. Note that
881 * threads may decide to exit while @thread_func is running, so
882 * without intimate knowledge about the lifetime of foreign threads,
883 * @thread_func shouldn't access the GThread* pointer passed in as
884 * first argument. However, @thread_func will not be called for threads
885 * which are known to have exited already.
887 * Due to thread lifetime checks, this function has an execution complexity
888 * which is quadratic in the number of existing threads.
893 g_thread_foreach (GFunc thread_func,
896 GSList *slist = NULL;
898 g_return_if_fail (thread_func != NULL);
899 /* snapshot the list of threads for iteration */
901 for (thread = g_thread_all_threads; thread; thread = thread->next)
902 slist = g_slist_prepend (slist, thread);
904 /* walk the list, skipping non-existant threads */
907 GSList *node = slist;
909 /* check whether the current thread still exists */
911 for (thread = g_thread_all_threads; thread; thread = thread->next)
912 if (thread == node->data)
916 thread_func (thread, user_data);
917 g_slist_free_1 (node);
921 #define __G_THREAD_C__
922 #include "galiasdef.c"