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/.
37 #ifdef G_THREAD_USE_PID_SURROGATE
38 #include <sys/types.h>
40 #include <sys/resource.h>
42 #endif /* G_THREAD_USE_PID_SURROGATE */
53 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
54 # define g_system_thread_equal_simple(thread1, thread2) \
55 ((thread1).dummy_pointer == (thread2).dummy_pointer)
56 # define g_system_thread_assign(dest, src) \
57 ((dest).dummy_pointer = (src).dummy_pointer)
58 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
59 # define g_system_thread_equal_simple(thread1, thread2) \
60 (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
61 # define g_system_thread_assign(dest, src) \
62 (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
63 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
65 #define g_system_thread_equal(thread1, thread2) \
66 (g_thread_functions_for_glib_use.thread_equal ? \
67 g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
68 g_system_thread_equal_simple((thread1), (thread2)))
71 g_thread_error_quark (void)
75 quark = g_quark_from_static_string ("g_thread_error");
79 /* Keep this in sync with GRealThread in gmain.c! */
80 typedef struct _GRealThread GRealThread;
84 gpointer private_data;
86 GSystemThread system_thread;
87 #ifdef G_THREAD_USE_PID_SURROGATE
89 #endif /* G_THREAD_USE_PID_SURROGATE */
92 #ifdef G_THREAD_USE_PID_SURROGATE
93 static gint priority_map[4];
94 static gboolean prio_warned = FALSE;
95 # define SET_PRIO(pid, prio) G_STMT_START{ \
96 gint error = setpriority (PRIO_PROCESS, (pid), priority_map[prio]); \
97 if (error == -1 && errno == EACCES && !prio_warned) \
100 g_warning ("Priorities can only be increased by root."); \
103 #endif /* G_THREAD_USE_PID_SURROGATE */
105 typedef struct _GStaticPrivateNode GStaticPrivateNode;
106 struct _GStaticPrivateNode
109 GDestroyNotify destroy;
112 static void g_thread_cleanup (gpointer data);
113 static void g_thread_fail (void);
115 /* Global variables */
117 static GSystemThread zero_thread; /* This is initialized to all zero */
118 gboolean g_thread_use_default_impl = TRUE;
119 gboolean g_threads_got_initialized = FALSE;
121 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
122 __declspec(dllexport)
124 GThreadFunctions g_thread_functions_for_glib_use = {
125 (GMutex*(*)())g_thread_fail, /* mutex_new */
126 NULL, /* mutex_lock */
127 NULL, /* mutex_trylock */
128 NULL, /* mutex_unlock */
129 NULL, /* mutex_free */
130 (GCond*(*)())g_thread_fail, /* cond_new */
131 NULL, /* cond_signal */
132 NULL, /* cond_broadcast */
133 NULL, /* cond_wait */
134 NULL, /* cond_timed_wait */
135 NULL, /* cond_free */
136 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
137 NULL, /* private_get */
138 NULL, /* private_set */
139 (void(*)(GThreadFunc, gpointer, gulong,
140 gboolean, gboolean, GThreadPriority,
141 gpointer, GError**))g_thread_fail, /* thread_create */
142 NULL, /* thread_yield */
143 NULL, /* thread_join */
144 NULL, /* thread_exit */
145 NULL, /* thread_set_priority */
146 NULL /* thread_self */
151 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
152 static GPrivate *g_thread_specific_private = NULL;
153 static GSList *g_thread_all_threads = NULL;
154 static GSList *g_thread_free_indeces = NULL;
156 G_LOCK_DEFINE_STATIC (g_thread);
158 /* This must be called only once, before any threads are created.
159 * It will only be called from g_thread_init() in -lgthread.
164 GRealThread* main_thread;
166 /* We let the main thread (the one that calls g_thread_init) inherit
167 * the data, that it set before calling g_thread_init
169 main_thread = (GRealThread*) g_thread_self ();
171 g_thread_specific_private = g_private_new (g_thread_cleanup);
172 G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
173 G_THREAD_UF (thread_self, (&main_thread->system_thread));
175 g_mutex_protect_static_mutex_allocation = g_mutex_new ();
177 #ifdef G_THREAD_USE_PID_SURROGATE
178 priority_map[G_THREAD_PRIORITY_NORMAL] =
179 getpriority (PRIO_PROCESS, (getpid ()));
180 priority_map[G_THREAD_PRIORITY_LOW] =
181 MIN (20, priority_map[G_THREAD_PRIORITY_NORMAL] + 10);
182 priority_map[G_THREAD_PRIORITY_HIGH] =
183 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 10);
184 priority_map[G_THREAD_PRIORITY_URGENT] =
185 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 15);
186 #endif /* G_THREAD_USE_PID_SURROGATE */
190 g_static_mutex_init (GStaticMutex *mutex)
192 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
194 g_return_if_fail (mutex);
200 g_static_mutex_get_mutex_impl (GMutex** mutex)
202 if (!g_thread_supported ())
205 g_assert (g_mutex_protect_static_mutex_allocation);
207 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
210 *mutex = g_mutex_new ();
212 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
218 g_static_mutex_free (GStaticMutex* mutex)
220 GMutex **runtime_mutex;
222 g_return_if_fail (mutex);
224 /* The runtime_mutex is the first (or only) member of GStaticMutex,
225 * see both versions (of glibconfig.h) in configure.in */
226 runtime_mutex = ((GMutex**)mutex);
229 g_mutex_free (*runtime_mutex);
231 *runtime_mutex = NULL;
235 g_static_rec_mutex_init (GStaticRecMutex *mutex)
237 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
239 g_return_if_fail (mutex);
245 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
249 g_return_if_fail (mutex);
251 if (!g_thread_supported ())
254 G_THREAD_UF (thread_self, (&self));
256 if (g_system_thread_equal (self, mutex->owner))
261 g_static_mutex_lock (&mutex->mutex);
262 g_system_thread_assign (mutex->owner, self);
267 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
271 g_return_val_if_fail (mutex, FALSE);
273 if (!g_thread_supported ())
276 G_THREAD_UF (thread_self, (&self));
278 if (g_system_thread_equal (self, mutex->owner))
284 if (!g_static_mutex_trylock (&mutex->mutex))
287 g_system_thread_assign (mutex->owner, self);
293 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
295 g_return_if_fail (mutex);
297 if (!g_thread_supported ())
300 if (mutex->depth > 1)
305 g_system_thread_assign (mutex->owner, zero_thread);
306 g_static_mutex_unlock (&mutex->mutex);
310 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
314 g_return_if_fail (mutex);
316 if (!g_thread_supported ())
319 G_THREAD_UF (thread_self, (&self));
321 if (g_system_thread_equal (self, mutex->owner))
323 mutex->depth += depth;
326 g_static_mutex_lock (&mutex->mutex);
327 g_system_thread_assign (mutex->owner, self);
328 mutex->depth = depth;
332 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
336 g_return_val_if_fail (mutex, 0);
338 if (!g_thread_supported ())
341 depth = mutex->depth;
343 g_system_thread_assign (mutex->owner, zero_thread);
345 g_static_mutex_unlock (&mutex->mutex);
351 g_static_rec_mutex_free (GStaticRecMutex *mutex)
353 g_return_if_fail (mutex);
355 g_static_mutex_free (&mutex->mutex);
359 g_static_private_init (GStaticPrivate *private_key)
361 private_key->index = 0;
365 g_static_private_get (GStaticPrivate *private_key)
367 GRealThread *self = (GRealThread*) g_thread_self ();
370 array = self->private_data;
374 if (!private_key->index)
376 else if (private_key->index <= array->len)
377 return g_array_index (array, GStaticPrivateNode,
378 private_key->index - 1).data;
384 g_static_private_set (GStaticPrivate *private_key,
386 GDestroyNotify notify)
388 GRealThread *self = (GRealThread*) g_thread_self ();
390 static guint next_index = 0;
391 GStaticPrivateNode *node;
393 array = self->private_data;
396 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
397 self->private_data = array;
400 if (!private_key->index)
404 if (!private_key->index)
406 if (g_thread_free_indeces)
409 GPOINTER_TO_UINT (g_thread_free_indeces->data);
410 g_thread_free_indeces =
411 g_slist_delete_link (g_thread_free_indeces,
412 g_thread_free_indeces);
415 private_key->index = ++next_index;
421 if (private_key->index > array->len)
422 g_array_set_size (array, private_key->index);
424 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
427 gpointer ddata = node->data;
428 GDestroyNotify ddestroy = node->destroy;
431 node->destroy = notify;
438 node->destroy = notify;
443 g_static_private_free (GStaticPrivate *private_key)
445 guint index = private_key->index;
451 private_key->index = 0;
454 list = g_thread_all_threads;
457 GRealThread *thread = list->data;
458 GArray *array = thread->private_data;
461 if (array && index <= array->len)
463 GStaticPrivateNode *node = &g_array_index (array,
466 gpointer ddata = node->data;
467 GDestroyNotify ddestroy = node->destroy;
470 node->destroy = NULL;
480 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
481 GUINT_TO_POINTER (index));
486 g_thread_cleanup (gpointer data)
490 GRealThread* thread = data;
491 if (thread->private_data)
493 GArray* array = thread->private_data;
496 for (i = 0; i < array->len; i++ )
498 GStaticPrivateNode *node =
499 &g_array_index (array, GStaticPrivateNode, i);
501 node->destroy (node->data);
503 g_array_free (array, TRUE);
506 /* We only free the thread structure, if it isn't joinable. If
507 it is, the structure is freed in g_thread_join */
508 if (!thread->thread.joinable)
511 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
514 /* Just to make sure, this isn't used any more */
515 g_system_thread_assign (thread->system_thread, zero_thread);
524 g_error ("The thread system is not yet initialized.");
528 g_thread_create_proxy (gpointer data)
530 GRealThread* thread = data;
534 #ifdef G_THREAD_USE_PID_SURROGATE
535 thread->pid = getpid ();
536 #endif /* G_THREAD_USE_PID_SURROGATE */
538 /* This has to happen before G_LOCK, as that might call g_thread_self */
539 g_private_set (g_thread_specific_private, data);
541 /* the lock makes sure, that thread->system_thread is written,
542 before thread->thread.func is called. See g_thread_create. */
546 #ifdef G_THREAD_USE_PID_SURROGATE
547 if (g_thread_use_default_impl)
548 SET_PRIO (thread->pid, thread->thread.priority);
549 #endif /* G_THREAD_USE_PID_SURROGATE */
551 thread->retval = thread->thread.func (thread->thread.data);
557 g_thread_create_full (GThreadFunc func,
562 GThreadPriority priority,
566 GError *local_error = NULL;
567 g_return_val_if_fail (func, NULL);
568 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
569 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
571 result = g_new (GRealThread, 1);
573 result->thread.joinable = joinable;
574 result->thread.priority = priority;
575 result->thread.func = func;
576 result->thread.data = data;
577 result->private_data = NULL;
579 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
580 stack_size, joinable, bound, priority,
581 &result->system_thread, &local_error));
582 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
587 g_propagate_error (error, local_error);
592 return (GThread*) result;
596 g_thread_exit (gpointer retval)
598 GRealThread* real = (GRealThread*) g_thread_self ();
599 real->retval = retval;
600 G_THREAD_CF (thread_exit, (void)0, ());
604 g_thread_join (GThread* thread)
606 GRealThread* real = (GRealThread*) thread;
609 g_return_val_if_fail (thread, NULL);
610 g_return_val_if_fail (thread->joinable, NULL);
611 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
614 G_THREAD_UF (thread_join, (&real->system_thread));
616 retval = real->retval;
619 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
622 /* Just to make sure, this isn't used any more */
623 thread->joinable = 0;
624 g_system_thread_assign (real->system_thread, zero_thread);
626 /* the thread structure for non-joinable threads is freed upon
627 thread end. We free the memory here. This will leave a loose end,
628 if a joinable thread is not joined. */
636 g_thread_set_priority (GThread* thread,
637 GThreadPriority priority)
639 GRealThread* real = (GRealThread*) thread;
641 g_return_if_fail (thread);
642 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
643 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
644 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
646 thread->priority = priority;
648 #ifdef G_THREAD_USE_PID_SURROGATE
649 if (g_thread_use_default_impl)
650 SET_PRIO (real->pid, priority);
652 #endif /* G_THREAD_USE_PID_SURROGATE */
653 G_THREAD_CF (thread_set_priority, (void)0,
654 (&real->system_thread, priority));
660 GRealThread* thread = g_private_get (g_thread_specific_private);
664 /* If no thread data is available, provide and set one. This
665 can happen for the main thread and for threads, that are not
667 thread = g_new (GRealThread, 1);
668 thread->thread.joinable = FALSE; /* This is a save guess */
669 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
671 thread->thread.func = NULL;
672 thread->thread.data = NULL;
673 thread->private_data = NULL;
675 if (g_thread_supported ())
676 G_THREAD_UF (thread_self, (&thread->system_thread));
678 #ifdef G_THREAD_USE_PID_SURROGATE
679 thread->pid = getpid ();
680 #endif /* G_THREAD_USE_PID_SURROGATE */
682 g_private_set (g_thread_specific_private, thread);
685 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
689 return (GThread*)thread;
693 g_static_rw_lock_init (GStaticRWLock* lock)
695 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
697 g_return_if_fail (lock);
703 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
706 *cond = g_cond_new ();
707 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
711 g_static_rw_lock_signal (GStaticRWLock* lock)
713 if (lock->want_to_write && lock->write_cond)
714 g_cond_signal (lock->write_cond);
715 else if (lock->want_to_read && lock->read_cond)
716 g_cond_broadcast (lock->read_cond);
720 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
722 g_return_if_fail (lock);
724 if (!g_threads_got_initialized)
727 g_static_mutex_lock (&lock->mutex);
728 lock->want_to_read++;
729 while (lock->write || lock->want_to_write)
730 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
731 lock->want_to_read--;
732 lock->read_counter++;
733 g_static_mutex_unlock (&lock->mutex);
737 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
739 gboolean ret_val = FALSE;
741 g_return_val_if_fail (lock, FALSE);
743 if (!g_threads_got_initialized)
746 g_static_mutex_lock (&lock->mutex);
747 if (!lock->write && !lock->want_to_write)
749 lock->read_counter++;
752 g_static_mutex_unlock (&lock->mutex);
757 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
759 g_return_if_fail (lock);
761 if (!g_threads_got_initialized)
764 g_static_mutex_lock (&lock->mutex);
765 lock->read_counter--;
766 if (lock->read_counter == 0)
767 g_static_rw_lock_signal (lock);
768 g_static_mutex_unlock (&lock->mutex);
772 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
774 g_return_if_fail (lock);
776 if (!g_threads_got_initialized)
779 g_static_mutex_lock (&lock->mutex);
780 lock->want_to_write++;
781 while (lock->write || lock->read_counter)
782 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
783 lock->want_to_write--;
785 g_static_mutex_unlock (&lock->mutex);
789 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
791 gboolean ret_val = FALSE;
793 g_return_val_if_fail (lock, FALSE);
795 if (!g_threads_got_initialized)
798 g_static_mutex_lock (&lock->mutex);
799 if (!lock->write && !lock->read_counter)
804 g_static_mutex_unlock (&lock->mutex);
809 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
811 g_return_if_fail (lock);
813 if (!g_threads_got_initialized)
816 g_static_mutex_lock (&lock->mutex);
818 g_static_rw_lock_signal (lock);
819 g_static_mutex_unlock (&lock->mutex);
823 g_static_rw_lock_free (GStaticRWLock* lock)
825 g_return_if_fail (lock);
829 g_cond_free (lock->read_cond);
830 lock->read_cond = NULL;
832 if (lock->write_cond)
834 g_cond_free (lock->write_cond);
835 lock->write_cond = NULL;
837 g_static_mutex_free (&lock->mutex);