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)
205 gboolean need_init = FALSE;
206 g_mutex_lock (g_once_mutex);
207 if (g_atomic_pointer_get (value_location) == NULL)
209 if (!g_slist_find (g_once_init_list, (void*) value_location))
212 g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
216 g_cond_wait (g_once_cond, g_once_mutex);
217 while (g_slist_find (g_once_init_list, (void*) value_location));
219 g_mutex_unlock (g_once_mutex);
224 g_once_init_leave (volatile gsize *value_location,
225 gsize initialization_value)
227 g_return_if_fail (g_atomic_pointer_get (value_location) == NULL);
228 g_return_if_fail (initialization_value != 0);
229 g_return_if_fail (g_once_init_list != NULL);
231 g_atomic_pointer_set (value_location, (void*) initialization_value);
232 g_mutex_lock (g_once_mutex);
233 g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
234 g_cond_broadcast (g_once_cond);
235 g_mutex_unlock (g_once_mutex);
239 g_static_mutex_init (GStaticMutex *mutex)
241 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
243 g_return_if_fail (mutex);
249 g_static_mutex_get_mutex_impl (GMutex** mutex)
251 if (!g_thread_supported ())
254 g_assert (g_once_mutex);
256 g_mutex_lock (g_once_mutex);
259 g_atomic_pointer_set (mutex, g_mutex_new());
261 g_mutex_unlock (g_once_mutex);
267 g_static_mutex_free (GStaticMutex* mutex)
269 GMutex **runtime_mutex;
271 g_return_if_fail (mutex);
273 /* The runtime_mutex is the first (or only) member of GStaticMutex,
274 * see both versions (of glibconfig.h) in configure.in. Note, that
275 * this variable is NULL, if g_thread_init() hasn't been called or
276 * if we're using the default thread implementation and it provides
278 runtime_mutex = ((GMutex**)mutex);
281 g_mutex_free (*runtime_mutex);
283 *runtime_mutex = NULL;
287 g_static_rec_mutex_init (GStaticRecMutex *mutex)
289 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
291 g_return_if_fail (mutex);
297 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
301 g_return_if_fail (mutex);
303 if (!g_thread_supported ())
306 G_THREAD_UF (thread_self, (&self));
308 if (g_system_thread_equal (self, mutex->owner))
313 g_static_mutex_lock (&mutex->mutex);
314 g_system_thread_assign (mutex->owner, self);
319 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
323 g_return_val_if_fail (mutex, FALSE);
325 if (!g_thread_supported ())
328 G_THREAD_UF (thread_self, (&self));
330 if (g_system_thread_equal (self, mutex->owner))
336 if (!g_static_mutex_trylock (&mutex->mutex))
339 g_system_thread_assign (mutex->owner, self);
345 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
347 g_return_if_fail (mutex);
349 if (!g_thread_supported ())
352 if (mutex->depth > 1)
357 g_system_thread_assign (mutex->owner, zero_thread);
358 g_static_mutex_unlock (&mutex->mutex);
362 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
366 g_return_if_fail (mutex);
368 if (!g_thread_supported ())
374 G_THREAD_UF (thread_self, (&self));
376 if (g_system_thread_equal (self, mutex->owner))
378 mutex->depth += depth;
381 g_static_mutex_lock (&mutex->mutex);
382 g_system_thread_assign (mutex->owner, self);
383 mutex->depth = depth;
387 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
391 g_return_val_if_fail (mutex, 0);
393 if (!g_thread_supported ())
396 depth = mutex->depth;
398 g_system_thread_assign (mutex->owner, zero_thread);
400 g_static_mutex_unlock (&mutex->mutex);
406 g_static_rec_mutex_free (GStaticRecMutex *mutex)
408 g_return_if_fail (mutex);
410 g_static_mutex_free (&mutex->mutex);
414 g_static_private_init (GStaticPrivate *private_key)
416 private_key->index = 0;
420 g_static_private_get (GStaticPrivate *private_key)
422 GRealThread *self = (GRealThread*) g_thread_self ();
425 array = self->private_data;
429 if (!private_key->index)
431 else if (private_key->index <= array->len)
432 return g_array_index (array, GStaticPrivateNode,
433 private_key->index - 1).data;
439 g_static_private_set (GStaticPrivate *private_key,
441 GDestroyNotify notify)
443 GRealThread *self = (GRealThread*) g_thread_self ();
445 static guint next_index = 0;
446 GStaticPrivateNode *node;
448 array = self->private_data;
451 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
452 self->private_data = array;
455 if (!private_key->index)
459 if (!private_key->index)
461 if (g_thread_free_indeces)
464 GPOINTER_TO_UINT (g_thread_free_indeces->data);
465 g_thread_free_indeces =
466 g_slist_delete_link (g_thread_free_indeces,
467 g_thread_free_indeces);
470 private_key->index = ++next_index;
476 if (private_key->index > array->len)
477 g_array_set_size (array, private_key->index);
479 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
482 gpointer ddata = node->data;
483 GDestroyNotify ddestroy = node->destroy;
486 node->destroy = notify;
493 node->destroy = notify;
498 g_static_private_free (GStaticPrivate *private_key)
500 guint idx = private_key->index;
506 private_key->index = 0;
510 thread = g_thread_all_threads;
513 GArray *array = thread->private_data;
514 thread = thread->next;
516 if (array && idx <= array->len)
518 GStaticPrivateNode *node = &g_array_index (array,
521 gpointer ddata = node->data;
522 GDestroyNotify ddestroy = node->destroy;
525 node->destroy = NULL;
535 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
536 GUINT_TO_POINTER (idx));
541 g_thread_cleanup (gpointer data)
545 GRealThread* thread = data;
546 if (thread->private_data)
548 GArray* array = thread->private_data;
551 for (i = 0; i < array->len; i++ )
553 GStaticPrivateNode *node =
554 &g_array_index (array, GStaticPrivateNode, i);
556 node->destroy (node->data);
558 g_array_free (array, TRUE);
561 /* We only free the thread structure, if it isn't joinable. If
562 it is, the structure is freed in g_thread_join */
563 if (!thread->thread.joinable)
568 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
575 g_thread_all_threads = t->next;
581 /* Just to make sure, this isn't used any more */
582 g_system_thread_assign (thread->system_thread, zero_thread);
591 g_error ("The thread system is not yet initialized.");
594 #define G_NSEC_PER_SEC 1000000000
602 /* Returns 100s of nanoseconds since start of 1601 */
603 GetSystemTimeAsFileTime ((FILETIME *)&v);
605 /* Offset to Unix epoch */
606 v -= G_GINT64_CONSTANT (116444736000000000);
607 /* Convert to nanoseconds */
614 gettimeofday (&tv, NULL);
616 return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
621 g_thread_create_proxy (gpointer data)
623 GRealThread* thread = data;
627 /* This has to happen before G_LOCK, as that might call g_thread_self */
628 g_private_set (g_thread_specific_private, data);
630 /* the lock makes sure, that thread->system_thread is written,
631 before thread->thread.func is called. See g_thread_create. */
635 thread->retval = thread->thread.func (thread->thread.data);
641 g_thread_create_full (GThreadFunc func,
646 GThreadPriority priority,
650 GError *local_error = NULL;
651 g_return_val_if_fail (func, NULL);
652 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
653 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
655 result = g_new0 (GRealThread, 1);
657 result->thread.joinable = joinable;
658 result->thread.priority = priority;
659 result->thread.func = func;
660 result->thread.data = data;
661 result->private_data = NULL;
663 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
664 stack_size, joinable, bound, priority,
665 &result->system_thread, &local_error));
668 result->next = g_thread_all_threads;
669 g_thread_all_threads = result;
675 g_propagate_error (error, local_error);
680 return (GThread*) result;
684 g_thread_exit (gpointer retval)
686 GRealThread* real = (GRealThread*) g_thread_self ();
687 real->retval = retval;
688 G_THREAD_CF (thread_exit, (void)0, ());
692 g_thread_join (GThread* thread)
694 GRealThread* real = (GRealThread*) thread;
698 g_return_val_if_fail (thread, NULL);
699 g_return_val_if_fail (thread->joinable, NULL);
700 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
703 G_THREAD_UF (thread_join, (&real->system_thread));
705 retval = real->retval;
708 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
710 if (t == (GRealThread*) thread)
715 g_thread_all_threads = t->next;
721 /* Just to make sure, this isn't used any more */
722 thread->joinable = 0;
723 g_system_thread_assign (real->system_thread, zero_thread);
725 /* the thread structure for non-joinable threads is freed upon
726 thread end. We free the memory here. This will leave a loose end,
727 if a joinable thread is not joined. */
735 g_thread_set_priority (GThread* thread,
736 GThreadPriority priority)
738 GRealThread* real = (GRealThread*) thread;
740 g_return_if_fail (thread);
741 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
742 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
743 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
745 thread->priority = priority;
747 G_THREAD_CF (thread_set_priority, (void)0,
748 (&real->system_thread, priority));
754 GRealThread* thread = g_private_get (g_thread_specific_private);
758 /* If no thread data is available, provide and set one. This
759 can happen for the main thread and for threads, that are not
761 thread = g_new0 (GRealThread, 1);
762 thread->thread.joinable = FALSE; /* This is a save guess */
763 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
765 thread->thread.func = NULL;
766 thread->thread.data = NULL;
767 thread->private_data = NULL;
769 if (g_thread_supported ())
770 G_THREAD_UF (thread_self, (&thread->system_thread));
772 g_private_set (g_thread_specific_private, thread);
775 thread->next = g_thread_all_threads;
776 g_thread_all_threads = thread;
780 return (GThread*)thread;
784 g_static_rw_lock_init (GStaticRWLock* lock)
786 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
788 g_return_if_fail (lock);
794 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
797 *cond = g_cond_new ();
798 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
802 g_static_rw_lock_signal (GStaticRWLock* lock)
804 if (lock->want_to_write && lock->write_cond)
805 g_cond_signal (lock->write_cond);
806 else if (lock->want_to_read && lock->read_cond)
807 g_cond_broadcast (lock->read_cond);
811 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
813 g_return_if_fail (lock);
815 if (!g_threads_got_initialized)
818 g_static_mutex_lock (&lock->mutex);
819 lock->want_to_read++;
820 while (lock->have_writer || lock->want_to_write)
821 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
822 lock->want_to_read--;
823 lock->read_counter++;
824 g_static_mutex_unlock (&lock->mutex);
828 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
830 gboolean ret_val = FALSE;
832 g_return_val_if_fail (lock, FALSE);
834 if (!g_threads_got_initialized)
837 g_static_mutex_lock (&lock->mutex);
838 if (!lock->have_writer && !lock->want_to_write)
840 lock->read_counter++;
843 g_static_mutex_unlock (&lock->mutex);
848 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
850 g_return_if_fail (lock);
852 if (!g_threads_got_initialized)
855 g_static_mutex_lock (&lock->mutex);
856 lock->read_counter--;
857 if (lock->read_counter == 0)
858 g_static_rw_lock_signal (lock);
859 g_static_mutex_unlock (&lock->mutex);
863 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
865 g_return_if_fail (lock);
867 if (!g_threads_got_initialized)
870 g_static_mutex_lock (&lock->mutex);
871 lock->want_to_write++;
872 while (lock->have_writer || lock->read_counter)
873 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
874 lock->want_to_write--;
875 lock->have_writer = TRUE;
876 g_static_mutex_unlock (&lock->mutex);
880 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
882 gboolean ret_val = FALSE;
884 g_return_val_if_fail (lock, FALSE);
886 if (!g_threads_got_initialized)
889 g_static_mutex_lock (&lock->mutex);
890 if (!lock->have_writer && !lock->read_counter)
892 lock->have_writer = TRUE;
895 g_static_mutex_unlock (&lock->mutex);
900 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
902 g_return_if_fail (lock);
904 if (!g_threads_got_initialized)
907 g_static_mutex_lock (&lock->mutex);
908 lock->have_writer = FALSE;
909 g_static_rw_lock_signal (lock);
910 g_static_mutex_unlock (&lock->mutex);
914 g_static_rw_lock_free (GStaticRWLock* lock)
916 g_return_if_fail (lock);
920 g_cond_free (lock->read_cond);
921 lock->read_cond = NULL;
923 if (lock->write_cond)
925 g_cond_free (lock->write_cond);
926 lock->write_cond = NULL;
928 g_static_mutex_free (&lock->mutex);
933 * @thread_func: function to call for all GThread structures
934 * @user_data: second argument to @thread_func
936 * Call @thread_func on all existing #GThread structures. Note that
937 * threads may decide to exit while @thread_func is running, so
938 * without intimate knowledge about the lifetime of foreign threads,
939 * @thread_func shouldn't access the GThread* pointer passed in as
940 * first argument. However, @thread_func will not be called for threads
941 * which are known to have exited already.
943 * Due to thread lifetime checks, this function has an execution complexity
944 * which is quadratic in the number of existing threads.
949 g_thread_foreach (GFunc thread_func,
952 GSList *slist = NULL;
954 g_return_if_fail (thread_func != NULL);
955 /* snapshot the list of threads for iteration */
957 for (thread = g_thread_all_threads; thread; thread = thread->next)
958 slist = g_slist_prepend (slist, thread);
960 /* walk the list, skipping non-existant threads */
963 GSList *node = slist;
965 /* check whether the current thread still exists */
967 for (thread = g_thread_all_threads; thread; thread = thread->next)
968 if (thread == node->data)
972 thread_func (thread, user_data);
973 g_slist_free_1 (node);
978 * g_thread_get_initialized
980 * Indicates if g_thread_init() has been called.
985 g_thread_get_initialized ()
987 return g_thread_supported ();
990 #define __G_THREAD_C__
991 #include "galiasdef.c"