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;
82 GSystemThread system_thread;
85 typedef struct _GStaticPrivateNode GStaticPrivateNode;
86 struct _GStaticPrivateNode
89 GDestroyNotify destroy;
92 static void g_thread_cleanup (gpointer data);
93 static void g_thread_fail (void);
95 /* Global variables */
97 static GSystemThread zero_thread; /* This is initialized to all zero */
98 gboolean g_thread_use_default_impl = TRUE;
99 gboolean g_threads_got_initialized = FALSE;
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 GRealThread *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 ();
157 _g_utils_thread_init ();
159 _g_win32_thread_init ();
162 g_threads_got_initialized = TRUE;
164 g_thread_specific_private = g_private_new (g_thread_cleanup);
165 g_private_set (g_thread_specific_private, main_thread);
166 G_THREAD_UF (thread_self, (&main_thread->system_thread));
168 _g_mem_thread_private_init ();
169 _g_messages_thread_private_init ();
172 #endif /* G_THREADS_ENABLED */
175 g_once_impl (GOnce *once,
179 g_mutex_lock (g_once_mutex);
181 while (once->status == G_ONCE_STATUS_PROGRESS)
182 g_cond_wait (g_once_cond, g_once_mutex);
184 if (once->status != G_ONCE_STATUS_READY)
186 once->status = G_ONCE_STATUS_PROGRESS;
187 g_mutex_unlock (g_once_mutex);
189 once->retval = func (arg);
191 g_mutex_lock (g_once_mutex);
192 once->status = G_ONCE_STATUS_READY;
193 g_cond_broadcast (g_once_cond);
196 g_mutex_unlock (g_once_mutex);
202 g_static_mutex_init (GStaticMutex *mutex)
204 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
206 g_return_if_fail (mutex);
212 g_static_mutex_get_mutex_impl (GMutex** mutex)
214 if (!g_thread_supported ())
217 g_assert (g_once_mutex);
219 g_mutex_lock (g_once_mutex);
223 GMutex *new_mutex = g_mutex_new ();
225 /* The following is a memory barrier to avoid the write
226 * to *new_mutex being reordered to after writing *mutex */
227 g_mutex_lock (new_mutex);
228 g_mutex_unlock (new_mutex);
233 g_mutex_unlock (g_once_mutex);
239 g_static_mutex_free (GStaticMutex* mutex)
241 GMutex **runtime_mutex;
243 g_return_if_fail (mutex);
245 /* The runtime_mutex is the first (or only) member of GStaticMutex,
246 * see both versions (of glibconfig.h) in configure.in */
247 runtime_mutex = ((GMutex**)mutex);
250 g_mutex_free (*runtime_mutex);
252 *runtime_mutex = NULL;
256 g_static_rec_mutex_init (GStaticRecMutex *mutex)
258 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
260 g_return_if_fail (mutex);
266 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
270 g_return_if_fail (mutex);
272 if (!g_thread_supported ())
275 G_THREAD_UF (thread_self, (&self));
277 if (g_system_thread_equal (self, mutex->owner))
282 g_static_mutex_lock (&mutex->mutex);
283 g_system_thread_assign (mutex->owner, self);
288 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
292 g_return_val_if_fail (mutex, FALSE);
294 if (!g_thread_supported ())
297 G_THREAD_UF (thread_self, (&self));
299 if (g_system_thread_equal (self, mutex->owner))
305 if (!g_static_mutex_trylock (&mutex->mutex))
308 g_system_thread_assign (mutex->owner, self);
314 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
316 g_return_if_fail (mutex);
318 if (!g_thread_supported ())
321 if (mutex->depth > 1)
326 g_system_thread_assign (mutex->owner, zero_thread);
327 g_static_mutex_unlock (&mutex->mutex);
331 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
335 g_return_if_fail (mutex);
337 if (!g_thread_supported ())
343 G_THREAD_UF (thread_self, (&self));
345 if (g_system_thread_equal (self, mutex->owner))
347 mutex->depth += depth;
350 g_static_mutex_lock (&mutex->mutex);
351 g_system_thread_assign (mutex->owner, self);
352 mutex->depth = depth;
356 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
360 g_return_val_if_fail (mutex, 0);
362 if (!g_thread_supported ())
365 depth = mutex->depth;
367 g_system_thread_assign (mutex->owner, zero_thread);
369 g_static_mutex_unlock (&mutex->mutex);
375 g_static_rec_mutex_free (GStaticRecMutex *mutex)
377 g_return_if_fail (mutex);
379 g_static_mutex_free (&mutex->mutex);
383 g_static_private_init (GStaticPrivate *private_key)
385 private_key->index = 0;
389 g_static_private_get (GStaticPrivate *private_key)
391 GRealThread *self = (GRealThread*) g_thread_self ();
394 array = self->private_data;
398 if (!private_key->index)
400 else if (private_key->index <= array->len)
401 return g_array_index (array, GStaticPrivateNode,
402 private_key->index - 1).data;
408 g_static_private_set (GStaticPrivate *private_key,
410 GDestroyNotify notify)
412 GRealThread *self = (GRealThread*) g_thread_self ();
414 static guint next_index = 0;
415 GStaticPrivateNode *node;
417 array = self->private_data;
420 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
421 self->private_data = array;
424 if (!private_key->index)
428 if (!private_key->index)
430 if (g_thread_free_indeces)
433 GPOINTER_TO_UINT (g_thread_free_indeces->data);
434 g_thread_free_indeces =
435 g_slist_delete_link (g_thread_free_indeces,
436 g_thread_free_indeces);
439 private_key->index = ++next_index;
445 if (private_key->index > array->len)
446 g_array_set_size (array, private_key->index);
448 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
451 gpointer ddata = node->data;
452 GDestroyNotify ddestroy = node->destroy;
455 node->destroy = notify;
462 node->destroy = notify;
467 g_static_private_free (GStaticPrivate *private_key)
469 guint index = private_key->index;
475 private_key->index = 0;
479 thread = g_thread_all_threads;
482 GArray *array = thread->private_data;
483 thread = thread->next;
485 if (array && index <= array->len)
487 GStaticPrivateNode *node = &g_array_index (array,
490 gpointer ddata = node->data;
491 GDestroyNotify ddestroy = node->destroy;
494 node->destroy = NULL;
504 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
505 GUINT_TO_POINTER (index));
510 g_thread_cleanup (gpointer data)
514 GRealThread* thread = data;
515 if (thread->private_data)
517 GArray* array = thread->private_data;
520 for (i = 0; i < array->len; i++ )
522 GStaticPrivateNode *node =
523 &g_array_index (array, GStaticPrivateNode, i);
525 node->destroy (node->data);
527 g_array_free (array, TRUE);
530 /* We only free the thread structure, if it isn't joinable. If
531 it is, the structure is freed in g_thread_join */
532 if (!thread->thread.joinable)
537 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
544 g_thread_all_threads = t->next;
550 /* Just to make sure, this isn't used any more */
551 g_system_thread_assign (thread->system_thread, zero_thread);
560 g_error ("The thread system is not yet initialized.");
564 g_thread_create_proxy (gpointer data)
566 GRealThread* thread = data;
570 /* This has to happen before G_LOCK, as that might call g_thread_self */
571 g_private_set (g_thread_specific_private, data);
573 /* the lock makes sure, that thread->system_thread is written,
574 before thread->thread.func is called. See g_thread_create. */
578 thread->retval = thread->thread.func (thread->thread.data);
584 g_thread_create_full (GThreadFunc func,
589 GThreadPriority priority,
593 GError *local_error = NULL;
594 g_return_val_if_fail (func, NULL);
595 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
596 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
598 result = g_new0 (GRealThread, 1);
600 result->thread.joinable = joinable;
601 result->thread.priority = priority;
602 result->thread.func = func;
603 result->thread.data = data;
604 result->private_data = NULL;
606 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
607 stack_size, joinable, bound, priority,
608 &result->system_thread, &local_error));
609 result->next = g_thread_all_threads;
610 g_thread_all_threads = result;
615 g_propagate_error (error, local_error);
620 return (GThread*) result;
624 g_thread_exit (gpointer retval)
626 GRealThread* real = (GRealThread*) g_thread_self ();
627 real->retval = retval;
628 G_THREAD_CF (thread_exit, (void)0, ());
632 g_thread_join (GThread* thread)
634 GRealThread* real = (GRealThread*) thread;
638 g_return_val_if_fail (thread, NULL);
639 g_return_val_if_fail (thread->joinable, NULL);
640 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
643 G_THREAD_UF (thread_join, (&real->system_thread));
645 retval = real->retval;
648 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
650 if (t == (GRealThread*) thread)
655 g_thread_all_threads = t->next;
661 /* Just to make sure, this isn't used any more */
662 thread->joinable = 0;
663 g_system_thread_assign (real->system_thread, zero_thread);
665 /* the thread structure for non-joinable threads is freed upon
666 thread end. We free the memory here. This will leave a loose end,
667 if a joinable thread is not joined. */
675 g_thread_set_priority (GThread* thread,
676 GThreadPriority priority)
678 GRealThread* real = (GRealThread*) thread;
680 g_return_if_fail (thread);
681 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
682 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
683 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
685 thread->priority = priority;
687 G_THREAD_CF (thread_set_priority, (void)0,
688 (&real->system_thread, priority));
694 GRealThread* thread = g_private_get (g_thread_specific_private);
698 /* If no thread data is available, provide and set one. This
699 can happen for the main thread and for threads, that are not
701 thread = g_new0 (GRealThread, 1);
702 thread->thread.joinable = FALSE; /* This is a save guess */
703 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
705 thread->thread.func = NULL;
706 thread->thread.data = NULL;
707 thread->private_data = NULL;
709 if (g_thread_supported ())
710 G_THREAD_UF (thread_self, (&thread->system_thread));
712 g_private_set (g_thread_specific_private, thread);
715 thread->next = g_thread_all_threads;
716 g_thread_all_threads = thread;
720 return (GThread*)thread;
724 g_static_rw_lock_init (GStaticRWLock* lock)
726 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
728 g_return_if_fail (lock);
734 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
737 *cond = g_cond_new ();
738 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
742 g_static_rw_lock_signal (GStaticRWLock* lock)
744 if (lock->want_to_write && lock->write_cond)
745 g_cond_signal (lock->write_cond);
746 else if (lock->want_to_read && lock->read_cond)
747 g_cond_broadcast (lock->read_cond);
751 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
753 g_return_if_fail (lock);
755 if (!g_threads_got_initialized)
758 g_static_mutex_lock (&lock->mutex);
759 lock->want_to_read++;
760 while (lock->have_writer || lock->want_to_write)
761 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
762 lock->want_to_read--;
763 lock->read_counter++;
764 g_static_mutex_unlock (&lock->mutex);
768 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
770 gboolean ret_val = FALSE;
772 g_return_val_if_fail (lock, FALSE);
774 if (!g_threads_got_initialized)
777 g_static_mutex_lock (&lock->mutex);
778 if (!lock->have_writer && !lock->want_to_write)
780 lock->read_counter++;
783 g_static_mutex_unlock (&lock->mutex);
788 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
790 g_return_if_fail (lock);
792 if (!g_threads_got_initialized)
795 g_static_mutex_lock (&lock->mutex);
796 lock->read_counter--;
797 if (lock->read_counter == 0)
798 g_static_rw_lock_signal (lock);
799 g_static_mutex_unlock (&lock->mutex);
803 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
805 g_return_if_fail (lock);
807 if (!g_threads_got_initialized)
810 g_static_mutex_lock (&lock->mutex);
811 lock->want_to_write++;
812 while (lock->have_writer || lock->read_counter)
813 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
814 lock->want_to_write--;
815 lock->have_writer = TRUE;
816 g_static_mutex_unlock (&lock->mutex);
820 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
822 gboolean ret_val = FALSE;
824 g_return_val_if_fail (lock, FALSE);
826 if (!g_threads_got_initialized)
829 g_static_mutex_lock (&lock->mutex);
830 if (!lock->have_writer && !lock->read_counter)
832 lock->have_writer = TRUE;
835 g_static_mutex_unlock (&lock->mutex);
840 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
842 g_return_if_fail (lock);
844 if (!g_threads_got_initialized)
847 g_static_mutex_lock (&lock->mutex);
848 lock->have_writer = FALSE;
849 g_static_rw_lock_signal (lock);
850 g_static_mutex_unlock (&lock->mutex);
854 g_static_rw_lock_free (GStaticRWLock* lock)
856 g_return_if_fail (lock);
860 g_cond_free (lock->read_cond);
861 lock->read_cond = NULL;
863 if (lock->write_cond)
865 g_cond_free (lock->write_cond);
866 lock->write_cond = NULL;
868 g_static_mutex_free (&lock->mutex);
872 * Memory allocation can't use the regular GPrivate
873 * API, since that relies on GArray, which uses
877 _g_thread_mem_private_get (GThread *thread)
879 GRealThread *real_thread = (GRealThread*) thread;
881 return real_thread->mem_private;
885 _g_thread_mem_private_set (GThread *thread,
888 GRealThread *real_thread = (GRealThread*) thread;
890 real_thread->mem_private = data;
895 * @thread_func: function to call for all GThread structures
896 * @user_data: second argument to @thread_func
897 * Call @thread_func on all existing GThread structures. Note that
898 * threads may decide to exit while @thread_func is running, so
899 * without intimate knowledge about the lifetime of foreign threads,
900 * @thread_func shouldn't access the GThread* pointer passed in as
901 * first argument. However, @thread_func will not be called for threads
902 * which are known to have exited already.
903 * Due to thread lifetime checks, this function has an execution complexity
904 * which is quadratic in the number of existing threads.
907 g_thread_foreach (GFunc thread_func,
910 GSList *slist = NULL;
912 g_return_if_fail (thread_func != NULL);
913 /* snapshot the list of threads for iteration */
915 for (thread = g_thread_all_threads; thread; thread = thread->next)
916 slist = g_slist_prepend (slist, thread);
918 /* walk the list, skipping non-existant threads */
921 GSList *node = slist;
923 /* check whether the current thread still exists */
925 for (thread = g_thread_all_threads; thread; thread = thread->next)
926 if (thread == node->data)
930 thread_func (thread, user_data);
931 g_slist_free_1 (node);
935 #define __G_THREAD_C__
936 #include "galiasdef.c"