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)
67 return g_quark_from_static_string ("g_thread_error");
70 /* Keep this in sync with GRealThread in gmain.c! */
71 typedef struct _GRealThread GRealThread;
75 gpointer private_data;
78 GSystemThread system_thread;
81 typedef struct _GStaticPrivateNode GStaticPrivateNode;
82 struct _GStaticPrivateNode
85 GDestroyNotify destroy;
88 static void g_thread_cleanup (gpointer data);
89 static void g_thread_fail (void);
91 /* Global variables */
93 static GSystemThread zero_thread; /* This is initialized to all zero */
94 gboolean g_thread_use_default_impl = TRUE;
95 gboolean g_threads_got_initialized = FALSE;
97 GThreadFunctions g_thread_functions_for_glib_use = {
98 (GMutex*(*)())g_thread_fail, /* mutex_new */
99 NULL, /* mutex_lock */
100 NULL, /* mutex_trylock */
101 NULL, /* mutex_unlock */
102 NULL, /* mutex_free */
103 (GCond*(*)())g_thread_fail, /* cond_new */
104 NULL, /* cond_signal */
105 NULL, /* cond_broadcast */
106 NULL, /* cond_wait */
107 NULL, /* cond_timed_wait */
108 NULL, /* cond_free */
109 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
110 NULL, /* private_get */
111 NULL, /* private_set */
112 (void(*)(GThreadFunc, gpointer, gulong,
113 gboolean, gboolean, GThreadPriority,
114 gpointer, GError**))g_thread_fail, /* thread_create */
115 NULL, /* thread_yield */
116 NULL, /* thread_join */
117 NULL, /* thread_exit */
118 NULL, /* thread_set_priority */
119 NULL /* thread_self */
124 static GMutex *g_once_mutex = NULL;
125 static GCond *g_once_cond = NULL;
126 static GPrivate *g_thread_specific_private = NULL;
127 static GRealThread *g_thread_all_threads = NULL;
128 static GSList *g_thread_free_indeces = NULL;
130 G_LOCK_DEFINE_STATIC (g_thread);
132 #ifdef G_THREADS_ENABLED
133 /* This must be called only once, before any threads are created.
134 * It will only be called from g_thread_init() in -lgthread.
137 g_thread_init_glib (void)
139 /* We let the main thread (the one that calls g_thread_init) inherit
140 * the static_private data set before calling g_thread_init
142 GRealThread* main_thread = (GRealThread*) g_thread_self ();
144 /* mutex and cond creation works without g_threads_got_initialized */
145 g_once_mutex = g_mutex_new ();
146 g_once_cond = g_cond_new ();
148 /* we may only create mutex and cond in here */
149 _g_mem_thread_init_noprivate_nomessage ();
151 /* setup the basic threading system */
152 g_threads_got_initialized = TRUE;
153 g_thread_specific_private = g_private_new (g_thread_cleanup);
154 g_private_set (g_thread_specific_private, main_thread);
155 G_THREAD_UF (thread_self, (&main_thread->system_thread));
157 /* complete memory system initialization, g_private_*() works now */
158 _g_slice_thread_init_nomessage ();
160 /* accomplish log system initialization to enable messaging */
161 _g_messages_thread_init_nomessage ();
163 /* we may run full-fledged initializers from here */
164 _g_convert_thread_init ();
165 _g_rand_thread_init ();
166 _g_main_thread_init ();
167 _g_atomic_thread_init ();
168 _g_utils_thread_init ();
170 _g_win32_thread_init ();
173 #endif /* G_THREADS_ENABLED */
176 g_once_impl (GOnce *once,
180 g_mutex_lock (g_once_mutex);
182 while (once->status == G_ONCE_STATUS_PROGRESS)
183 g_cond_wait (g_once_cond, g_once_mutex);
185 if (once->status != G_ONCE_STATUS_READY)
187 once->status = G_ONCE_STATUS_PROGRESS;
188 g_mutex_unlock (g_once_mutex);
190 once->retval = func (arg);
192 g_mutex_lock (g_once_mutex);
193 once->status = G_ONCE_STATUS_READY;
194 g_cond_broadcast (g_once_cond);
197 g_mutex_unlock (g_once_mutex);
203 g_static_mutex_init (GStaticMutex *mutex)
205 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
207 g_return_if_fail (mutex);
213 g_static_mutex_get_mutex_impl (GMutex** mutex)
215 if (!g_thread_supported ())
218 g_assert (g_once_mutex);
220 g_mutex_lock (g_once_mutex);
224 GMutex *new_mutex = g_mutex_new ();
226 /* The following is a memory barrier to avoid the write
227 * to *new_mutex being reordered to after writing *mutex */
228 g_mutex_lock (new_mutex);
229 g_mutex_unlock (new_mutex);
234 g_mutex_unlock (g_once_mutex);
240 g_static_mutex_free (GStaticMutex* mutex)
242 GMutex **runtime_mutex;
244 g_return_if_fail (mutex);
246 /* The runtime_mutex is the first (or only) member of GStaticMutex,
247 * see both versions (of glibconfig.h) in configure.in */
248 runtime_mutex = ((GMutex**)mutex);
251 g_mutex_free (*runtime_mutex);
253 *runtime_mutex = NULL;
257 g_static_rec_mutex_init (GStaticRecMutex *mutex)
259 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
261 g_return_if_fail (mutex);
267 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
271 g_return_if_fail (mutex);
273 if (!g_thread_supported ())
276 G_THREAD_UF (thread_self, (&self));
278 if (g_system_thread_equal (self, mutex->owner))
283 g_static_mutex_lock (&mutex->mutex);
284 g_system_thread_assign (mutex->owner, self);
289 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
293 g_return_val_if_fail (mutex, FALSE);
295 if (!g_thread_supported ())
298 G_THREAD_UF (thread_self, (&self));
300 if (g_system_thread_equal (self, mutex->owner))
306 if (!g_static_mutex_trylock (&mutex->mutex))
309 g_system_thread_assign (mutex->owner, self);
315 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
317 g_return_if_fail (mutex);
319 if (!g_thread_supported ())
322 if (mutex->depth > 1)
327 g_system_thread_assign (mutex->owner, zero_thread);
328 g_static_mutex_unlock (&mutex->mutex);
332 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
336 g_return_if_fail (mutex);
338 if (!g_thread_supported ())
344 G_THREAD_UF (thread_self, (&self));
346 if (g_system_thread_equal (self, mutex->owner))
348 mutex->depth += depth;
351 g_static_mutex_lock (&mutex->mutex);
352 g_system_thread_assign (mutex->owner, self);
353 mutex->depth = depth;
357 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
361 g_return_val_if_fail (mutex, 0);
363 if (!g_thread_supported ())
366 depth = mutex->depth;
368 g_system_thread_assign (mutex->owner, zero_thread);
370 g_static_mutex_unlock (&mutex->mutex);
376 g_static_rec_mutex_free (GStaticRecMutex *mutex)
378 g_return_if_fail (mutex);
380 g_static_mutex_free (&mutex->mutex);
384 g_static_private_init (GStaticPrivate *private_key)
386 private_key->index = 0;
390 g_static_private_get (GStaticPrivate *private_key)
392 GRealThread *self = (GRealThread*) g_thread_self ();
395 array = self->private_data;
399 if (!private_key->index)
401 else if (private_key->index <= array->len)
402 return g_array_index (array, GStaticPrivateNode,
403 private_key->index - 1).data;
409 g_static_private_set (GStaticPrivate *private_key,
411 GDestroyNotify notify)
413 GRealThread *self = (GRealThread*) g_thread_self ();
415 static guint next_index = 0;
416 GStaticPrivateNode *node;
418 array = self->private_data;
421 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
422 self->private_data = array;
425 if (!private_key->index)
429 if (!private_key->index)
431 if (g_thread_free_indeces)
434 GPOINTER_TO_UINT (g_thread_free_indeces->data);
435 g_thread_free_indeces =
436 g_slist_delete_link (g_thread_free_indeces,
437 g_thread_free_indeces);
440 private_key->index = ++next_index;
446 if (private_key->index > array->len)
447 g_array_set_size (array, private_key->index);
449 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
452 gpointer ddata = node->data;
453 GDestroyNotify ddestroy = node->destroy;
456 node->destroy = notify;
463 node->destroy = notify;
468 g_static_private_free (GStaticPrivate *private_key)
470 guint index = private_key->index;
476 private_key->index = 0;
480 thread = g_thread_all_threads;
483 GArray *array = thread->private_data;
484 thread = thread->next;
486 if (array && index <= array->len)
488 GStaticPrivateNode *node = &g_array_index (array,
491 gpointer ddata = node->data;
492 GDestroyNotify ddestroy = node->destroy;
495 node->destroy = NULL;
505 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
506 GUINT_TO_POINTER (index));
511 g_thread_cleanup (gpointer data)
515 GRealThread* thread = data;
516 if (thread->private_data)
518 GArray* array = thread->private_data;
521 for (i = 0; i < array->len; i++ )
523 GStaticPrivateNode *node =
524 &g_array_index (array, GStaticPrivateNode, i);
526 node->destroy (node->data);
528 g_array_free (array, TRUE);
531 /* We only free the thread structure, if it isn't joinable. If
532 it is, the structure is freed in g_thread_join */
533 if (!thread->thread.joinable)
538 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
545 g_thread_all_threads = t->next;
551 /* Just to make sure, this isn't used any more */
552 g_system_thread_assign (thread->system_thread, zero_thread);
561 g_error ("The thread system is not yet initialized.");
565 g_thread_create_proxy (gpointer data)
567 GRealThread* thread = data;
571 /* This has to happen before G_LOCK, as that might call g_thread_self */
572 g_private_set (g_thread_specific_private, data);
574 /* the lock makes sure, that thread->system_thread is written,
575 before thread->thread.func is called. See g_thread_create. */
579 thread->retval = thread->thread.func (thread->thread.data);
585 g_thread_create_full (GThreadFunc func,
590 GThreadPriority priority,
594 GError *local_error = NULL;
595 g_return_val_if_fail (func, NULL);
596 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
597 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
599 result = g_new0 (GRealThread, 1);
601 result->thread.joinable = joinable;
602 result->thread.priority = priority;
603 result->thread.func = func;
604 result->thread.data = data;
605 result->private_data = NULL;
607 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
608 stack_size, joinable, bound, priority,
609 &result->system_thread, &local_error));
610 result->next = g_thread_all_threads;
611 g_thread_all_threads = result;
616 g_propagate_error (error, local_error);
621 return (GThread*) result;
625 g_thread_exit (gpointer retval)
627 GRealThread* real = (GRealThread*) g_thread_self ();
628 real->retval = retval;
629 G_THREAD_CF (thread_exit, (void)0, ());
633 g_thread_join (GThread* thread)
635 GRealThread* real = (GRealThread*) thread;
639 g_return_val_if_fail (thread, NULL);
640 g_return_val_if_fail (thread->joinable, NULL);
641 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
644 G_THREAD_UF (thread_join, (&real->system_thread));
646 retval = real->retval;
649 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
651 if (t == (GRealThread*) thread)
656 g_thread_all_threads = t->next;
662 /* Just to make sure, this isn't used any more */
663 thread->joinable = 0;
664 g_system_thread_assign (real->system_thread, zero_thread);
666 /* the thread structure for non-joinable threads is freed upon
667 thread end. We free the memory here. This will leave a loose end,
668 if a joinable thread is not joined. */
676 g_thread_set_priority (GThread* thread,
677 GThreadPriority priority)
679 GRealThread* real = (GRealThread*) thread;
681 g_return_if_fail (thread);
682 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
683 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
684 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
686 thread->priority = priority;
688 G_THREAD_CF (thread_set_priority, (void)0,
689 (&real->system_thread, priority));
695 GRealThread* thread = g_private_get (g_thread_specific_private);
699 /* If no thread data is available, provide and set one. This
700 can happen for the main thread and for threads, that are not
702 thread = g_new0 (GRealThread, 1);
703 thread->thread.joinable = FALSE; /* This is a save guess */
704 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
706 thread->thread.func = NULL;
707 thread->thread.data = NULL;
708 thread->private_data = NULL;
710 if (g_thread_supported ())
711 G_THREAD_UF (thread_self, (&thread->system_thread));
713 g_private_set (g_thread_specific_private, thread);
716 thread->next = g_thread_all_threads;
717 g_thread_all_threads = thread;
721 return (GThread*)thread;
725 g_static_rw_lock_init (GStaticRWLock* lock)
727 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
729 g_return_if_fail (lock);
735 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
738 *cond = g_cond_new ();
739 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
743 g_static_rw_lock_signal (GStaticRWLock* lock)
745 if (lock->want_to_write && lock->write_cond)
746 g_cond_signal (lock->write_cond);
747 else if (lock->want_to_read && lock->read_cond)
748 g_cond_broadcast (lock->read_cond);
752 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
754 g_return_if_fail (lock);
756 if (!g_threads_got_initialized)
759 g_static_mutex_lock (&lock->mutex);
760 lock->want_to_read++;
761 while (lock->have_writer || lock->want_to_write)
762 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
763 lock->want_to_read--;
764 lock->read_counter++;
765 g_static_mutex_unlock (&lock->mutex);
769 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
771 gboolean ret_val = FALSE;
773 g_return_val_if_fail (lock, FALSE);
775 if (!g_threads_got_initialized)
778 g_static_mutex_lock (&lock->mutex);
779 if (!lock->have_writer && !lock->want_to_write)
781 lock->read_counter++;
784 g_static_mutex_unlock (&lock->mutex);
789 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
791 g_return_if_fail (lock);
793 if (!g_threads_got_initialized)
796 g_static_mutex_lock (&lock->mutex);
797 lock->read_counter--;
798 if (lock->read_counter == 0)
799 g_static_rw_lock_signal (lock);
800 g_static_mutex_unlock (&lock->mutex);
804 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
806 g_return_if_fail (lock);
808 if (!g_threads_got_initialized)
811 g_static_mutex_lock (&lock->mutex);
812 lock->want_to_write++;
813 while (lock->have_writer || lock->read_counter)
814 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
815 lock->want_to_write--;
816 lock->have_writer = TRUE;
817 g_static_mutex_unlock (&lock->mutex);
821 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
823 gboolean ret_val = FALSE;
825 g_return_val_if_fail (lock, FALSE);
827 if (!g_threads_got_initialized)
830 g_static_mutex_lock (&lock->mutex);
831 if (!lock->have_writer && !lock->read_counter)
833 lock->have_writer = TRUE;
836 g_static_mutex_unlock (&lock->mutex);
841 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
843 g_return_if_fail (lock);
845 if (!g_threads_got_initialized)
848 g_static_mutex_lock (&lock->mutex);
849 lock->have_writer = FALSE;
850 g_static_rw_lock_signal (lock);
851 g_static_mutex_unlock (&lock->mutex);
855 g_static_rw_lock_free (GStaticRWLock* lock)
857 g_return_if_fail (lock);
861 g_cond_free (lock->read_cond);
862 lock->read_cond = NULL;
864 if (lock->write_cond)
866 g_cond_free (lock->write_cond);
867 lock->write_cond = NULL;
869 g_static_mutex_free (&lock->mutex);
874 * @thread_func: function to call for all GThread structures
875 * @user_data: second argument to @thread_func
877 * Call @thread_func on all existing #GThread structures. Note that
878 * threads may decide to exit while @thread_func is running, so
879 * without intimate knowledge about the lifetime of foreign threads,
880 * @thread_func shouldn't access the GThread* pointer passed in as
881 * first argument. However, @thread_func will not be called for threads
882 * which are known to have exited already.
884 * Due to thread lifetime checks, this function has an execution complexity
885 * which is quadratic in the number of existing threads.
890 g_thread_foreach (GFunc thread_func,
893 GSList *slist = NULL;
895 g_return_if_fail (thread_func != NULL);
896 /* snapshot the list of threads for iteration */
898 for (thread = g_thread_all_threads; thread; thread = thread->next)
899 slist = g_slist_prepend (slist, thread);
901 /* walk the list, skipping non-existant threads */
904 GSList *node = slist;
906 /* check whether the current thread still exists */
908 for (thread = g_thread_all_threads; thread; thread = thread->next)
909 if (thread == node->data)
913 thread_func (thread, user_data);
914 g_slist_free_1 (node);
918 #define __G_THREAD_C__
919 #include "galiasdef.c"