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 */
49 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
50 # define g_system_thread_equal(thread1, thread2) \
51 (thread1.dummy_pointer == thread2.dummy_pointer)
52 # define g_system_thread_assign(dest, src) \
53 (dest.dummy_pointer = src.dummy_pointer)
54 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
55 # define g_system_thread_equal(thread1, thread2) \
56 (memcmp (&thread1, &thread2, GLIB_SIZEOF_SYSTEM_THREAD) == 0)
57 # define g_system_thread_assign(dest, src) \
58 (memcpy (&dest, &src, GLIB_SIZEOF_SYSTEM_THREAD))
59 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
62 g_thread_error_quark (void)
66 quark = g_quark_from_static_string ("g_thread_error");
70 typedef struct _GRealThread GRealThread;
76 gpointer private_data;
77 GSystemThread system_thread;
78 #ifdef G_THREAD_USE_PID_SURROGATE
80 #endif /* G_THREAD_USE_PID_SURROGATE */
83 #ifdef G_THREAD_USE_PID_SURROGATE
84 static gint priority_map[] = { 15, 0, -15, -20 };
85 static gboolean prio_warned = FALSE;
86 # define SET_PRIO(pid, prio) G_STMT_START{ \
87 gint error = setpriority (PRIO_PROCESS, (pid), priority_map[prio]); \
88 if (error == -1 && errno == EACCES && !prio_warned) \
91 g_warning ("Priorities can only be increased by root."); \
94 #endif /* G_THREAD_USE_PID_SURROGATE */
96 typedef struct _GStaticPrivateNode GStaticPrivateNode;
97 struct _GStaticPrivateNode
100 GDestroyNotify destroy;
103 static void g_thread_cleanup (gpointer data);
104 static void g_thread_fail (void);
106 /* Global variables */
108 static GSystemThread zero_thread; /* This is initialized to all zero */
109 gboolean g_thread_use_default_impl = TRUE;
110 gboolean g_threads_got_initialized = FALSE;
112 #if defined(G_OS_WIN32) && defined(__GNUC__)
113 __declspec(dllexport)
115 GThreadFunctions g_thread_functions_for_glib_use = {
116 (GMutex*(*)())g_thread_fail, /* mutex_new */
117 NULL, /* mutex_lock */
118 NULL, /* mutex_trylock */
119 NULL, /* mutex_unlock */
120 NULL, /* mutex_free */
121 (GCond*(*)())g_thread_fail, /* cond_new */
122 NULL, /* cond_signal */
123 NULL, /* cond_broadcast */
124 NULL, /* cond_wait */
125 NULL, /* cond_timed_wait */
126 NULL, /* cond_free */
127 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
128 NULL, /* private_get */
129 NULL, /* private_set */
130 (void(*)(GThreadFunc, gpointer, gulong,
131 gboolean, gboolean, GThreadPriority,
132 gpointer, GError**))g_thread_fail, /* thread_create */
133 NULL, /* thread_yield */
134 NULL, /* thread_join */
135 NULL, /* thread_exit */
136 NULL, /* thread_set_priority */
137 NULL /* thread_self */
142 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
143 static GPrivate *g_thread_specific_private = NULL;
144 static GSList *g_thread_all_threads = NULL;
145 static GSList *g_thread_free_indeces = NULL;
147 G_LOCK_DEFINE_STATIC (g_thread);
149 /* This must be called only once, before any threads are created.
150 * It will only be called from g_thread_init() in -lgthread.
155 GRealThread* main_thread;
157 /* We let the main thread (the one that calls g_thread_init) inherit
158 * the data, that it set before calling g_thread_init
160 main_thread = (GRealThread*) g_thread_self ();
162 g_thread_specific_private = g_private_new (g_thread_cleanup);
163 G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
164 G_THREAD_UF (thread_self, (&main_thread->system_thread));
166 g_mutex_protect_static_mutex_allocation = g_mutex_new ();
170 g_static_mutex_init (GStaticMutex *mutex)
172 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
174 g_return_if_fail (mutex);
176 memcpy (mutex, &init_mutex, sizeof (GStaticMutex));
180 g_static_mutex_get_mutex_impl (GMutex** mutex)
182 if (!g_thread_supported ())
185 g_assert (g_mutex_protect_static_mutex_allocation);
187 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
190 *mutex = g_mutex_new ();
192 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
198 g_static_mutex_free (GStaticMutex* mutex)
200 GMutex **runtime_mutex;
202 g_return_if_fail (mutex);
204 /* The runtime_mutex is the first (or only) member of GStaticMutex,
205 * see both versions (of glibconfig.h) in configure.in */
206 runtime_mutex = ((GMutex**)mutex);
209 g_mutex_free (*runtime_mutex);
211 *runtime_mutex = NULL;
215 g_static_rec_mutex_init (GStaticRecMutex *mutex)
217 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
219 g_return_if_fail (mutex);
221 memcpy (mutex, &init_mutex, sizeof (GStaticRecMutex));
225 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
229 g_return_if_fail (mutex);
231 if (!g_thread_supported ())
234 G_THREAD_UF (thread_self, (&self));
236 if (g_system_thread_equal (self, mutex->owner))
241 g_static_mutex_lock (&mutex->mutex);
242 g_system_thread_assign (mutex->owner, self);
247 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
251 g_return_val_if_fail (mutex, FALSE);
253 if (!g_thread_supported ())
256 G_THREAD_UF (thread_self, (&self));
258 if (g_system_thread_equal (self, mutex->owner))
264 if (!g_static_mutex_trylock (&mutex->mutex))
267 g_system_thread_assign (mutex->owner, self);
273 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
275 g_return_if_fail (mutex);
277 if (!g_thread_supported ())
280 if (mutex->depth > 1)
285 g_system_thread_assign (mutex->owner, zero_thread);
286 g_static_mutex_unlock (&mutex->mutex);
290 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
293 g_return_if_fail (mutex);
295 if (!g_thread_supported ())
298 g_static_mutex_lock (&mutex->mutex);
299 G_THREAD_UF (thread_self, (&mutex->owner));
300 mutex->depth = depth;
304 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
308 g_return_val_if_fail (mutex, 0);
310 if (!g_thread_supported ())
313 depth = mutex->depth;
315 g_system_thread_assign (mutex->owner, zero_thread);
317 g_static_mutex_unlock (&mutex->mutex);
323 g_static_rec_mutex_free (GStaticRecMutex *mutex)
325 g_return_if_fail (mutex);
327 g_static_mutex_free (&mutex->mutex);
331 g_static_private_init (GStaticPrivate *private_key)
333 private_key->index = 0;
337 g_static_private_get (GStaticPrivate *private_key)
339 return g_static_private_get_for_thread (private_key, g_thread_self ());
343 g_static_private_get_for_thread (GStaticPrivate *private_key,
347 GRealThread *self = (GRealThread*) thread;
349 g_return_val_if_fail (thread, NULL);
351 array = self->private_data;
355 if (!private_key->index)
357 else if (private_key->index <= array->len)
358 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
364 g_static_private_set (GStaticPrivate *private_key,
366 GDestroyNotify notify)
368 g_static_private_set_for_thread (private_key, g_thread_self (),
373 g_static_private_set_for_thread (GStaticPrivate *private_key,
376 GDestroyNotify notify)
379 GRealThread *self =(GRealThread*) thread;
380 static guint next_index = 0;
381 GStaticPrivateNode *node;
383 g_return_if_fail (thread);
385 array = self->private_data;
388 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
389 self->private_data = array;
392 if (!private_key->index)
396 if (!private_key->index)
398 if (g_thread_free_indeces)
401 GPOINTER_TO_UINT (g_thread_free_indeces->data);
402 g_thread_free_indeces =
403 g_slist_delete_link (g_thread_free_indeces,
404 g_thread_free_indeces);
407 private_key->index = ++next_index;
413 if (private_key->index > array->len)
414 g_array_set_size (array, private_key->index);
416 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
419 gpointer ddata = node->data;
420 GDestroyNotify ddestroy = node->destroy;
423 node->destroy = notify;
430 node->destroy = notify;
435 g_static_private_free (GStaticPrivate *private_key)
437 GStaticPrivate copied_key;
440 copied_key.index = private_key->index;
441 private_key->index = 0;
443 if (!copied_key.index)
447 list = g_thread_all_threads;
450 GThread *thread = list->data;
454 g_static_private_set_for_thread (&copied_key, thread, NULL, NULL);
457 g_thread_free_indeces =
458 g_slist_prepend (g_thread_free_indeces,
459 GUINT_TO_POINTER (copied_key.index));
464 g_thread_cleanup (gpointer data)
468 GRealThread* thread = data;
469 if (thread->private_data)
471 GArray* array = thread->private_data;
474 for (i = 0; i < array->len; i++ )
476 GStaticPrivateNode *node =
477 &g_array_index (array, GStaticPrivateNode, i);
479 node->destroy (node->data);
481 g_array_free (array, TRUE);
483 /* We only free the thread structure, if it isn't joinable. If
484 it is, the structure is freed in g_thread_join */
485 if (!thread->thread.joinable)
488 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
491 /* Just to make sure, this isn't used any more */
492 g_system_thread_assign (thread->system_thread, zero_thread);
501 g_error ("The thread system is not yet initialized.");
505 g_thread_create_proxy (gpointer data)
507 GRealThread* thread = data;
511 #ifdef G_THREAD_USE_PID_SURROGATE
512 thread->pid = getpid ();
513 #endif /* G_THREAD_USE_PID_SURROGATE */
515 /* This has to happen before G_LOCK, as that might call g_thread_self */
516 g_private_set (g_thread_specific_private, data);
518 /* the lock makes sure, that thread->system_thread is written,
519 before thread->func is called. See g_thread_create. */
523 if (g_thread_use_default_impl)
524 SET_PRIO (thread->pid, thread->thread.priority);
526 thread->func (thread->arg);
530 g_thread_create (GThreadFunc thread_func,
535 GThreadPriority priority,
538 GRealThread* result = g_new (GRealThread, 1);
539 GError *local_error = NULL;
540 g_return_val_if_fail (thread_func, NULL);
541 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
542 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
544 result->thread.joinable = joinable;
545 result->thread.bound = bound;
546 result->thread.priority = priority;
547 result->func = thread_func;
549 result->private_data = NULL;
551 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
552 stack_size, joinable, bound, priority,
553 &result->system_thread, &local_error));
554 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
559 g_propagate_error (error, local_error);
564 return (GThread*) result;
568 g_thread_join (GThread* thread)
570 GRealThread* real = (GRealThread*) thread;
573 g_return_if_fail (thread);
574 g_return_if_fail (thread->joinable);
575 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
577 G_THREAD_UF (thread_join, (&real->system_thread));
580 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
583 /* Just to make sure, this isn't used any more */
584 thread->joinable = 0;
585 g_system_thread_assign (real->system_thread, zero_thread);
587 /* the thread structure for non-joinable threads is freed upon
588 thread end. We free the memory here. This will leave loose end,
589 if a joinable thread is not joined. */
595 g_thread_set_priority (GThread* thread,
596 GThreadPriority priority)
598 GRealThread* real = (GRealThread*) thread;
600 g_return_if_fail (thread);
601 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
602 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
603 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
605 thread->priority = priority;
607 #ifdef G_THREAD_USE_PID_SURROGATE
608 if (g_thread_use_default_impl)
609 SET_PRIO (real->pid, priority);
611 #endif /* G_THREAD_USE_PID_SURROGATE */
612 G_THREAD_CF (thread_set_priority, (void)0,
613 (&real->system_thread, priority));
619 GRealThread* thread = g_private_get (g_thread_specific_private);
623 /* If no thread data is available, provide and set one. This
624 can happen for the main thread and for threads, that are not
626 thread = g_new (GRealThread, 1);
627 thread->thread.joinable = FALSE; /* This is a save guess */
628 thread->thread.bound = TRUE; /* This isn't important at all */
629 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
633 thread->private_data = NULL;
635 if (g_thread_supported ())
636 G_THREAD_UF (thread_self, (&thread->system_thread));
638 #ifdef G_THREAD_USE_PID_SURROGATE
639 thread->pid = getpid ();
640 #endif /* G_THREAD_USE_PID_SURROGATE */
642 g_private_set (g_thread_specific_private, thread);
645 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
649 return (GThread*)thread;
653 g_static_rw_lock_init (GStaticRWLock* lock)
655 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
657 g_return_if_fail (lock);
659 memcpy (lock, &init_lock, sizeof (GStaticRWLock));
663 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
666 *cond = g_cond_new ();
667 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
671 g_static_rw_lock_signal (GStaticRWLock* lock)
673 if (lock->want_to_write && lock->write_cond)
674 g_cond_signal (lock->write_cond);
675 else if (lock->read_cond)
676 g_cond_broadcast (lock->read_cond);
680 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
682 g_return_if_fail (lock);
684 if (!g_threads_got_initialized)
687 g_static_mutex_lock (&lock->mutex);
688 while (lock->write || lock->want_to_write)
689 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
690 lock->read_counter++;
691 g_static_mutex_unlock (&lock->mutex);
695 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
697 gboolean ret_val = FALSE;
699 g_return_val_if_fail (lock, FALSE);
701 if (!g_threads_got_initialized)
704 g_static_mutex_lock (&lock->mutex);
705 if (!lock->write && !lock->want_to_write)
707 lock->read_counter++;
710 g_static_mutex_unlock (&lock->mutex);
715 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
717 g_return_if_fail (lock);
719 if (!g_threads_got_initialized)
722 g_static_mutex_lock (&lock->mutex);
723 lock->read_counter--;
724 if (lock->read_counter == 0)
725 g_static_rw_lock_signal (lock);
726 g_static_mutex_unlock (&lock->mutex);
730 g_static_rw_lock_writer_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_write++;
739 while (lock->write || lock->read_counter)
740 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
741 lock->want_to_write--;
743 g_static_mutex_unlock (&lock->mutex);
747 g_static_rw_lock_writer_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->write && !lock->read_counter)
762 g_static_mutex_unlock (&lock->mutex);
767 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
769 g_return_if_fail (lock);
771 if (!g_threads_got_initialized)
774 g_static_mutex_lock (&lock->mutex);
776 g_static_rw_lock_signal (lock);
777 g_static_mutex_unlock (&lock->mutex);
781 g_static_rw_lock_free (GStaticRWLock* lock)
783 g_return_if_fail (lock);
787 g_cond_free (lock->read_cond);
788 lock->read_cond = NULL;
790 if (lock->write_cond)
792 g_cond_free (lock->write_cond);
793 lock->write_cond = NULL;
795 g_static_mutex_free (&lock->mutex);