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 typedef struct _GRealThread GRealThread;
78 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_OS_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);
178 memcpy (mutex, &init_mutex, sizeof (GStaticMutex));
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);
223 memcpy (mutex, &init_mutex, sizeof (GStaticRecMutex));
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 return g_static_private_get_for_thread (private_key, g_thread_self ());
353 g_static_private_get_for_thread (GStaticPrivate *private_key,
357 GRealThread *self = (GRealThread*) thread;
359 g_return_val_if_fail (thread, NULL);
361 array = self->private_data;
365 if (!private_key->index)
367 else if (private_key->index <= array->len)
368 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
374 g_static_private_set (GStaticPrivate *private_key,
376 GDestroyNotify notify)
378 g_static_private_set_for_thread (private_key, g_thread_self (),
383 g_static_private_set_for_thread (GStaticPrivate *private_key,
386 GDestroyNotify notify)
389 GRealThread *self =(GRealThread*) thread;
390 static guint next_index = 0;
391 GStaticPrivateNode *node;
393 g_return_if_fail (thread);
395 array = self->private_data;
398 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
399 self->private_data = array;
402 if (!private_key->index)
406 if (!private_key->index)
408 if (g_thread_free_indeces)
411 GPOINTER_TO_UINT (g_thread_free_indeces->data);
412 g_thread_free_indeces =
413 g_slist_delete_link (g_thread_free_indeces,
414 g_thread_free_indeces);
417 private_key->index = ++next_index;
423 if (private_key->index > array->len)
424 g_array_set_size (array, private_key->index);
426 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
429 gpointer ddata = node->data;
430 GDestroyNotify ddestroy = node->destroy;
433 node->destroy = notify;
440 node->destroy = notify;
445 g_static_private_free (GStaticPrivate *private_key)
447 GStaticPrivate copied_key;
450 copied_key.index = private_key->index;
451 private_key->index = 0;
453 if (!copied_key.index)
457 list = g_thread_all_threads;
460 GThread *thread = list->data;
464 g_static_private_set_for_thread (&copied_key, thread, NULL, NULL);
467 g_thread_free_indeces =
468 g_slist_prepend (g_thread_free_indeces,
469 GUINT_TO_POINTER (copied_key.index));
474 g_thread_cleanup (gpointer data)
478 GRealThread* thread = data;
479 if (thread->private_data)
481 GArray* array = thread->private_data;
484 for (i = 0; i < array->len; i++ )
486 GStaticPrivateNode *node =
487 &g_array_index (array, GStaticPrivateNode, i);
489 node->destroy (node->data);
491 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->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->func (thread->arg);
542 g_thread_create (GThreadFunc thread_func,
547 GThreadPriority priority,
550 GRealThread* result = g_new (GRealThread, 1);
551 GError *local_error = NULL;
552 g_return_val_if_fail (thread_func, NULL);
553 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
554 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
556 result->thread.joinable = joinable;
557 result->thread.bound = bound;
558 result->thread.priority = priority;
559 result->func = thread_func;
561 result->private_data = NULL;
563 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
564 stack_size, joinable, bound, priority,
565 &result->system_thread, &local_error));
566 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
571 g_propagate_error (error, local_error);
576 return (GThread*) result;
580 g_thread_join (GThread* thread)
582 GRealThread* real = (GRealThread*) thread;
585 g_return_if_fail (thread);
586 g_return_if_fail (thread->joinable);
587 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
589 G_THREAD_UF (thread_join, (&real->system_thread));
592 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
595 /* Just to make sure, this isn't used any more */
596 thread->joinable = 0;
597 g_system_thread_assign (real->system_thread, zero_thread);
599 /* the thread structure for non-joinable threads is freed upon
600 thread end. We free the memory here. This will leave loose end,
601 if a joinable thread is not joined. */
607 g_thread_set_priority (GThread* thread,
608 GThreadPriority priority)
610 GRealThread* real = (GRealThread*) thread;
612 g_return_if_fail (thread);
613 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
614 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
615 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
617 thread->priority = priority;
619 #ifdef G_THREAD_USE_PID_SURROGATE
620 if (g_thread_use_default_impl)
621 SET_PRIO (real->pid, priority);
623 #endif /* G_THREAD_USE_PID_SURROGATE */
624 G_THREAD_CF (thread_set_priority, (void)0,
625 (&real->system_thread, priority));
631 GRealThread* thread = g_private_get (g_thread_specific_private);
635 /* If no thread data is available, provide and set one. This
636 can happen for the main thread and for threads, that are not
638 thread = g_new (GRealThread, 1);
639 thread->thread.joinable = FALSE; /* This is a save guess */
640 thread->thread.bound = TRUE; /* This isn't important at all */
641 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
645 thread->private_data = NULL;
647 if (g_thread_supported ())
648 G_THREAD_UF (thread_self, (&thread->system_thread));
650 #ifdef G_THREAD_USE_PID_SURROGATE
651 thread->pid = getpid ();
652 #endif /* G_THREAD_USE_PID_SURROGATE */
654 g_private_set (g_thread_specific_private, thread);
657 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
661 return (GThread*)thread;
665 g_static_rw_lock_init (GStaticRWLock* lock)
667 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
669 g_return_if_fail (lock);
671 memcpy (lock, &init_lock, sizeof (GStaticRWLock));
675 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
678 *cond = g_cond_new ();
679 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
683 g_static_rw_lock_signal (GStaticRWLock* lock)
685 if (lock->want_to_write && lock->write_cond)
686 g_cond_signal (lock->write_cond);
687 else if (lock->read_cond)
688 g_cond_broadcast (lock->read_cond);
692 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
694 g_return_if_fail (lock);
696 if (!g_threads_got_initialized)
699 g_static_mutex_lock (&lock->mutex);
700 while (lock->write || lock->want_to_write)
701 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
702 lock->read_counter++;
703 g_static_mutex_unlock (&lock->mutex);
707 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
709 gboolean ret_val = FALSE;
711 g_return_val_if_fail (lock, FALSE);
713 if (!g_threads_got_initialized)
716 g_static_mutex_lock (&lock->mutex);
717 if (!lock->write && !lock->want_to_write)
719 lock->read_counter++;
722 g_static_mutex_unlock (&lock->mutex);
727 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
729 g_return_if_fail (lock);
731 if (!g_threads_got_initialized)
734 g_static_mutex_lock (&lock->mutex);
735 lock->read_counter--;
736 if (lock->read_counter == 0)
737 g_static_rw_lock_signal (lock);
738 g_static_mutex_unlock (&lock->mutex);
742 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
744 g_return_if_fail (lock);
746 if (!g_threads_got_initialized)
749 g_static_mutex_lock (&lock->mutex);
750 lock->want_to_write++;
751 while (lock->write || lock->read_counter)
752 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
753 lock->want_to_write--;
755 g_static_mutex_unlock (&lock->mutex);
759 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
761 gboolean ret_val = FALSE;
763 g_return_val_if_fail (lock, FALSE);
765 if (!g_threads_got_initialized)
768 g_static_mutex_lock (&lock->mutex);
769 if (!lock->write && !lock->read_counter)
774 g_static_mutex_unlock (&lock->mutex);
779 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
781 g_return_if_fail (lock);
783 if (!g_threads_got_initialized)
786 g_static_mutex_lock (&lock->mutex);
788 g_static_rw_lock_signal (lock);
789 g_static_mutex_unlock (&lock->mutex);
793 g_static_rw_lock_free (GStaticRWLock* lock)
795 g_return_if_fail (lock);
799 g_cond_free (lock->read_cond);
800 lock->read_cond = NULL;
802 if (lock->write_cond)
804 g_cond_free (lock->write_cond);
805 lock->write_cond = NULL;
807 g_static_mutex_free (&lock->mutex);