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 /* Global variables */
85 static GSystemThread zero_thread; /* This is initialized to all zero */
86 gboolean g_thread_use_default_impl = TRUE;
87 gboolean g_threads_got_initialized = FALSE;
89 GThreadFunctions g_thread_functions_for_glib_use = {
90 (GMutex*(*)())g_thread_fail, /* mutex_new */
91 NULL, /* mutex_lock */
92 NULL, /* mutex_trylock */
93 NULL, /* mutex_unlock */
94 NULL, /* mutex_free */
95 (GCond*(*)())g_thread_fail, /* cond_new */
96 NULL, /* cond_signal */
97 NULL, /* cond_broadcast */
99 NULL, /* cond_timed_wait */
100 NULL, /* cond_free */
101 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
102 NULL, /* private_get */
103 NULL, /* private_set */
104 (void(*)(GThreadFunc, gpointer, gulong,
105 gboolean, gboolean, GThreadPriority,
106 gpointer, GError**))g_thread_fail, /* thread_create */
107 NULL, /* thread_yield */
108 NULL, /* thread_join */
109 NULL, /* thread_exit */
110 NULL, /* thread_set_priority */
111 NULL, /* thread_self */
112 NULL, /* thread_equal */
113 gettime /* gettime */
118 static GMutex *g_once_mutex = NULL;
119 static GCond *g_once_cond = NULL;
120 static GPrivate *g_thread_specific_private = NULL;
121 static GRealThread *g_thread_all_threads = NULL;
122 static GSList *g_thread_free_indeces = NULL;
124 G_LOCK_DEFINE_STATIC (g_thread);
126 #ifdef G_THREADS_ENABLED
127 /* This must be called only once, before any threads are created.
128 * It will only be called from g_thread_init() in -lgthread.
131 g_thread_init_glib (void)
133 /* We let the main thread (the one that calls g_thread_init) inherit
134 * the static_private data set before calling g_thread_init
136 GRealThread* main_thread = (GRealThread*) g_thread_self ();
138 /* mutex and cond creation works without g_threads_got_initialized */
139 g_once_mutex = g_mutex_new ();
140 g_once_cond = g_cond_new ();
142 /* we may only create mutex and cond in here */
143 _g_mem_thread_init_noprivate_nomessage ();
145 /* setup the basic threading system */
146 g_threads_got_initialized = TRUE;
147 g_thread_specific_private = g_private_new (g_thread_cleanup);
148 g_private_set (g_thread_specific_private, main_thread);
149 G_THREAD_UF (thread_self, (&main_thread->system_thread));
151 /* complete memory system initialization, g_private_*() works now */
152 _g_slice_thread_init_nomessage ();
154 /* accomplish log system initialization to enable messaging */
155 _g_messages_thread_init_nomessage ();
157 /* we may run full-fledged initializers from here */
158 _g_atomic_thread_init ();
159 _g_convert_thread_init ();
160 _g_rand_thread_init ();
161 _g_main_thread_init ();
162 _g_utils_thread_init ();
164 _g_win32_thread_init ();
167 #endif /* G_THREADS_ENABLED */
170 g_once_impl (GOnce *once,
174 g_mutex_lock (g_once_mutex);
176 while (once->status == G_ONCE_STATUS_PROGRESS)
177 g_cond_wait (g_once_cond, g_once_mutex);
179 if (once->status != G_ONCE_STATUS_READY)
181 once->status = G_ONCE_STATUS_PROGRESS;
182 g_mutex_unlock (g_once_mutex);
184 once->retval = func (arg);
186 g_mutex_lock (g_once_mutex);
187 once->status = G_ONCE_STATUS_READY;
188 g_cond_broadcast (g_once_cond);
191 g_mutex_unlock (g_once_mutex);
197 g_static_mutex_init (GStaticMutex *mutex)
199 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
201 g_return_if_fail (mutex);
207 g_static_mutex_get_mutex_impl (GMutex** mutex)
209 if (!g_thread_supported ())
212 g_assert (g_once_mutex);
214 g_mutex_lock (g_once_mutex);
217 g_atomic_pointer_set (mutex, g_mutex_new());
219 g_mutex_unlock (g_once_mutex);
225 g_static_mutex_free (GStaticMutex* mutex)
227 GMutex **runtime_mutex;
229 g_return_if_fail (mutex);
231 /* The runtime_mutex is the first (or only) member of GStaticMutex,
232 * see both versions (of glibconfig.h) in configure.in */
233 runtime_mutex = ((GMutex**)mutex);
236 g_mutex_free (*runtime_mutex);
238 *runtime_mutex = NULL;
242 g_static_rec_mutex_init (GStaticRecMutex *mutex)
244 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
246 g_return_if_fail (mutex);
252 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
256 g_return_if_fail (mutex);
258 if (!g_thread_supported ())
261 G_THREAD_UF (thread_self, (&self));
263 if (g_system_thread_equal (self, mutex->owner))
268 g_static_mutex_lock (&mutex->mutex);
269 g_system_thread_assign (mutex->owner, self);
274 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
278 g_return_val_if_fail (mutex, FALSE);
280 if (!g_thread_supported ())
283 G_THREAD_UF (thread_self, (&self));
285 if (g_system_thread_equal (self, mutex->owner))
291 if (!g_static_mutex_trylock (&mutex->mutex))
294 g_system_thread_assign (mutex->owner, self);
300 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
302 g_return_if_fail (mutex);
304 if (!g_thread_supported ())
307 if (mutex->depth > 1)
312 g_system_thread_assign (mutex->owner, zero_thread);
313 g_static_mutex_unlock (&mutex->mutex);
317 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
321 g_return_if_fail (mutex);
323 if (!g_thread_supported ())
329 G_THREAD_UF (thread_self, (&self));
331 if (g_system_thread_equal (self, mutex->owner))
333 mutex->depth += depth;
336 g_static_mutex_lock (&mutex->mutex);
337 g_system_thread_assign (mutex->owner, self);
338 mutex->depth = depth;
342 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
346 g_return_val_if_fail (mutex, 0);
348 if (!g_thread_supported ())
351 depth = mutex->depth;
353 g_system_thread_assign (mutex->owner, zero_thread);
355 g_static_mutex_unlock (&mutex->mutex);
361 g_static_rec_mutex_free (GStaticRecMutex *mutex)
363 g_return_if_fail (mutex);
365 g_static_mutex_free (&mutex->mutex);
369 g_static_private_init (GStaticPrivate *private_key)
371 private_key->index = 0;
375 g_static_private_get (GStaticPrivate *private_key)
377 GRealThread *self = (GRealThread*) g_thread_self ();
380 array = self->private_data;
384 if (!private_key->index)
386 else if (private_key->index <= array->len)
387 return g_array_index (array, GStaticPrivateNode,
388 private_key->index - 1).data;
394 g_static_private_set (GStaticPrivate *private_key,
396 GDestroyNotify notify)
398 GRealThread *self = (GRealThread*) g_thread_self ();
400 static guint next_index = 0;
401 GStaticPrivateNode *node;
403 array = self->private_data;
406 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
407 self->private_data = array;
410 if (!private_key->index)
414 if (!private_key->index)
416 if (g_thread_free_indeces)
419 GPOINTER_TO_UINT (g_thread_free_indeces->data);
420 g_thread_free_indeces =
421 g_slist_delete_link (g_thread_free_indeces,
422 g_thread_free_indeces);
425 private_key->index = ++next_index;
431 if (private_key->index > array->len)
432 g_array_set_size (array, private_key->index);
434 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
437 gpointer ddata = node->data;
438 GDestroyNotify ddestroy = node->destroy;
441 node->destroy = notify;
448 node->destroy = notify;
453 g_static_private_free (GStaticPrivate *private_key)
455 guint index = private_key->index;
461 private_key->index = 0;
465 thread = g_thread_all_threads;
468 GArray *array = thread->private_data;
469 thread = thread->next;
471 if (array && index <= array->len)
473 GStaticPrivateNode *node = &g_array_index (array,
476 gpointer ddata = node->data;
477 GDestroyNotify ddestroy = node->destroy;
480 node->destroy = NULL;
490 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
491 GUINT_TO_POINTER (index));
496 g_thread_cleanup (gpointer data)
500 GRealThread* thread = data;
501 if (thread->private_data)
503 GArray* array = thread->private_data;
506 for (i = 0; i < array->len; i++ )
508 GStaticPrivateNode *node =
509 &g_array_index (array, GStaticPrivateNode, i);
511 node->destroy (node->data);
513 g_array_free (array, TRUE);
516 /* We only free the thread structure, if it isn't joinable. If
517 it is, the structure is freed in g_thread_join */
518 if (!thread->thread.joinable)
523 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
530 g_thread_all_threads = t->next;
536 /* Just to make sure, this isn't used any more */
537 g_system_thread_assign (thread->system_thread, zero_thread);
546 g_error ("The thread system is not yet initialized.");
549 #define G_NSEC_PER_SEC 1000000000
557 GetSystemTimeAsFileTime ((FILETIME *)&v);
563 gettimeofday (&tv, NULL);
565 return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
570 g_thread_create_proxy (gpointer data)
572 GRealThread* thread = data;
576 /* This has to happen before G_LOCK, as that might call g_thread_self */
577 g_private_set (g_thread_specific_private, data);
579 /* the lock makes sure, that thread->system_thread is written,
580 before thread->thread.func is called. See g_thread_create. */
584 thread->retval = thread->thread.func (thread->thread.data);
590 g_thread_create_full (GThreadFunc func,
595 GThreadPriority priority,
599 GError *local_error = NULL;
600 g_return_val_if_fail (func, NULL);
601 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
602 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
604 result = g_new0 (GRealThread, 1);
606 result->thread.joinable = joinable;
607 result->thread.priority = priority;
608 result->thread.func = func;
609 result->thread.data = data;
610 result->private_data = NULL;
612 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
613 stack_size, joinable, bound, priority,
614 &result->system_thread, &local_error));
615 result->next = g_thread_all_threads;
616 g_thread_all_threads = result;
621 g_propagate_error (error, local_error);
626 return (GThread*) result;
630 g_thread_exit (gpointer retval)
632 GRealThread* real = (GRealThread*) g_thread_self ();
633 real->retval = retval;
634 G_THREAD_CF (thread_exit, (void)0, ());
638 g_thread_join (GThread* thread)
640 GRealThread* real = (GRealThread*) thread;
644 g_return_val_if_fail (thread, NULL);
645 g_return_val_if_fail (thread->joinable, NULL);
646 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
649 G_THREAD_UF (thread_join, (&real->system_thread));
651 retval = real->retval;
654 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
656 if (t == (GRealThread*) thread)
661 g_thread_all_threads = t->next;
667 /* Just to make sure, this isn't used any more */
668 thread->joinable = 0;
669 g_system_thread_assign (real->system_thread, zero_thread);
671 /* the thread structure for non-joinable threads is freed upon
672 thread end. We free the memory here. This will leave a loose end,
673 if a joinable thread is not joined. */
681 g_thread_set_priority (GThread* thread,
682 GThreadPriority priority)
684 GRealThread* real = (GRealThread*) thread;
686 g_return_if_fail (thread);
687 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
688 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
689 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
691 thread->priority = priority;
693 G_THREAD_CF (thread_set_priority, (void)0,
694 (&real->system_thread, priority));
700 GRealThread* thread = g_private_get (g_thread_specific_private);
704 /* If no thread data is available, provide and set one. This
705 can happen for the main thread and for threads, that are not
707 thread = g_new0 (GRealThread, 1);
708 thread->thread.joinable = FALSE; /* This is a save guess */
709 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
711 thread->thread.func = NULL;
712 thread->thread.data = NULL;
713 thread->private_data = NULL;
715 if (g_thread_supported ())
716 G_THREAD_UF (thread_self, (&thread->system_thread));
718 g_private_set (g_thread_specific_private, thread);
721 thread->next = g_thread_all_threads;
722 g_thread_all_threads = thread;
726 return (GThread*)thread;
730 g_static_rw_lock_init (GStaticRWLock* lock)
732 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
734 g_return_if_fail (lock);
740 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
743 *cond = g_cond_new ();
744 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
748 g_static_rw_lock_signal (GStaticRWLock* lock)
750 if (lock->want_to_write && lock->write_cond)
751 g_cond_signal (lock->write_cond);
752 else if (lock->want_to_read && lock->read_cond)
753 g_cond_broadcast (lock->read_cond);
757 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
759 g_return_if_fail (lock);
761 if (!g_threads_got_initialized)
764 g_static_mutex_lock (&lock->mutex);
765 lock->want_to_read++;
766 while (lock->have_writer || lock->want_to_write)
767 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
768 lock->want_to_read--;
769 lock->read_counter++;
770 g_static_mutex_unlock (&lock->mutex);
774 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
776 gboolean ret_val = FALSE;
778 g_return_val_if_fail (lock, FALSE);
780 if (!g_threads_got_initialized)
783 g_static_mutex_lock (&lock->mutex);
784 if (!lock->have_writer && !lock->want_to_write)
786 lock->read_counter++;
789 g_static_mutex_unlock (&lock->mutex);
794 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
796 g_return_if_fail (lock);
798 if (!g_threads_got_initialized)
801 g_static_mutex_lock (&lock->mutex);
802 lock->read_counter--;
803 if (lock->read_counter == 0)
804 g_static_rw_lock_signal (lock);
805 g_static_mutex_unlock (&lock->mutex);
809 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
811 g_return_if_fail (lock);
813 if (!g_threads_got_initialized)
816 g_static_mutex_lock (&lock->mutex);
817 lock->want_to_write++;
818 while (lock->have_writer || lock->read_counter)
819 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
820 lock->want_to_write--;
821 lock->have_writer = TRUE;
822 g_static_mutex_unlock (&lock->mutex);
826 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
828 gboolean ret_val = FALSE;
830 g_return_val_if_fail (lock, FALSE);
832 if (!g_threads_got_initialized)
835 g_static_mutex_lock (&lock->mutex);
836 if (!lock->have_writer && !lock->read_counter)
838 lock->have_writer = TRUE;
841 g_static_mutex_unlock (&lock->mutex);
846 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
848 g_return_if_fail (lock);
850 if (!g_threads_got_initialized)
853 g_static_mutex_lock (&lock->mutex);
854 lock->have_writer = FALSE;
855 g_static_rw_lock_signal (lock);
856 g_static_mutex_unlock (&lock->mutex);
860 g_static_rw_lock_free (GStaticRWLock* lock)
862 g_return_if_fail (lock);
866 g_cond_free (lock->read_cond);
867 lock->read_cond = NULL;
869 if (lock->write_cond)
871 g_cond_free (lock->write_cond);
872 lock->write_cond = NULL;
874 g_static_mutex_free (&lock->mutex);
879 * @thread_func: function to call for all GThread structures
880 * @user_data: second argument to @thread_func
882 * Call @thread_func on all existing #GThread structures. Note that
883 * threads may decide to exit while @thread_func is running, so
884 * without intimate knowledge about the lifetime of foreign threads,
885 * @thread_func shouldn't access the GThread* pointer passed in as
886 * first argument. However, @thread_func will not be called for threads
887 * which are known to have exited already.
889 * Due to thread lifetime checks, this function has an execution complexity
890 * which is quadratic in the number of existing threads.
895 g_thread_foreach (GFunc thread_func,
898 GSList *slist = NULL;
900 g_return_if_fail (thread_func != NULL);
901 /* snapshot the list of threads for iteration */
903 for (thread = g_thread_all_threads; thread; thread = thread->next)
904 slist = g_slist_prepend (slist, thread);
906 /* walk the list, skipping non-existant threads */
909 GSList *node = slist;
911 /* check whether the current thread still exists */
913 for (thread = g_thread_all_threads; thread; thread = thread->next)
914 if (thread == node->data)
918 thread_func (thread, user_data);
919 g_slist_free_1 (node);
923 #define __G_THREAD_C__
924 #include "galiasdef.c"