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[4];
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 ();
175 #ifdef G_THREAD_USE_PID_SURROGATE
176 priority_map[G_THREAD_PRIORITY_NORMAL] =
177 getpriority (PRIO_PROCESS, (getpid ()));
178 priority_map[G_THREAD_PRIORITY_LOW] =
179 MIN (20, priority_map[G_THREAD_PRIORITY_NORMAL] + 10);
180 priority_map[G_THREAD_PRIORITY_HIGH] =
181 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 10);
182 priority_map[G_THREAD_PRIORITY_URGENT] =
183 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 15);
184 #endif /* G_THREAD_USE_PID_SURROGATE */
188 g_static_mutex_init (GStaticMutex *mutex)
190 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
192 g_return_if_fail (mutex);
198 g_static_mutex_get_mutex_impl (GMutex** mutex)
200 if (!g_thread_supported ())
203 g_assert (g_mutex_protect_static_mutex_allocation);
205 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
208 *mutex = g_mutex_new ();
210 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
216 g_static_mutex_free (GStaticMutex* mutex)
218 GMutex **runtime_mutex;
220 g_return_if_fail (mutex);
222 /* The runtime_mutex is the first (or only) member of GStaticMutex,
223 * see both versions (of glibconfig.h) in configure.in */
224 runtime_mutex = ((GMutex**)mutex);
227 g_mutex_free (*runtime_mutex);
229 *runtime_mutex = NULL;
233 g_static_rec_mutex_init (GStaticRecMutex *mutex)
235 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
237 g_return_if_fail (mutex);
243 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
247 g_return_if_fail (mutex);
249 if (!g_thread_supported ())
252 G_THREAD_UF (thread_self, (&self));
254 if (g_system_thread_equal (self, mutex->owner))
259 g_static_mutex_lock (&mutex->mutex);
260 g_system_thread_assign (mutex->owner, self);
265 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
269 g_return_val_if_fail (mutex, FALSE);
271 if (!g_thread_supported ())
274 G_THREAD_UF (thread_self, (&self));
276 if (g_system_thread_equal (self, mutex->owner))
282 if (!g_static_mutex_trylock (&mutex->mutex))
285 g_system_thread_assign (mutex->owner, self);
291 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
293 g_return_if_fail (mutex);
295 if (!g_thread_supported ())
298 if (mutex->depth > 1)
303 g_system_thread_assign (mutex->owner, zero_thread);
304 g_static_mutex_unlock (&mutex->mutex);
308 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
312 g_return_if_fail (mutex);
314 if (!g_thread_supported ())
317 G_THREAD_UF (thread_self, (&self));
319 if (g_system_thread_equal (self, mutex->owner))
321 mutex->depth += depth;
324 g_static_mutex_lock (&mutex->mutex);
325 g_system_thread_assign (mutex->owner, self);
326 mutex->depth = depth;
330 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
334 g_return_val_if_fail (mutex, 0);
336 if (!g_thread_supported ())
339 depth = mutex->depth;
341 g_system_thread_assign (mutex->owner, zero_thread);
343 g_static_mutex_unlock (&mutex->mutex);
349 g_static_rec_mutex_free (GStaticRecMutex *mutex)
351 g_return_if_fail (mutex);
353 g_static_mutex_free (&mutex->mutex);
357 g_static_private_init (GStaticPrivate *private_key)
359 private_key->index = 0;
363 g_static_private_get (GStaticPrivate *private_key)
365 GRealThread *self = (GRealThread*) g_thread_self ();
368 array = self->private_data;
372 if (!private_key->index)
374 else if (private_key->index <= array->len)
375 return g_array_index (array, GStaticPrivateNode,
376 private_key->index - 1).data;
382 g_static_private_set (GStaticPrivate *private_key,
384 GDestroyNotify notify)
386 GRealThread *self = (GRealThread*) g_thread_self ();
388 static guint next_index = 0;
389 GStaticPrivateNode *node;
391 array = self->private_data;
394 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
395 self->private_data = array;
398 if (!private_key->index)
402 if (!private_key->index)
404 if (g_thread_free_indeces)
407 GPOINTER_TO_UINT (g_thread_free_indeces->data);
408 g_thread_free_indeces =
409 g_slist_delete_link (g_thread_free_indeces,
410 g_thread_free_indeces);
413 private_key->index = ++next_index;
419 if (private_key->index > array->len)
420 g_array_set_size (array, private_key->index);
422 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
425 gpointer ddata = node->data;
426 GDestroyNotify ddestroy = node->destroy;
429 node->destroy = notify;
436 node->destroy = notify;
441 g_static_private_free (GStaticPrivate *private_key)
443 guint index = private_key->index;
449 private_key->index = 0;
452 list = g_thread_all_threads;
455 GRealThread *thread = list->data;
456 GArray *array = thread->private_data;
459 if (array && index <= array->len)
461 GStaticPrivateNode *node = &g_array_index (array,
464 gpointer ddata = node->data;
465 GDestroyNotify ddestroy = node->destroy;
468 node->destroy = NULL;
478 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
479 GUINT_TO_POINTER (index));
484 g_thread_cleanup (gpointer data)
488 GRealThread* thread = data;
489 if (thread->private_data)
491 GArray* array = thread->private_data;
494 for (i = 0; i < array->len; i++ )
496 GStaticPrivateNode *node =
497 &g_array_index (array, GStaticPrivateNode, i);
499 node->destroy (node->data);
501 g_array_free (array, TRUE);
504 /* We only free the thread structure, if it isn't joinable. If
505 it is, the structure is freed in g_thread_join */
506 if (!thread->thread.joinable)
509 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
512 /* Just to make sure, this isn't used any more */
513 g_system_thread_assign (thread->system_thread, zero_thread);
522 g_error ("The thread system is not yet initialized.");
526 g_thread_create_proxy (gpointer data)
528 GRealThread* thread = data;
532 #ifdef G_THREAD_USE_PID_SURROGATE
533 thread->pid = getpid ();
534 #endif /* G_THREAD_USE_PID_SURROGATE */
536 /* This has to happen before G_LOCK, as that might call g_thread_self */
537 g_private_set (g_thread_specific_private, data);
539 /* the lock makes sure, that thread->system_thread is written,
540 before thread->thread.func is called. See g_thread_create. */
544 #ifdef G_THREAD_USE_PID_SURROGATE
545 if (g_thread_use_default_impl)
546 SET_PRIO (thread->pid, thread->thread.priority);
547 #endif /* G_THREAD_USE_PID_SURROGATE */
549 thread->retval = thread->thread.func (thread->thread.data);
555 g_thread_create_full (GThreadFunc func,
560 GThreadPriority priority,
564 GError *local_error = NULL;
565 g_return_val_if_fail (func, NULL);
566 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
567 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
569 result = g_new (GRealThread, 1);
571 result->thread.joinable = joinable;
572 result->thread.priority = priority;
573 result->thread.func = func;
574 result->thread.data = data;
575 result->private_data = NULL;
577 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
578 stack_size, joinable, bound, priority,
579 &result->system_thread, &local_error));
580 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
585 g_propagate_error (error, local_error);
590 return (GThread*) result;
594 g_thread_exit (gpointer retval)
596 GRealThread* real = (GRealThread*) g_thread_self ();
597 real->retval = retval;
598 G_THREAD_CF (thread_exit, (void)0, ());
602 g_thread_join (GThread* thread)
604 GRealThread* real = (GRealThread*) thread;
607 g_return_val_if_fail (thread, NULL);
608 g_return_val_if_fail (thread->joinable, NULL);
609 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
612 G_THREAD_UF (thread_join, (&real->system_thread));
614 retval = real->retval;
617 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
620 /* Just to make sure, this isn't used any more */
621 thread->joinable = 0;
622 g_system_thread_assign (real->system_thread, zero_thread);
624 /* the thread structure for non-joinable threads is freed upon
625 thread end. We free the memory here. This will leave a loose end,
626 if a joinable thread is not joined. */
634 g_thread_set_priority (GThread* thread,
635 GThreadPriority priority)
637 GRealThread* real = (GRealThread*) thread;
639 g_return_if_fail (thread);
640 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
641 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
642 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
644 thread->priority = priority;
646 #ifdef G_THREAD_USE_PID_SURROGATE
647 if (g_thread_use_default_impl)
648 SET_PRIO (real->pid, priority);
650 #endif /* G_THREAD_USE_PID_SURROGATE */
651 G_THREAD_CF (thread_set_priority, (void)0,
652 (&real->system_thread, priority));
658 GRealThread* thread = g_private_get (g_thread_specific_private);
662 /* If no thread data is available, provide and set one. This
663 can happen for the main thread and for threads, that are not
665 thread = g_new (GRealThread, 1);
666 thread->thread.joinable = FALSE; /* This is a save guess */
667 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
669 thread->thread.func = NULL;
670 thread->thread.data = NULL;
671 thread->private_data = NULL;
673 if (g_thread_supported ())
674 G_THREAD_UF (thread_self, (&thread->system_thread));
676 #ifdef G_THREAD_USE_PID_SURROGATE
677 thread->pid = getpid ();
678 #endif /* G_THREAD_USE_PID_SURROGATE */
680 g_private_set (g_thread_specific_private, thread);
683 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
687 return (GThread*)thread;
691 g_static_rw_lock_init (GStaticRWLock* lock)
693 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
695 g_return_if_fail (lock);
701 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
704 *cond = g_cond_new ();
705 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
709 g_static_rw_lock_signal (GStaticRWLock* lock)
711 if (lock->want_to_write && lock->write_cond)
712 g_cond_signal (lock->write_cond);
713 else if (lock->want_to_read && lock->read_cond)
714 g_cond_broadcast (lock->read_cond);
718 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
720 g_return_if_fail (lock);
722 if (!g_threads_got_initialized)
725 g_static_mutex_lock (&lock->mutex);
726 lock->want_to_read++;
727 while (lock->write || lock->want_to_write)
728 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
729 lock->want_to_read--;
730 lock->read_counter++;
731 g_static_mutex_unlock (&lock->mutex);
735 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
737 gboolean ret_val = FALSE;
739 g_return_val_if_fail (lock, FALSE);
741 if (!g_threads_got_initialized)
744 g_static_mutex_lock (&lock->mutex);
745 if (!lock->write && !lock->want_to_write)
747 lock->read_counter++;
750 g_static_mutex_unlock (&lock->mutex);
755 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
757 g_return_if_fail (lock);
759 if (!g_threads_got_initialized)
762 g_static_mutex_lock (&lock->mutex);
763 lock->read_counter--;
764 if (lock->read_counter == 0)
765 g_static_rw_lock_signal (lock);
766 g_static_mutex_unlock (&lock->mutex);
770 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
772 g_return_if_fail (lock);
774 if (!g_threads_got_initialized)
777 g_static_mutex_lock (&lock->mutex);
778 lock->want_to_write++;
779 while (lock->write || lock->read_counter)
780 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
781 lock->want_to_write--;
783 g_static_mutex_unlock (&lock->mutex);
787 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
789 gboolean ret_val = FALSE;
791 g_return_val_if_fail (lock, FALSE);
793 if (!g_threads_got_initialized)
796 g_static_mutex_lock (&lock->mutex);
797 if (!lock->write && !lock->read_counter)
802 g_static_mutex_unlock (&lock->mutex);
807 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
809 g_return_if_fail (lock);
811 if (!g_threads_got_initialized)
814 g_static_mutex_lock (&lock->mutex);
816 g_static_rw_lock_signal (lock);
817 g_static_mutex_unlock (&lock->mutex);
821 g_static_rw_lock_free (GStaticRWLock* lock)
823 g_return_if_fail (lock);
827 g_cond_free (lock->read_cond);
828 lock->read_cond = NULL;
830 if (lock->write_cond)
832 g_cond_free (lock->write_cond);
833 lock->write_cond = NULL;
835 g_static_mutex_free (&lock->mutex);