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/.
45 #include "gthreadinit.h"
47 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
48 # define g_system_thread_equal_simple(thread1, thread2) \
49 ((thread1).dummy_pointer == (thread2).dummy_pointer)
50 # define g_system_thread_assign(dest, src) \
51 ((dest).dummy_pointer = (src).dummy_pointer)
52 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
53 # define g_system_thread_equal_simple(thread1, thread2) \
54 (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
55 # define g_system_thread_assign(dest, src) \
56 (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
57 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
59 #define g_system_thread_equal(thread1, thread2) \
60 (g_thread_functions_for_glib_use.thread_equal ? \
61 g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
62 g_system_thread_equal_simple((thread1), (thread2)))
65 g_thread_error_quark (void)
69 quark = g_quark_from_static_string ("g_thread_error");
73 /* Keep this in sync with GRealThread in gmain.c! */
74 typedef struct _GRealThread GRealThread;
78 gpointer private_data;
80 GSystemThread system_thread;
83 typedef struct _GStaticPrivateNode GStaticPrivateNode;
84 struct _GStaticPrivateNode
87 GDestroyNotify destroy;
90 static void g_thread_cleanup (gpointer data);
91 static void g_thread_fail (void);
93 /* Global variables */
95 static GSystemThread zero_thread; /* This is initialized to all zero */
96 gboolean g_thread_use_default_impl = TRUE;
97 gboolean g_threads_got_initialized = FALSE;
99 GThreadFunctions g_thread_functions_for_glib_use = {
100 (GMutex*(*)())g_thread_fail, /* mutex_new */
101 NULL, /* mutex_lock */
102 NULL, /* mutex_trylock */
103 NULL, /* mutex_unlock */
104 NULL, /* mutex_free */
105 (GCond*(*)())g_thread_fail, /* cond_new */
106 NULL, /* cond_signal */
107 NULL, /* cond_broadcast */
108 NULL, /* cond_wait */
109 NULL, /* cond_timed_wait */
110 NULL, /* cond_free */
111 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
112 NULL, /* private_get */
113 NULL, /* private_set */
114 (void(*)(GThreadFunc, gpointer, gulong,
115 gboolean, gboolean, GThreadPriority,
116 gpointer, GError**))g_thread_fail, /* thread_create */
117 NULL, /* thread_yield */
118 NULL, /* thread_join */
119 NULL, /* thread_exit */
120 NULL, /* thread_set_priority */
121 NULL /* thread_self */
126 static GMutex *g_once_mutex = NULL;
127 static GCond *g_once_cond = NULL;
128 static GPrivate *g_thread_specific_private = NULL;
129 static GSList *g_thread_all_threads = NULL;
130 static GSList *g_thread_free_indeces = NULL;
132 G_LOCK_DEFINE_STATIC (g_thread);
134 #ifdef G_THREADS_ENABLED
135 /* This must be called only once, before any threads are created.
136 * It will only be called from g_thread_init() in -lgthread.
139 g_thread_init_glib (void)
141 /* We let the main thread (the one that calls g_thread_init) inherit
142 * the static_private data set before calling g_thread_init
144 GRealThread* main_thread = (GRealThread*) g_thread_self ();
146 g_once_mutex = g_mutex_new ();
147 g_once_cond = g_cond_new ();
149 _g_convert_thread_init ();
150 _g_rand_thread_init ();
151 _g_main_thread_init ();
152 _g_mem_thread_init ();
153 _g_messages_thread_init ();
154 _g_atomic_thread_init ();
156 _g_win32_thread_init ();
159 g_threads_got_initialized = TRUE;
161 g_thread_specific_private = g_private_new (g_thread_cleanup);
162 g_private_set (g_thread_specific_private, main_thread);
163 G_THREAD_UF (thread_self, (&main_thread->system_thread));
165 _g_mem_thread_private_init ();
166 _g_messages_thread_private_init ();
169 #endif /* G_THREADS_ENABLED */
172 g_once_impl (GOnce *once,
176 g_mutex_lock (g_once_mutex);
178 while (once->status == G_ONCE_STATUS_PROGRESS)
179 g_cond_wait (g_once_cond, g_once_mutex);
181 if (once->status != G_ONCE_STATUS_READY)
183 once->status = G_ONCE_STATUS_PROGRESS;
184 g_mutex_unlock (g_once_mutex);
186 once->retval = func (arg);
188 g_mutex_lock (g_once_mutex);
189 once->status = G_ONCE_STATUS_READY;
190 g_cond_broadcast (g_once_cond);
193 g_mutex_unlock (g_once_mutex);
199 g_static_mutex_init (GStaticMutex *mutex)
201 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
203 g_return_if_fail (mutex);
209 g_static_mutex_get_mutex_impl (GMutex** mutex)
211 if (!g_thread_supported ())
214 g_assert (g_once_mutex);
216 g_mutex_lock (g_once_mutex);
220 GMutex *new_mutex = g_mutex_new ();
222 /* The following is a memory barrier to avoid the write
223 * to *new_mutex being reordered to after writing *mutex */
224 g_mutex_lock (new_mutex);
225 g_mutex_unlock (new_mutex);
230 g_mutex_unlock (g_once_mutex);
236 g_static_mutex_free (GStaticMutex* mutex)
238 GMutex **runtime_mutex;
240 g_return_if_fail (mutex);
242 /* The runtime_mutex is the first (or only) member of GStaticMutex,
243 * see both versions (of glibconfig.h) in configure.in */
244 runtime_mutex = ((GMutex**)mutex);
247 g_mutex_free (*runtime_mutex);
249 *runtime_mutex = NULL;
253 g_static_rec_mutex_init (GStaticRecMutex *mutex)
255 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
257 g_return_if_fail (mutex);
263 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
267 g_return_if_fail (mutex);
269 if (!g_thread_supported ())
272 G_THREAD_UF (thread_self, (&self));
274 if (g_system_thread_equal (self, mutex->owner))
279 g_static_mutex_lock (&mutex->mutex);
280 g_system_thread_assign (mutex->owner, self);
285 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
289 g_return_val_if_fail (mutex, FALSE);
291 if (!g_thread_supported ())
294 G_THREAD_UF (thread_self, (&self));
296 if (g_system_thread_equal (self, mutex->owner))
302 if (!g_static_mutex_trylock (&mutex->mutex))
305 g_system_thread_assign (mutex->owner, self);
311 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
313 g_return_if_fail (mutex);
315 if (!g_thread_supported ())
318 if (mutex->depth > 1)
323 g_system_thread_assign (mutex->owner, zero_thread);
324 g_static_mutex_unlock (&mutex->mutex);
328 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
332 g_return_if_fail (mutex);
334 if (!g_thread_supported ())
337 G_THREAD_UF (thread_self, (&self));
339 if (g_system_thread_equal (self, mutex->owner))
341 mutex->depth += depth;
344 g_static_mutex_lock (&mutex->mutex);
345 g_system_thread_assign (mutex->owner, self);
346 mutex->depth = depth;
350 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
354 g_return_val_if_fail (mutex, 0);
356 if (!g_thread_supported ())
359 depth = mutex->depth;
361 g_system_thread_assign (mutex->owner, zero_thread);
363 g_static_mutex_unlock (&mutex->mutex);
369 g_static_rec_mutex_free (GStaticRecMutex *mutex)
371 g_return_if_fail (mutex);
373 g_static_mutex_free (&mutex->mutex);
377 g_static_private_init (GStaticPrivate *private_key)
379 private_key->index = 0;
383 g_static_private_get (GStaticPrivate *private_key)
385 GRealThread *self = (GRealThread*) g_thread_self ();
388 array = self->private_data;
392 if (!private_key->index)
394 else if (private_key->index <= array->len)
395 return g_array_index (array, GStaticPrivateNode,
396 private_key->index - 1).data;
402 g_static_private_set (GStaticPrivate *private_key,
404 GDestroyNotify notify)
406 GRealThread *self = (GRealThread*) g_thread_self ();
408 static guint next_index = 0;
409 GStaticPrivateNode *node;
411 array = self->private_data;
414 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
415 self->private_data = array;
418 if (!private_key->index)
422 if (!private_key->index)
424 if (g_thread_free_indeces)
427 GPOINTER_TO_UINT (g_thread_free_indeces->data);
428 g_thread_free_indeces =
429 g_slist_delete_link (g_thread_free_indeces,
430 g_thread_free_indeces);
433 private_key->index = ++next_index;
439 if (private_key->index > array->len)
440 g_array_set_size (array, private_key->index);
442 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
445 gpointer ddata = node->data;
446 GDestroyNotify ddestroy = node->destroy;
449 node->destroy = notify;
456 node->destroy = notify;
461 g_static_private_free (GStaticPrivate *private_key)
463 guint index = private_key->index;
469 private_key->index = 0;
472 list = g_thread_all_threads;
475 GRealThread *thread = list->data;
476 GArray *array = thread->private_data;
479 if (array && index <= array->len)
481 GStaticPrivateNode *node = &g_array_index (array,
484 gpointer ddata = node->data;
485 GDestroyNotify ddestroy = node->destroy;
488 node->destroy = NULL;
498 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
499 GUINT_TO_POINTER (index));
504 g_thread_cleanup (gpointer data)
508 GRealThread* thread = data;
509 if (thread->private_data)
511 GArray* array = thread->private_data;
514 for (i = 0; i < array->len; i++ )
516 GStaticPrivateNode *node =
517 &g_array_index (array, GStaticPrivateNode, i);
519 node->destroy (node->data);
521 g_array_free (array, TRUE);
524 /* We only free the thread structure, if it isn't joinable. If
525 it is, the structure is freed in g_thread_join */
526 if (!thread->thread.joinable)
529 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
532 /* Just to make sure, this isn't used any more */
533 g_system_thread_assign (thread->system_thread, zero_thread);
542 g_error ("The thread system is not yet initialized.");
546 g_thread_create_proxy (gpointer data)
548 GRealThread* thread = data;
552 /* This has to happen before G_LOCK, as that might call g_thread_self */
553 g_private_set (g_thread_specific_private, data);
555 /* the lock makes sure, that thread->system_thread is written,
556 before thread->thread.func is called. See g_thread_create. */
560 thread->retval = thread->thread.func (thread->thread.data);
566 g_thread_create_full (GThreadFunc func,
571 GThreadPriority priority,
575 GError *local_error = NULL;
576 g_return_val_if_fail (func, NULL);
577 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
578 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
580 result = g_new (GRealThread, 1);
582 result->thread.joinable = joinable;
583 result->thread.priority = priority;
584 result->thread.func = func;
585 result->thread.data = data;
586 result->private_data = NULL;
588 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
589 stack_size, joinable, bound, priority,
590 &result->system_thread, &local_error));
591 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
596 g_propagate_error (error, local_error);
601 return (GThread*) result;
605 g_thread_exit (gpointer retval)
607 GRealThread* real = (GRealThread*) g_thread_self ();
608 real->retval = retval;
609 G_THREAD_CF (thread_exit, (void)0, ());
613 g_thread_join (GThread* thread)
615 GRealThread* real = (GRealThread*) thread;
618 g_return_val_if_fail (thread, NULL);
619 g_return_val_if_fail (thread->joinable, NULL);
620 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
623 G_THREAD_UF (thread_join, (&real->system_thread));
625 retval = real->retval;
628 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
631 /* Just to make sure, this isn't used any more */
632 thread->joinable = 0;
633 g_system_thread_assign (real->system_thread, zero_thread);
635 /* the thread structure for non-joinable threads is freed upon
636 thread end. We free the memory here. This will leave a loose end,
637 if a joinable thread is not joined. */
645 g_thread_set_priority (GThread* thread,
646 GThreadPriority priority)
648 GRealThread* real = (GRealThread*) thread;
650 g_return_if_fail (thread);
651 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
652 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
653 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
655 thread->priority = priority;
657 G_THREAD_CF (thread_set_priority, (void)0,
658 (&real->system_thread, priority));
664 GRealThread* thread = g_private_get (g_thread_specific_private);
668 /* If no thread data is available, provide and set one. This
669 can happen for the main thread and for threads, that are not
671 thread = g_new (GRealThread, 1);
672 thread->thread.joinable = FALSE; /* This is a save guess */
673 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
675 thread->thread.func = NULL;
676 thread->thread.data = NULL;
677 thread->private_data = NULL;
679 if (g_thread_supported ())
680 G_THREAD_UF (thread_self, (&thread->system_thread));
682 g_private_set (g_thread_specific_private, thread);
685 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
689 return (GThread*)thread;
693 g_static_rw_lock_init (GStaticRWLock* lock)
695 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
697 g_return_if_fail (lock);
703 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
706 *cond = g_cond_new ();
707 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
711 g_static_rw_lock_signal (GStaticRWLock* lock)
713 if (lock->want_to_write && lock->write_cond)
714 g_cond_signal (lock->write_cond);
715 else if (lock->want_to_read && lock->read_cond)
716 g_cond_broadcast (lock->read_cond);
720 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
722 g_return_if_fail (lock);
724 if (!g_threads_got_initialized)
727 g_static_mutex_lock (&lock->mutex);
728 lock->want_to_read++;
729 while (lock->have_writer || lock->want_to_write)
730 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
731 lock->want_to_read--;
732 lock->read_counter++;
733 g_static_mutex_unlock (&lock->mutex);
737 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
739 gboolean ret_val = FALSE;
741 g_return_val_if_fail (lock, FALSE);
743 if (!g_threads_got_initialized)
746 g_static_mutex_lock (&lock->mutex);
747 if (!lock->have_writer && !lock->want_to_write)
749 lock->read_counter++;
752 g_static_mutex_unlock (&lock->mutex);
757 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
759 g_return_if_fail (lock);
761 if (!g_threads_got_initialized)
764 g_static_mutex_lock (&lock->mutex);
765 lock->read_counter--;
766 if (lock->read_counter == 0)
767 g_static_rw_lock_signal (lock);
768 g_static_mutex_unlock (&lock->mutex);
772 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
774 g_return_if_fail (lock);
776 if (!g_threads_got_initialized)
779 g_static_mutex_lock (&lock->mutex);
780 lock->want_to_write++;
781 while (lock->have_writer || lock->read_counter)
782 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
783 lock->want_to_write--;
784 lock->have_writer = TRUE;
785 g_static_mutex_unlock (&lock->mutex);
789 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
791 gboolean ret_val = FALSE;
793 g_return_val_if_fail (lock, FALSE);
795 if (!g_threads_got_initialized)
798 g_static_mutex_lock (&lock->mutex);
799 if (!lock->have_writer && !lock->read_counter)
801 lock->have_writer = TRUE;
804 g_static_mutex_unlock (&lock->mutex);
809 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
811 g_return_if_fail (lock);
813 if (!g_threads_got_initialized)
816 g_static_mutex_lock (&lock->mutex);
817 lock->have_writer = FALSE;
818 g_static_rw_lock_signal (lock);
819 g_static_mutex_unlock (&lock->mutex);
823 g_static_rw_lock_free (GStaticRWLock* lock)
825 g_return_if_fail (lock);
829 g_cond_free (lock->read_cond);
830 lock->read_cond = NULL;
832 if (lock->write_cond)
834 g_cond_free (lock->write_cond);
835 lock->write_cond = NULL;
837 g_static_mutex_free (&lock->mutex);