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 #endif /* G_OS_WIN32 */
49 #include "gthreadprivate.h"
53 g_thread_error_quark (void)
55 return g_quark_from_static_string ("g_thread_error");
58 /* Keep this in sync with GRealThread in gmain.c! */
59 typedef struct _GRealThread GRealThread;
63 gpointer private_data;
66 GSystemThread system_thread;
69 typedef struct _GStaticPrivateNode GStaticPrivateNode;
70 struct _GStaticPrivateNode
73 GDestroyNotify destroy;
76 static void g_thread_cleanup (gpointer data);
77 static void g_thread_fail (void);
78 static guint64 gettime (void);
80 /* Global variables */
82 static GSystemThread zero_thread; /* This is initialized to all zero */
83 gboolean g_thread_use_default_impl = TRUE;
84 gboolean g_threads_got_initialized = FALSE;
86 GThreadFunctions g_thread_functions_for_glib_use = {
87 (GMutex*(*)())g_thread_fail, /* mutex_new */
88 NULL, /* mutex_lock */
89 NULL, /* mutex_trylock */
90 NULL, /* mutex_unlock */
91 NULL, /* mutex_free */
92 (GCond*(*)())g_thread_fail, /* cond_new */
93 NULL, /* cond_signal */
94 NULL, /* cond_broadcast */
96 NULL, /* cond_timed_wait */
98 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
99 NULL, /* private_get */
100 NULL, /* private_set */
101 (void(*)(GThreadFunc, gpointer, gulong,
102 gboolean, gboolean, GThreadPriority,
103 gpointer, GError**))g_thread_fail, /* thread_create */
104 NULL, /* thread_yield */
105 NULL, /* thread_join */
106 NULL, /* thread_exit */
107 NULL, /* thread_set_priority */
108 NULL, /* thread_self */
109 NULL, /* thread_equal */
110 gettime /* gettime */
115 static GMutex *g_once_mutex = NULL;
116 static GCond *g_once_cond = NULL;
117 static GPrivate *g_thread_specific_private = NULL;
118 static GRealThread *g_thread_all_threads = NULL;
119 static GSList *g_thread_free_indeces = NULL;
121 G_LOCK_DEFINE_STATIC (g_thread);
123 #ifdef G_THREADS_ENABLED
124 /* This must be called only once, before any threads are created.
125 * It will only be called from g_thread_init() in -lgthread.
128 g_thread_init_glib (void)
130 /* We let the main thread (the one that calls g_thread_init) inherit
131 * the static_private data set before calling g_thread_init
133 GRealThread* main_thread = (GRealThread*) g_thread_self ();
135 /* mutex and cond creation works without g_threads_got_initialized */
136 g_once_mutex = g_mutex_new ();
137 g_once_cond = g_cond_new ();
139 /* we may only create mutex and cond in here */
140 _g_mem_thread_init_noprivate_nomessage ();
142 /* setup the basic threading system */
143 g_threads_got_initialized = TRUE;
144 g_thread_specific_private = g_private_new (g_thread_cleanup);
145 g_private_set (g_thread_specific_private, main_thread);
146 G_THREAD_UF (thread_self, (&main_thread->system_thread));
148 /* complete memory system initialization, g_private_*() works now */
149 _g_slice_thread_init_nomessage ();
151 /* accomplish log system initialization to enable messaging */
152 _g_messages_thread_init_nomessage ();
154 /* we may run full-fledged initializers from here */
155 _g_atomic_thread_init ();
156 _g_convert_thread_init ();
157 _g_rand_thread_init ();
158 _g_main_thread_init ();
159 _g_utils_thread_init ();
161 _g_win32_thread_init ();
164 #endif /* G_THREADS_ENABLED */
167 g_once_impl (GOnce *once,
171 g_mutex_lock (g_once_mutex);
173 while (once->status == G_ONCE_STATUS_PROGRESS)
174 g_cond_wait (g_once_cond, g_once_mutex);
176 if (once->status != G_ONCE_STATUS_READY)
178 once->status = G_ONCE_STATUS_PROGRESS;
179 g_mutex_unlock (g_once_mutex);
181 once->retval = func (arg);
183 g_mutex_lock (g_once_mutex);
184 once->status = G_ONCE_STATUS_READY;
185 g_cond_broadcast (g_once_cond);
188 g_mutex_unlock (g_once_mutex);
194 g_static_mutex_init (GStaticMutex *mutex)
196 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
198 g_return_if_fail (mutex);
204 g_static_mutex_get_mutex_impl (GMutex** mutex)
206 if (!g_thread_supported ())
209 g_assert (g_once_mutex);
211 g_mutex_lock (g_once_mutex);
214 g_atomic_pointer_set (mutex, g_mutex_new());
216 g_mutex_unlock (g_once_mutex);
222 g_static_mutex_free (GStaticMutex* mutex)
224 GMutex **runtime_mutex;
226 g_return_if_fail (mutex);
228 /* The runtime_mutex is the first (or only) member of GStaticMutex,
229 * see both versions (of glibconfig.h) in configure.in */
230 runtime_mutex = ((GMutex**)mutex);
233 g_mutex_free (*runtime_mutex);
235 *runtime_mutex = NULL;
239 g_static_rec_mutex_init (GStaticRecMutex *mutex)
241 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
243 g_return_if_fail (mutex);
249 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
253 g_return_if_fail (mutex);
255 if (!g_thread_supported ())
258 G_THREAD_UF (thread_self, (&self));
260 if (g_system_thread_equal (self, mutex->owner))
265 g_static_mutex_lock (&mutex->mutex);
266 g_system_thread_assign (mutex->owner, self);
271 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
275 g_return_val_if_fail (mutex, FALSE);
277 if (!g_thread_supported ())
280 G_THREAD_UF (thread_self, (&self));
282 if (g_system_thread_equal (self, mutex->owner))
288 if (!g_static_mutex_trylock (&mutex->mutex))
291 g_system_thread_assign (mutex->owner, self);
297 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
299 g_return_if_fail (mutex);
301 if (!g_thread_supported ())
304 if (mutex->depth > 1)
309 g_system_thread_assign (mutex->owner, zero_thread);
310 g_static_mutex_unlock (&mutex->mutex);
314 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
318 g_return_if_fail (mutex);
320 if (!g_thread_supported ())
326 G_THREAD_UF (thread_self, (&self));
328 if (g_system_thread_equal (self, mutex->owner))
330 mutex->depth += depth;
333 g_static_mutex_lock (&mutex->mutex);
334 g_system_thread_assign (mutex->owner, self);
335 mutex->depth = depth;
339 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
343 g_return_val_if_fail (mutex, 0);
345 if (!g_thread_supported ())
348 depth = mutex->depth;
350 g_system_thread_assign (mutex->owner, zero_thread);
352 g_static_mutex_unlock (&mutex->mutex);
358 g_static_rec_mutex_free (GStaticRecMutex *mutex)
360 g_return_if_fail (mutex);
362 g_static_mutex_free (&mutex->mutex);
366 g_static_private_init (GStaticPrivate *private_key)
368 private_key->index = 0;
372 g_static_private_get (GStaticPrivate *private_key)
374 GRealThread *self = (GRealThread*) g_thread_self ();
377 array = self->private_data;
381 if (!private_key->index)
383 else if (private_key->index <= array->len)
384 return g_array_index (array, GStaticPrivateNode,
385 private_key->index - 1).data;
391 g_static_private_set (GStaticPrivate *private_key,
393 GDestroyNotify notify)
395 GRealThread *self = (GRealThread*) g_thread_self ();
397 static guint next_index = 0;
398 GStaticPrivateNode *node;
400 array = self->private_data;
403 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
404 self->private_data = array;
407 if (!private_key->index)
411 if (!private_key->index)
413 if (g_thread_free_indeces)
416 GPOINTER_TO_UINT (g_thread_free_indeces->data);
417 g_thread_free_indeces =
418 g_slist_delete_link (g_thread_free_indeces,
419 g_thread_free_indeces);
422 private_key->index = ++next_index;
428 if (private_key->index > array->len)
429 g_array_set_size (array, private_key->index);
431 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
434 gpointer ddata = node->data;
435 GDestroyNotify ddestroy = node->destroy;
438 node->destroy = notify;
445 node->destroy = notify;
450 g_static_private_free (GStaticPrivate *private_key)
452 guint index = private_key->index;
458 private_key->index = 0;
462 thread = g_thread_all_threads;
465 GArray *array = thread->private_data;
466 thread = thread->next;
468 if (array && index <= array->len)
470 GStaticPrivateNode *node = &g_array_index (array,
473 gpointer ddata = node->data;
474 GDestroyNotify ddestroy = node->destroy;
477 node->destroy = NULL;
487 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
488 GUINT_TO_POINTER (index));
493 g_thread_cleanup (gpointer data)
497 GRealThread* thread = data;
498 if (thread->private_data)
500 GArray* array = thread->private_data;
503 for (i = 0; i < array->len; i++ )
505 GStaticPrivateNode *node =
506 &g_array_index (array, GStaticPrivateNode, i);
508 node->destroy (node->data);
510 g_array_free (array, TRUE);
513 /* We only free the thread structure, if it isn't joinable. If
514 it is, the structure is freed in g_thread_join */
515 if (!thread->thread.joinable)
520 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
527 g_thread_all_threads = t->next;
533 /* Just to make sure, this isn't used any more */
534 g_system_thread_assign (thread->system_thread, zero_thread);
543 g_error ("The thread system is not yet initialized.");
552 GetSystemTimeAsFileTime ((FILETIME *)&v);
558 gettimeofday (&tv, NULL);
560 return tv.tv_sec * 1e9 + tv.tv_usec * 1000;
565 g_thread_create_proxy (gpointer data)
567 GRealThread* thread = data;
571 /* This has to happen before G_LOCK, as that might call g_thread_self */
572 g_private_set (g_thread_specific_private, data);
574 /* the lock makes sure, that thread->system_thread is written,
575 before thread->thread.func is called. See g_thread_create. */
579 thread->retval = thread->thread.func (thread->thread.data);
585 g_thread_create_full (GThreadFunc func,
590 GThreadPriority priority,
594 GError *local_error = NULL;
595 g_return_val_if_fail (func, NULL);
596 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
597 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
599 result = g_new0 (GRealThread, 1);
601 result->thread.joinable = joinable;
602 result->thread.priority = priority;
603 result->thread.func = func;
604 result->thread.data = data;
605 result->private_data = NULL;
607 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
608 stack_size, joinable, bound, priority,
609 &result->system_thread, &local_error));
610 result->next = g_thread_all_threads;
611 g_thread_all_threads = result;
616 g_propagate_error (error, local_error);
621 return (GThread*) result;
625 g_thread_exit (gpointer retval)
627 GRealThread* real = (GRealThread*) g_thread_self ();
628 real->retval = retval;
629 G_THREAD_CF (thread_exit, (void)0, ());
633 g_thread_join (GThread* thread)
635 GRealThread* real = (GRealThread*) thread;
639 g_return_val_if_fail (thread, NULL);
640 g_return_val_if_fail (thread->joinable, NULL);
641 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
644 G_THREAD_UF (thread_join, (&real->system_thread));
646 retval = real->retval;
649 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
651 if (t == (GRealThread*) thread)
656 g_thread_all_threads = t->next;
662 /* Just to make sure, this isn't used any more */
663 thread->joinable = 0;
664 g_system_thread_assign (real->system_thread, zero_thread);
666 /* the thread structure for non-joinable threads is freed upon
667 thread end. We free the memory here. This will leave a loose end,
668 if a joinable thread is not joined. */
676 g_thread_set_priority (GThread* thread,
677 GThreadPriority priority)
679 GRealThread* real = (GRealThread*) thread;
681 g_return_if_fail (thread);
682 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
683 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
684 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
686 thread->priority = priority;
688 G_THREAD_CF (thread_set_priority, (void)0,
689 (&real->system_thread, priority));
695 GRealThread* thread = g_private_get (g_thread_specific_private);
699 /* If no thread data is available, provide and set one. This
700 can happen for the main thread and for threads, that are not
702 thread = g_new0 (GRealThread, 1);
703 thread->thread.joinable = FALSE; /* This is a save guess */
704 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
706 thread->thread.func = NULL;
707 thread->thread.data = NULL;
708 thread->private_data = NULL;
710 if (g_thread_supported ())
711 G_THREAD_UF (thread_self, (&thread->system_thread));
713 g_private_set (g_thread_specific_private, thread);
716 thread->next = g_thread_all_threads;
717 g_thread_all_threads = thread;
721 return (GThread*)thread;
725 g_static_rw_lock_init (GStaticRWLock* lock)
727 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
729 g_return_if_fail (lock);
735 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
738 *cond = g_cond_new ();
739 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
743 g_static_rw_lock_signal (GStaticRWLock* lock)
745 if (lock->want_to_write && lock->write_cond)
746 g_cond_signal (lock->write_cond);
747 else if (lock->want_to_read && lock->read_cond)
748 g_cond_broadcast (lock->read_cond);
752 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
754 g_return_if_fail (lock);
756 if (!g_threads_got_initialized)
759 g_static_mutex_lock (&lock->mutex);
760 lock->want_to_read++;
761 while (lock->have_writer || lock->want_to_write)
762 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
763 lock->want_to_read--;
764 lock->read_counter++;
765 g_static_mutex_unlock (&lock->mutex);
769 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
771 gboolean ret_val = FALSE;
773 g_return_val_if_fail (lock, FALSE);
775 if (!g_threads_got_initialized)
778 g_static_mutex_lock (&lock->mutex);
779 if (!lock->have_writer && !lock->want_to_write)
781 lock->read_counter++;
784 g_static_mutex_unlock (&lock->mutex);
789 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
791 g_return_if_fail (lock);
793 if (!g_threads_got_initialized)
796 g_static_mutex_lock (&lock->mutex);
797 lock->read_counter--;
798 if (lock->read_counter == 0)
799 g_static_rw_lock_signal (lock);
800 g_static_mutex_unlock (&lock->mutex);
804 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
806 g_return_if_fail (lock);
808 if (!g_threads_got_initialized)
811 g_static_mutex_lock (&lock->mutex);
812 lock->want_to_write++;
813 while (lock->have_writer || lock->read_counter)
814 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
815 lock->want_to_write--;
816 lock->have_writer = TRUE;
817 g_static_mutex_unlock (&lock->mutex);
821 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
823 gboolean ret_val = FALSE;
825 g_return_val_if_fail (lock, FALSE);
827 if (!g_threads_got_initialized)
830 g_static_mutex_lock (&lock->mutex);
831 if (!lock->have_writer && !lock->read_counter)
833 lock->have_writer = TRUE;
836 g_static_mutex_unlock (&lock->mutex);
841 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
843 g_return_if_fail (lock);
845 if (!g_threads_got_initialized)
848 g_static_mutex_lock (&lock->mutex);
849 lock->have_writer = FALSE;
850 g_static_rw_lock_signal (lock);
851 g_static_mutex_unlock (&lock->mutex);
855 g_static_rw_lock_free (GStaticRWLock* lock)
857 g_return_if_fail (lock);
861 g_cond_free (lock->read_cond);
862 lock->read_cond = NULL;
864 if (lock->write_cond)
866 g_cond_free (lock->write_cond);
867 lock->write_cond = NULL;
869 g_static_mutex_free (&lock->mutex);
874 * @thread_func: function to call for all GThread structures
875 * @user_data: second argument to @thread_func
877 * Call @thread_func on all existing #GThread structures. Note that
878 * threads may decide to exit while @thread_func is running, so
879 * without intimate knowledge about the lifetime of foreign threads,
880 * @thread_func shouldn't access the GThread* pointer passed in as
881 * first argument. However, @thread_func will not be called for threads
882 * which are known to have exited already.
884 * Due to thread lifetime checks, this function has an execution complexity
885 * which is quadratic in the number of existing threads.
890 g_thread_foreach (GFunc thread_func,
893 GSList *slist = NULL;
895 g_return_if_fail (thread_func != NULL);
896 /* snapshot the list of threads for iteration */
898 for (thread = g_thread_all_threads; thread; thread = thread->next)
899 slist = g_slist_prepend (slist, thread);
901 /* walk the list, skipping non-existant threads */
904 GSList *node = slist;
906 /* check whether the current thread still exists */
908 for (thread = g_thread_all_threads; thread; thread = thread->next)
909 if (thread == node->data)
913 thread_func (thread, user_data);
914 g_slist_free_1 (node);
918 #define __G_THREAD_C__
919 #include "galiasdef.c"