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 "gthreadinit.h"
46 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
47 # define g_system_thread_equal_simple(thread1, thread2) \
48 ((thread1).dummy_pointer == (thread2).dummy_pointer)
49 # define g_system_thread_assign(dest, src) \
50 ((dest).dummy_pointer = (src).dummy_pointer)
51 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
52 # define g_system_thread_equal_simple(thread1, thread2) \
53 (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
54 # define g_system_thread_assign(dest, src) \
55 (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
56 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
58 #define g_system_thread_equal(thread1, thread2) \
59 (g_thread_functions_for_glib_use.thread_equal ? \
60 g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
61 g_system_thread_equal_simple((thread1), (thread2)))
64 g_thread_error_quark (void)
68 quark = g_quark_from_static_string ("g_thread_error");
72 /* Keep this in sync with GRealThread in gmain.c! */
73 typedef struct _GRealThread GRealThread;
77 gpointer private_data;
79 GSystemThread system_thread;
82 typedef struct _GStaticPrivateNode GStaticPrivateNode;
83 struct _GStaticPrivateNode
86 GDestroyNotify destroy;
89 static void g_thread_cleanup (gpointer data);
90 static void g_thread_fail (void);
92 /* Global variables */
94 static GSystemThread zero_thread; /* This is initialized to all zero */
95 gboolean g_thread_use_default_impl = TRUE;
96 gboolean g_threads_got_initialized = FALSE;
98 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
101 GThreadFunctions g_thread_functions_for_glib_use = {
102 (GMutex*(*)())g_thread_fail, /* mutex_new */
103 NULL, /* mutex_lock */
104 NULL, /* mutex_trylock */
105 NULL, /* mutex_unlock */
106 NULL, /* mutex_free */
107 (GCond*(*)())g_thread_fail, /* cond_new */
108 NULL, /* cond_signal */
109 NULL, /* cond_broadcast */
110 NULL, /* cond_wait */
111 NULL, /* cond_timed_wait */
112 NULL, /* cond_free */
113 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
114 NULL, /* private_get */
115 NULL, /* private_set */
116 (void(*)(GThreadFunc, gpointer, gulong,
117 gboolean, gboolean, GThreadPriority,
118 gpointer, GError**))g_thread_fail, /* thread_create */
119 NULL, /* thread_yield */
120 NULL, /* thread_join */
121 NULL, /* thread_exit */
122 NULL, /* thread_set_priority */
123 NULL /* thread_self */
128 static GMutex *g_once_mutex = NULL;
129 static GCond *g_once_cond = NULL;
130 static GPrivate *g_thread_specific_private = NULL;
131 static GSList *g_thread_all_threads = NULL;
132 static GSList *g_thread_free_indeces = NULL;
134 G_LOCK_DEFINE_STATIC (g_thread);
136 #ifdef G_THREADS_ENABLED
137 /* This must be called only once, before any threads are created.
138 * It will only be called from g_thread_init() in -lgthread.
141 g_thread_init_glib (void)
143 /* We let the main thread (the one that calls g_thread_init) inherit
144 * the static_private data set before calling g_thread_init
146 GRealThread* main_thread = (GRealThread*) g_thread_self ();
148 g_once_mutex = g_mutex_new ();
149 g_once_cond = g_cond_new ();
151 _g_convert_thread_init ();
152 _g_rand_thread_init ();
153 _g_main_thread_init ();
154 _g_mem_thread_init ();
155 _g_messages_thread_init ();
157 g_threads_got_initialized = TRUE;
159 g_thread_specific_private = g_private_new (g_thread_cleanup);
160 g_private_set (g_thread_specific_private, main_thread);
161 G_THREAD_UF (thread_self, (&main_thread->system_thread));
163 _g_mem_thread_private_init ();
164 _g_messages_thread_private_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 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);
218 GMutex *new_mutex = g_mutex_new ();
220 /* The following is a memory barrier to avoid the write
221 * to *new_mutex being reordered to after writing *mutex */
222 g_mutex_lock (new_mutex);
223 g_mutex_unlock (new_mutex);
228 g_mutex_unlock (g_once_mutex);
234 g_static_mutex_free (GStaticMutex* mutex)
236 GMutex **runtime_mutex;
238 g_return_if_fail (mutex);
240 /* The runtime_mutex is the first (or only) member of GStaticMutex,
241 * see both versions (of glibconfig.h) in configure.in */
242 runtime_mutex = ((GMutex**)mutex);
245 g_mutex_free (*runtime_mutex);
247 *runtime_mutex = NULL;
251 g_static_rec_mutex_init (GStaticRecMutex *mutex)
253 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
255 g_return_if_fail (mutex);
261 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
265 g_return_if_fail (mutex);
267 if (!g_thread_supported ())
270 G_THREAD_UF (thread_self, (&self));
272 if (g_system_thread_equal (self, mutex->owner))
277 g_static_mutex_lock (&mutex->mutex);
278 g_system_thread_assign (mutex->owner, self);
283 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
287 g_return_val_if_fail (mutex, FALSE);
289 if (!g_thread_supported ())
292 G_THREAD_UF (thread_self, (&self));
294 if (g_system_thread_equal (self, mutex->owner))
300 if (!g_static_mutex_trylock (&mutex->mutex))
303 g_system_thread_assign (mutex->owner, self);
309 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
311 g_return_if_fail (mutex);
313 if (!g_thread_supported ())
316 if (mutex->depth > 1)
321 g_system_thread_assign (mutex->owner, zero_thread);
322 g_static_mutex_unlock (&mutex->mutex);
326 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
330 g_return_if_fail (mutex);
332 if (!g_thread_supported ())
335 G_THREAD_UF (thread_self, (&self));
337 if (g_system_thread_equal (self, mutex->owner))
339 mutex->depth += depth;
342 g_static_mutex_lock (&mutex->mutex);
343 g_system_thread_assign (mutex->owner, self);
344 mutex->depth = depth;
348 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
352 g_return_val_if_fail (mutex, 0);
354 if (!g_thread_supported ())
357 depth = mutex->depth;
359 g_system_thread_assign (mutex->owner, zero_thread);
361 g_static_mutex_unlock (&mutex->mutex);
367 g_static_rec_mutex_free (GStaticRecMutex *mutex)
369 g_return_if_fail (mutex);
371 g_static_mutex_free (&mutex->mutex);
375 g_static_private_init (GStaticPrivate *private_key)
377 private_key->index = 0;
381 g_static_private_get (GStaticPrivate *private_key)
383 GRealThread *self = (GRealThread*) g_thread_self ();
386 array = self->private_data;
390 if (!private_key->index)
392 else if (private_key->index <= array->len)
393 return g_array_index (array, GStaticPrivateNode,
394 private_key->index - 1).data;
400 g_static_private_set (GStaticPrivate *private_key,
402 GDestroyNotify notify)
404 GRealThread *self = (GRealThread*) g_thread_self ();
406 static guint next_index = 0;
407 GStaticPrivateNode *node;
409 array = self->private_data;
412 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
413 self->private_data = array;
416 if (!private_key->index)
420 if (!private_key->index)
422 if (g_thread_free_indeces)
425 GPOINTER_TO_UINT (g_thread_free_indeces->data);
426 g_thread_free_indeces =
427 g_slist_delete_link (g_thread_free_indeces,
428 g_thread_free_indeces);
431 private_key->index = ++next_index;
437 if (private_key->index > array->len)
438 g_array_set_size (array, private_key->index);
440 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
443 gpointer ddata = node->data;
444 GDestroyNotify ddestroy = node->destroy;
447 node->destroy = notify;
454 node->destroy = notify;
459 g_static_private_free (GStaticPrivate *private_key)
461 guint index = private_key->index;
467 private_key->index = 0;
470 list = g_thread_all_threads;
473 GRealThread *thread = list->data;
474 GArray *array = thread->private_data;
477 if (array && index <= array->len)
479 GStaticPrivateNode *node = &g_array_index (array,
482 gpointer ddata = node->data;
483 GDestroyNotify ddestroy = node->destroy;
486 node->destroy = NULL;
496 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
497 GUINT_TO_POINTER (index));
502 g_thread_cleanup (gpointer data)
506 GRealThread* thread = data;
507 if (thread->private_data)
509 GArray* array = thread->private_data;
512 for (i = 0; i < array->len; i++ )
514 GStaticPrivateNode *node =
515 &g_array_index (array, GStaticPrivateNode, i);
517 node->destroy (node->data);
519 g_array_free (array, TRUE);
522 /* We only free the thread structure, if it isn't joinable. If
523 it is, the structure is freed in g_thread_join */
524 if (!thread->thread.joinable)
527 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
530 /* Just to make sure, this isn't used any more */
531 g_system_thread_assign (thread->system_thread, zero_thread);
540 g_error ("The thread system is not yet initialized.");
544 g_thread_create_proxy (gpointer data)
546 GRealThread* thread = data;
550 /* This has to happen before G_LOCK, as that might call g_thread_self */
551 g_private_set (g_thread_specific_private, data);
553 /* the lock makes sure, that thread->system_thread is written,
554 before thread->thread.func is called. See g_thread_create. */
558 thread->retval = thread->thread.func (thread->thread.data);
564 g_thread_create_full (GThreadFunc func,
569 GThreadPriority priority,
573 GError *local_error = NULL;
574 g_return_val_if_fail (func, NULL);
575 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
576 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
578 result = g_new (GRealThread, 1);
580 result->thread.joinable = joinable;
581 result->thread.priority = priority;
582 result->thread.func = func;
583 result->thread.data = data;
584 result->private_data = NULL;
586 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
587 stack_size, joinable, bound, priority,
588 &result->system_thread, &local_error));
589 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
594 g_propagate_error (error, local_error);
599 return (GThread*) result;
603 g_thread_exit (gpointer retval)
605 GRealThread* real = (GRealThread*) g_thread_self ();
606 real->retval = retval;
607 G_THREAD_CF (thread_exit, (void)0, ());
611 g_thread_join (GThread* thread)
613 GRealThread* real = (GRealThread*) thread;
616 g_return_val_if_fail (thread, NULL);
617 g_return_val_if_fail (thread->joinable, NULL);
618 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
621 G_THREAD_UF (thread_join, (&real->system_thread));
623 retval = real->retval;
626 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
629 /* Just to make sure, this isn't used any more */
630 thread->joinable = 0;
631 g_system_thread_assign (real->system_thread, zero_thread);
633 /* the thread structure for non-joinable threads is freed upon
634 thread end. We free the memory here. This will leave a loose end,
635 if a joinable thread is not joined. */
643 g_thread_set_priority (GThread* thread,
644 GThreadPriority priority)
646 GRealThread* real = (GRealThread*) thread;
648 g_return_if_fail (thread);
649 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
650 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
651 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
653 thread->priority = priority;
655 G_THREAD_CF (thread_set_priority, (void)0,
656 (&real->system_thread, priority));
662 GRealThread* thread = g_private_get (g_thread_specific_private);
666 /* If no thread data is available, provide and set one. This
667 can happen for the main thread and for threads, that are not
669 thread = g_new (GRealThread, 1);
670 thread->thread.joinable = FALSE; /* This is a save guess */
671 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
673 thread->thread.func = NULL;
674 thread->thread.data = NULL;
675 thread->private_data = NULL;
677 if (g_thread_supported ())
678 G_THREAD_UF (thread_self, (&thread->system_thread));
680 g_private_set (g_thread_specific_private, thread);
683 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
687 return (GThread*)thread;
691 g_static_rw_lock_init (GStaticRWLock* lock)
693 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
695 g_return_if_fail (lock);
701 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
704 *cond = g_cond_new ();
705 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
709 g_static_rw_lock_signal (GStaticRWLock* lock)
711 if (lock->want_to_write && lock->write_cond)
712 g_cond_signal (lock->write_cond);
713 else if (lock->want_to_read && lock->read_cond)
714 g_cond_broadcast (lock->read_cond);
718 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
720 g_return_if_fail (lock);
722 if (!g_threads_got_initialized)
725 g_static_mutex_lock (&lock->mutex);
726 lock->want_to_read++;
727 while (lock->have_writer || lock->want_to_write)
728 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
729 lock->want_to_read--;
730 lock->read_counter++;
731 g_static_mutex_unlock (&lock->mutex);
735 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
737 gboolean ret_val = FALSE;
739 g_return_val_if_fail (lock, FALSE);
741 if (!g_threads_got_initialized)
744 g_static_mutex_lock (&lock->mutex);
745 if (!lock->have_writer && !lock->want_to_write)
747 lock->read_counter++;
750 g_static_mutex_unlock (&lock->mutex);
755 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
757 g_return_if_fail (lock);
759 if (!g_threads_got_initialized)
762 g_static_mutex_lock (&lock->mutex);
763 lock->read_counter--;
764 if (lock->read_counter == 0)
765 g_static_rw_lock_signal (lock);
766 g_static_mutex_unlock (&lock->mutex);
770 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
772 g_return_if_fail (lock);
774 if (!g_threads_got_initialized)
777 g_static_mutex_lock (&lock->mutex);
778 lock->want_to_write++;
779 while (lock->have_writer || lock->read_counter)
780 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
781 lock->want_to_write--;
782 lock->have_writer = TRUE;
783 g_static_mutex_unlock (&lock->mutex);
787 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
789 gboolean ret_val = FALSE;
791 g_return_val_if_fail (lock, FALSE);
793 if (!g_threads_got_initialized)
796 g_static_mutex_lock (&lock->mutex);
797 if (!lock->have_writer && !lock->read_counter)
799 lock->have_writer = TRUE;
802 g_static_mutex_unlock (&lock->mutex);
807 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
809 g_return_if_fail (lock);
811 if (!g_threads_got_initialized)
814 g_static_mutex_lock (&lock->mutex);
815 lock->have_writer = FALSE;
816 g_static_rw_lock_signal (lock);
817 g_static_mutex_unlock (&lock->mutex);
821 g_static_rw_lock_free (GStaticRWLock* lock)
823 g_return_if_fail (lock);
827 g_cond_free (lock->read_cond);
828 lock->read_cond = NULL;
830 if (lock->write_cond)
832 g_cond_free (lock->write_cond);
833 lock->write_cond = NULL;
835 g_static_mutex_free (&lock->mutex);