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 #include "gthreadprivate.h"
48 g_thread_error_quark (void)
50 return g_quark_from_static_string ("g_thread_error");
53 /* Keep this in sync with GRealThread in gmain.c! */
54 typedef struct _GRealThread GRealThread;
58 gpointer private_data;
61 GSystemThread system_thread;
64 typedef struct _GStaticPrivateNode GStaticPrivateNode;
65 struct _GStaticPrivateNode
68 GDestroyNotify destroy;
71 static void g_thread_cleanup (gpointer data);
72 static void g_thread_fail (void);
74 /* Global variables */
76 static GSystemThread zero_thread; /* This is initialized to all zero */
77 gboolean g_thread_use_default_impl = TRUE;
78 gboolean g_threads_got_initialized = FALSE;
80 GThreadFunctions g_thread_functions_for_glib_use = {
81 (GMutex*(*)())g_thread_fail, /* mutex_new */
82 NULL, /* mutex_lock */
83 NULL, /* mutex_trylock */
84 NULL, /* mutex_unlock */
85 NULL, /* mutex_free */
86 (GCond*(*)())g_thread_fail, /* cond_new */
87 NULL, /* cond_signal */
88 NULL, /* cond_broadcast */
90 NULL, /* cond_timed_wait */
92 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
93 NULL, /* private_get */
94 NULL, /* private_set */
95 (void(*)(GThreadFunc, gpointer, gulong,
96 gboolean, gboolean, GThreadPriority,
97 gpointer, GError**))g_thread_fail, /* thread_create */
98 NULL, /* thread_yield */
99 NULL, /* thread_join */
100 NULL, /* thread_exit */
101 NULL, /* thread_set_priority */
102 NULL /* thread_self */
107 static GMutex *g_once_mutex = NULL;
108 static GCond *g_once_cond = NULL;
109 static GPrivate *g_thread_specific_private = NULL;
110 static GRealThread *g_thread_all_threads = NULL;
111 static GSList *g_thread_free_indeces = NULL;
113 G_LOCK_DEFINE_STATIC (g_thread);
115 #ifdef G_THREADS_ENABLED
116 /* This must be called only once, before any threads are created.
117 * It will only be called from g_thread_init() in -lgthread.
120 g_thread_init_glib (void)
122 /* We let the main thread (the one that calls g_thread_init) inherit
123 * the static_private data set before calling g_thread_init
125 GRealThread* main_thread = (GRealThread*) g_thread_self ();
127 /* mutex and cond creation works without g_threads_got_initialized */
128 g_once_mutex = g_mutex_new ();
129 g_once_cond = g_cond_new ();
131 /* we may only create mutex and cond in here */
132 _g_mem_thread_init_noprivate_nomessage ();
134 /* setup the basic threading system */
135 g_threads_got_initialized = TRUE;
136 g_thread_specific_private = g_private_new (g_thread_cleanup);
137 g_private_set (g_thread_specific_private, main_thread);
138 G_THREAD_UF (thread_self, (&main_thread->system_thread));
140 /* complete memory system initialization, g_private_*() works now */
141 _g_slice_thread_init_nomessage ();
143 /* accomplish log system initialization to enable messaging */
144 _g_messages_thread_init_nomessage ();
146 /* we may run full-fledged initializers from here */
147 _g_convert_thread_init ();
148 _g_rand_thread_init ();
149 _g_main_thread_init ();
150 _g_atomic_thread_init ();
151 _g_utils_thread_init ();
153 _g_win32_thread_init ();
156 #endif /* G_THREADS_ENABLED */
159 g_once_impl (GOnce *once,
163 g_mutex_lock (g_once_mutex);
165 while (once->status == G_ONCE_STATUS_PROGRESS)
166 g_cond_wait (g_once_cond, g_once_mutex);
168 if (once->status != G_ONCE_STATUS_READY)
170 once->status = G_ONCE_STATUS_PROGRESS;
171 g_mutex_unlock (g_once_mutex);
173 once->retval = func (arg);
175 g_mutex_lock (g_once_mutex);
176 once->status = G_ONCE_STATUS_READY;
177 g_cond_broadcast (g_once_cond);
180 g_mutex_unlock (g_once_mutex);
186 g_static_mutex_init (GStaticMutex *mutex)
188 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
190 g_return_if_fail (mutex);
196 g_static_mutex_get_mutex_impl (GMutex** mutex)
198 if (!g_thread_supported ())
201 g_assert (g_once_mutex);
203 g_mutex_lock (g_once_mutex);
207 GMutex *new_mutex = g_mutex_new ();
209 /* The following is a memory barrier to avoid the write
210 * to *new_mutex being reordered to after writing *mutex */
211 g_mutex_lock (new_mutex);
212 g_mutex_unlock (new_mutex);
217 g_mutex_unlock (g_once_mutex);
223 g_static_mutex_free (GStaticMutex* mutex)
225 GMutex **runtime_mutex;
227 g_return_if_fail (mutex);
229 /* The runtime_mutex is the first (or only) member of GStaticMutex,
230 * see both versions (of glibconfig.h) in configure.in */
231 runtime_mutex = ((GMutex**)mutex);
234 g_mutex_free (*runtime_mutex);
236 *runtime_mutex = NULL;
240 g_static_rec_mutex_init (GStaticRecMutex *mutex)
242 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
244 g_return_if_fail (mutex);
250 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
254 g_return_if_fail (mutex);
256 if (!g_thread_supported ())
259 G_THREAD_UF (thread_self, (&self));
261 if (g_system_thread_equal (self, mutex->owner))
266 g_static_mutex_lock (&mutex->mutex);
267 g_system_thread_assign (mutex->owner, self);
272 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
276 g_return_val_if_fail (mutex, FALSE);
278 if (!g_thread_supported ())
281 G_THREAD_UF (thread_self, (&self));
283 if (g_system_thread_equal (self, mutex->owner))
289 if (!g_static_mutex_trylock (&mutex->mutex))
292 g_system_thread_assign (mutex->owner, self);
298 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
300 g_return_if_fail (mutex);
302 if (!g_thread_supported ())
305 if (mutex->depth > 1)
310 g_system_thread_assign (mutex->owner, zero_thread);
311 g_static_mutex_unlock (&mutex->mutex);
315 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
319 g_return_if_fail (mutex);
321 if (!g_thread_supported ())
327 G_THREAD_UF (thread_self, (&self));
329 if (g_system_thread_equal (self, mutex->owner))
331 mutex->depth += depth;
334 g_static_mutex_lock (&mutex->mutex);
335 g_system_thread_assign (mutex->owner, self);
336 mutex->depth = depth;
340 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
344 g_return_val_if_fail (mutex, 0);
346 if (!g_thread_supported ())
349 depth = mutex->depth;
351 g_system_thread_assign (mutex->owner, zero_thread);
353 g_static_mutex_unlock (&mutex->mutex);
359 g_static_rec_mutex_free (GStaticRecMutex *mutex)
361 g_return_if_fail (mutex);
363 g_static_mutex_free (&mutex->mutex);
367 g_static_private_init (GStaticPrivate *private_key)
369 private_key->index = 0;
373 g_static_private_get (GStaticPrivate *private_key)
375 GRealThread *self = (GRealThread*) g_thread_self ();
378 array = self->private_data;
382 if (!private_key->index)
384 else if (private_key->index <= array->len)
385 return g_array_index (array, GStaticPrivateNode,
386 private_key->index - 1).data;
392 g_static_private_set (GStaticPrivate *private_key,
394 GDestroyNotify notify)
396 GRealThread *self = (GRealThread*) g_thread_self ();
398 static guint next_index = 0;
399 GStaticPrivateNode *node;
401 array = self->private_data;
404 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
405 self->private_data = array;
408 if (!private_key->index)
412 if (!private_key->index)
414 if (g_thread_free_indeces)
417 GPOINTER_TO_UINT (g_thread_free_indeces->data);
418 g_thread_free_indeces =
419 g_slist_delete_link (g_thread_free_indeces,
420 g_thread_free_indeces);
423 private_key->index = ++next_index;
429 if (private_key->index > array->len)
430 g_array_set_size (array, private_key->index);
432 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
435 gpointer ddata = node->data;
436 GDestroyNotify ddestroy = node->destroy;
439 node->destroy = notify;
446 node->destroy = notify;
451 g_static_private_free (GStaticPrivate *private_key)
453 guint index = private_key->index;
459 private_key->index = 0;
463 thread = g_thread_all_threads;
466 GArray *array = thread->private_data;
467 thread = thread->next;
469 if (array && index <= array->len)
471 GStaticPrivateNode *node = &g_array_index (array,
474 gpointer ddata = node->data;
475 GDestroyNotify ddestroy = node->destroy;
478 node->destroy = NULL;
488 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
489 GUINT_TO_POINTER (index));
494 g_thread_cleanup (gpointer data)
498 GRealThread* thread = data;
499 if (thread->private_data)
501 GArray* array = thread->private_data;
504 for (i = 0; i < array->len; i++ )
506 GStaticPrivateNode *node =
507 &g_array_index (array, GStaticPrivateNode, i);
509 node->destroy (node->data);
511 g_array_free (array, TRUE);
514 /* We only free the thread structure, if it isn't joinable. If
515 it is, the structure is freed in g_thread_join */
516 if (!thread->thread.joinable)
521 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
528 g_thread_all_threads = t->next;
534 /* Just to make sure, this isn't used any more */
535 g_system_thread_assign (thread->system_thread, zero_thread);
544 g_error ("The thread system is not yet initialized.");
548 g_thread_create_proxy (gpointer data)
550 GRealThread* thread = data;
554 /* This has to happen before G_LOCK, as that might call g_thread_self */
555 g_private_set (g_thread_specific_private, data);
557 /* the lock makes sure, that thread->system_thread is written,
558 before thread->thread.func is called. See g_thread_create. */
562 thread->retval = thread->thread.func (thread->thread.data);
568 g_thread_create_full (GThreadFunc func,
573 GThreadPriority priority,
577 GError *local_error = NULL;
578 g_return_val_if_fail (func, NULL);
579 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
580 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
582 result = g_new0 (GRealThread, 1);
584 result->thread.joinable = joinable;
585 result->thread.priority = priority;
586 result->thread.func = func;
587 result->thread.data = data;
588 result->private_data = NULL;
590 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
591 stack_size, joinable, bound, priority,
592 &result->system_thread, &local_error));
593 result->next = g_thread_all_threads;
594 g_thread_all_threads = result;
599 g_propagate_error (error, local_error);
604 return (GThread*) result;
608 g_thread_exit (gpointer retval)
610 GRealThread* real = (GRealThread*) g_thread_self ();
611 real->retval = retval;
612 G_THREAD_CF (thread_exit, (void)0, ());
616 g_thread_join (GThread* thread)
618 GRealThread* real = (GRealThread*) thread;
622 g_return_val_if_fail (thread, NULL);
623 g_return_val_if_fail (thread->joinable, NULL);
624 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
627 G_THREAD_UF (thread_join, (&real->system_thread));
629 retval = real->retval;
632 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
634 if (t == (GRealThread*) thread)
639 g_thread_all_threads = t->next;
645 /* Just to make sure, this isn't used any more */
646 thread->joinable = 0;
647 g_system_thread_assign (real->system_thread, zero_thread);
649 /* the thread structure for non-joinable threads is freed upon
650 thread end. We free the memory here. This will leave a loose end,
651 if a joinable thread is not joined. */
659 g_thread_set_priority (GThread* thread,
660 GThreadPriority priority)
662 GRealThread* real = (GRealThread*) thread;
664 g_return_if_fail (thread);
665 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
666 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
667 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
669 thread->priority = priority;
671 G_THREAD_CF (thread_set_priority, (void)0,
672 (&real->system_thread, priority));
678 GRealThread* thread = g_private_get (g_thread_specific_private);
682 /* If no thread data is available, provide and set one. This
683 can happen for the main thread and for threads, that are not
685 thread = g_new0 (GRealThread, 1);
686 thread->thread.joinable = FALSE; /* This is a save guess */
687 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
689 thread->thread.func = NULL;
690 thread->thread.data = NULL;
691 thread->private_data = NULL;
693 if (g_thread_supported ())
694 G_THREAD_UF (thread_self, (&thread->system_thread));
696 g_private_set (g_thread_specific_private, thread);
699 thread->next = g_thread_all_threads;
700 g_thread_all_threads = thread;
704 return (GThread*)thread;
708 g_static_rw_lock_init (GStaticRWLock* lock)
710 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
712 g_return_if_fail (lock);
718 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
721 *cond = g_cond_new ();
722 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
726 g_static_rw_lock_signal (GStaticRWLock* lock)
728 if (lock->want_to_write && lock->write_cond)
729 g_cond_signal (lock->write_cond);
730 else if (lock->want_to_read && lock->read_cond)
731 g_cond_broadcast (lock->read_cond);
735 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
737 g_return_if_fail (lock);
739 if (!g_threads_got_initialized)
742 g_static_mutex_lock (&lock->mutex);
743 lock->want_to_read++;
744 while (lock->have_writer || lock->want_to_write)
745 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
746 lock->want_to_read--;
747 lock->read_counter++;
748 g_static_mutex_unlock (&lock->mutex);
752 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
754 gboolean ret_val = FALSE;
756 g_return_val_if_fail (lock, FALSE);
758 if (!g_threads_got_initialized)
761 g_static_mutex_lock (&lock->mutex);
762 if (!lock->have_writer && !lock->want_to_write)
764 lock->read_counter++;
767 g_static_mutex_unlock (&lock->mutex);
772 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
774 g_return_if_fail (lock);
776 if (!g_threads_got_initialized)
779 g_static_mutex_lock (&lock->mutex);
780 lock->read_counter--;
781 if (lock->read_counter == 0)
782 g_static_rw_lock_signal (lock);
783 g_static_mutex_unlock (&lock->mutex);
787 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
789 g_return_if_fail (lock);
791 if (!g_threads_got_initialized)
794 g_static_mutex_lock (&lock->mutex);
795 lock->want_to_write++;
796 while (lock->have_writer || lock->read_counter)
797 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
798 lock->want_to_write--;
799 lock->have_writer = TRUE;
800 g_static_mutex_unlock (&lock->mutex);
804 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
806 gboolean ret_val = FALSE;
808 g_return_val_if_fail (lock, FALSE);
810 if (!g_threads_got_initialized)
813 g_static_mutex_lock (&lock->mutex);
814 if (!lock->have_writer && !lock->read_counter)
816 lock->have_writer = TRUE;
819 g_static_mutex_unlock (&lock->mutex);
824 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
826 g_return_if_fail (lock);
828 if (!g_threads_got_initialized)
831 g_static_mutex_lock (&lock->mutex);
832 lock->have_writer = FALSE;
833 g_static_rw_lock_signal (lock);
834 g_static_mutex_unlock (&lock->mutex);
838 g_static_rw_lock_free (GStaticRWLock* lock)
840 g_return_if_fail (lock);
844 g_cond_free (lock->read_cond);
845 lock->read_cond = NULL;
847 if (lock->write_cond)
849 g_cond_free (lock->write_cond);
850 lock->write_cond = NULL;
852 g_static_mutex_free (&lock->mutex);
857 * @thread_func: function to call for all GThread structures
858 * @user_data: second argument to @thread_func
860 * Call @thread_func on all existing #GThread structures. Note that
861 * threads may decide to exit while @thread_func is running, so
862 * without intimate knowledge about the lifetime of foreign threads,
863 * @thread_func shouldn't access the GThread* pointer passed in as
864 * first argument. However, @thread_func will not be called for threads
865 * which are known to have exited already.
867 * Due to thread lifetime checks, this function has an execution complexity
868 * which is quadratic in the number of existing threads.
873 g_thread_foreach (GFunc thread_func,
876 GSList *slist = NULL;
878 g_return_if_fail (thread_func != NULL);
879 /* snapshot the list of threads for iteration */
881 for (thread = g_thread_all_threads; thread; thread = thread->next)
882 slist = g_slist_prepend (slist, thread);
884 /* walk the list, skipping non-existant threads */
887 GSList *node = slist;
889 /* check whether the current thread still exists */
891 for (thread = g_thread_all_threads; thread; thread = thread->next)
892 if (thread == node->data)
896 thread_func (thread, user_data);
897 g_slist_free_1 (node);
901 #define __G_THREAD_C__
902 #include "galiasdef.c"