1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gmutex.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/.
37 #ifdef G_THREAD_USE_PID_SURROGATE
38 #include <sys/types.h>
40 #include <sys/resource.h>
42 #endif /* G_THREAD_USE_PID_SURROGATE */
51 #include "gthreadinit.h"
53 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
54 # define g_system_thread_equal_simple(thread1, thread2) \
55 ((thread1).dummy_pointer == (thread2).dummy_pointer)
56 # define g_system_thread_assign(dest, src) \
57 ((dest).dummy_pointer = (src).dummy_pointer)
58 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
59 # define g_system_thread_equal_simple(thread1, thread2) \
60 (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
61 # define g_system_thread_assign(dest, src) \
62 (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
63 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
65 #define g_system_thread_equal(thread1, thread2) \
66 (g_thread_functions_for_glib_use.thread_equal ? \
67 g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
68 g_system_thread_equal_simple((thread1), (thread2)))
71 g_thread_error_quark (void)
75 quark = g_quark_from_static_string ("g_thread_error");
79 /* Keep this in sync with GRealThread in gmain.c! */
80 typedef struct _GRealThread GRealThread;
84 gpointer private_data;
86 GSystemThread system_thread;
87 #ifdef G_THREAD_USE_PID_SURROGATE
89 #endif /* G_THREAD_USE_PID_SURROGATE */
92 #ifdef G_THREAD_USE_PID_SURROGATE
93 static gint priority_map[4];
94 static gboolean prio_warned = FALSE;
95 # define SET_PRIO(pid, prio) G_STMT_START{ \
96 gint error = setpriority (PRIO_PROCESS, (pid), priority_map[prio]); \
97 if (error == -1 && errno == EACCES && !prio_warned) \
100 g_warning ("Priorities can only be increased by root."); \
103 #endif /* G_THREAD_USE_PID_SURROGATE */
105 typedef struct _GStaticPrivateNode GStaticPrivateNode;
106 struct _GStaticPrivateNode
109 GDestroyNotify destroy;
112 static void g_thread_cleanup (gpointer data);
113 static void g_thread_fail (void);
115 /* Global variables */
117 static GSystemThread zero_thread; /* This is initialized to all zero */
118 gboolean g_thread_use_default_impl = TRUE;
119 gboolean g_threads_got_initialized = FALSE;
121 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
122 __declspec(dllexport)
124 GThreadFunctions g_thread_functions_for_glib_use = {
125 (GMutex*(*)())g_thread_fail, /* mutex_new */
126 NULL, /* mutex_lock */
127 NULL, /* mutex_trylock */
128 NULL, /* mutex_unlock */
129 NULL, /* mutex_free */
130 (GCond*(*)())g_thread_fail, /* cond_new */
131 NULL, /* cond_signal */
132 NULL, /* cond_broadcast */
133 NULL, /* cond_wait */
134 NULL, /* cond_timed_wait */
135 NULL, /* cond_free */
136 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
137 NULL, /* private_get */
138 NULL, /* private_set */
139 (void(*)(GThreadFunc, gpointer, gulong,
140 gboolean, gboolean, GThreadPriority,
141 gpointer, GError**))g_thread_fail, /* thread_create */
142 NULL, /* thread_yield */
143 NULL, /* thread_join */
144 NULL, /* thread_exit */
145 NULL, /* thread_set_priority */
146 NULL /* thread_self */
151 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
152 static GPrivate *g_thread_specific_private = NULL;
153 static GSList *g_thread_all_threads = NULL;
154 static GSList *g_thread_free_indeces = NULL;
156 G_LOCK_DEFINE_STATIC (g_thread);
158 /* This must be called only once, before any threads are created.
159 * It will only be called from g_thread_init() in -lgthread.
162 g_thread_init_glib (void)
164 /* We let the main thread (the one that calls g_thread_init) inherit
165 * the static_private data set before calling g_thread_init
167 GRealThread* main_thread = (GRealThread*) g_thread_self ();
169 g_mutex_protect_static_mutex_allocation = g_mutex_new ();
171 _g_convert_thread_init ();
172 _g_rand_thread_init ();
173 _g_main_thread_init ();
174 _g_mem_thread_init ();
175 _g_messages_thread_init ();
177 #ifdef G_THREAD_USE_PID_SURROGATE
178 priority_map[G_THREAD_PRIORITY_NORMAL] =
179 getpriority (PRIO_PROCESS, (getpid ()));
180 priority_map[G_THREAD_PRIORITY_LOW] =
181 MIN (20, priority_map[G_THREAD_PRIORITY_NORMAL] + 10);
182 priority_map[G_THREAD_PRIORITY_HIGH] =
183 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 10);
184 priority_map[G_THREAD_PRIORITY_URGENT] =
185 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 15);
186 #endif /* G_THREAD_USE_PID_SURROGATE */
188 g_threads_got_initialized = TRUE;
190 g_thread_specific_private = g_private_new (g_thread_cleanup);
191 g_private_set (g_thread_specific_private, main_thread);
192 G_THREAD_UF (thread_self, (&main_thread->system_thread));
194 _g_mem_thread_private_init ();
195 _g_messages_thread_private_init ();
200 g_static_mutex_init (GStaticMutex *mutex)
202 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
204 g_return_if_fail (mutex);
210 g_static_mutex_get_mutex_impl (GMutex** mutex)
212 if (!g_thread_supported ())
215 g_assert (g_mutex_protect_static_mutex_allocation);
217 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
220 *mutex = g_mutex_new ();
222 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
228 g_static_mutex_free (GStaticMutex* mutex)
230 GMutex **runtime_mutex;
232 g_return_if_fail (mutex);
234 /* The runtime_mutex is the first (or only) member of GStaticMutex,
235 * see both versions (of glibconfig.h) in configure.in */
236 runtime_mutex = ((GMutex**)mutex);
239 g_mutex_free (*runtime_mutex);
241 *runtime_mutex = NULL;
245 g_static_rec_mutex_init (GStaticRecMutex *mutex)
247 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
249 g_return_if_fail (mutex);
255 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
259 g_return_if_fail (mutex);
261 if (!g_thread_supported ())
264 G_THREAD_UF (thread_self, (&self));
266 if (g_system_thread_equal (self, mutex->owner))
271 g_static_mutex_lock (&mutex->mutex);
272 g_system_thread_assign (mutex->owner, self);
277 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
281 g_return_val_if_fail (mutex, FALSE);
283 if (!g_thread_supported ())
286 G_THREAD_UF (thread_self, (&self));
288 if (g_system_thread_equal (self, mutex->owner))
294 if (!g_static_mutex_trylock (&mutex->mutex))
297 g_system_thread_assign (mutex->owner, self);
303 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
305 g_return_if_fail (mutex);
307 if (!g_thread_supported ())
310 if (mutex->depth > 1)
315 g_system_thread_assign (mutex->owner, zero_thread);
316 g_static_mutex_unlock (&mutex->mutex);
320 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
324 g_return_if_fail (mutex);
326 if (!g_thread_supported ())
329 G_THREAD_UF (thread_self, (&self));
331 if (g_system_thread_equal (self, mutex->owner))
333 mutex->depth += depth;
336 g_static_mutex_lock (&mutex->mutex);
337 g_system_thread_assign (mutex->owner, self);
338 mutex->depth = depth;
342 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
346 g_return_val_if_fail (mutex, 0);
348 if (!g_thread_supported ())
351 depth = mutex->depth;
353 g_system_thread_assign (mutex->owner, zero_thread);
355 g_static_mutex_unlock (&mutex->mutex);
361 g_static_rec_mutex_free (GStaticRecMutex *mutex)
363 g_return_if_fail (mutex);
365 g_static_mutex_free (&mutex->mutex);
369 g_static_private_init (GStaticPrivate *private_key)
371 private_key->index = 0;
375 g_static_private_get (GStaticPrivate *private_key)
377 GRealThread *self = (GRealThread*) g_thread_self ();
380 array = self->private_data;
384 if (!private_key->index)
386 else if (private_key->index <= array->len)
387 return g_array_index (array, GStaticPrivateNode,
388 private_key->index - 1).data;
394 g_static_private_set (GStaticPrivate *private_key,
396 GDestroyNotify notify)
398 GRealThread *self = (GRealThread*) g_thread_self ();
400 static guint next_index = 0;
401 GStaticPrivateNode *node;
403 array = self->private_data;
406 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
407 self->private_data = array;
410 if (!private_key->index)
414 if (!private_key->index)
416 if (g_thread_free_indeces)
419 GPOINTER_TO_UINT (g_thread_free_indeces->data);
420 g_thread_free_indeces =
421 g_slist_delete_link (g_thread_free_indeces,
422 g_thread_free_indeces);
425 private_key->index = ++next_index;
431 if (private_key->index > array->len)
432 g_array_set_size (array, private_key->index);
434 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
437 gpointer ddata = node->data;
438 GDestroyNotify ddestroy = node->destroy;
441 node->destroy = notify;
448 node->destroy = notify;
453 g_static_private_free (GStaticPrivate *private_key)
455 guint index = private_key->index;
461 private_key->index = 0;
464 list = g_thread_all_threads;
467 GRealThread *thread = list->data;
468 GArray *array = thread->private_data;
471 if (array && index <= array->len)
473 GStaticPrivateNode *node = &g_array_index (array,
476 gpointer ddata = node->data;
477 GDestroyNotify ddestroy = node->destroy;
480 node->destroy = NULL;
490 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
491 GUINT_TO_POINTER (index));
496 g_thread_cleanup (gpointer data)
500 GRealThread* thread = data;
501 if (thread->private_data)
503 GArray* array = thread->private_data;
506 for (i = 0; i < array->len; i++ )
508 GStaticPrivateNode *node =
509 &g_array_index (array, GStaticPrivateNode, i);
511 node->destroy (node->data);
513 g_array_free (array, TRUE);
516 /* We only free the thread structure, if it isn't joinable. If
517 it is, the structure is freed in g_thread_join */
518 if (!thread->thread.joinable)
521 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
524 /* Just to make sure, this isn't used any more */
525 g_system_thread_assign (thread->system_thread, zero_thread);
534 g_error ("The thread system is not yet initialized.");
538 g_thread_create_proxy (gpointer data)
540 GRealThread* thread = data;
544 #ifdef G_THREAD_USE_PID_SURROGATE
545 thread->pid = getpid ();
546 #endif /* G_THREAD_USE_PID_SURROGATE */
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 #ifdef G_THREAD_USE_PID_SURROGATE
557 if (g_thread_use_default_impl)
558 SET_PRIO (thread->pid, thread->thread.priority);
559 #endif /* G_THREAD_USE_PID_SURROGATE */
561 thread->retval = thread->thread.func (thread->thread.data);
567 g_thread_create_full (GThreadFunc func,
572 GThreadPriority priority,
576 GError *local_error = NULL;
577 g_return_val_if_fail (func, NULL);
578 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
579 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
581 result = g_new (GRealThread, 1);
583 result->thread.joinable = joinable;
584 result->thread.priority = priority;
585 result->thread.func = func;
586 result->thread.data = data;
587 result->private_data = NULL;
589 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
590 stack_size, joinable, bound, priority,
591 &result->system_thread, &local_error));
592 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
597 g_propagate_error (error, local_error);
602 return (GThread*) result;
606 g_thread_exit (gpointer retval)
608 GRealThread* real = (GRealThread*) g_thread_self ();
609 real->retval = retval;
610 G_THREAD_CF (thread_exit, (void)0, ());
614 g_thread_join (GThread* thread)
616 GRealThread* real = (GRealThread*) thread;
619 g_return_val_if_fail (thread, NULL);
620 g_return_val_if_fail (thread->joinable, NULL);
621 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
624 G_THREAD_UF (thread_join, (&real->system_thread));
626 retval = real->retval;
629 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
632 /* Just to make sure, this isn't used any more */
633 thread->joinable = 0;
634 g_system_thread_assign (real->system_thread, zero_thread);
636 /* the thread structure for non-joinable threads is freed upon
637 thread end. We free the memory here. This will leave a loose end,
638 if a joinable thread is not joined. */
646 g_thread_set_priority (GThread* thread,
647 GThreadPriority priority)
649 GRealThread* real = (GRealThread*) thread;
651 g_return_if_fail (thread);
652 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
653 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
654 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
656 thread->priority = priority;
658 #ifdef G_THREAD_USE_PID_SURROGATE
659 if (g_thread_use_default_impl)
660 SET_PRIO (real->pid, priority);
662 #endif /* G_THREAD_USE_PID_SURROGATE */
663 G_THREAD_CF (thread_set_priority, (void)0,
664 (&real->system_thread, priority));
670 GRealThread* thread = g_private_get (g_thread_specific_private);
674 /* If no thread data is available, provide and set one. This
675 can happen for the main thread and for threads, that are not
677 thread = g_new (GRealThread, 1);
678 thread->thread.joinable = FALSE; /* This is a save guess */
679 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
681 thread->thread.func = NULL;
682 thread->thread.data = NULL;
683 thread->private_data = NULL;
685 if (g_thread_supported ())
686 G_THREAD_UF (thread_self, (&thread->system_thread));
688 #ifdef G_THREAD_USE_PID_SURROGATE
689 thread->pid = getpid ();
690 #endif /* G_THREAD_USE_PID_SURROGATE */
692 g_private_set (g_thread_specific_private, thread);
695 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
699 return (GThread*)thread;
703 g_static_rw_lock_init (GStaticRWLock* lock)
705 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
707 g_return_if_fail (lock);
713 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
716 *cond = g_cond_new ();
717 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
721 g_static_rw_lock_signal (GStaticRWLock* lock)
723 if (lock->want_to_write && lock->write_cond)
724 g_cond_signal (lock->write_cond);
725 else if (lock->want_to_read && lock->read_cond)
726 g_cond_broadcast (lock->read_cond);
730 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
732 g_return_if_fail (lock);
734 if (!g_threads_got_initialized)
737 g_static_mutex_lock (&lock->mutex);
738 lock->want_to_read++;
739 while (lock->have_writer || lock->want_to_write)
740 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
741 lock->want_to_read--;
742 lock->read_counter++;
743 g_static_mutex_unlock (&lock->mutex);
747 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
749 gboolean ret_val = FALSE;
751 g_return_val_if_fail (lock, FALSE);
753 if (!g_threads_got_initialized)
756 g_static_mutex_lock (&lock->mutex);
757 if (!lock->have_writer && !lock->want_to_write)
759 lock->read_counter++;
762 g_static_mutex_unlock (&lock->mutex);
767 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
769 g_return_if_fail (lock);
771 if (!g_threads_got_initialized)
774 g_static_mutex_lock (&lock->mutex);
775 lock->read_counter--;
776 if (lock->read_counter == 0)
777 g_static_rw_lock_signal (lock);
778 g_static_mutex_unlock (&lock->mutex);
782 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
784 g_return_if_fail (lock);
786 if (!g_threads_got_initialized)
789 g_static_mutex_lock (&lock->mutex);
790 lock->want_to_write++;
791 while (lock->have_writer || lock->read_counter)
792 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
793 lock->want_to_write--;
794 lock->have_writer = TRUE;
795 g_static_mutex_unlock (&lock->mutex);
799 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
801 gboolean ret_val = FALSE;
803 g_return_val_if_fail (lock, FALSE);
805 if (!g_threads_got_initialized)
808 g_static_mutex_lock (&lock->mutex);
809 if (!lock->have_writer && !lock->read_counter)
811 lock->have_writer = TRUE;
814 g_static_mutex_unlock (&lock->mutex);
819 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
821 g_return_if_fail (lock);
823 if (!g_threads_got_initialized)
826 g_static_mutex_lock (&lock->mutex);
827 lock->have_writer = FALSE;
828 g_static_rw_lock_signal (lock);
829 g_static_mutex_unlock (&lock->mutex);
833 g_static_rw_lock_free (GStaticRWLock* lock)
835 g_return_if_fail (lock);
839 g_cond_free (lock->read_cond);
840 lock->read_cond = NULL;
842 if (lock->write_cond)
844 g_cond_free (lock->write_cond);
845 lock->write_cond = NULL;
847 g_static_mutex_free (&lock->mutex);