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 guint64 (*g_thread_gettime) (void) = gettime;
85 /* Global variables */
87 static GSystemThread zero_thread; /* This is initialized to all zero */
88 gboolean g_thread_use_default_impl = TRUE;
89 gboolean g_threads_got_initialized = FALSE;
91 GThreadFunctions g_thread_functions_for_glib_use = {
92 (GMutex*(*)())g_thread_fail, /* mutex_new */
93 NULL, /* mutex_lock */
94 NULL, /* mutex_trylock */
95 NULL, /* mutex_unlock */
96 NULL, /* mutex_free */
97 (GCond*(*)())g_thread_fail, /* cond_new */
98 NULL, /* cond_signal */
99 NULL, /* cond_broadcast */
100 NULL, /* cond_wait */
101 NULL, /* cond_timed_wait */
102 NULL, /* cond_free */
103 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
104 NULL, /* private_get */
105 NULL, /* private_set */
106 (void(*)(GThreadFunc, gpointer, gulong,
107 gboolean, gboolean, GThreadPriority,
108 gpointer, GError**))g_thread_fail, /* thread_create */
109 NULL, /* thread_yield */
110 NULL, /* thread_join */
111 NULL, /* thread_exit */
112 NULL, /* thread_set_priority */
113 NULL, /* thread_self */
114 NULL /* thread_equal */
119 static GMutex *g_once_mutex = NULL;
120 static GCond *g_once_cond = NULL;
121 static GPrivate *g_thread_specific_private = NULL;
122 static GRealThread *g_thread_all_threads = NULL;
123 static GSList *g_thread_free_indeces = NULL;
125 G_LOCK_DEFINE_STATIC (g_thread);
127 #ifdef G_THREADS_ENABLED
128 /* This must be called only once, before any threads are created.
129 * It will only be called from g_thread_init() in -lgthread.
132 g_thread_init_glib (void)
134 /* We let the main thread (the one that calls g_thread_init) inherit
135 * the static_private data set before calling g_thread_init
137 GRealThread* main_thread = (GRealThread*) g_thread_self ();
139 /* mutex and cond creation works without g_threads_got_initialized */
140 g_once_mutex = g_mutex_new ();
141 g_once_cond = g_cond_new ();
143 /* we may only create mutex and cond in here */
144 _g_mem_thread_init_noprivate_nomessage ();
146 /* setup the basic threading system */
147 g_threads_got_initialized = TRUE;
148 g_thread_specific_private = g_private_new (g_thread_cleanup);
149 g_private_set (g_thread_specific_private, main_thread);
150 G_THREAD_UF (thread_self, (&main_thread->system_thread));
152 /* complete memory system initialization, g_private_*() works now */
153 _g_slice_thread_init_nomessage ();
155 /* accomplish log system initialization to enable messaging */
156 _g_messages_thread_init_nomessage ();
158 /* we may run full-fledged initializers from here */
159 _g_atomic_thread_init ();
160 _g_convert_thread_init ();
161 _g_rand_thread_init ();
162 _g_main_thread_init ();
163 _g_utils_thread_init ();
165 _g_win32_thread_init ();
168 #endif /* G_THREADS_ENABLED */
171 g_once_impl (GOnce *once,
175 g_mutex_lock (g_once_mutex);
177 while (once->status == G_ONCE_STATUS_PROGRESS)
178 g_cond_wait (g_once_cond, g_once_mutex);
180 if (once->status != G_ONCE_STATUS_READY)
182 once->status = G_ONCE_STATUS_PROGRESS;
183 g_mutex_unlock (g_once_mutex);
185 once->retval = func (arg);
187 g_mutex_lock (g_once_mutex);
188 once->status = G_ONCE_STATUS_READY;
189 g_cond_broadcast (g_once_cond);
192 g_mutex_unlock (g_once_mutex);
198 g_static_mutex_init (GStaticMutex *mutex)
200 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
202 g_return_if_fail (mutex);
208 g_static_mutex_get_mutex_impl (GMutex** mutex)
210 if (!g_thread_supported ())
213 g_assert (g_once_mutex);
215 g_mutex_lock (g_once_mutex);
218 g_atomic_pointer_set (mutex, g_mutex_new());
220 g_mutex_unlock (g_once_mutex);
226 g_static_mutex_free (GStaticMutex* mutex)
228 GMutex **runtime_mutex;
230 g_return_if_fail (mutex);
232 /* The runtime_mutex is the first (or only) member of GStaticMutex,
233 * see both versions (of glibconfig.h) in configure.in. Note, that
234 * this variable is NULL, if g_thread_init() hasn't been called or
235 * if we're using the default thread implementation and it provides
237 runtime_mutex = ((GMutex**)mutex);
240 g_mutex_free (*runtime_mutex);
242 *runtime_mutex = NULL;
246 g_static_rec_mutex_init (GStaticRecMutex *mutex)
248 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
250 g_return_if_fail (mutex);
256 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
260 g_return_if_fail (mutex);
262 if (!g_thread_supported ())
265 G_THREAD_UF (thread_self, (&self));
267 if (g_system_thread_equal (self, mutex->owner))
272 g_static_mutex_lock (&mutex->mutex);
273 g_system_thread_assign (mutex->owner, self);
278 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
282 g_return_val_if_fail (mutex, FALSE);
284 if (!g_thread_supported ())
287 G_THREAD_UF (thread_self, (&self));
289 if (g_system_thread_equal (self, mutex->owner))
295 if (!g_static_mutex_trylock (&mutex->mutex))
298 g_system_thread_assign (mutex->owner, self);
304 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
306 g_return_if_fail (mutex);
308 if (!g_thread_supported ())
311 if (mutex->depth > 1)
316 g_system_thread_assign (mutex->owner, zero_thread);
317 g_static_mutex_unlock (&mutex->mutex);
321 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
325 g_return_if_fail (mutex);
327 if (!g_thread_supported ())
333 G_THREAD_UF (thread_self, (&self));
335 if (g_system_thread_equal (self, mutex->owner))
337 mutex->depth += depth;
340 g_static_mutex_lock (&mutex->mutex);
341 g_system_thread_assign (mutex->owner, self);
342 mutex->depth = depth;
346 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
350 g_return_val_if_fail (mutex, 0);
352 if (!g_thread_supported ())
355 depth = mutex->depth;
357 g_system_thread_assign (mutex->owner, zero_thread);
359 g_static_mutex_unlock (&mutex->mutex);
365 g_static_rec_mutex_free (GStaticRecMutex *mutex)
367 g_return_if_fail (mutex);
369 g_static_mutex_free (&mutex->mutex);
373 g_static_private_init (GStaticPrivate *private_key)
375 private_key->index = 0;
379 g_static_private_get (GStaticPrivate *private_key)
381 GRealThread *self = (GRealThread*) g_thread_self ();
384 array = self->private_data;
388 if (!private_key->index)
390 else if (private_key->index <= array->len)
391 return g_array_index (array, GStaticPrivateNode,
392 private_key->index - 1).data;
398 g_static_private_set (GStaticPrivate *private_key,
400 GDestroyNotify notify)
402 GRealThread *self = (GRealThread*) g_thread_self ();
404 static guint next_index = 0;
405 GStaticPrivateNode *node;
407 array = self->private_data;
410 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
411 self->private_data = array;
414 if (!private_key->index)
418 if (!private_key->index)
420 if (g_thread_free_indeces)
423 GPOINTER_TO_UINT (g_thread_free_indeces->data);
424 g_thread_free_indeces =
425 g_slist_delete_link (g_thread_free_indeces,
426 g_thread_free_indeces);
429 private_key->index = ++next_index;
435 if (private_key->index > array->len)
436 g_array_set_size (array, private_key->index);
438 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
441 gpointer ddata = node->data;
442 GDestroyNotify ddestroy = node->destroy;
445 node->destroy = notify;
452 node->destroy = notify;
457 g_static_private_free (GStaticPrivate *private_key)
459 guint index = private_key->index;
465 private_key->index = 0;
469 thread = g_thread_all_threads;
472 GArray *array = thread->private_data;
473 thread = thread->next;
475 if (array && index <= array->len)
477 GStaticPrivateNode *node = &g_array_index (array,
480 gpointer ddata = node->data;
481 GDestroyNotify ddestroy = node->destroy;
484 node->destroy = NULL;
494 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
495 GUINT_TO_POINTER (index));
500 g_thread_cleanup (gpointer data)
504 GRealThread* thread = data;
505 if (thread->private_data)
507 GArray* array = thread->private_data;
510 for (i = 0; i < array->len; i++ )
512 GStaticPrivateNode *node =
513 &g_array_index (array, GStaticPrivateNode, i);
515 node->destroy (node->data);
517 g_array_free (array, TRUE);
520 /* We only free the thread structure, if it isn't joinable. If
521 it is, the structure is freed in g_thread_join */
522 if (!thread->thread.joinable)
527 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
534 g_thread_all_threads = t->next;
540 /* Just to make sure, this isn't used any more */
541 g_system_thread_assign (thread->system_thread, zero_thread);
550 g_error ("The thread system is not yet initialized.");
553 #define G_NSEC_PER_SEC 1000000000
561 /* Returns 100s of nanoseconds since start of 1601 */
562 GetSystemTimeAsFileTime ((FILETIME *)&v);
564 /* Offset to Unix epoch */
565 v -= G_GINT64_CONSTANT (116444736000000000);
566 /* Convert to nanoseconds */
573 gettimeofday (&tv, NULL);
575 return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
580 g_thread_create_proxy (gpointer data)
582 GRealThread* thread = data;
586 /* This has to happen before G_LOCK, as that might call g_thread_self */
587 g_private_set (g_thread_specific_private, data);
589 /* the lock makes sure, that thread->system_thread is written,
590 before thread->thread.func is called. See g_thread_create. */
594 thread->retval = thread->thread.func (thread->thread.data);
600 g_thread_create_full (GThreadFunc func,
605 GThreadPriority priority,
609 GError *local_error = NULL;
610 g_return_val_if_fail (func, NULL);
611 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
612 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
614 result = g_new0 (GRealThread, 1);
616 result->thread.joinable = joinable;
617 result->thread.priority = priority;
618 result->thread.func = func;
619 result->thread.data = data;
620 result->private_data = NULL;
622 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
623 stack_size, joinable, bound, priority,
624 &result->system_thread, &local_error));
625 result->next = g_thread_all_threads;
626 g_thread_all_threads = result;
631 g_propagate_error (error, local_error);
636 return (GThread*) result;
640 g_thread_exit (gpointer retval)
642 GRealThread* real = (GRealThread*) g_thread_self ();
643 real->retval = retval;
644 G_THREAD_CF (thread_exit, (void)0, ());
648 g_thread_join (GThread* thread)
650 GRealThread* real = (GRealThread*) thread;
654 g_return_val_if_fail (thread, NULL);
655 g_return_val_if_fail (thread->joinable, NULL);
656 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
659 G_THREAD_UF (thread_join, (&real->system_thread));
661 retval = real->retval;
664 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
666 if (t == (GRealThread*) thread)
671 g_thread_all_threads = t->next;
677 /* Just to make sure, this isn't used any more */
678 thread->joinable = 0;
679 g_system_thread_assign (real->system_thread, zero_thread);
681 /* the thread structure for non-joinable threads is freed upon
682 thread end. We free the memory here. This will leave a loose end,
683 if a joinable thread is not joined. */
691 g_thread_set_priority (GThread* thread,
692 GThreadPriority priority)
694 GRealThread* real = (GRealThread*) thread;
696 g_return_if_fail (thread);
697 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
698 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
699 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
701 thread->priority = priority;
703 G_THREAD_CF (thread_set_priority, (void)0,
704 (&real->system_thread, priority));
710 GRealThread* thread = g_private_get (g_thread_specific_private);
714 /* If no thread data is available, provide and set one. This
715 can happen for the main thread and for threads, that are not
717 thread = g_new0 (GRealThread, 1);
718 thread->thread.joinable = FALSE; /* This is a save guess */
719 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
721 thread->thread.func = NULL;
722 thread->thread.data = NULL;
723 thread->private_data = NULL;
725 if (g_thread_supported ())
726 G_THREAD_UF (thread_self, (&thread->system_thread));
728 g_private_set (g_thread_specific_private, thread);
731 thread->next = g_thread_all_threads;
732 g_thread_all_threads = thread;
736 return (GThread*)thread;
740 g_static_rw_lock_init (GStaticRWLock* lock)
742 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
744 g_return_if_fail (lock);
750 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
753 *cond = g_cond_new ();
754 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
758 g_static_rw_lock_signal (GStaticRWLock* lock)
760 if (lock->want_to_write && lock->write_cond)
761 g_cond_signal (lock->write_cond);
762 else if (lock->want_to_read && lock->read_cond)
763 g_cond_broadcast (lock->read_cond);
767 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
769 g_return_if_fail (lock);
771 if (!g_threads_got_initialized)
774 g_static_mutex_lock (&lock->mutex);
775 lock->want_to_read++;
776 while (lock->have_writer || lock->want_to_write)
777 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
778 lock->want_to_read--;
779 lock->read_counter++;
780 g_static_mutex_unlock (&lock->mutex);
784 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
786 gboolean ret_val = FALSE;
788 g_return_val_if_fail (lock, FALSE);
790 if (!g_threads_got_initialized)
793 g_static_mutex_lock (&lock->mutex);
794 if (!lock->have_writer && !lock->want_to_write)
796 lock->read_counter++;
799 g_static_mutex_unlock (&lock->mutex);
804 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
806 g_return_if_fail (lock);
808 if (!g_threads_got_initialized)
811 g_static_mutex_lock (&lock->mutex);
812 lock->read_counter--;
813 if (lock->read_counter == 0)
814 g_static_rw_lock_signal (lock);
815 g_static_mutex_unlock (&lock->mutex);
819 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
821 g_return_if_fail (lock);
823 if (!g_threads_got_initialized)
826 g_static_mutex_lock (&lock->mutex);
827 lock->want_to_write++;
828 while (lock->have_writer || lock->read_counter)
829 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
830 lock->want_to_write--;
831 lock->have_writer = TRUE;
832 g_static_mutex_unlock (&lock->mutex);
836 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
838 gboolean ret_val = FALSE;
840 g_return_val_if_fail (lock, FALSE);
842 if (!g_threads_got_initialized)
845 g_static_mutex_lock (&lock->mutex);
846 if (!lock->have_writer && !lock->read_counter)
848 lock->have_writer = TRUE;
851 g_static_mutex_unlock (&lock->mutex);
856 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
858 g_return_if_fail (lock);
860 if (!g_threads_got_initialized)
863 g_static_mutex_lock (&lock->mutex);
864 lock->have_writer = FALSE;
865 g_static_rw_lock_signal (lock);
866 g_static_mutex_unlock (&lock->mutex);
870 g_static_rw_lock_free (GStaticRWLock* lock)
872 g_return_if_fail (lock);
876 g_cond_free (lock->read_cond);
877 lock->read_cond = NULL;
879 if (lock->write_cond)
881 g_cond_free (lock->write_cond);
882 lock->write_cond = NULL;
884 g_static_mutex_free (&lock->mutex);
889 * @thread_func: function to call for all GThread structures
890 * @user_data: second argument to @thread_func
892 * Call @thread_func on all existing #GThread structures. Note that
893 * threads may decide to exit while @thread_func is running, so
894 * without intimate knowledge about the lifetime of foreign threads,
895 * @thread_func shouldn't access the GThread* pointer passed in as
896 * first argument. However, @thread_func will not be called for threads
897 * which are known to have exited already.
899 * Due to thread lifetime checks, this function has an execution complexity
900 * which is quadratic in the number of existing threads.
905 g_thread_foreach (GFunc thread_func,
908 GSList *slist = NULL;
910 g_return_if_fail (thread_func != NULL);
911 /* snapshot the list of threads for iteration */
913 for (thread = g_thread_all_threads; thread; thread = thread->next)
914 slist = g_slist_prepend (slist, thread);
916 /* walk the list, skipping non-existant threads */
919 GSList *node = slist;
921 /* check whether the current thread still exists */
923 for (thread = g_thread_all_threads; thread; thread = thread->next)
924 if (thread == node->data)
928 thread_func (thread, user_data);
929 g_slist_free_1 (node);
933 #define __G_THREAD_C__
934 #include "galiasdef.c"