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/.
35 /* implement gthread.h's inline functions */
36 #define G_IMPLEMENT_INLINES 1
37 #define __G_THREAD_C__
42 #include "gthreadprivate.h"
53 #endif /* G_OS_WIN32 */
60 g_thread_error_quark (void)
62 return g_quark_from_static_string ("g_thread_error");
65 /* Keep this in sync with GRealThread in gmain.c! */
66 typedef struct _GRealThread GRealThread;
70 gpointer private_data;
73 GSystemThread system_thread;
76 typedef struct _GStaticPrivateNode GStaticPrivateNode;
77 struct _GStaticPrivateNode
80 GDestroyNotify destroy;
83 static void g_thread_cleanup (gpointer data);
84 static void g_thread_fail (void);
85 static guint64 gettime (void);
87 guint64 (*g_thread_gettime) (void) = gettime;
89 /* Global variables */
91 static GSystemThread zero_thread; /* This is initialized to all zero */
92 gboolean g_thread_use_default_impl = TRUE;
93 gboolean g_threads_got_initialized = FALSE;
95 GThreadFunctions g_thread_functions_for_glib_use = {
96 (GMutex*(*)())g_thread_fail, /* mutex_new */
97 NULL, /* mutex_lock */
98 NULL, /* mutex_trylock */
99 NULL, /* mutex_unlock */
100 NULL, /* mutex_free */
101 (GCond*(*)())g_thread_fail, /* cond_new */
102 NULL, /* cond_signal */
103 NULL, /* cond_broadcast */
104 NULL, /* cond_wait */
105 NULL, /* cond_timed_wait */
106 NULL, /* cond_free */
107 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
108 NULL, /* private_get */
109 NULL, /* private_set */
110 (void(*)(GThreadFunc, gpointer, gulong,
111 gboolean, gboolean, GThreadPriority,
112 gpointer, GError**))g_thread_fail, /* thread_create */
113 NULL, /* thread_yield */
114 NULL, /* thread_join */
115 NULL, /* thread_exit */
116 NULL, /* thread_set_priority */
117 NULL, /* thread_self */
118 NULL /* thread_equal */
123 static GMutex *g_once_mutex = NULL;
124 static GCond *g_once_cond = NULL;
125 static GPrivate *g_thread_specific_private = NULL;
126 static GRealThread *g_thread_all_threads = NULL;
127 static GSList *g_thread_free_indeces = NULL;
128 static GSList* g_once_init_list = 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_atomic_thread_init ();
165 _g_convert_thread_init ();
166 _g_rand_thread_init ();
167 _g_main_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_once_init_enter_impl (volatile gsize *value_location)
206 g_mutex_lock (g_once_mutex);
207 if (!g_once_init_list || !g_slist_find (g_once_init_list, (void*) value_location))
209 g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
214 while (g_slist_find (g_once_init_list, (void*) value_location))
215 g_cond_wait (g_once_cond, g_once_mutex);
218 g_mutex_unlock (g_once_mutex);
223 g_once_init_leave (volatile gsize *value_location,
224 gsize initialization_value)
226 g_return_if_fail (g_atomic_pointer_get (value_location) == 0);
227 g_return_if_fail (initialization_value != 0);
228 g_return_if_fail (g_once_init_list != NULL);
230 g_atomic_pointer_set (value_location, initialization_value);
231 g_mutex_lock (g_once_mutex);
232 g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
233 g_cond_broadcast (g_once_cond);
234 g_mutex_unlock (g_once_mutex);
238 g_static_mutex_init (GStaticMutex *mutex)
240 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
242 g_return_if_fail (mutex);
248 g_static_mutex_get_mutex_impl (GMutex** mutex)
250 if (!g_thread_supported ())
253 g_assert (g_once_mutex);
255 g_mutex_lock (g_once_mutex);
258 g_atomic_pointer_set (mutex, g_mutex_new());
260 g_mutex_unlock (g_once_mutex);
266 g_static_mutex_free (GStaticMutex* mutex)
268 GMutex **runtime_mutex;
270 g_return_if_fail (mutex);
272 /* The runtime_mutex is the first (or only) member of GStaticMutex,
273 * see both versions (of glibconfig.h) in configure.in. Note, that
274 * this variable is NULL, if g_thread_init() hasn't been called or
275 * if we're using the default thread implementation and it provides
277 runtime_mutex = ((GMutex**)mutex);
280 g_mutex_free (*runtime_mutex);
282 *runtime_mutex = NULL;
286 g_static_rec_mutex_init (GStaticRecMutex *mutex)
288 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
290 g_return_if_fail (mutex);
296 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
300 g_return_if_fail (mutex);
302 if (!g_thread_supported ())
305 G_THREAD_UF (thread_self, (&self));
307 if (g_system_thread_equal (self, mutex->owner))
312 g_static_mutex_lock (&mutex->mutex);
313 g_system_thread_assign (mutex->owner, self);
318 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
322 g_return_val_if_fail (mutex, FALSE);
324 if (!g_thread_supported ())
327 G_THREAD_UF (thread_self, (&self));
329 if (g_system_thread_equal (self, mutex->owner))
335 if (!g_static_mutex_trylock (&mutex->mutex))
338 g_system_thread_assign (mutex->owner, self);
344 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
346 g_return_if_fail (mutex);
348 if (!g_thread_supported ())
351 if (mutex->depth > 1)
356 g_system_thread_assign (mutex->owner, zero_thread);
357 g_static_mutex_unlock (&mutex->mutex);
361 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
365 g_return_if_fail (mutex);
367 if (!g_thread_supported ())
373 G_THREAD_UF (thread_self, (&self));
375 if (g_system_thread_equal (self, mutex->owner))
377 mutex->depth += depth;
380 g_static_mutex_lock (&mutex->mutex);
381 g_system_thread_assign (mutex->owner, self);
382 mutex->depth = depth;
386 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
390 g_return_val_if_fail (mutex, 0);
392 if (!g_thread_supported ())
395 depth = mutex->depth;
397 g_system_thread_assign (mutex->owner, zero_thread);
399 g_static_mutex_unlock (&mutex->mutex);
405 g_static_rec_mutex_free (GStaticRecMutex *mutex)
407 g_return_if_fail (mutex);
409 g_static_mutex_free (&mutex->mutex);
413 g_static_private_init (GStaticPrivate *private_key)
415 private_key->index = 0;
419 g_static_private_get (GStaticPrivate *private_key)
421 GRealThread *self = (GRealThread*) g_thread_self ();
424 array = self->private_data;
428 if (!private_key->index)
430 else if (private_key->index <= array->len)
431 return g_array_index (array, GStaticPrivateNode,
432 private_key->index - 1).data;
438 g_static_private_set (GStaticPrivate *private_key,
440 GDestroyNotify notify)
442 GRealThread *self = (GRealThread*) g_thread_self ();
444 static guint next_index = 0;
445 GStaticPrivateNode *node;
447 array = self->private_data;
450 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
451 self->private_data = array;
454 if (!private_key->index)
458 if (!private_key->index)
460 if (g_thread_free_indeces)
463 GPOINTER_TO_UINT (g_thread_free_indeces->data);
464 g_thread_free_indeces =
465 g_slist_delete_link (g_thread_free_indeces,
466 g_thread_free_indeces);
469 private_key->index = ++next_index;
475 if (private_key->index > array->len)
476 g_array_set_size (array, private_key->index);
478 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
481 gpointer ddata = node->data;
482 GDestroyNotify ddestroy = node->destroy;
485 node->destroy = notify;
492 node->destroy = notify;
497 g_static_private_free (GStaticPrivate *private_key)
499 guint index = private_key->index;
505 private_key->index = 0;
509 thread = g_thread_all_threads;
512 GArray *array = thread->private_data;
513 thread = thread->next;
515 if (array && index <= array->len)
517 GStaticPrivateNode *node = &g_array_index (array,
520 gpointer ddata = node->data;
521 GDestroyNotify ddestroy = node->destroy;
524 node->destroy = NULL;
534 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
535 GUINT_TO_POINTER (index));
540 g_thread_cleanup (gpointer data)
544 GRealThread* thread = data;
545 if (thread->private_data)
547 GArray* array = thread->private_data;
550 for (i = 0; i < array->len; i++ )
552 GStaticPrivateNode *node =
553 &g_array_index (array, GStaticPrivateNode, i);
555 node->destroy (node->data);
557 g_array_free (array, TRUE);
560 /* We only free the thread structure, if it isn't joinable. If
561 it is, the structure is freed in g_thread_join */
562 if (!thread->thread.joinable)
567 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
574 g_thread_all_threads = t->next;
580 /* Just to make sure, this isn't used any more */
581 g_system_thread_assign (thread->system_thread, zero_thread);
590 g_error ("The thread system is not yet initialized.");
593 #define G_NSEC_PER_SEC 1000000000
601 /* Returns 100s of nanoseconds since start of 1601 */
602 GetSystemTimeAsFileTime ((FILETIME *)&v);
604 /* Offset to Unix epoch */
605 v -= G_GINT64_CONSTANT (116444736000000000);
606 /* Convert to nanoseconds */
613 gettimeofday (&tv, NULL);
615 return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
620 g_thread_create_proxy (gpointer data)
622 GRealThread* thread = data;
626 /* This has to happen before G_LOCK, as that might call g_thread_self */
627 g_private_set (g_thread_specific_private, data);
629 /* the lock makes sure, that thread->system_thread is written,
630 before thread->thread.func is called. See g_thread_create. */
634 thread->retval = thread->thread.func (thread->thread.data);
640 g_thread_create_full (GThreadFunc func,
645 GThreadPriority priority,
649 GError *local_error = NULL;
650 g_return_val_if_fail (func, NULL);
651 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
652 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
654 result = g_new0 (GRealThread, 1);
656 result->thread.joinable = joinable;
657 result->thread.priority = priority;
658 result->thread.func = func;
659 result->thread.data = data;
660 result->private_data = NULL;
662 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
663 stack_size, joinable, bound, priority,
664 &result->system_thread, &local_error));
665 result->next = g_thread_all_threads;
666 g_thread_all_threads = result;
671 g_propagate_error (error, local_error);
676 return (GThread*) result;
680 g_thread_exit (gpointer retval)
682 GRealThread* real = (GRealThread*) g_thread_self ();
683 real->retval = retval;
684 G_THREAD_CF (thread_exit, (void)0, ());
688 g_thread_join (GThread* thread)
690 GRealThread* real = (GRealThread*) thread;
694 g_return_val_if_fail (thread, NULL);
695 g_return_val_if_fail (thread->joinable, NULL);
696 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
699 G_THREAD_UF (thread_join, (&real->system_thread));
701 retval = real->retval;
704 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
706 if (t == (GRealThread*) thread)
711 g_thread_all_threads = t->next;
717 /* Just to make sure, this isn't used any more */
718 thread->joinable = 0;
719 g_system_thread_assign (real->system_thread, zero_thread);
721 /* the thread structure for non-joinable threads is freed upon
722 thread end. We free the memory here. This will leave a loose end,
723 if a joinable thread is not joined. */
731 g_thread_set_priority (GThread* thread,
732 GThreadPriority priority)
734 GRealThread* real = (GRealThread*) thread;
736 g_return_if_fail (thread);
737 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
738 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
739 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
741 thread->priority = priority;
743 G_THREAD_CF (thread_set_priority, (void)0,
744 (&real->system_thread, priority));
750 GRealThread* thread = g_private_get (g_thread_specific_private);
754 /* If no thread data is available, provide and set one. This
755 can happen for the main thread and for threads, that are not
757 thread = g_new0 (GRealThread, 1);
758 thread->thread.joinable = FALSE; /* This is a save guess */
759 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
761 thread->thread.func = NULL;
762 thread->thread.data = NULL;
763 thread->private_data = NULL;
765 if (g_thread_supported ())
766 G_THREAD_UF (thread_self, (&thread->system_thread));
768 g_private_set (g_thread_specific_private, thread);
771 thread->next = g_thread_all_threads;
772 g_thread_all_threads = thread;
776 return (GThread*)thread;
780 g_static_rw_lock_init (GStaticRWLock* lock)
782 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
784 g_return_if_fail (lock);
790 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
793 *cond = g_cond_new ();
794 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
798 g_static_rw_lock_signal (GStaticRWLock* lock)
800 if (lock->want_to_write && lock->write_cond)
801 g_cond_signal (lock->write_cond);
802 else if (lock->want_to_read && lock->read_cond)
803 g_cond_broadcast (lock->read_cond);
807 g_static_rw_lock_reader_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_read++;
816 while (lock->have_writer || lock->want_to_write)
817 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
818 lock->want_to_read--;
819 lock->read_counter++;
820 g_static_mutex_unlock (&lock->mutex);
824 g_static_rw_lock_reader_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->want_to_write)
836 lock->read_counter++;
839 g_static_mutex_unlock (&lock->mutex);
844 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
846 g_return_if_fail (lock);
848 if (!g_threads_got_initialized)
851 g_static_mutex_lock (&lock->mutex);
852 lock->read_counter--;
853 if (lock->read_counter == 0)
854 g_static_rw_lock_signal (lock);
855 g_static_mutex_unlock (&lock->mutex);
859 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
861 g_return_if_fail (lock);
863 if (!g_threads_got_initialized)
866 g_static_mutex_lock (&lock->mutex);
867 lock->want_to_write++;
868 while (lock->have_writer || lock->read_counter)
869 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
870 lock->want_to_write--;
871 lock->have_writer = TRUE;
872 g_static_mutex_unlock (&lock->mutex);
876 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
878 gboolean ret_val = FALSE;
880 g_return_val_if_fail (lock, FALSE);
882 if (!g_threads_got_initialized)
885 g_static_mutex_lock (&lock->mutex);
886 if (!lock->have_writer && !lock->read_counter)
888 lock->have_writer = TRUE;
891 g_static_mutex_unlock (&lock->mutex);
896 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
898 g_return_if_fail (lock);
900 if (!g_threads_got_initialized)
903 g_static_mutex_lock (&lock->mutex);
904 lock->have_writer = FALSE;
905 g_static_rw_lock_signal (lock);
906 g_static_mutex_unlock (&lock->mutex);
910 g_static_rw_lock_free (GStaticRWLock* lock)
912 g_return_if_fail (lock);
916 g_cond_free (lock->read_cond);
917 lock->read_cond = NULL;
919 if (lock->write_cond)
921 g_cond_free (lock->write_cond);
922 lock->write_cond = NULL;
924 g_static_mutex_free (&lock->mutex);
929 * @thread_func: function to call for all GThread structures
930 * @user_data: second argument to @thread_func
932 * Call @thread_func on all existing #GThread structures. Note that
933 * threads may decide to exit while @thread_func is running, so
934 * without intimate knowledge about the lifetime of foreign threads,
935 * @thread_func shouldn't access the GThread* pointer passed in as
936 * first argument. However, @thread_func will not be called for threads
937 * which are known to have exited already.
939 * Due to thread lifetime checks, this function has an execution complexity
940 * which is quadratic in the number of existing threads.
945 g_thread_foreach (GFunc thread_func,
948 GSList *slist = NULL;
950 g_return_if_fail (thread_func != NULL);
951 /* snapshot the list of threads for iteration */
953 for (thread = g_thread_all_threads; thread; thread = thread->next)
954 slist = g_slist_prepend (slist, thread);
956 /* walk the list, skipping non-existant threads */
959 GSList *node = slist;
961 /* check whether the current thread still exists */
963 for (thread = g_thread_all_threads; thread; thread = thread->next)
964 if (thread == node->data)
968 thread_func (thread, user_data);
969 g_slist_free_1 (node);
973 #define __G_THREAD_C__
974 #include "galiasdef.c"