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"
48 #endif /* G_OS_WIN32 */
55 g_thread_error_quark (void)
57 return g_quark_from_static_string ("g_thread_error");
60 /* Keep this in sync with GRealThread in gmain.c! */
61 typedef struct _GRealThread GRealThread;
65 gpointer private_data;
68 GSystemThread system_thread;
71 typedef struct _GStaticPrivateNode GStaticPrivateNode;
72 struct _GStaticPrivateNode
75 GDestroyNotify destroy;
78 static void g_thread_cleanup (gpointer data);
79 static void g_thread_fail (void);
80 static guint64 gettime (void);
82 /* Global variables */
84 static GSystemThread zero_thread; /* This is initialized to all zero */
85 gboolean g_thread_use_default_impl = TRUE;
86 gboolean g_threads_got_initialized = FALSE;
88 GThreadFunctions g_thread_functions_for_glib_use = {
89 (GMutex*(*)())g_thread_fail, /* mutex_new */
90 NULL, /* mutex_lock */
91 NULL, /* mutex_trylock */
92 NULL, /* mutex_unlock */
93 NULL, /* mutex_free */
94 (GCond*(*)())g_thread_fail, /* cond_new */
95 NULL, /* cond_signal */
96 NULL, /* cond_broadcast */
98 NULL, /* cond_timed_wait */
100 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
101 NULL, /* private_get */
102 NULL, /* private_set */
103 (void(*)(GThreadFunc, gpointer, gulong,
104 gboolean, gboolean, GThreadPriority,
105 gpointer, GError**))g_thread_fail, /* thread_create */
106 NULL, /* thread_yield */
107 NULL, /* thread_join */
108 NULL, /* thread_exit */
109 NULL, /* thread_set_priority */
110 NULL, /* thread_self */
111 NULL, /* thread_equal */
112 gettime /* gettime */
117 static GMutex *g_once_mutex = NULL;
118 static GCond *g_once_cond = NULL;
119 static GPrivate *g_thread_specific_private = NULL;
120 static GRealThread *g_thread_all_threads = NULL;
121 static GSList *g_thread_free_indeces = NULL;
123 G_LOCK_DEFINE_STATIC (g_thread);
125 #ifdef G_THREADS_ENABLED
126 /* This must be called only once, before any threads are created.
127 * It will only be called from g_thread_init() in -lgthread.
130 g_thread_init_glib (void)
132 /* We let the main thread (the one that calls g_thread_init) inherit
133 * the static_private data set before calling g_thread_init
135 GRealThread* main_thread = (GRealThread*) g_thread_self ();
137 /* mutex and cond creation works without g_threads_got_initialized */
138 g_once_mutex = g_mutex_new ();
139 g_once_cond = g_cond_new ();
141 /* we may only create mutex and cond in here */
142 _g_mem_thread_init_noprivate_nomessage ();
144 /* setup the basic threading system */
145 g_threads_got_initialized = TRUE;
146 g_thread_specific_private = g_private_new (g_thread_cleanup);
147 g_private_set (g_thread_specific_private, main_thread);
148 G_THREAD_UF (thread_self, (&main_thread->system_thread));
150 /* complete memory system initialization, g_private_*() works now */
151 _g_slice_thread_init_nomessage ();
153 /* accomplish log system initialization to enable messaging */
154 _g_messages_thread_init_nomessage ();
156 /* we may run full-fledged initializers from here */
157 _g_atomic_thread_init ();
158 _g_convert_thread_init ();
159 _g_rand_thread_init ();
160 _g_main_thread_init ();
161 _g_utils_thread_init ();
163 _g_win32_thread_init ();
166 #endif /* G_THREADS_ENABLED */
169 g_once_impl (GOnce *once,
173 g_mutex_lock (g_once_mutex);
175 while (once->status == G_ONCE_STATUS_PROGRESS)
176 g_cond_wait (g_once_cond, g_once_mutex);
178 if (once->status != G_ONCE_STATUS_READY)
180 once->status = G_ONCE_STATUS_PROGRESS;
181 g_mutex_unlock (g_once_mutex);
183 once->retval = func (arg);
185 g_mutex_lock (g_once_mutex);
186 once->status = G_ONCE_STATUS_READY;
187 g_cond_broadcast (g_once_cond);
190 g_mutex_unlock (g_once_mutex);
196 g_static_mutex_init (GStaticMutex *mutex)
198 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
200 g_return_if_fail (mutex);
206 g_static_mutex_get_mutex_impl (GMutex** mutex)
208 if (!g_thread_supported ())
211 g_assert (g_once_mutex);
213 g_mutex_lock (g_once_mutex);
216 g_atomic_pointer_set (mutex, g_mutex_new());
218 g_mutex_unlock (g_once_mutex);
224 g_static_mutex_free (GStaticMutex* mutex)
226 GMutex **runtime_mutex;
228 g_return_if_fail (mutex);
230 /* The runtime_mutex is the first (or only) member of GStaticMutex,
231 * see both versions (of glibconfig.h) in configure.in */
232 runtime_mutex = ((GMutex**)mutex);
235 g_mutex_free (*runtime_mutex);
237 *runtime_mutex = NULL;
241 g_static_rec_mutex_init (GStaticRecMutex *mutex)
243 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
245 g_return_if_fail (mutex);
251 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
255 g_return_if_fail (mutex);
257 if (!g_thread_supported ())
260 G_THREAD_UF (thread_self, (&self));
262 if (g_system_thread_equal (self, mutex->owner))
267 g_static_mutex_lock (&mutex->mutex);
268 g_system_thread_assign (mutex->owner, self);
273 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
277 g_return_val_if_fail (mutex, FALSE);
279 if (!g_thread_supported ())
282 G_THREAD_UF (thread_self, (&self));
284 if (g_system_thread_equal (self, mutex->owner))
290 if (!g_static_mutex_trylock (&mutex->mutex))
293 g_system_thread_assign (mutex->owner, self);
299 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
301 g_return_if_fail (mutex);
303 if (!g_thread_supported ())
306 if (mutex->depth > 1)
311 g_system_thread_assign (mutex->owner, zero_thread);
312 g_static_mutex_unlock (&mutex->mutex);
316 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
320 g_return_if_fail (mutex);
322 if (!g_thread_supported ())
328 G_THREAD_UF (thread_self, (&self));
330 if (g_system_thread_equal (self, mutex->owner))
332 mutex->depth += depth;
335 g_static_mutex_lock (&mutex->mutex);
336 g_system_thread_assign (mutex->owner, self);
337 mutex->depth = depth;
341 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
345 g_return_val_if_fail (mutex, 0);
347 if (!g_thread_supported ())
350 depth = mutex->depth;
352 g_system_thread_assign (mutex->owner, zero_thread);
354 g_static_mutex_unlock (&mutex->mutex);
360 g_static_rec_mutex_free (GStaticRecMutex *mutex)
362 g_return_if_fail (mutex);
364 g_static_mutex_free (&mutex->mutex);
368 g_static_private_init (GStaticPrivate *private_key)
370 private_key->index = 0;
374 g_static_private_get (GStaticPrivate *private_key)
376 GRealThread *self = (GRealThread*) g_thread_self ();
379 array = self->private_data;
383 if (!private_key->index)
385 else if (private_key->index <= array->len)
386 return g_array_index (array, GStaticPrivateNode,
387 private_key->index - 1).data;
393 g_static_private_set (GStaticPrivate *private_key,
395 GDestroyNotify notify)
397 GRealThread *self = (GRealThread*) g_thread_self ();
399 static guint next_index = 0;
400 GStaticPrivateNode *node;
402 array = self->private_data;
405 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
406 self->private_data = array;
409 if (!private_key->index)
413 if (!private_key->index)
415 if (g_thread_free_indeces)
418 GPOINTER_TO_UINT (g_thread_free_indeces->data);
419 g_thread_free_indeces =
420 g_slist_delete_link (g_thread_free_indeces,
421 g_thread_free_indeces);
424 private_key->index = ++next_index;
430 if (private_key->index > array->len)
431 g_array_set_size (array, private_key->index);
433 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
436 gpointer ddata = node->data;
437 GDestroyNotify ddestroy = node->destroy;
440 node->destroy = notify;
447 node->destroy = notify;
452 g_static_private_free (GStaticPrivate *private_key)
454 guint index = private_key->index;
460 private_key->index = 0;
464 thread = g_thread_all_threads;
467 GArray *array = thread->private_data;
468 thread = thread->next;
470 if (array && index <= array->len)
472 GStaticPrivateNode *node = &g_array_index (array,
475 gpointer ddata = node->data;
476 GDestroyNotify ddestroy = node->destroy;
479 node->destroy = NULL;
489 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
490 GUINT_TO_POINTER (index));
495 g_thread_cleanup (gpointer data)
499 GRealThread* thread = data;
500 if (thread->private_data)
502 GArray* array = thread->private_data;
505 for (i = 0; i < array->len; i++ )
507 GStaticPrivateNode *node =
508 &g_array_index (array, GStaticPrivateNode, i);
510 node->destroy (node->data);
512 g_array_free (array, TRUE);
515 /* We only free the thread structure, if it isn't joinable. If
516 it is, the structure is freed in g_thread_join */
517 if (!thread->thread.joinable)
522 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
529 g_thread_all_threads = t->next;
535 /* Just to make sure, this isn't used any more */
536 g_system_thread_assign (thread->system_thread, zero_thread);
545 g_error ("The thread system is not yet initialized.");
554 GetSystemTimeAsFileTime ((FILETIME *)&v);
560 gettimeofday (&tv, NULL);
562 return (guint64) tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
567 g_thread_create_proxy (gpointer data)
569 GRealThread* thread = data;
573 /* This has to happen before G_LOCK, as that might call g_thread_self */
574 g_private_set (g_thread_specific_private, data);
576 /* the lock makes sure, that thread->system_thread is written,
577 before thread->thread.func is called. See g_thread_create. */
581 thread->retval = thread->thread.func (thread->thread.data);
587 g_thread_create_full (GThreadFunc func,
592 GThreadPriority priority,
596 GError *local_error = NULL;
597 g_return_val_if_fail (func, NULL);
598 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
599 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
601 result = g_new0 (GRealThread, 1);
603 result->thread.joinable = joinable;
604 result->thread.priority = priority;
605 result->thread.func = func;
606 result->thread.data = data;
607 result->private_data = NULL;
609 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
610 stack_size, joinable, bound, priority,
611 &result->system_thread, &local_error));
612 result->next = g_thread_all_threads;
613 g_thread_all_threads = result;
618 g_propagate_error (error, local_error);
623 return (GThread*) result;
627 g_thread_exit (gpointer retval)
629 GRealThread* real = (GRealThread*) g_thread_self ();
630 real->retval = retval;
631 G_THREAD_CF (thread_exit, (void)0, ());
635 g_thread_join (GThread* thread)
637 GRealThread* real = (GRealThread*) thread;
641 g_return_val_if_fail (thread, NULL);
642 g_return_val_if_fail (thread->joinable, NULL);
643 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
646 G_THREAD_UF (thread_join, (&real->system_thread));
648 retval = real->retval;
651 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
653 if (t == (GRealThread*) thread)
658 g_thread_all_threads = t->next;
664 /* Just to make sure, this isn't used any more */
665 thread->joinable = 0;
666 g_system_thread_assign (real->system_thread, zero_thread);
668 /* the thread structure for non-joinable threads is freed upon
669 thread end. We free the memory here. This will leave a loose end,
670 if a joinable thread is not joined. */
678 g_thread_set_priority (GThread* thread,
679 GThreadPriority priority)
681 GRealThread* real = (GRealThread*) thread;
683 g_return_if_fail (thread);
684 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
685 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
686 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
688 thread->priority = priority;
690 G_THREAD_CF (thread_set_priority, (void)0,
691 (&real->system_thread, priority));
697 GRealThread* thread = g_private_get (g_thread_specific_private);
701 /* If no thread data is available, provide and set one. This
702 can happen for the main thread and for threads, that are not
704 thread = g_new0 (GRealThread, 1);
705 thread->thread.joinable = FALSE; /* This is a save guess */
706 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
708 thread->thread.func = NULL;
709 thread->thread.data = NULL;
710 thread->private_data = NULL;
712 if (g_thread_supported ())
713 G_THREAD_UF (thread_self, (&thread->system_thread));
715 g_private_set (g_thread_specific_private, thread);
718 thread->next = g_thread_all_threads;
719 g_thread_all_threads = thread;
723 return (GThread*)thread;
727 g_static_rw_lock_init (GStaticRWLock* lock)
729 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
731 g_return_if_fail (lock);
737 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
740 *cond = g_cond_new ();
741 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
745 g_static_rw_lock_signal (GStaticRWLock* lock)
747 if (lock->want_to_write && lock->write_cond)
748 g_cond_signal (lock->write_cond);
749 else if (lock->want_to_read && lock->read_cond)
750 g_cond_broadcast (lock->read_cond);
754 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
756 g_return_if_fail (lock);
758 if (!g_threads_got_initialized)
761 g_static_mutex_lock (&lock->mutex);
762 lock->want_to_read++;
763 while (lock->have_writer || lock->want_to_write)
764 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
765 lock->want_to_read--;
766 lock->read_counter++;
767 g_static_mutex_unlock (&lock->mutex);
771 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
773 gboolean ret_val = FALSE;
775 g_return_val_if_fail (lock, FALSE);
777 if (!g_threads_got_initialized)
780 g_static_mutex_lock (&lock->mutex);
781 if (!lock->have_writer && !lock->want_to_write)
783 lock->read_counter++;
786 g_static_mutex_unlock (&lock->mutex);
791 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
793 g_return_if_fail (lock);
795 if (!g_threads_got_initialized)
798 g_static_mutex_lock (&lock->mutex);
799 lock->read_counter--;
800 if (lock->read_counter == 0)
801 g_static_rw_lock_signal (lock);
802 g_static_mutex_unlock (&lock->mutex);
806 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
808 g_return_if_fail (lock);
810 if (!g_threads_got_initialized)
813 g_static_mutex_lock (&lock->mutex);
814 lock->want_to_write++;
815 while (lock->have_writer || lock->read_counter)
816 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
817 lock->want_to_write--;
818 lock->have_writer = TRUE;
819 g_static_mutex_unlock (&lock->mutex);
823 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
825 gboolean ret_val = FALSE;
827 g_return_val_if_fail (lock, FALSE);
829 if (!g_threads_got_initialized)
832 g_static_mutex_lock (&lock->mutex);
833 if (!lock->have_writer && !lock->read_counter)
835 lock->have_writer = TRUE;
838 g_static_mutex_unlock (&lock->mutex);
843 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
845 g_return_if_fail (lock);
847 if (!g_threads_got_initialized)
850 g_static_mutex_lock (&lock->mutex);
851 lock->have_writer = FALSE;
852 g_static_rw_lock_signal (lock);
853 g_static_mutex_unlock (&lock->mutex);
857 g_static_rw_lock_free (GStaticRWLock* lock)
859 g_return_if_fail (lock);
863 g_cond_free (lock->read_cond);
864 lock->read_cond = NULL;
866 if (lock->write_cond)
868 g_cond_free (lock->write_cond);
869 lock->write_cond = NULL;
871 g_static_mutex_free (&lock->mutex);
876 * @thread_func: function to call for all GThread structures
877 * @user_data: second argument to @thread_func
879 * Call @thread_func on all existing #GThread structures. Note that
880 * threads may decide to exit while @thread_func is running, so
881 * without intimate knowledge about the lifetime of foreign threads,
882 * @thread_func shouldn't access the GThread* pointer passed in as
883 * first argument. However, @thread_func will not be called for threads
884 * which are known to have exited already.
886 * Due to thread lifetime checks, this function has an execution complexity
887 * which is quadratic in the number of existing threads.
892 g_thread_foreach (GFunc thread_func,
895 GSList *slist = NULL;
897 g_return_if_fail (thread_func != NULL);
898 /* snapshot the list of threads for iteration */
900 for (thread = g_thread_all_threads; thread; thread = thread->next)
901 slist = g_slist_prepend (slist, thread);
903 /* walk the list, skipping non-existant threads */
906 GSList *node = slist;
908 /* check whether the current thread still exists */
910 for (thread = g_thread_all_threads; thread; thread = thread->next)
911 if (thread == node->data)
915 thread_func (thread, user_data);
916 g_slist_free_1 (node);
920 #define __G_THREAD_C__
921 #include "galiasdef.c"