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 ();
169 _g_futex_thread_init ();
171 _g_win32_thread_init ();
174 #endif /* G_THREADS_ENABLED */
177 g_once_impl (GOnce *once,
181 g_mutex_lock (g_once_mutex);
183 while (once->status == G_ONCE_STATUS_PROGRESS)
184 g_cond_wait (g_once_cond, g_once_mutex);
186 if (once->status != G_ONCE_STATUS_READY)
188 once->status = G_ONCE_STATUS_PROGRESS;
189 g_mutex_unlock (g_once_mutex);
191 once->retval = func (arg);
193 g_mutex_lock (g_once_mutex);
194 once->status = G_ONCE_STATUS_READY;
195 g_cond_broadcast (g_once_cond);
198 g_mutex_unlock (g_once_mutex);
204 g_once_init_enter_impl (volatile gsize *value_location)
206 gboolean need_init = FALSE;
207 g_mutex_lock (g_once_mutex);
208 if (g_atomic_pointer_get (value_location) == NULL)
210 if (!g_slist_find (g_once_init_list, (void*) value_location))
213 g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
217 g_cond_wait (g_once_cond, g_once_mutex);
218 while (g_slist_find (g_once_init_list, (void*) value_location));
220 g_mutex_unlock (g_once_mutex);
225 g_once_init_leave (volatile gsize *value_location,
226 gsize initialization_value)
228 g_return_if_fail (g_atomic_pointer_get (value_location) == NULL);
229 g_return_if_fail (initialization_value != 0);
230 g_return_if_fail (g_once_init_list != NULL);
232 g_atomic_pointer_set ((void**)value_location, (void*) initialization_value);
233 g_mutex_lock (g_once_mutex);
234 g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
235 g_cond_broadcast (g_once_cond);
236 g_mutex_unlock (g_once_mutex);
240 g_static_mutex_init (GStaticMutex *mutex)
242 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
244 g_return_if_fail (mutex);
250 g_static_mutex_get_mutex_impl (GMutex** mutex)
252 if (!g_thread_supported ())
255 g_assert (g_once_mutex);
257 g_mutex_lock (g_once_mutex);
260 g_atomic_pointer_set (mutex, g_mutex_new());
262 g_mutex_unlock (g_once_mutex);
268 g_static_mutex_free (GStaticMutex* mutex)
270 GMutex **runtime_mutex;
272 g_return_if_fail (mutex);
274 /* The runtime_mutex is the first (or only) member of GStaticMutex,
275 * see both versions (of glibconfig.h) in configure.in. Note, that
276 * this variable is NULL, if g_thread_init() hasn't been called or
277 * if we're using the default thread implementation and it provides
279 runtime_mutex = ((GMutex**)mutex);
282 g_mutex_free (*runtime_mutex);
284 *runtime_mutex = NULL;
288 g_static_rec_mutex_init (GStaticRecMutex *mutex)
290 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
292 g_return_if_fail (mutex);
298 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
302 g_return_if_fail (mutex);
304 if (!g_thread_supported ())
307 G_THREAD_UF (thread_self, (&self));
309 if (g_system_thread_equal (self, mutex->owner))
314 g_static_mutex_lock (&mutex->mutex);
315 g_system_thread_assign (mutex->owner, self);
320 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
324 g_return_val_if_fail (mutex, FALSE);
326 if (!g_thread_supported ())
329 G_THREAD_UF (thread_self, (&self));
331 if (g_system_thread_equal (self, mutex->owner))
337 if (!g_static_mutex_trylock (&mutex->mutex))
340 g_system_thread_assign (mutex->owner, self);
346 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
348 g_return_if_fail (mutex);
350 if (!g_thread_supported ())
353 if (mutex->depth > 1)
358 g_system_thread_assign (mutex->owner, zero_thread);
359 g_static_mutex_unlock (&mutex->mutex);
363 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
367 g_return_if_fail (mutex);
369 if (!g_thread_supported ())
375 G_THREAD_UF (thread_self, (&self));
377 if (g_system_thread_equal (self, mutex->owner))
379 mutex->depth += depth;
382 g_static_mutex_lock (&mutex->mutex);
383 g_system_thread_assign (mutex->owner, self);
384 mutex->depth = depth;
388 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
392 g_return_val_if_fail (mutex, 0);
394 if (!g_thread_supported ())
397 depth = mutex->depth;
399 g_system_thread_assign (mutex->owner, zero_thread);
401 g_static_mutex_unlock (&mutex->mutex);
407 g_static_rec_mutex_free (GStaticRecMutex *mutex)
409 g_return_if_fail (mutex);
411 g_static_mutex_free (&mutex->mutex);
415 g_static_private_init (GStaticPrivate *private_key)
417 private_key->index = 0;
421 g_static_private_get (GStaticPrivate *private_key)
423 GRealThread *self = (GRealThread*) g_thread_self ();
426 array = self->private_data;
430 if (!private_key->index)
432 else if (private_key->index <= array->len)
433 return g_array_index (array, GStaticPrivateNode,
434 private_key->index - 1).data;
440 g_static_private_set (GStaticPrivate *private_key,
442 GDestroyNotify notify)
444 GRealThread *self = (GRealThread*) g_thread_self ();
446 static guint next_index = 0;
447 GStaticPrivateNode *node;
449 array = self->private_data;
452 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
453 self->private_data = array;
456 if (!private_key->index)
460 if (!private_key->index)
462 if (g_thread_free_indeces)
465 GPOINTER_TO_UINT (g_thread_free_indeces->data);
466 g_thread_free_indeces =
467 g_slist_delete_link (g_thread_free_indeces,
468 g_thread_free_indeces);
471 private_key->index = ++next_index;
477 if (private_key->index > array->len)
478 g_array_set_size (array, private_key->index);
480 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
483 gpointer ddata = node->data;
484 GDestroyNotify ddestroy = node->destroy;
487 node->destroy = notify;
494 node->destroy = notify;
499 g_static_private_free (GStaticPrivate *private_key)
501 guint idx = private_key->index;
507 private_key->index = 0;
511 thread = g_thread_all_threads;
514 GArray *array = thread->private_data;
515 thread = thread->next;
517 if (array && idx <= array->len)
519 GStaticPrivateNode *node = &g_array_index (array,
522 gpointer ddata = node->data;
523 GDestroyNotify ddestroy = node->destroy;
526 node->destroy = NULL;
536 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
537 GUINT_TO_POINTER (idx));
542 g_thread_cleanup (gpointer data)
546 GRealThread* thread = data;
547 if (thread->private_data)
549 GArray* array = thread->private_data;
552 for (i = 0; i < array->len; i++ )
554 GStaticPrivateNode *node =
555 &g_array_index (array, GStaticPrivateNode, i);
557 node->destroy (node->data);
559 g_array_free (array, TRUE);
562 /* We only free the thread structure, if it isn't joinable. If
563 it is, the structure is freed in g_thread_join */
564 if (!thread->thread.joinable)
569 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
576 g_thread_all_threads = t->next;
582 /* Just to make sure, this isn't used any more */
583 g_system_thread_assign (thread->system_thread, zero_thread);
592 g_error ("The thread system is not yet initialized.");
595 #define G_NSEC_PER_SEC 1000000000
603 /* Returns 100s of nanoseconds since start of 1601 */
604 GetSystemTimeAsFileTime ((FILETIME *)&v);
606 /* Offset to Unix epoch */
607 v -= G_GINT64_CONSTANT (116444736000000000);
608 /* Convert to nanoseconds */
615 gettimeofday (&tv, NULL);
617 return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
622 g_thread_create_proxy (gpointer data)
624 GRealThread* thread = data;
628 /* This has to happen before G_LOCK, as that might call g_thread_self */
629 g_private_set (g_thread_specific_private, data);
631 /* the lock makes sure, that thread->system_thread is written,
632 before thread->thread.func is called. See g_thread_create. */
636 thread->retval = thread->thread.func (thread->thread.data);
642 g_thread_create_full (GThreadFunc func,
647 GThreadPriority priority,
651 GError *local_error = NULL;
652 g_return_val_if_fail (func, NULL);
653 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
654 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
656 result = g_new0 (GRealThread, 1);
658 result->thread.joinable = joinable;
659 result->thread.priority = priority;
660 result->thread.func = func;
661 result->thread.data = data;
662 result->private_data = NULL;
664 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
665 stack_size, joinable, bound, priority,
666 &result->system_thread, &local_error));
669 result->next = g_thread_all_threads;
670 g_thread_all_threads = result;
676 g_propagate_error (error, local_error);
681 return (GThread*) result;
685 g_thread_exit (gpointer retval)
687 GRealThread* real = (GRealThread*) g_thread_self ();
688 real->retval = retval;
689 G_THREAD_CF (thread_exit, (void)0, ());
693 g_thread_join (GThread* thread)
695 GRealThread* real = (GRealThread*) thread;
699 g_return_val_if_fail (thread, NULL);
700 g_return_val_if_fail (thread->joinable, NULL);
701 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
704 G_THREAD_UF (thread_join, (&real->system_thread));
706 retval = real->retval;
709 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
711 if (t == (GRealThread*) thread)
716 g_thread_all_threads = t->next;
722 /* Just to make sure, this isn't used any more */
723 thread->joinable = 0;
724 g_system_thread_assign (real->system_thread, zero_thread);
726 /* the thread structure for non-joinable threads is freed upon
727 thread end. We free the memory here. This will leave a loose end,
728 if a joinable thread is not joined. */
736 g_thread_set_priority (GThread* thread,
737 GThreadPriority priority)
739 GRealThread* real = (GRealThread*) thread;
741 g_return_if_fail (thread);
742 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
743 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
744 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
746 thread->priority = priority;
748 G_THREAD_CF (thread_set_priority, (void)0,
749 (&real->system_thread, priority));
755 GRealThread* thread = g_private_get (g_thread_specific_private);
759 /* If no thread data is available, provide and set one. This
760 can happen for the main thread and for threads, that are not
762 thread = g_new0 (GRealThread, 1);
763 thread->thread.joinable = FALSE; /* This is a save guess */
764 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
766 thread->thread.func = NULL;
767 thread->thread.data = NULL;
768 thread->private_data = NULL;
770 if (g_thread_supported ())
771 G_THREAD_UF (thread_self, (&thread->system_thread));
773 g_private_set (g_thread_specific_private, thread);
776 thread->next = g_thread_all_threads;
777 g_thread_all_threads = thread;
781 return (GThread*)thread;
785 g_static_rw_lock_init (GStaticRWLock* lock)
787 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
789 g_return_if_fail (lock);
795 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
798 *cond = g_cond_new ();
799 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
803 g_static_rw_lock_signal (GStaticRWLock* lock)
805 if (lock->want_to_write && lock->write_cond)
806 g_cond_signal (lock->write_cond);
807 else if (lock->want_to_read && lock->read_cond)
808 g_cond_broadcast (lock->read_cond);
812 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
814 g_return_if_fail (lock);
816 if (!g_threads_got_initialized)
819 g_static_mutex_lock (&lock->mutex);
820 lock->want_to_read++;
821 while (lock->have_writer || lock->want_to_write)
822 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
823 lock->want_to_read--;
824 lock->read_counter++;
825 g_static_mutex_unlock (&lock->mutex);
829 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
831 gboolean ret_val = FALSE;
833 g_return_val_if_fail (lock, FALSE);
835 if (!g_threads_got_initialized)
838 g_static_mutex_lock (&lock->mutex);
839 if (!lock->have_writer && !lock->want_to_write)
841 lock->read_counter++;
844 g_static_mutex_unlock (&lock->mutex);
849 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
851 g_return_if_fail (lock);
853 if (!g_threads_got_initialized)
856 g_static_mutex_lock (&lock->mutex);
857 lock->read_counter--;
858 if (lock->read_counter == 0)
859 g_static_rw_lock_signal (lock);
860 g_static_mutex_unlock (&lock->mutex);
864 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
866 g_return_if_fail (lock);
868 if (!g_threads_got_initialized)
871 g_static_mutex_lock (&lock->mutex);
872 lock->want_to_write++;
873 while (lock->have_writer || lock->read_counter)
874 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
875 lock->want_to_write--;
876 lock->have_writer = TRUE;
877 g_static_mutex_unlock (&lock->mutex);
881 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
883 gboolean ret_val = FALSE;
885 g_return_val_if_fail (lock, FALSE);
887 if (!g_threads_got_initialized)
890 g_static_mutex_lock (&lock->mutex);
891 if (!lock->have_writer && !lock->read_counter)
893 lock->have_writer = TRUE;
896 g_static_mutex_unlock (&lock->mutex);
901 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
903 g_return_if_fail (lock);
905 if (!g_threads_got_initialized)
908 g_static_mutex_lock (&lock->mutex);
909 lock->have_writer = FALSE;
910 g_static_rw_lock_signal (lock);
911 g_static_mutex_unlock (&lock->mutex);
915 g_static_rw_lock_free (GStaticRWLock* lock)
917 g_return_if_fail (lock);
921 g_cond_free (lock->read_cond);
922 lock->read_cond = NULL;
924 if (lock->write_cond)
926 g_cond_free (lock->write_cond);
927 lock->write_cond = NULL;
929 g_static_mutex_free (&lock->mutex);
934 * @thread_func: function to call for all GThread structures
935 * @user_data: second argument to @thread_func
937 * Call @thread_func on all existing #GThread structures. Note that
938 * threads may decide to exit while @thread_func is running, so
939 * without intimate knowledge about the lifetime of foreign threads,
940 * @thread_func shouldn't access the GThread* pointer passed in as
941 * first argument. However, @thread_func will not be called for threads
942 * which are known to have exited already.
944 * Due to thread lifetime checks, this function has an execution complexity
945 * which is quadratic in the number of existing threads.
950 g_thread_foreach (GFunc thread_func,
953 GSList *slist = NULL;
955 g_return_if_fail (thread_func != NULL);
956 /* snapshot the list of threads for iteration */
958 for (thread = g_thread_all_threads; thread; thread = thread->next)
959 slist = g_slist_prepend (slist, thread);
961 /* walk the list, skipping non-existant threads */
964 GSList *node = slist;
966 /* check whether the current thread still exists */
968 for (thread = g_thread_all_threads; thread; thread = thread->next)
969 if (thread == node->data)
973 thread_func (thread, user_data);
974 g_slist_free_1 (node);
979 * g_thread_get_initialized
981 * Indicates if g_thread_init() has been called.
983 * Returns: %TRUE if threads have been initialized.
988 g_thread_get_initialized ()
990 return g_thread_supported ();
993 #define __G_THREAD_C__
994 #include "galiasdef.c"