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 /* Keep this in sync with GRealThread in gmain.c! */
73 typedef struct _GRealThread GRealThread;
77 GMainContext *context;
78 gpointer private_data;
80 GSystemThread system_thread;
81 #ifdef G_THREAD_USE_PID_SURROGATE
83 #endif /* G_THREAD_USE_PID_SURROGATE */
86 #ifdef G_THREAD_USE_PID_SURROGATE
87 static gint priority_map[] = { 15, 0, -15, -20 };
88 static gboolean prio_warned = FALSE;
89 # define SET_PRIO(pid, prio) G_STMT_START{ \
90 gint error = setpriority (PRIO_PROCESS, (pid), priority_map[prio]); \
91 if (error == -1 && errno == EACCES && !prio_warned) \
94 g_warning ("Priorities can only be increased by root."); \
97 #endif /* G_THREAD_USE_PID_SURROGATE */
99 typedef struct _GStaticPrivateNode GStaticPrivateNode;
100 struct _GStaticPrivateNode
103 GDestroyNotify destroy;
106 static void g_thread_cleanup (gpointer data);
107 static void g_thread_fail (void);
109 /* Global variables */
111 static GSystemThread zero_thread; /* This is initialized to all zero */
112 gboolean g_thread_use_default_impl = TRUE;
113 gboolean g_threads_got_initialized = FALSE;
115 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
116 __declspec(dllexport)
118 GThreadFunctions g_thread_functions_for_glib_use = {
119 (GMutex*(*)())g_thread_fail, /* mutex_new */
120 NULL, /* mutex_lock */
121 NULL, /* mutex_trylock */
122 NULL, /* mutex_unlock */
123 NULL, /* mutex_free */
124 (GCond*(*)())g_thread_fail, /* cond_new */
125 NULL, /* cond_signal */
126 NULL, /* cond_broadcast */
127 NULL, /* cond_wait */
128 NULL, /* cond_timed_wait */
129 NULL, /* cond_free */
130 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
131 NULL, /* private_get */
132 NULL, /* private_set */
133 (void(*)(GThreadFunc, gpointer, gulong,
134 gboolean, gboolean, GThreadPriority,
135 gpointer, GError**))g_thread_fail, /* thread_create */
136 NULL, /* thread_yield */
137 NULL, /* thread_join */
138 NULL, /* thread_exit */
139 NULL, /* thread_set_priority */
140 NULL /* thread_self */
145 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
146 static GPrivate *g_thread_specific_private = NULL;
147 static GSList *g_thread_all_threads = NULL;
148 static GSList *g_thread_free_indeces = NULL;
150 G_LOCK_DEFINE_STATIC (g_thread);
152 /* This must be called only once, before any threads are created.
153 * It will only be called from g_thread_init() in -lgthread.
158 GRealThread* main_thread;
160 /* We let the main thread (the one that calls g_thread_init) inherit
161 * the data, that it set before calling g_thread_init
163 main_thread = (GRealThread*) g_thread_self ();
165 g_thread_specific_private = g_private_new (g_thread_cleanup);
166 G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
167 G_THREAD_UF (thread_self, (&main_thread->system_thread));
169 g_mutex_protect_static_mutex_allocation = g_mutex_new ();
173 g_static_mutex_init (GStaticMutex *mutex)
175 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
177 g_return_if_fail (mutex);
179 memcpy (mutex, &init_mutex, sizeof (GStaticMutex));
183 g_static_mutex_get_mutex_impl (GMutex** mutex)
185 if (!g_thread_supported ())
188 g_assert (g_mutex_protect_static_mutex_allocation);
190 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
193 *mutex = g_mutex_new ();
195 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
201 g_static_mutex_free (GStaticMutex* mutex)
203 GMutex **runtime_mutex;
205 g_return_if_fail (mutex);
207 /* The runtime_mutex is the first (or only) member of GStaticMutex,
208 * see both versions (of glibconfig.h) in configure.in */
209 runtime_mutex = ((GMutex**)mutex);
212 g_mutex_free (*runtime_mutex);
214 *runtime_mutex = NULL;
218 g_static_rec_mutex_init (GStaticRecMutex *mutex)
220 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
222 g_return_if_fail (mutex);
224 memcpy (mutex, &init_mutex, sizeof (GStaticRecMutex));
228 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
232 g_return_if_fail (mutex);
234 if (!g_thread_supported ())
237 G_THREAD_UF (thread_self, (&self));
239 if (g_system_thread_equal (self, mutex->owner))
244 g_static_mutex_lock (&mutex->mutex);
245 g_system_thread_assign (mutex->owner, self);
250 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
254 g_return_val_if_fail (mutex, FALSE);
256 if (!g_thread_supported ())
259 G_THREAD_UF (thread_self, (&self));
261 if (g_system_thread_equal (self, mutex->owner))
267 if (!g_static_mutex_trylock (&mutex->mutex))
270 g_system_thread_assign (mutex->owner, self);
276 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
278 g_return_if_fail (mutex);
280 if (!g_thread_supported ())
283 if (mutex->depth > 1)
288 g_system_thread_assign (mutex->owner, zero_thread);
289 g_static_mutex_unlock (&mutex->mutex);
293 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
297 g_return_if_fail (mutex);
299 if (!g_thread_supported ())
302 G_THREAD_UF (thread_self, (&self));
304 if (g_system_thread_equal (self, mutex->owner))
306 mutex->depth += depth;
309 g_static_mutex_lock (&mutex->mutex);
310 g_system_thread_assign (mutex->owner, self);
311 mutex->depth = depth;
315 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
319 g_return_val_if_fail (mutex, 0);
321 if (!g_thread_supported ())
324 depth = mutex->depth;
326 g_system_thread_assign (mutex->owner, zero_thread);
328 g_static_mutex_unlock (&mutex->mutex);
334 g_static_rec_mutex_free (GStaticRecMutex *mutex)
336 g_return_if_fail (mutex);
338 g_static_mutex_free (&mutex->mutex);
342 g_static_private_init (GStaticPrivate *private_key)
344 private_key->index = 0;
348 g_static_private_get (GStaticPrivate *private_key)
350 GRealThread *self = (GRealThread*) g_thread_self ();
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,
361 private_key->index - 1).data;
367 g_static_private_set (GStaticPrivate *private_key,
369 GDestroyNotify notify)
371 GRealThread *self = (GRealThread*) g_thread_self ();
373 static guint next_index = 0;
374 GStaticPrivateNode *node;
376 array = self->private_data;
379 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
380 self->private_data = array;
383 if (!private_key->index)
387 if (!private_key->index)
389 if (g_thread_free_indeces)
392 GPOINTER_TO_UINT (g_thread_free_indeces->data);
393 g_thread_free_indeces =
394 g_slist_delete_link (g_thread_free_indeces,
395 g_thread_free_indeces);
398 private_key->index = ++next_index;
404 if (private_key->index > array->len)
405 g_array_set_size (array, private_key->index);
407 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
410 gpointer ddata = node->data;
411 GDestroyNotify ddestroy = node->destroy;
414 node->destroy = notify;
421 node->destroy = notify;
426 g_static_private_free (GStaticPrivate *private_key)
428 guint index = private_key->index;
434 private_key->index = 0;
437 list = g_thread_all_threads;
440 GRealThread *thread = list->data;
441 GArray *array = thread->private_data;
444 if (array && index <= array->len)
446 GStaticPrivateNode *node = &g_array_index (array,
449 gpointer ddata = node->data;
450 GDestroyNotify ddestroy = node->destroy;
453 node->destroy = NULL;
463 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
464 GUINT_TO_POINTER (index));
468 void g_main_context_destroy (GMainContext *context);
471 g_thread_cleanup (gpointer data)
475 GRealThread* thread = data;
476 if (thread->private_data)
478 GArray* array = thread->private_data;
481 for (i = 0; i < array->len; i++ )
483 GStaticPrivateNode *node =
484 &g_array_index (array, GStaticPrivateNode, i);
486 node->destroy (node->data);
488 g_array_free (array, TRUE);
491 g_main_context_destroy (thread->context);
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->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->retval = thread->thread.func (thread->thread.data);
544 g_thread_create_full (GThreadFunc func,
549 GThreadPriority priority,
552 GRealThread* result = g_new (GRealThread, 1);
553 GError *local_error = NULL;
554 g_return_val_if_fail (func, NULL);
555 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
556 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
558 result->thread.joinable = joinable;
559 result->thread.priority = priority;
560 result->thread.func = func;
561 result->thread.data = data;
562 result->private_data = NULL;
563 result->context = NULL;
565 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
566 stack_size, joinable, bound, priority,
567 &result->system_thread, &local_error));
568 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
573 g_propagate_error (error, local_error);
578 return (GThread*) result;
582 g_thread_exit (gpointer retval)
584 GRealThread* real = (GRealThread*) g_thread_self ();
585 real->retval = retval;
586 G_THREAD_CF (thread_exit, (void)0, ());
590 g_thread_join (GThread* thread)
592 GRealThread* real = (GRealThread*) thread;
595 g_return_val_if_fail (thread, NULL);
596 g_return_val_if_fail (thread->joinable, NULL);
597 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
600 G_THREAD_UF (thread_join, (&real->system_thread));
602 retval = real->retval;
605 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
608 /* Just to make sure, this isn't used any more */
609 thread->joinable = 0;
610 g_system_thread_assign (real->system_thread, zero_thread);
612 /* the thread structure for non-joinable threads is freed upon
613 thread end. We free the memory here. This will leave a loose end,
614 if a joinable thread is not joined. */
622 g_thread_set_priority (GThread* thread,
623 GThreadPriority priority)
625 GRealThread* real = (GRealThread*) thread;
627 g_return_if_fail (thread);
628 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
629 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
630 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
632 thread->priority = priority;
634 #ifdef G_THREAD_USE_PID_SURROGATE
635 if (g_thread_use_default_impl)
636 SET_PRIO (real->pid, priority);
638 #endif /* G_THREAD_USE_PID_SURROGATE */
639 G_THREAD_CF (thread_set_priority, (void)0,
640 (&real->system_thread, priority));
646 GRealThread* thread = g_private_get (g_thread_specific_private);
650 /* If no thread data is available, provide and set one. This
651 can happen for the main thread and for threads, that are not
653 thread = g_new (GRealThread, 1);
654 thread->thread.joinable = FALSE; /* This is a save guess */
655 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
657 thread->thread.func = NULL;
658 thread->thread.data = NULL;
659 thread->private_data = NULL;
660 thread->context = NULL;
662 if (g_thread_supported ())
663 G_THREAD_UF (thread_self, (&thread->system_thread));
665 #ifdef G_THREAD_USE_PID_SURROGATE
666 thread->pid = getpid ();
667 #endif /* G_THREAD_USE_PID_SURROGATE */
669 g_private_set (g_thread_specific_private, thread);
672 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
676 return (GThread*)thread;
680 g_static_rw_lock_init (GStaticRWLock* lock)
682 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
684 g_return_if_fail (lock);
686 memcpy (lock, &init_lock, sizeof (GStaticRWLock));
690 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
693 *cond = g_cond_new ();
694 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
698 g_static_rw_lock_signal (GStaticRWLock* lock)
700 if (lock->want_to_write && lock->write_cond)
701 g_cond_signal (lock->write_cond);
702 else if (lock->read_cond)
703 g_cond_broadcast (lock->read_cond);
707 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
709 g_return_if_fail (lock);
711 if (!g_threads_got_initialized)
714 g_static_mutex_lock (&lock->mutex);
715 while (lock->write || lock->want_to_write)
716 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
717 lock->read_counter++;
718 g_static_mutex_unlock (&lock->mutex);
722 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
724 gboolean ret_val = FALSE;
726 g_return_val_if_fail (lock, FALSE);
728 if (!g_threads_got_initialized)
731 g_static_mutex_lock (&lock->mutex);
732 if (!lock->write && !lock->want_to_write)
734 lock->read_counter++;
737 g_static_mutex_unlock (&lock->mutex);
742 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
744 g_return_if_fail (lock);
746 if (!g_threads_got_initialized)
749 g_static_mutex_lock (&lock->mutex);
750 lock->read_counter--;
751 if (lock->read_counter == 0)
752 g_static_rw_lock_signal (lock);
753 g_static_mutex_unlock (&lock->mutex);
757 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
759 g_return_if_fail (lock);
761 if (!g_threads_got_initialized)
764 g_static_mutex_lock (&lock->mutex);
765 lock->want_to_write++;
766 while (lock->write || lock->read_counter)
767 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
768 lock->want_to_write--;
770 g_static_mutex_unlock (&lock->mutex);
774 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
776 gboolean ret_val = FALSE;
778 g_return_val_if_fail (lock, FALSE);
780 if (!g_threads_got_initialized)
783 g_static_mutex_lock (&lock->mutex);
784 if (!lock->write && !lock->read_counter)
789 g_static_mutex_unlock (&lock->mutex);
794 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
796 g_return_if_fail (lock);
798 if (!g_threads_got_initialized)
801 g_static_mutex_lock (&lock->mutex);
803 g_static_rw_lock_signal (lock);
804 g_static_mutex_unlock (&lock->mutex);
808 g_static_rw_lock_free (GStaticRWLock* lock)
810 g_return_if_fail (lock);
814 g_cond_free (lock->read_cond);
815 lock->read_cond = NULL;
817 if (lock->write_cond)
819 g_cond_free (lock->write_cond);
820 lock->write_cond = NULL;
822 g_static_mutex_free (&lock->mutex);