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(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(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 */
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;
80 #ifdef G_THREAD_USE_PID_SURROGATE
82 #endif /* G_THREAD_USE_PID_SURROGATE */
85 #ifdef G_THREAD_USE_PID_SURROGATE
86 static gint priority_map[] = { 15, 0, -15, -20 };
87 static gboolean prio_warned = FALSE;
88 # define SET_PRIO(pid, prio) G_STMT_START{ \
89 gint error = setpriority (PRIO_PROCESS, (pid), priority_map[prio]); \
90 if (error == -1 && errno == EACCES && !prio_warned) \
93 g_warning ("Priorities can only be increased by root."); \
96 #endif /* G_THREAD_USE_PID_SURROGATE */
98 typedef struct _GStaticPrivateNode GStaticPrivateNode;
99 struct _GStaticPrivateNode
102 GDestroyNotify destroy;
105 static void g_thread_cleanup (gpointer data);
106 static void g_thread_fail (void);
108 /* Global variables */
110 static GSystemThread zero_thread; /* This is initialized to all zero */
111 gboolean g_thread_use_default_impl = TRUE;
112 gboolean g_threads_got_initialized = FALSE;
114 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
115 __declspec(dllexport)
117 GThreadFunctions g_thread_functions_for_glib_use = {
118 (GMutex*(*)())g_thread_fail, /* mutex_new */
119 NULL, /* mutex_lock */
120 NULL, /* mutex_trylock */
121 NULL, /* mutex_unlock */
122 NULL, /* mutex_free */
123 (GCond*(*)())g_thread_fail, /* cond_new */
124 NULL, /* cond_signal */
125 NULL, /* cond_broadcast */
126 NULL, /* cond_wait */
127 NULL, /* cond_timed_wait */
128 NULL, /* cond_free */
129 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
130 NULL, /* private_get */
131 NULL, /* private_set */
132 (void(*)(GThreadFunc, gpointer, gulong,
133 gboolean, gboolean, GThreadPriority,
134 gpointer, GError**))g_thread_fail, /* thread_create */
135 NULL, /* thread_yield */
136 NULL, /* thread_join */
137 NULL, /* thread_exit */
138 NULL, /* thread_set_priority */
139 NULL /* thread_self */
144 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
145 static GPrivate *g_thread_specific_private = NULL;
146 static GSList *g_thread_all_threads = NULL;
147 static GSList *g_thread_free_indeces = NULL;
149 G_LOCK_DEFINE_STATIC (g_thread);
151 /* This must be called only once, before any threads are created.
152 * It will only be called from g_thread_init() in -lgthread.
157 GRealThread* main_thread;
159 /* We let the main thread (the one that calls g_thread_init) inherit
160 * the data, that it set before calling g_thread_init
162 main_thread = (GRealThread*) g_thread_self ();
164 g_thread_specific_private = g_private_new (g_thread_cleanup);
165 G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
166 G_THREAD_UF (thread_self, (&main_thread->system_thread));
168 g_mutex_protect_static_mutex_allocation = g_mutex_new ();
172 g_static_mutex_init (GStaticMutex *mutex)
174 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
176 g_return_if_fail (mutex);
182 g_static_mutex_get_mutex_impl (GMutex** mutex)
184 if (!g_thread_supported ())
187 g_assert (g_mutex_protect_static_mutex_allocation);
189 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
192 *mutex = g_mutex_new ();
194 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
200 g_static_mutex_free (GStaticMutex* mutex)
202 GMutex **runtime_mutex;
204 g_return_if_fail (mutex);
206 /* The runtime_mutex is the first (or only) member of GStaticMutex,
207 * see both versions (of glibconfig.h) in configure.in */
208 runtime_mutex = ((GMutex**)mutex);
211 g_mutex_free (*runtime_mutex);
213 *runtime_mutex = NULL;
217 g_static_rec_mutex_init (GStaticRecMutex *mutex)
219 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
221 g_return_if_fail (mutex);
227 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
231 g_return_if_fail (mutex);
233 if (!g_thread_supported ())
236 G_THREAD_UF (thread_self, (&self));
238 if (g_system_thread_equal (self, mutex->owner))
243 g_static_mutex_lock (&mutex->mutex);
244 g_system_thread_assign (mutex->owner, self);
249 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
253 g_return_val_if_fail (mutex, FALSE);
255 if (!g_thread_supported ())
258 G_THREAD_UF (thread_self, (&self));
260 if (g_system_thread_equal (self, mutex->owner))
266 if (!g_static_mutex_trylock (&mutex->mutex))
269 g_system_thread_assign (mutex->owner, self);
275 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
277 g_return_if_fail (mutex);
279 if (!g_thread_supported ())
282 if (mutex->depth > 1)
287 g_system_thread_assign (mutex->owner, zero_thread);
288 g_static_mutex_unlock (&mutex->mutex);
292 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
296 g_return_if_fail (mutex);
298 if (!g_thread_supported ())
301 G_THREAD_UF (thread_self, (&self));
303 if (g_system_thread_equal (self, mutex->owner))
305 mutex->depth += depth;
308 g_static_mutex_lock (&mutex->mutex);
309 g_system_thread_assign (mutex->owner, self);
310 mutex->depth = depth;
314 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
318 g_return_val_if_fail (mutex, 0);
320 if (!g_thread_supported ())
323 depth = mutex->depth;
325 g_system_thread_assign (mutex->owner, zero_thread);
327 g_static_mutex_unlock (&mutex->mutex);
333 g_static_rec_mutex_free (GStaticRecMutex *mutex)
335 g_return_if_fail (mutex);
337 g_static_mutex_free (&mutex->mutex);
341 g_static_private_init (GStaticPrivate *private_key)
343 private_key->index = 0;
347 g_static_private_get (GStaticPrivate *private_key)
349 GRealThread *self = (GRealThread*) g_thread_self ();
352 array = self->private_data;
356 if (!private_key->index)
358 else if (private_key->index <= array->len)
359 return g_array_index (array, GStaticPrivateNode,
360 private_key->index - 1).data;
366 g_static_private_set (GStaticPrivate *private_key,
368 GDestroyNotify notify)
370 GRealThread *self = (GRealThread*) g_thread_self ();
372 static guint next_index = 0;
373 GStaticPrivateNode *node;
375 array = self->private_data;
378 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
379 self->private_data = array;
382 if (!private_key->index)
386 if (!private_key->index)
388 if (g_thread_free_indeces)
391 GPOINTER_TO_UINT (g_thread_free_indeces->data);
392 g_thread_free_indeces =
393 g_slist_delete_link (g_thread_free_indeces,
394 g_thread_free_indeces);
397 private_key->index = ++next_index;
403 if (private_key->index > array->len)
404 g_array_set_size (array, private_key->index);
406 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
409 gpointer ddata = node->data;
410 GDestroyNotify ddestroy = node->destroy;
413 node->destroy = notify;
420 node->destroy = notify;
425 g_static_private_free (GStaticPrivate *private_key)
427 guint index = private_key->index;
433 private_key->index = 0;
436 list = g_thread_all_threads;
439 GRealThread *thread = list->data;
440 GArray *array = thread->private_data;
443 if (array && index <= array->len)
445 GStaticPrivateNode *node = &g_array_index (array,
448 gpointer ddata = node->data;
449 GDestroyNotify ddestroy = node->destroy;
452 node->destroy = NULL;
462 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
463 GUINT_TO_POINTER (index));
468 g_thread_cleanup (gpointer data)
472 GRealThread* thread = data;
473 if (thread->private_data)
475 GArray* array = thread->private_data;
478 for (i = 0; i < array->len; i++ )
480 GStaticPrivateNode *node =
481 &g_array_index (array, GStaticPrivateNode, i);
483 node->destroy (node->data);
485 g_array_free (array, TRUE);
488 /* We only free the thread structure, if it isn't joinable. If
489 it is, the structure is freed in g_thread_join */
490 if (!thread->thread.joinable)
493 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
496 /* Just to make sure, this isn't used any more */
497 g_system_thread_assign (thread->system_thread, zero_thread);
506 g_error ("The thread system is not yet initialized.");
510 g_thread_create_proxy (gpointer data)
512 GRealThread* thread = data;
516 #ifdef G_THREAD_USE_PID_SURROGATE
517 thread->pid = getpid ();
518 #endif /* G_THREAD_USE_PID_SURROGATE */
520 /* This has to happen before G_LOCK, as that might call g_thread_self */
521 g_private_set (g_thread_specific_private, data);
523 /* the lock makes sure, that thread->system_thread is written,
524 before thread->thread.func is called. See g_thread_create. */
528 #ifdef G_THREAD_USE_PID_SURROGATE
529 if (g_thread_use_default_impl)
530 SET_PRIO (thread->pid, thread->thread.priority);
531 #endif /* G_THREAD_USE_PID_SURROGATE */
533 thread->retval = thread->thread.func (thread->thread.data);
539 g_thread_create_full (GThreadFunc func,
544 GThreadPriority priority,
547 GRealThread* result = g_new (GRealThread, 1);
548 GError *local_error = NULL;
549 g_return_val_if_fail (func, NULL);
550 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
551 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
553 result->thread.joinable = joinable;
554 result->thread.priority = priority;
555 result->thread.func = func;
556 result->thread.data = data;
557 result->private_data = NULL;
559 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
560 stack_size, joinable, bound, priority,
561 &result->system_thread, &local_error));
562 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
567 g_propagate_error (error, local_error);
572 return (GThread*) result;
576 g_thread_exit (gpointer retval)
578 GRealThread* real = (GRealThread*) g_thread_self ();
579 real->retval = retval;
580 G_THREAD_CF (thread_exit, (void)0, ());
584 g_thread_join (GThread* thread)
586 GRealThread* real = (GRealThread*) thread;
589 g_return_val_if_fail (thread, NULL);
590 g_return_val_if_fail (thread->joinable, NULL);
591 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
594 G_THREAD_UF (thread_join, (&real->system_thread));
596 retval = real->retval;
599 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
602 /* Just to make sure, this isn't used any more */
603 thread->joinable = 0;
604 g_system_thread_assign (real->system_thread, zero_thread);
606 /* the thread structure for non-joinable threads is freed upon
607 thread end. We free the memory here. This will leave a loose end,
608 if a joinable thread is not joined. */
616 g_thread_set_priority (GThread* thread,
617 GThreadPriority priority)
619 GRealThread* real = (GRealThread*) thread;
621 g_return_if_fail (thread);
622 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
623 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
624 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
626 thread->priority = priority;
628 #ifdef G_THREAD_USE_PID_SURROGATE
629 if (g_thread_use_default_impl)
630 SET_PRIO (real->pid, priority);
632 #endif /* G_THREAD_USE_PID_SURROGATE */
633 G_THREAD_CF (thread_set_priority, (void)0,
634 (&real->system_thread, priority));
640 GRealThread* thread = g_private_get (g_thread_specific_private);
644 /* If no thread data is available, provide and set one. This
645 can happen for the main thread and for threads, that are not
647 thread = g_new (GRealThread, 1);
648 thread->thread.joinable = FALSE; /* This is a save guess */
649 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
651 thread->thread.func = NULL;
652 thread->thread.data = NULL;
653 thread->private_data = NULL;
655 if (g_thread_supported ())
656 G_THREAD_UF (thread_self, (&thread->system_thread));
658 #ifdef G_THREAD_USE_PID_SURROGATE
659 thread->pid = getpid ();
660 #endif /* G_THREAD_USE_PID_SURROGATE */
662 g_private_set (g_thread_specific_private, thread);
665 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
669 return (GThread*)thread;
673 g_static_rw_lock_init (GStaticRWLock* lock)
675 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
677 g_return_if_fail (lock);
683 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
686 *cond = g_cond_new ();
687 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
691 g_static_rw_lock_signal (GStaticRWLock* lock)
693 if (lock->want_to_write && lock->write_cond)
694 g_cond_signal (lock->write_cond);
695 else if (lock->want_to_read && lock->read_cond)
696 g_cond_broadcast (lock->read_cond);
700 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
702 g_return_if_fail (lock);
704 if (!g_threads_got_initialized)
707 g_static_mutex_lock (&lock->mutex);
708 lock->want_to_read++;
709 while (lock->write || lock->want_to_write)
710 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
711 lock->want_to_read--;
712 lock->read_counter++;
713 g_static_mutex_unlock (&lock->mutex);
717 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
719 gboolean ret_val = FALSE;
721 g_return_val_if_fail (lock, FALSE);
723 if (!g_threads_got_initialized)
726 g_static_mutex_lock (&lock->mutex);
727 if (!lock->write && !lock->want_to_write)
729 lock->read_counter++;
732 g_static_mutex_unlock (&lock->mutex);
737 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
739 g_return_if_fail (lock);
741 if (!g_threads_got_initialized)
744 g_static_mutex_lock (&lock->mutex);
745 lock->read_counter--;
746 if (lock->read_counter == 0)
747 g_static_rw_lock_signal (lock);
748 g_static_mutex_unlock (&lock->mutex);
752 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
754 g_return_if_fail (lock);
756 if (!g_threads_got_initialized)
759 g_static_mutex_lock (&lock->mutex);
760 lock->want_to_write++;
761 while (lock->write || lock->read_counter)
762 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
763 lock->want_to_write--;
765 g_static_mutex_unlock (&lock->mutex);
769 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
771 gboolean ret_val = FALSE;
773 g_return_val_if_fail (lock, FALSE);
775 if (!g_threads_got_initialized)
778 g_static_mutex_lock (&lock->mutex);
779 if (!lock->write && !lock->read_counter)
784 g_static_mutex_unlock (&lock->mutex);
789 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
791 g_return_if_fail (lock);
793 if (!g_threads_got_initialized)
796 g_static_mutex_lock (&lock->mutex);
798 g_static_rw_lock_signal (lock);
799 g_static_mutex_unlock (&lock->mutex);
803 g_static_rw_lock_free (GStaticRWLock* lock)
805 g_return_if_fail (lock);
809 g_cond_free (lock->read_cond);
810 lock->read_cond = NULL;
812 if (lock->write_cond)
814 g_cond_free (lock->write_cond);
815 lock->write_cond = NULL;
817 g_static_mutex_free (&lock->mutex);