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/.
38 #include "gthreadprivate.h"
49 #endif /* G_OS_WIN32 */
56 g_thread_error_quark (void)
58 return g_quark_from_static_string ("g_thread_error");
61 /* Keep this in sync with GRealThread in gmain.c! */
62 typedef struct _GRealThread GRealThread;
66 gpointer private_data;
69 GSystemThread system_thread;
72 typedef struct _GStaticPrivateNode GStaticPrivateNode;
73 struct _GStaticPrivateNode
76 GDestroyNotify destroy;
79 static void g_thread_cleanup (gpointer data);
80 static void g_thread_fail (void);
81 static guint64 gettime (void);
83 guint64 (*g_thread_gettime) (void) = gettime;
85 /* Global variables */
87 static GSystemThread zero_thread; /* This is initialized to all zero */
88 gboolean g_thread_use_default_impl = TRUE;
89 gboolean g_threads_got_initialized = FALSE;
91 GThreadFunctions g_thread_functions_for_glib_use = {
92 (GMutex*(*)())g_thread_fail, /* mutex_new */
93 NULL, /* mutex_lock */
94 NULL, /* mutex_trylock */
95 NULL, /* mutex_unlock */
96 NULL, /* mutex_free */
97 (GCond*(*)())g_thread_fail, /* cond_new */
98 NULL, /* cond_signal */
99 NULL, /* cond_broadcast */
100 NULL, /* cond_wait */
101 NULL, /* cond_timed_wait */
102 NULL, /* cond_free */
103 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
104 NULL, /* private_get */
105 NULL, /* private_set */
106 (void(*)(GThreadFunc, gpointer, gulong,
107 gboolean, gboolean, GThreadPriority,
108 gpointer, GError**))g_thread_fail, /* thread_create */
109 NULL, /* thread_yield */
110 NULL, /* thread_join */
111 NULL, /* thread_exit */
112 NULL, /* thread_set_priority */
113 NULL, /* thread_self */
114 NULL /* thread_equal */
119 static GMutex *g_once_mutex = NULL;
120 static GCond *g_once_cond = NULL;
121 static GPrivate *g_thread_specific_private = NULL;
122 static GRealThread *g_thread_all_threads = NULL;
123 static GSList *g_thread_free_indeces = NULL;
125 G_LOCK_DEFINE_STATIC (g_thread);
127 #ifdef G_THREADS_ENABLED
128 /* This must be called only once, before any threads are created.
129 * It will only be called from g_thread_init() in -lgthread.
132 g_thread_init_glib (void)
134 /* We let the main thread (the one that calls g_thread_init) inherit
135 * the static_private data set before calling g_thread_init
137 GRealThread* main_thread = (GRealThread*) g_thread_self ();
139 /* mutex and cond creation works without g_threads_got_initialized */
140 g_once_mutex = g_mutex_new ();
141 g_once_cond = g_cond_new ();
143 /* we may only create mutex and cond in here */
144 _g_mem_thread_init_noprivate_nomessage ();
146 /* setup the basic threading system */
147 g_threads_got_initialized = TRUE;
148 g_thread_specific_private = g_private_new (g_thread_cleanup);
149 g_private_set (g_thread_specific_private, main_thread);
150 G_THREAD_UF (thread_self, (&main_thread->system_thread));
152 /* complete memory system initialization, g_private_*() works now */
153 _g_slice_thread_init_nomessage ();
155 /* accomplish log system initialization to enable messaging */
156 _g_messages_thread_init_nomessage ();
158 /* we may run full-fledged initializers from here */
159 _g_atomic_thread_init ();
160 _g_convert_thread_init ();
161 _g_rand_thread_init ();
162 _g_main_thread_init ();
163 _g_utils_thread_init ();
165 _g_win32_thread_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 const 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);
218 g_atomic_pointer_set (mutex, g_mutex_new());
220 g_mutex_unlock (g_once_mutex);
226 g_static_mutex_free (GStaticMutex* mutex)
228 GMutex **runtime_mutex;
230 g_return_if_fail (mutex);
232 /* The runtime_mutex is the first (or only) member of GStaticMutex,
233 * see both versions (of glibconfig.h) in configure.in */
234 runtime_mutex = ((GMutex**)mutex);
237 g_mutex_free (*runtime_mutex);
239 *runtime_mutex = NULL;
243 g_static_rec_mutex_init (GStaticRecMutex *mutex)
245 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
247 g_return_if_fail (mutex);
253 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
257 g_return_if_fail (mutex);
259 if (!g_thread_supported ())
262 G_THREAD_UF (thread_self, (&self));
264 if (g_system_thread_equal (self, mutex->owner))
269 g_static_mutex_lock (&mutex->mutex);
270 g_system_thread_assign (mutex->owner, self);
275 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
279 g_return_val_if_fail (mutex, FALSE);
281 if (!g_thread_supported ())
284 G_THREAD_UF (thread_self, (&self));
286 if (g_system_thread_equal (self, mutex->owner))
292 if (!g_static_mutex_trylock (&mutex->mutex))
295 g_system_thread_assign (mutex->owner, self);
301 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
303 g_return_if_fail (mutex);
305 if (!g_thread_supported ())
308 if (mutex->depth > 1)
313 g_system_thread_assign (mutex->owner, zero_thread);
314 g_static_mutex_unlock (&mutex->mutex);
318 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
322 g_return_if_fail (mutex);
324 if (!g_thread_supported ())
330 G_THREAD_UF (thread_self, (&self));
332 if (g_system_thread_equal (self, mutex->owner))
334 mutex->depth += depth;
337 g_static_mutex_lock (&mutex->mutex);
338 g_system_thread_assign (mutex->owner, self);
339 mutex->depth = depth;
343 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
347 g_return_val_if_fail (mutex, 0);
349 if (!g_thread_supported ())
352 depth = mutex->depth;
354 g_system_thread_assign (mutex->owner, zero_thread);
356 g_static_mutex_unlock (&mutex->mutex);
362 g_static_rec_mutex_free (GStaticRecMutex *mutex)
364 g_return_if_fail (mutex);
366 g_static_mutex_free (&mutex->mutex);
370 g_static_private_init (GStaticPrivate *private_key)
372 private_key->index = 0;
376 g_static_private_get (GStaticPrivate *private_key)
378 GRealThread *self = (GRealThread*) g_thread_self ();
381 array = self->private_data;
385 if (!private_key->index)
387 else if (private_key->index <= array->len)
388 return g_array_index (array, GStaticPrivateNode,
389 private_key->index - 1).data;
395 g_static_private_set (GStaticPrivate *private_key,
397 GDestroyNotify notify)
399 GRealThread *self = (GRealThread*) g_thread_self ();
401 static guint next_index = 0;
402 GStaticPrivateNode *node;
404 array = self->private_data;
407 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
408 self->private_data = array;
411 if (!private_key->index)
415 if (!private_key->index)
417 if (g_thread_free_indeces)
420 GPOINTER_TO_UINT (g_thread_free_indeces->data);
421 g_thread_free_indeces =
422 g_slist_delete_link (g_thread_free_indeces,
423 g_thread_free_indeces);
426 private_key->index = ++next_index;
432 if (private_key->index > array->len)
433 g_array_set_size (array, private_key->index);
435 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
438 gpointer ddata = node->data;
439 GDestroyNotify ddestroy = node->destroy;
442 node->destroy = notify;
449 node->destroy = notify;
454 g_static_private_free (GStaticPrivate *private_key)
456 guint index = private_key->index;
462 private_key->index = 0;
466 thread = g_thread_all_threads;
469 GArray *array = thread->private_data;
470 thread = thread->next;
472 if (array && index <= array->len)
474 GStaticPrivateNode *node = &g_array_index (array,
477 gpointer ddata = node->data;
478 GDestroyNotify ddestroy = node->destroy;
481 node->destroy = NULL;
491 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
492 GUINT_TO_POINTER (index));
497 g_thread_cleanup (gpointer data)
501 GRealThread* thread = data;
502 if (thread->private_data)
504 GArray* array = thread->private_data;
507 for (i = 0; i < array->len; i++ )
509 GStaticPrivateNode *node =
510 &g_array_index (array, GStaticPrivateNode, i);
512 node->destroy (node->data);
514 g_array_free (array, TRUE);
517 /* We only free the thread structure, if it isn't joinable. If
518 it is, the structure is freed in g_thread_join */
519 if (!thread->thread.joinable)
524 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
531 g_thread_all_threads = t->next;
537 /* Just to make sure, this isn't used any more */
538 g_system_thread_assign (thread->system_thread, zero_thread);
547 g_error ("The thread system is not yet initialized.");
550 #define G_NSEC_PER_SEC 1000000000
558 /* Returns 100s of nanoseconds since start of 1601 */
559 GetSystemTimeAsFileTime ((FILETIME *)&v);
561 /* Offset to Unix epoch */
562 v -= G_GINT64_CONSTANT (116444736000000000);
563 /* Convert to nanoseconds */
570 gettimeofday (&tv, NULL);
572 return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
577 g_thread_create_proxy (gpointer data)
579 GRealThread* thread = data;
583 /* This has to happen before G_LOCK, as that might call g_thread_self */
584 g_private_set (g_thread_specific_private, data);
586 /* the lock makes sure, that thread->system_thread is written,
587 before thread->thread.func is called. See g_thread_create. */
591 thread->retval = thread->thread.func (thread->thread.data);
597 g_thread_create_full (GThreadFunc func,
602 GThreadPriority priority,
606 GError *local_error = NULL;
607 g_return_val_if_fail (func, NULL);
608 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
609 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
611 result = g_new0 (GRealThread, 1);
613 result->thread.joinable = joinable;
614 result->thread.priority = priority;
615 result->thread.func = func;
616 result->thread.data = data;
617 result->private_data = NULL;
619 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
620 stack_size, joinable, bound, priority,
621 &result->system_thread, &local_error));
622 result->next = g_thread_all_threads;
623 g_thread_all_threads = result;
628 g_propagate_error (error, local_error);
633 return (GThread*) result;
637 g_thread_exit (gpointer retval)
639 GRealThread* real = (GRealThread*) g_thread_self ();
640 real->retval = retval;
641 G_THREAD_CF (thread_exit, (void)0, ());
645 g_thread_join (GThread* thread)
647 GRealThread* real = (GRealThread*) thread;
651 g_return_val_if_fail (thread, NULL);
652 g_return_val_if_fail (thread->joinable, NULL);
653 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
656 G_THREAD_UF (thread_join, (&real->system_thread));
658 retval = real->retval;
661 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
663 if (t == (GRealThread*) thread)
668 g_thread_all_threads = t->next;
674 /* Just to make sure, this isn't used any more */
675 thread->joinable = 0;
676 g_system_thread_assign (real->system_thread, zero_thread);
678 /* the thread structure for non-joinable threads is freed upon
679 thread end. We free the memory here. This will leave a loose end,
680 if a joinable thread is not joined. */
688 g_thread_set_priority (GThread* thread,
689 GThreadPriority priority)
691 GRealThread* real = (GRealThread*) thread;
693 g_return_if_fail (thread);
694 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
695 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
696 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
698 thread->priority = priority;
700 G_THREAD_CF (thread_set_priority, (void)0,
701 (&real->system_thread, priority));
707 GRealThread* thread = g_private_get (g_thread_specific_private);
711 /* If no thread data is available, provide and set one. This
712 can happen for the main thread and for threads, that are not
714 thread = g_new0 (GRealThread, 1);
715 thread->thread.joinable = FALSE; /* This is a save guess */
716 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
718 thread->thread.func = NULL;
719 thread->thread.data = NULL;
720 thread->private_data = NULL;
722 if (g_thread_supported ())
723 G_THREAD_UF (thread_self, (&thread->system_thread));
725 g_private_set (g_thread_specific_private, thread);
728 thread->next = g_thread_all_threads;
729 g_thread_all_threads = thread;
733 return (GThread*)thread;
737 g_static_rw_lock_init (GStaticRWLock* lock)
739 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
741 g_return_if_fail (lock);
747 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
750 *cond = g_cond_new ();
751 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
755 g_static_rw_lock_signal (GStaticRWLock* lock)
757 if (lock->want_to_write && lock->write_cond)
758 g_cond_signal (lock->write_cond);
759 else if (lock->want_to_read && lock->read_cond)
760 g_cond_broadcast (lock->read_cond);
764 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
766 g_return_if_fail (lock);
768 if (!g_threads_got_initialized)
771 g_static_mutex_lock (&lock->mutex);
772 lock->want_to_read++;
773 while (lock->have_writer || lock->want_to_write)
774 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
775 lock->want_to_read--;
776 lock->read_counter++;
777 g_static_mutex_unlock (&lock->mutex);
781 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
783 gboolean ret_val = FALSE;
785 g_return_val_if_fail (lock, FALSE);
787 if (!g_threads_got_initialized)
790 g_static_mutex_lock (&lock->mutex);
791 if (!lock->have_writer && !lock->want_to_write)
793 lock->read_counter++;
796 g_static_mutex_unlock (&lock->mutex);
801 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
803 g_return_if_fail (lock);
805 if (!g_threads_got_initialized)
808 g_static_mutex_lock (&lock->mutex);
809 lock->read_counter--;
810 if (lock->read_counter == 0)
811 g_static_rw_lock_signal (lock);
812 g_static_mutex_unlock (&lock->mutex);
816 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
818 g_return_if_fail (lock);
820 if (!g_threads_got_initialized)
823 g_static_mutex_lock (&lock->mutex);
824 lock->want_to_write++;
825 while (lock->have_writer || lock->read_counter)
826 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
827 lock->want_to_write--;
828 lock->have_writer = TRUE;
829 g_static_mutex_unlock (&lock->mutex);
833 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
835 gboolean ret_val = FALSE;
837 g_return_val_if_fail (lock, FALSE);
839 if (!g_threads_got_initialized)
842 g_static_mutex_lock (&lock->mutex);
843 if (!lock->have_writer && !lock->read_counter)
845 lock->have_writer = TRUE;
848 g_static_mutex_unlock (&lock->mutex);
853 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
855 g_return_if_fail (lock);
857 if (!g_threads_got_initialized)
860 g_static_mutex_lock (&lock->mutex);
861 lock->have_writer = FALSE;
862 g_static_rw_lock_signal (lock);
863 g_static_mutex_unlock (&lock->mutex);
867 g_static_rw_lock_free (GStaticRWLock* lock)
869 g_return_if_fail (lock);
873 g_cond_free (lock->read_cond);
874 lock->read_cond = NULL;
876 if (lock->write_cond)
878 g_cond_free (lock->write_cond);
879 lock->write_cond = NULL;
881 g_static_mutex_free (&lock->mutex);
886 * @thread_func: function to call for all GThread structures
887 * @user_data: second argument to @thread_func
889 * Call @thread_func on all existing #GThread structures. Note that
890 * threads may decide to exit while @thread_func is running, so
891 * without intimate knowledge about the lifetime of foreign threads,
892 * @thread_func shouldn't access the GThread* pointer passed in as
893 * first argument. However, @thread_func will not be called for threads
894 * which are known to have exited already.
896 * Due to thread lifetime checks, this function has an execution complexity
897 * which is quadratic in the number of existing threads.
902 g_thread_foreach (GFunc thread_func,
905 GSList *slist = NULL;
907 g_return_if_fail (thread_func != NULL);
908 /* snapshot the list of threads for iteration */
910 for (thread = g_thread_all_threads; thread; thread = thread->next)
911 slist = g_slist_prepend (slist, thread);
913 /* walk the list, skipping non-existant threads */
916 GSList *node = slist;
918 /* check whether the current thread still exists */
920 for (thread = g_thread_all_threads; thread; thread = thread->next)
921 if (thread == node->data)
925 thread_func (thread, user_data);
926 g_slist_free_1 (node);
930 #define __G_THREAD_C__
931 #include "galiasdef.c"