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 GThreadFunctions g_thread_functions_for_glib_use = {
99 (GMutex*(*)())g_thread_fail, /* mutex_new */
100 NULL, /* mutex_lock */
101 NULL, /* mutex_trylock */
102 NULL, /* mutex_unlock */
103 NULL, /* mutex_free */
104 (GCond*(*)())g_thread_fail, /* cond_new */
105 NULL, /* cond_signal */
106 NULL, /* cond_broadcast */
107 NULL, /* cond_wait */
108 NULL, /* cond_timed_wait */
109 NULL, /* cond_free */
110 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
111 NULL, /* private_get */
112 NULL, /* private_set */
113 (void(*)(GThreadFunc, gpointer, gulong,
114 gboolean, gboolean, GThreadPriority,
115 gpointer, GError**))g_thread_fail, /* thread_create */
116 NULL, /* thread_yield */
117 NULL, /* thread_join */
118 NULL, /* thread_exit */
119 NULL, /* thread_set_priority */
120 NULL /* thread_self */
125 static GMutex *g_once_mutex = NULL;
126 static GCond *g_once_cond = NULL;
127 static GPrivate *g_thread_specific_private = NULL;
128 static GSList *g_thread_all_threads = NULL;
129 static GSList *g_thread_free_indeces = NULL;
131 G_LOCK_DEFINE_STATIC (g_thread);
133 #ifdef G_THREADS_ENABLED
134 /* This must be called only once, before any threads are created.
135 * It will only be called from g_thread_init() in -lgthread.
138 g_thread_init_glib (void)
140 /* We let the main thread (the one that calls g_thread_init) inherit
141 * the static_private data set before calling g_thread_init
143 GRealThread* main_thread = (GRealThread*) g_thread_self ();
145 g_once_mutex = g_mutex_new ();
146 g_once_cond = g_cond_new ();
148 _g_convert_thread_init ();
149 _g_rand_thread_init ();
150 _g_main_thread_init ();
151 _g_mem_thread_init ();
152 _g_messages_thread_init ();
153 _g_atomic_thread_init ();
155 g_threads_got_initialized = TRUE;
157 g_thread_specific_private = g_private_new (g_thread_cleanup);
158 g_private_set (g_thread_specific_private, main_thread);
159 G_THREAD_UF (thread_self, (&main_thread->system_thread));
161 _g_mem_thread_private_init ();
162 _g_messages_thread_private_init ();
165 #endif /* G_THREADS_ENABLED */
168 g_once_impl (GOnce *once,
172 g_mutex_lock (g_once_mutex);
174 while (once->status == G_ONCE_STATUS_PROGRESS)
175 g_cond_wait (g_once_cond, g_once_mutex);
177 if (once->status != G_ONCE_STATUS_READY)
179 once->status = G_ONCE_STATUS_PROGRESS;
180 g_mutex_unlock (g_once_mutex);
182 once->retval = func (arg);
184 g_mutex_lock (g_once_mutex);
185 once->status = G_ONCE_STATUS_READY;
186 g_cond_broadcast (g_once_cond);
189 g_mutex_unlock (g_once_mutex);
195 g_static_mutex_init (GStaticMutex *mutex)
197 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
199 g_return_if_fail (mutex);
205 g_static_mutex_get_mutex_impl (GMutex** mutex)
207 if (!g_thread_supported ())
210 g_assert (g_once_mutex);
212 g_mutex_lock (g_once_mutex);
216 GMutex *new_mutex = g_mutex_new ();
218 /* The following is a memory barrier to avoid the write
219 * to *new_mutex being reordered to after writing *mutex */
220 g_mutex_lock (new_mutex);
221 g_mutex_unlock (new_mutex);
226 g_mutex_unlock (g_once_mutex);
232 g_static_mutex_free (GStaticMutex* mutex)
234 GMutex **runtime_mutex;
236 g_return_if_fail (mutex);
238 /* The runtime_mutex is the first (or only) member of GStaticMutex,
239 * see both versions (of glibconfig.h) in configure.in */
240 runtime_mutex = ((GMutex**)mutex);
243 g_mutex_free (*runtime_mutex);
245 *runtime_mutex = NULL;
249 g_static_rec_mutex_init (GStaticRecMutex *mutex)
251 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
253 g_return_if_fail (mutex);
259 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
263 g_return_if_fail (mutex);
265 if (!g_thread_supported ())
268 G_THREAD_UF (thread_self, (&self));
270 if (g_system_thread_equal (self, mutex->owner))
275 g_static_mutex_lock (&mutex->mutex);
276 g_system_thread_assign (mutex->owner, self);
281 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
285 g_return_val_if_fail (mutex, FALSE);
287 if (!g_thread_supported ())
290 G_THREAD_UF (thread_self, (&self));
292 if (g_system_thread_equal (self, mutex->owner))
298 if (!g_static_mutex_trylock (&mutex->mutex))
301 g_system_thread_assign (mutex->owner, self);
307 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
309 g_return_if_fail (mutex);
311 if (!g_thread_supported ())
314 if (mutex->depth > 1)
319 g_system_thread_assign (mutex->owner, zero_thread);
320 g_static_mutex_unlock (&mutex->mutex);
324 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
328 g_return_if_fail (mutex);
330 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;
468 list = g_thread_all_threads;
471 GRealThread *thread = list->data;
472 GArray *array = thread->private_data;
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)
525 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
528 /* Just to make sure, this isn't used any more */
529 g_system_thread_assign (thread->system_thread, zero_thread);
538 g_error ("The thread system is not yet initialized.");
542 g_thread_create_proxy (gpointer data)
544 GRealThread* thread = data;
548 /* This has to happen before G_LOCK, as that might call g_thread_self */
549 g_private_set (g_thread_specific_private, data);
551 /* the lock makes sure, that thread->system_thread is written,
552 before thread->thread.func is called. See g_thread_create. */
556 thread->retval = thread->thread.func (thread->thread.data);
562 g_thread_create_full (GThreadFunc func,
567 GThreadPriority priority,
571 GError *local_error = NULL;
572 g_return_val_if_fail (func, NULL);
573 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
574 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
576 result = g_new (GRealThread, 1);
578 result->thread.joinable = joinable;
579 result->thread.priority = priority;
580 result->thread.func = func;
581 result->thread.data = data;
582 result->private_data = NULL;
584 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
585 stack_size, joinable, bound, priority,
586 &result->system_thread, &local_error));
587 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
592 g_propagate_error (error, local_error);
597 return (GThread*) result;
601 g_thread_exit (gpointer retval)
603 GRealThread* real = (GRealThread*) g_thread_self ();
604 real->retval = retval;
605 G_THREAD_CF (thread_exit, (void)0, ());
609 g_thread_join (GThread* thread)
611 GRealThread* real = (GRealThread*) thread;
614 g_return_val_if_fail (thread, NULL);
615 g_return_val_if_fail (thread->joinable, NULL);
616 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
619 G_THREAD_UF (thread_join, (&real->system_thread));
621 retval = real->retval;
624 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
627 /* Just to make sure, this isn't used any more */
628 thread->joinable = 0;
629 g_system_thread_assign (real->system_thread, zero_thread);
631 /* the thread structure for non-joinable threads is freed upon
632 thread end. We free the memory here. This will leave a loose end,
633 if a joinable thread is not joined. */
641 g_thread_set_priority (GThread* thread,
642 GThreadPriority priority)
644 GRealThread* real = (GRealThread*) thread;
646 g_return_if_fail (thread);
647 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
648 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
649 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
651 thread->priority = priority;
653 G_THREAD_CF (thread_set_priority, (void)0,
654 (&real->system_thread, priority));
660 GRealThread* thread = g_private_get (g_thread_specific_private);
664 /* If no thread data is available, provide and set one. This
665 can happen for the main thread and for threads, that are not
667 thread = g_new (GRealThread, 1);
668 thread->thread.joinable = FALSE; /* This is a save guess */
669 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
671 thread->thread.func = NULL;
672 thread->thread.data = NULL;
673 thread->private_data = NULL;
675 if (g_thread_supported ())
676 G_THREAD_UF (thread_self, (&thread->system_thread));
678 g_private_set (g_thread_specific_private, thread);
681 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
685 return (GThread*)thread;
689 g_static_rw_lock_init (GStaticRWLock* lock)
691 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
693 g_return_if_fail (lock);
699 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
702 *cond = g_cond_new ();
703 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
707 g_static_rw_lock_signal (GStaticRWLock* lock)
709 if (lock->want_to_write && lock->write_cond)
710 g_cond_signal (lock->write_cond);
711 else if (lock->want_to_read && lock->read_cond)
712 g_cond_broadcast (lock->read_cond);
716 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
718 g_return_if_fail (lock);
720 if (!g_threads_got_initialized)
723 g_static_mutex_lock (&lock->mutex);
724 lock->want_to_read++;
725 while (lock->have_writer || lock->want_to_write)
726 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
727 lock->want_to_read--;
728 lock->read_counter++;
729 g_static_mutex_unlock (&lock->mutex);
733 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
735 gboolean ret_val = FALSE;
737 g_return_val_if_fail (lock, FALSE);
739 if (!g_threads_got_initialized)
742 g_static_mutex_lock (&lock->mutex);
743 if (!lock->have_writer && !lock->want_to_write)
745 lock->read_counter++;
748 g_static_mutex_unlock (&lock->mutex);
753 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
755 g_return_if_fail (lock);
757 if (!g_threads_got_initialized)
760 g_static_mutex_lock (&lock->mutex);
761 lock->read_counter--;
762 if (lock->read_counter == 0)
763 g_static_rw_lock_signal (lock);
764 g_static_mutex_unlock (&lock->mutex);
768 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
770 g_return_if_fail (lock);
772 if (!g_threads_got_initialized)
775 g_static_mutex_lock (&lock->mutex);
776 lock->want_to_write++;
777 while (lock->have_writer || lock->read_counter)
778 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
779 lock->want_to_write--;
780 lock->have_writer = TRUE;
781 g_static_mutex_unlock (&lock->mutex);
785 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
787 gboolean ret_val = FALSE;
789 g_return_val_if_fail (lock, FALSE);
791 if (!g_threads_got_initialized)
794 g_static_mutex_lock (&lock->mutex);
795 if (!lock->have_writer && !lock->read_counter)
797 lock->have_writer = TRUE;
800 g_static_mutex_unlock (&lock->mutex);
805 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
807 g_return_if_fail (lock);
809 if (!g_threads_got_initialized)
812 g_static_mutex_lock (&lock->mutex);
813 lock->have_writer = FALSE;
814 g_static_rw_lock_signal (lock);
815 g_static_mutex_unlock (&lock->mutex);
819 g_static_rw_lock_free (GStaticRWLock* lock)
821 g_return_if_fail (lock);
825 g_cond_free (lock->read_cond);
826 lock->read_cond = NULL;
828 if (lock->write_cond)
830 g_cond_free (lock->write_cond);
831 lock->write_cond = NULL;
833 g_static_mutex_free (&lock->mutex);