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,
295 g_return_if_fail (mutex);
297 if (!g_thread_supported ())
300 g_static_mutex_lock (&mutex->mutex);
301 G_THREAD_UF (thread_self, (&mutex->owner));
302 mutex->depth = depth;
306 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
310 g_return_val_if_fail (mutex, 0);
312 if (!g_thread_supported ())
315 depth = mutex->depth;
317 g_system_thread_assign (mutex->owner, zero_thread);
319 g_static_mutex_unlock (&mutex->mutex);
325 g_static_rec_mutex_free (GStaticRecMutex *mutex)
327 g_return_if_fail (mutex);
329 g_static_mutex_free (&mutex->mutex);
333 g_static_private_init (GStaticPrivate *private_key)
335 private_key->index = 0;
339 g_static_private_get (GStaticPrivate *private_key)
341 return g_static_private_get_for_thread (private_key, g_thread_self ());
345 g_static_private_get_for_thread (GStaticPrivate *private_key,
349 GRealThread *self = (GRealThread*) thread;
351 g_return_val_if_fail (thread, NULL);
353 array = self->private_data;
357 if (!private_key->index)
359 else if (private_key->index <= array->len)
360 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
366 g_static_private_set (GStaticPrivate *private_key,
368 GDestroyNotify notify)
370 g_static_private_set_for_thread (private_key, g_thread_self (),
375 g_static_private_set_for_thread (GStaticPrivate *private_key,
378 GDestroyNotify notify)
381 GRealThread *self =(GRealThread*) thread;
382 static guint next_index = 0;
383 GStaticPrivateNode *node;
385 g_return_if_fail (thread);
387 array = self->private_data;
390 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
391 self->private_data = array;
394 if (!private_key->index)
398 if (!private_key->index)
400 if (g_thread_free_indeces)
403 GPOINTER_TO_UINT (g_thread_free_indeces->data);
404 g_thread_free_indeces =
405 g_slist_delete_link (g_thread_free_indeces,
406 g_thread_free_indeces);
409 private_key->index = ++next_index;
415 if (private_key->index > array->len)
416 g_array_set_size (array, private_key->index);
418 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
421 gpointer ddata = node->data;
422 GDestroyNotify ddestroy = node->destroy;
425 node->destroy = notify;
432 node->destroy = notify;
437 g_static_private_free (GStaticPrivate *private_key)
439 GStaticPrivate copied_key;
442 copied_key.index = private_key->index;
443 private_key->index = 0;
445 if (!copied_key.index)
449 list = g_thread_all_threads;
452 GThread *thread = list->data;
456 g_static_private_set_for_thread (&copied_key, thread, NULL, NULL);
459 g_thread_free_indeces =
460 g_slist_prepend (g_thread_free_indeces,
461 GUINT_TO_POINTER (copied_key.index));
466 g_thread_cleanup (gpointer data)
470 GRealThread* thread = data;
471 if (thread->private_data)
473 GArray* array = thread->private_data;
476 for (i = 0; i < array->len; i++ )
478 GStaticPrivateNode *node =
479 &g_array_index (array, GStaticPrivateNode, i);
481 node->destroy (node->data);
483 g_array_free (array, TRUE);
485 /* We only free the thread structure, if it isn't joinable. If
486 it is, the structure is freed in g_thread_join */
487 if (!thread->thread.joinable)
490 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
493 /* Just to make sure, this isn't used any more */
494 g_system_thread_assign (thread->system_thread, zero_thread);
503 g_error ("The thread system is not yet initialized.");
507 g_thread_create_proxy (gpointer data)
509 GRealThread* thread = data;
513 #ifdef G_THREAD_USE_PID_SURROGATE
514 thread->pid = getpid ();
515 #endif /* G_THREAD_USE_PID_SURROGATE */
517 /* This has to happen before G_LOCK, as that might call g_thread_self */
518 g_private_set (g_thread_specific_private, data);
520 /* the lock makes sure, that thread->system_thread is written,
521 before thread->func is called. See g_thread_create. */
525 #ifdef G_THREAD_USE_PID_SURROGATE
526 if (g_thread_use_default_impl)
527 SET_PRIO (thread->pid, thread->thread.priority);
528 #endif /* G_THREAD_USE_PID_SURROGATE */
530 thread->func (thread->arg);
534 g_thread_create (GThreadFunc thread_func,
539 GThreadPriority priority,
542 GRealThread* result = g_new (GRealThread, 1);
543 GError *local_error = NULL;
544 g_return_val_if_fail (thread_func, NULL);
545 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
546 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
548 result->thread.joinable = joinable;
549 result->thread.bound = bound;
550 result->thread.priority = priority;
551 result->func = thread_func;
553 result->private_data = NULL;
555 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
556 stack_size, joinable, bound, priority,
557 &result->system_thread, &local_error));
558 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
563 g_propagate_error (error, local_error);
568 return (GThread*) result;
572 g_thread_join (GThread* thread)
574 GRealThread* real = (GRealThread*) thread;
577 g_return_if_fail (thread);
578 g_return_if_fail (thread->joinable);
579 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
581 G_THREAD_UF (thread_join, (&real->system_thread));
584 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
587 /* Just to make sure, this isn't used any more */
588 thread->joinable = 0;
589 g_system_thread_assign (real->system_thread, zero_thread);
591 /* the thread structure for non-joinable threads is freed upon
592 thread end. We free the memory here. This will leave loose end,
593 if a joinable thread is not joined. */
599 g_thread_set_priority (GThread* thread,
600 GThreadPriority priority)
602 GRealThread* real = (GRealThread*) thread;
604 g_return_if_fail (thread);
605 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
606 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
607 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
609 thread->priority = priority;
611 #ifdef G_THREAD_USE_PID_SURROGATE
612 if (g_thread_use_default_impl)
613 SET_PRIO (real->pid, priority);
615 #endif /* G_THREAD_USE_PID_SURROGATE */
616 G_THREAD_CF (thread_set_priority, (void)0,
617 (&real->system_thread, priority));
623 GRealThread* thread = g_private_get (g_thread_specific_private);
627 /* If no thread data is available, provide and set one. This
628 can happen for the main thread and for threads, that are not
630 thread = g_new (GRealThread, 1);
631 thread->thread.joinable = FALSE; /* This is a save guess */
632 thread->thread.bound = TRUE; /* This isn't important at all */
633 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
637 thread->private_data = NULL;
639 if (g_thread_supported ())
640 G_THREAD_UF (thread_self, (&thread->system_thread));
642 #ifdef G_THREAD_USE_PID_SURROGATE
643 thread->pid = getpid ();
644 #endif /* G_THREAD_USE_PID_SURROGATE */
646 g_private_set (g_thread_specific_private, thread);
649 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
653 return (GThread*)thread;
657 g_static_rw_lock_init (GStaticRWLock* lock)
659 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
661 g_return_if_fail (lock);
663 memcpy (lock, &init_lock, sizeof (GStaticRWLock));
667 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
670 *cond = g_cond_new ();
671 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
675 g_static_rw_lock_signal (GStaticRWLock* lock)
677 if (lock->want_to_write && lock->write_cond)
678 g_cond_signal (lock->write_cond);
679 else if (lock->read_cond)
680 g_cond_broadcast (lock->read_cond);
684 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
686 g_return_if_fail (lock);
688 if (!g_threads_got_initialized)
691 g_static_mutex_lock (&lock->mutex);
692 while (lock->write || lock->want_to_write)
693 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
694 lock->read_counter++;
695 g_static_mutex_unlock (&lock->mutex);
699 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
701 gboolean ret_val = FALSE;
703 g_return_val_if_fail (lock, FALSE);
705 if (!g_threads_got_initialized)
708 g_static_mutex_lock (&lock->mutex);
709 if (!lock->write && !lock->want_to_write)
711 lock->read_counter++;
714 g_static_mutex_unlock (&lock->mutex);
719 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
721 g_return_if_fail (lock);
723 if (!g_threads_got_initialized)
726 g_static_mutex_lock (&lock->mutex);
727 lock->read_counter--;
728 if (lock->read_counter == 0)
729 g_static_rw_lock_signal (lock);
730 g_static_mutex_unlock (&lock->mutex);
734 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
736 g_return_if_fail (lock);
738 if (!g_threads_got_initialized)
741 g_static_mutex_lock (&lock->mutex);
742 lock->want_to_write++;
743 while (lock->write || lock->read_counter)
744 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
745 lock->want_to_write--;
747 g_static_mutex_unlock (&lock->mutex);
751 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
753 gboolean ret_val = FALSE;
755 g_return_val_if_fail (lock, FALSE);
757 if (!g_threads_got_initialized)
760 g_static_mutex_lock (&lock->mutex);
761 if (!lock->write && !lock->read_counter)
766 g_static_mutex_unlock (&lock->mutex);
771 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
773 g_return_if_fail (lock);
775 if (!g_threads_got_initialized)
778 g_static_mutex_lock (&lock->mutex);
780 g_static_rw_lock_signal (lock);
781 g_static_mutex_unlock (&lock->mutex);
785 g_static_rw_lock_free (GStaticRWLock* lock)
787 g_return_if_fail (lock);
791 g_cond_free (lock->read_cond);
792 lock->read_cond = NULL;
794 if (lock->write_cond)
796 g_cond_free (lock->write_cond);
797 lock->write_cond = NULL;
799 g_static_mutex_free (&lock->mutex);