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/.
38 #ifdef G_THREAD_USE_PID_SURROGATE
39 #include <sys/types.h>
41 #include <sys/resource.h>
43 #endif /* G_THREAD_USE_PID_SURROGATE */
51 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
52 # define g_system_thread_equal_simple(thread1, thread2) \
53 ((thread1).dummy_pointer == (thread2).dummy_pointer)
54 # define g_system_thread_assign(dest, src) \
55 ((dest).dummy_pointer = (src).dummy_pointer)
56 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
57 # define g_system_thread_equal_simple(thread1, thread2) \
58 (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
59 # define g_system_thread_assign(dest, src) \
60 (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
61 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
63 #define g_system_thread_equal(thread1, thread2) \
64 (g_thread_functions_for_glib_use.thread_equal ? \
65 g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
66 g_system_thread_equal_simple((thread1), (thread2)))
69 g_thread_error_quark (void)
73 quark = g_quark_from_static_string ("g_thread_error");
77 /* Keep this in sync with GRealThread in gmain.c! */
78 typedef struct _GRealThread GRealThread;
82 gpointer private_data;
84 GSystemThread system_thread;
85 #ifdef G_THREAD_USE_PID_SURROGATE
87 #endif /* G_THREAD_USE_PID_SURROGATE */
90 #ifdef G_THREAD_USE_PID_SURROGATE
91 static gint priority_map[] = { 15, 0, -15, -20 };
92 static gboolean prio_warned = FALSE;
93 # define SET_PRIO(pid, prio) G_STMT_START{ \
94 gint error = setpriority (PRIO_PROCESS, (pid), priority_map[prio]); \
95 if (error == -1 && errno == EACCES && !prio_warned) \
98 g_warning ("Priorities can only be increased by root."); \
101 #endif /* G_THREAD_USE_PID_SURROGATE */
103 typedef struct _GStaticPrivateNode GStaticPrivateNode;
104 struct _GStaticPrivateNode
107 GDestroyNotify destroy;
110 static void g_thread_cleanup (gpointer data);
111 static void g_thread_fail (void);
113 /* Global variables */
115 static GSystemThread zero_thread; /* This is initialized to all zero */
116 gboolean g_thread_use_default_impl = TRUE;
117 gboolean g_threads_got_initialized = FALSE;
119 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
120 __declspec(dllexport)
122 GThreadFunctions g_thread_functions_for_glib_use = {
123 (GMutex*(*)())g_thread_fail, /* mutex_new */
124 NULL, /* mutex_lock */
125 NULL, /* mutex_trylock */
126 NULL, /* mutex_unlock */
127 NULL, /* mutex_free */
128 (GCond*(*)())g_thread_fail, /* cond_new */
129 NULL, /* cond_signal */
130 NULL, /* cond_broadcast */
131 NULL, /* cond_wait */
132 NULL, /* cond_timed_wait */
133 NULL, /* cond_free */
134 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
135 NULL, /* private_get */
136 NULL, /* private_set */
137 (void(*)(GThreadFunc, gpointer, gulong,
138 gboolean, gboolean, GThreadPriority,
139 gpointer, GError**))g_thread_fail, /* thread_create */
140 NULL, /* thread_yield */
141 NULL, /* thread_join */
142 NULL, /* thread_exit */
143 NULL, /* thread_set_priority */
144 NULL /* thread_self */
149 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
150 static GPrivate *g_thread_specific_private = NULL;
151 static GSList *g_thread_all_threads = NULL;
152 static GSList *g_thread_free_indeces = NULL;
154 G_LOCK_DEFINE_STATIC (g_thread);
156 /* This must be called only once, before any threads are created.
157 * It will only be called from g_thread_init() in -lgthread.
162 GRealThread* main_thread;
164 /* We let the main thread (the one that calls g_thread_init) inherit
165 * the data, that it set before calling g_thread_init
167 main_thread = (GRealThread*) g_thread_self ();
169 g_thread_specific_private = g_private_new (g_thread_cleanup);
170 G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
171 G_THREAD_UF (thread_self, (&main_thread->system_thread));
173 g_mutex_protect_static_mutex_allocation = g_mutex_new ();
177 g_static_mutex_init (GStaticMutex *mutex)
179 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
181 g_return_if_fail (mutex);
187 g_static_mutex_get_mutex_impl (GMutex** mutex)
189 if (!g_thread_supported ())
192 g_assert (g_mutex_protect_static_mutex_allocation);
194 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
197 *mutex = g_mutex_new ();
199 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
205 g_static_mutex_free (GStaticMutex* mutex)
207 GMutex **runtime_mutex;
209 g_return_if_fail (mutex);
211 /* The runtime_mutex is the first (or only) member of GStaticMutex,
212 * see both versions (of glibconfig.h) in configure.in */
213 runtime_mutex = ((GMutex**)mutex);
216 g_mutex_free (*runtime_mutex);
218 *runtime_mutex = NULL;
222 g_static_rec_mutex_init (GStaticRecMutex *mutex)
224 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
226 g_return_if_fail (mutex);
232 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
236 g_return_if_fail (mutex);
238 if (!g_thread_supported ())
241 G_THREAD_UF (thread_self, (&self));
243 if (g_system_thread_equal (self, mutex->owner))
248 g_static_mutex_lock (&mutex->mutex);
249 g_system_thread_assign (mutex->owner, self);
254 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
258 g_return_val_if_fail (mutex, FALSE);
260 if (!g_thread_supported ())
263 G_THREAD_UF (thread_self, (&self));
265 if (g_system_thread_equal (self, mutex->owner))
271 if (!g_static_mutex_trylock (&mutex->mutex))
274 g_system_thread_assign (mutex->owner, self);
280 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
282 g_return_if_fail (mutex);
284 if (!g_thread_supported ())
287 if (mutex->depth > 1)
292 g_system_thread_assign (mutex->owner, zero_thread);
293 g_static_mutex_unlock (&mutex->mutex);
297 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
301 g_return_if_fail (mutex);
303 if (!g_thread_supported ())
306 G_THREAD_UF (thread_self, (&self));
308 if (g_system_thread_equal (self, mutex->owner))
310 mutex->depth += depth;
313 g_static_mutex_lock (&mutex->mutex);
314 g_system_thread_assign (mutex->owner, self);
315 mutex->depth = depth;
319 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
323 g_return_val_if_fail (mutex, 0);
325 if (!g_thread_supported ())
328 depth = mutex->depth;
330 g_system_thread_assign (mutex->owner, zero_thread);
332 g_static_mutex_unlock (&mutex->mutex);
338 g_static_rec_mutex_free (GStaticRecMutex *mutex)
340 g_return_if_fail (mutex);
342 g_static_mutex_free (&mutex->mutex);
346 g_static_private_init (GStaticPrivate *private_key)
348 private_key->index = 0;
352 g_static_private_get (GStaticPrivate *private_key)
354 GRealThread *self = (GRealThread*) g_thread_self ();
357 array = self->private_data;
361 if (!private_key->index)
363 else if (private_key->index <= array->len)
364 return g_array_index (array, GStaticPrivateNode,
365 private_key->index - 1).data;
371 g_static_private_set (GStaticPrivate *private_key,
373 GDestroyNotify notify)
375 GRealThread *self = (GRealThread*) g_thread_self ();
377 static guint next_index = 0;
378 GStaticPrivateNode *node;
380 array = self->private_data;
383 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
384 self->private_data = array;
387 if (!private_key->index)
391 if (!private_key->index)
393 if (g_thread_free_indeces)
396 GPOINTER_TO_UINT (g_thread_free_indeces->data);
397 g_thread_free_indeces =
398 g_slist_delete_link (g_thread_free_indeces,
399 g_thread_free_indeces);
402 private_key->index = ++next_index;
408 if (private_key->index > array->len)
409 g_array_set_size (array, private_key->index);
411 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
414 gpointer ddata = node->data;
415 GDestroyNotify ddestroy = node->destroy;
418 node->destroy = notify;
425 node->destroy = notify;
430 g_static_private_free (GStaticPrivate *private_key)
432 guint index = private_key->index;
438 private_key->index = 0;
441 list = g_thread_all_threads;
444 GRealThread *thread = list->data;
445 GArray *array = thread->private_data;
448 if (array && index <= array->len)
450 GStaticPrivateNode *node = &g_array_index (array,
453 gpointer ddata = node->data;
454 GDestroyNotify ddestroy = node->destroy;
457 node->destroy = NULL;
467 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
468 GUINT_TO_POINTER (index));
473 g_thread_cleanup (gpointer data)
477 GRealThread* thread = data;
478 if (thread->private_data)
480 GArray* array = thread->private_data;
483 for (i = 0; i < array->len; i++ )
485 GStaticPrivateNode *node =
486 &g_array_index (array, GStaticPrivateNode, i);
488 node->destroy (node->data);
490 g_array_free (array, TRUE);
493 /* We only free the thread structure, if it isn't joinable. If
494 it is, the structure is freed in g_thread_join */
495 if (!thread->thread.joinable)
498 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
501 /* Just to make sure, this isn't used any more */
502 g_system_thread_assign (thread->system_thread, zero_thread);
511 g_error ("The thread system is not yet initialized.");
515 g_thread_create_proxy (gpointer data)
517 GRealThread* thread = data;
521 #ifdef G_THREAD_USE_PID_SURROGATE
522 thread->pid = getpid ();
523 #endif /* G_THREAD_USE_PID_SURROGATE */
525 /* This has to happen before G_LOCK, as that might call g_thread_self */
526 g_private_set (g_thread_specific_private, data);
528 /* the lock makes sure, that thread->system_thread is written,
529 before thread->thread.func is called. See g_thread_create. */
533 #ifdef G_THREAD_USE_PID_SURROGATE
534 if (g_thread_use_default_impl)
535 SET_PRIO (thread->pid, thread->thread.priority);
536 #endif /* G_THREAD_USE_PID_SURROGATE */
538 thread->retval = thread->thread.func (thread->thread.data);
544 g_thread_create_full (GThreadFunc func,
549 GThreadPriority priority,
552 GRealThread* result = g_new (GRealThread, 1);
553 GError *local_error = NULL;
554 g_return_val_if_fail (func, NULL);
555 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
556 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
558 result->thread.joinable = joinable;
559 result->thread.priority = priority;
560 result->thread.func = func;
561 result->thread.data = data;
562 result->private_data = NULL;
564 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
565 stack_size, joinable, bound, priority,
566 &result->system_thread, &local_error));
567 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
572 g_propagate_error (error, local_error);
577 return (GThread*) result;
581 g_thread_exit (gpointer retval)
583 GRealThread* real = (GRealThread*) g_thread_self ();
584 real->retval = retval;
585 G_THREAD_CF (thread_exit, (void)0, ());
589 g_thread_join (GThread* thread)
591 GRealThread* real = (GRealThread*) thread;
594 g_return_val_if_fail (thread, NULL);
595 g_return_val_if_fail (thread->joinable, NULL);
596 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
599 G_THREAD_UF (thread_join, (&real->system_thread));
601 retval = real->retval;
604 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
607 /* Just to make sure, this isn't used any more */
608 thread->joinable = 0;
609 g_system_thread_assign (real->system_thread, zero_thread);
611 /* the thread structure for non-joinable threads is freed upon
612 thread end. We free the memory here. This will leave a loose end,
613 if a joinable thread is not joined. */
621 g_thread_set_priority (GThread* thread,
622 GThreadPriority priority)
624 GRealThread* real = (GRealThread*) thread;
626 g_return_if_fail (thread);
627 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
628 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
629 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
631 thread->priority = priority;
633 #ifdef G_THREAD_USE_PID_SURROGATE
634 if (g_thread_use_default_impl)
635 SET_PRIO (real->pid, priority);
637 #endif /* G_THREAD_USE_PID_SURROGATE */
638 G_THREAD_CF (thread_set_priority, (void)0,
639 (&real->system_thread, priority));
645 GRealThread* thread = g_private_get (g_thread_specific_private);
649 /* If no thread data is available, provide and set one. This
650 can happen for the main thread and for threads, that are not
652 thread = g_new (GRealThread, 1);
653 thread->thread.joinable = FALSE; /* This is a save guess */
654 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
656 thread->thread.func = NULL;
657 thread->thread.data = NULL;
658 thread->private_data = NULL;
660 if (g_thread_supported ())
661 G_THREAD_UF (thread_self, (&thread->system_thread));
663 #ifdef G_THREAD_USE_PID_SURROGATE
664 thread->pid = getpid ();
665 #endif /* G_THREAD_USE_PID_SURROGATE */
667 g_private_set (g_thread_specific_private, thread);
670 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
674 return (GThread*)thread;
678 g_static_rw_lock_init (GStaticRWLock* lock)
680 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
682 g_return_if_fail (lock);
688 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
691 *cond = g_cond_new ();
692 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
696 g_static_rw_lock_signal (GStaticRWLock* lock)
698 if (lock->want_to_write && lock->write_cond)
699 g_cond_signal (lock->write_cond);
700 else if (lock->want_to_read && lock->read_cond)
701 g_cond_broadcast (lock->read_cond);
705 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
707 g_return_if_fail (lock);
709 if (!g_threads_got_initialized)
712 g_static_mutex_lock (&lock->mutex);
713 lock->want_to_read++;
714 while (lock->write || lock->want_to_write)
715 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
716 lock->want_to_read--;
717 lock->read_counter++;
718 g_static_mutex_unlock (&lock->mutex);
722 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
724 gboolean ret_val = FALSE;
726 g_return_val_if_fail (lock, FALSE);
728 if (!g_threads_got_initialized)
731 g_static_mutex_lock (&lock->mutex);
732 if (!lock->write && !lock->want_to_write)
734 lock->read_counter++;
737 g_static_mutex_unlock (&lock->mutex);
742 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
744 g_return_if_fail (lock);
746 if (!g_threads_got_initialized)
749 g_static_mutex_lock (&lock->mutex);
750 lock->read_counter--;
751 if (lock->read_counter == 0)
752 g_static_rw_lock_signal (lock);
753 g_static_mutex_unlock (&lock->mutex);
757 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
759 g_return_if_fail (lock);
761 if (!g_threads_got_initialized)
764 g_static_mutex_lock (&lock->mutex);
765 lock->want_to_write++;
766 while (lock->write || lock->read_counter)
767 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
768 lock->want_to_write--;
770 g_static_mutex_unlock (&lock->mutex);
774 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
776 gboolean ret_val = FALSE;
778 g_return_val_if_fail (lock, FALSE);
780 if (!g_threads_got_initialized)
783 g_static_mutex_lock (&lock->mutex);
784 if (!lock->write && !lock->read_counter)
789 g_static_mutex_unlock (&lock->mutex);
794 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
796 g_return_if_fail (lock);
798 if (!g_threads_got_initialized)
801 g_static_mutex_lock (&lock->mutex);
803 g_static_rw_lock_signal (lock);
804 g_static_mutex_unlock (&lock->mutex);
808 g_static_rw_lock_free (GStaticRWLock* lock)
810 g_return_if_fail (lock);
814 g_cond_free (lock->read_cond);
815 lock->read_cond = NULL;
817 if (lock->write_cond)
819 g_cond_free (lock->write_cond);
820 lock->write_cond = NULL;
822 g_static_mutex_free (&lock->mutex);