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;
79 gpointer private_data;
80 GMainContext *context;
81 GSystemThread system_thread;
82 #ifdef G_THREAD_USE_PID_SURROGATE
84 #endif /* G_THREAD_USE_PID_SURROGATE */
87 #ifdef G_THREAD_USE_PID_SURROGATE
88 static gint priority_map[] = { 15, 0, -15, -20 };
89 static gboolean prio_warned = FALSE;
90 # define SET_PRIO(pid, prio) G_STMT_START{ \
91 gint error = setpriority (PRIO_PROCESS, (pid), priority_map[prio]); \
92 if (error == -1 && errno == EACCES && !prio_warned) \
95 g_warning ("Priorities can only be increased by root."); \
98 #endif /* G_THREAD_USE_PID_SURROGATE */
100 typedef struct _GStaticPrivateNode GStaticPrivateNode;
101 struct _GStaticPrivateNode
104 GDestroyNotify destroy;
107 static void g_thread_cleanup (gpointer data);
108 static void g_thread_fail (void);
110 /* Global variables */
112 static GSystemThread zero_thread; /* This is initialized to all zero */
113 gboolean g_thread_use_default_impl = TRUE;
114 gboolean g_threads_got_initialized = FALSE;
116 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
117 __declspec(dllexport)
119 GThreadFunctions g_thread_functions_for_glib_use = {
120 (GMutex*(*)())g_thread_fail, /* mutex_new */
121 NULL, /* mutex_lock */
122 NULL, /* mutex_trylock */
123 NULL, /* mutex_unlock */
124 NULL, /* mutex_free */
125 (GCond*(*)())g_thread_fail, /* cond_new */
126 NULL, /* cond_signal */
127 NULL, /* cond_broadcast */
128 NULL, /* cond_wait */
129 NULL, /* cond_timed_wait */
130 NULL, /* cond_free */
131 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
132 NULL, /* private_get */
133 NULL, /* private_set */
134 (void(*)(GThreadFunc, gpointer, gulong,
135 gboolean, gboolean, GThreadPriority,
136 gpointer, GError**))g_thread_fail, /* thread_create */
137 NULL, /* thread_yield */
138 NULL, /* thread_join */
139 NULL, /* thread_exit */
140 NULL, /* thread_set_priority */
141 NULL /* thread_self */
146 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
147 static GPrivate *g_thread_specific_private = NULL;
148 static GSList *g_thread_all_threads = NULL;
149 static GSList *g_thread_free_indeces = NULL;
151 G_LOCK_DEFINE_STATIC (g_thread);
153 /* This must be called only once, before any threads are created.
154 * It will only be called from g_thread_init() in -lgthread.
159 GRealThread* main_thread;
161 /* We let the main thread (the one that calls g_thread_init) inherit
162 * the data, that it set before calling g_thread_init
164 main_thread = (GRealThread*) g_thread_self ();
166 g_thread_specific_private = g_private_new (g_thread_cleanup);
167 G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
168 G_THREAD_UF (thread_self, (&main_thread->system_thread));
170 g_mutex_protect_static_mutex_allocation = g_mutex_new ();
174 g_static_mutex_init (GStaticMutex *mutex)
176 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
178 g_return_if_fail (mutex);
180 memcpy (mutex, &init_mutex, sizeof (GStaticMutex));
184 g_static_mutex_get_mutex_impl (GMutex** mutex)
186 if (!g_thread_supported ())
189 g_assert (g_mutex_protect_static_mutex_allocation);
191 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
194 *mutex = g_mutex_new ();
196 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
202 g_static_mutex_free (GStaticMutex* mutex)
204 GMutex **runtime_mutex;
206 g_return_if_fail (mutex);
208 /* The runtime_mutex is the first (or only) member of GStaticMutex,
209 * see both versions (of glibconfig.h) in configure.in */
210 runtime_mutex = ((GMutex**)mutex);
213 g_mutex_free (*runtime_mutex);
215 *runtime_mutex = NULL;
219 g_static_rec_mutex_init (GStaticRecMutex *mutex)
221 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
223 g_return_if_fail (mutex);
225 memcpy (mutex, &init_mutex, sizeof (GStaticRecMutex));
229 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
233 g_return_if_fail (mutex);
235 if (!g_thread_supported ())
238 G_THREAD_UF (thread_self, (&self));
240 if (g_system_thread_equal (self, mutex->owner))
245 g_static_mutex_lock (&mutex->mutex);
246 g_system_thread_assign (mutex->owner, self);
251 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
255 g_return_val_if_fail (mutex, FALSE);
257 if (!g_thread_supported ())
260 G_THREAD_UF (thread_self, (&self));
262 if (g_system_thread_equal (self, mutex->owner))
268 if (!g_static_mutex_trylock (&mutex->mutex))
271 g_system_thread_assign (mutex->owner, self);
277 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
279 g_return_if_fail (mutex);
281 if (!g_thread_supported ())
284 if (mutex->depth > 1)
289 g_system_thread_assign (mutex->owner, zero_thread);
290 g_static_mutex_unlock (&mutex->mutex);
294 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
298 g_return_if_fail (mutex);
300 if (!g_thread_supported ())
303 G_THREAD_UF (thread_self, (&self));
305 if (g_system_thread_equal (self, mutex->owner))
307 mutex->depth += depth;
310 g_static_mutex_lock (&mutex->mutex);
311 g_system_thread_assign (mutex->owner, self);
312 mutex->depth = depth;
316 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
320 g_return_val_if_fail (mutex, 0);
322 if (!g_thread_supported ())
325 depth = mutex->depth;
327 g_system_thread_assign (mutex->owner, zero_thread);
329 g_static_mutex_unlock (&mutex->mutex);
335 g_static_rec_mutex_free (GStaticRecMutex *mutex)
337 g_return_if_fail (mutex);
339 g_static_mutex_free (&mutex->mutex);
343 g_static_private_init (GStaticPrivate *private_key)
345 private_key->index = 0;
349 g_static_private_get (GStaticPrivate *private_key)
351 GRealThread *self = (GRealThread*) g_thread_self ();
354 array = self->private_data;
358 if (!private_key->index)
360 else if (private_key->index <= array->len)
361 return g_array_index (array, GStaticPrivateNode,
362 private_key->index - 1).data;
368 g_static_private_set (GStaticPrivate *private_key,
370 GDestroyNotify notify)
372 GRealThread *self = (GRealThread*) g_thread_self ();
374 static guint next_index = 0;
375 GStaticPrivateNode *node;
377 array = self->private_data;
380 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
381 self->private_data = array;
384 if (!private_key->index)
388 if (!private_key->index)
390 if (g_thread_free_indeces)
393 GPOINTER_TO_UINT (g_thread_free_indeces->data);
394 g_thread_free_indeces =
395 g_slist_delete_link (g_thread_free_indeces,
396 g_thread_free_indeces);
399 private_key->index = ++next_index;
405 if (private_key->index > array->len)
406 g_array_set_size (array, private_key->index);
408 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
411 gpointer ddata = node->data;
412 GDestroyNotify ddestroy = node->destroy;
415 node->destroy = notify;
422 node->destroy = notify;
427 g_static_private_free (GStaticPrivate *private_key)
429 guint index = private_key->index;
435 private_key->index = 0;
438 list = g_thread_all_threads;
441 GRealThread *thread = list->data;
442 GArray *array = thread->private_data;
445 if (array && index <= array->len)
447 GStaticPrivateNode *node = &g_array_index (array,
450 gpointer ddata = node->data;
451 GDestroyNotify ddestroy = node->destroy;
454 node->destroy = NULL;
464 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
465 GUINT_TO_POINTER (index));
469 void g_main_context_destroy (GMainContext *context);
472 g_thread_cleanup (gpointer data)
476 GRealThread* thread = data;
477 if (thread->private_data)
479 GArray* array = thread->private_data;
482 for (i = 0; i < array->len; i++ )
484 GStaticPrivateNode *node =
485 &g_array_index (array, GStaticPrivateNode, i);
487 node->destroy (node->data);
489 g_array_free (array, TRUE);
492 g_main_context_destroy (thread->context);
494 /* We only free the thread structure, if it isn't joinable. If
495 it is, the structure is freed in g_thread_join */
496 if (!thread->thread.joinable)
499 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
502 /* Just to make sure, this isn't used any more */
503 g_system_thread_assign (thread->system_thread, zero_thread);
512 g_error ("The thread system is not yet initialized.");
516 g_thread_create_proxy (gpointer data)
518 GRealThread* thread = data;
522 #ifdef G_THREAD_USE_PID_SURROGATE
523 thread->pid = getpid ();
524 #endif /* G_THREAD_USE_PID_SURROGATE */
526 /* This has to happen before G_LOCK, as that might call g_thread_self */
527 g_private_set (g_thread_specific_private, data);
529 /* the lock makes sure, that thread->system_thread is written,
530 before thread->func is called. See g_thread_create. */
534 #ifdef G_THREAD_USE_PID_SURROGATE
535 if (g_thread_use_default_impl)
536 SET_PRIO (thread->pid, thread->thread.priority);
537 #endif /* G_THREAD_USE_PID_SURROGATE */
539 thread->func (thread->arg);
543 g_thread_create (GThreadFunc thread_func,
548 GThreadPriority priority,
551 GRealThread* result = g_new (GRealThread, 1);
552 GError *local_error = NULL;
553 g_return_val_if_fail (thread_func, NULL);
554 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
555 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
557 result->thread.joinable = joinable;
558 result->thread.bound = bound;
559 result->thread.priority = priority;
560 result->func = thread_func;
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_join (GThread* thread)
584 GRealThread* real = (GRealThread*) thread;
587 g_return_if_fail (thread);
588 g_return_if_fail (thread->joinable);
589 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
591 G_THREAD_UF (thread_join, (&real->system_thread));
594 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
597 /* Just to make sure, this isn't used any more */
598 thread->joinable = 0;
599 g_system_thread_assign (real->system_thread, zero_thread);
601 /* the thread structure for non-joinable threads is freed upon
602 thread end. We free the memory here. This will leave loose end,
603 if a joinable thread is not joined. */
609 g_thread_set_priority (GThread* thread,
610 GThreadPriority priority)
612 GRealThread* real = (GRealThread*) thread;
614 g_return_if_fail (thread);
615 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
616 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
617 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
619 thread->priority = priority;
621 #ifdef G_THREAD_USE_PID_SURROGATE
622 if (g_thread_use_default_impl)
623 SET_PRIO (real->pid, priority);
625 #endif /* G_THREAD_USE_PID_SURROGATE */
626 G_THREAD_CF (thread_set_priority, (void)0,
627 (&real->system_thread, priority));
633 GRealThread* thread = g_private_get (g_thread_specific_private);
637 /* If no thread data is available, provide and set one. This
638 can happen for the main thread and for threads, that are not
640 thread = g_new (GRealThread, 1);
641 thread->thread.joinable = FALSE; /* This is a save guess */
642 thread->thread.bound = TRUE; /* This isn't important at all */
643 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
647 thread->private_data = NULL;
648 thread->context = NULL;
650 if (g_thread_supported ())
651 G_THREAD_UF (thread_self, (&thread->system_thread));
653 #ifdef G_THREAD_USE_PID_SURROGATE
654 thread->pid = getpid ();
655 #endif /* G_THREAD_USE_PID_SURROGATE */
657 g_private_set (g_thread_specific_private, thread);
660 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
664 return (GThread*)thread;
668 g_static_rw_lock_init (GStaticRWLock* lock)
670 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
672 g_return_if_fail (lock);
674 memcpy (lock, &init_lock, sizeof (GStaticRWLock));
678 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
681 *cond = g_cond_new ();
682 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
686 g_static_rw_lock_signal (GStaticRWLock* lock)
688 if (lock->want_to_write && lock->write_cond)
689 g_cond_signal (lock->write_cond);
690 else if (lock->read_cond)
691 g_cond_broadcast (lock->read_cond);
695 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
697 g_return_if_fail (lock);
699 if (!g_threads_got_initialized)
702 g_static_mutex_lock (&lock->mutex);
703 while (lock->write || lock->want_to_write)
704 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
705 lock->read_counter++;
706 g_static_mutex_unlock (&lock->mutex);
710 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
712 gboolean ret_val = FALSE;
714 g_return_val_if_fail (lock, FALSE);
716 if (!g_threads_got_initialized)
719 g_static_mutex_lock (&lock->mutex);
720 if (!lock->write && !lock->want_to_write)
722 lock->read_counter++;
725 g_static_mutex_unlock (&lock->mutex);
730 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
732 g_return_if_fail (lock);
734 if (!g_threads_got_initialized)
737 g_static_mutex_lock (&lock->mutex);
738 lock->read_counter--;
739 if (lock->read_counter == 0)
740 g_static_rw_lock_signal (lock);
741 g_static_mutex_unlock (&lock->mutex);
745 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
747 g_return_if_fail (lock);
749 if (!g_threads_got_initialized)
752 g_static_mutex_lock (&lock->mutex);
753 lock->want_to_write++;
754 while (lock->write || lock->read_counter)
755 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
756 lock->want_to_write--;
758 g_static_mutex_unlock (&lock->mutex);
762 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
764 gboolean ret_val = FALSE;
766 g_return_val_if_fail (lock, FALSE);
768 if (!g_threads_got_initialized)
771 g_static_mutex_lock (&lock->mutex);
772 if (!lock->write && !lock->read_counter)
777 g_static_mutex_unlock (&lock->mutex);
782 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
784 g_return_if_fail (lock);
786 if (!g_threads_got_initialized)
789 g_static_mutex_lock (&lock->mutex);
791 g_static_rw_lock_signal (lock);
792 g_static_mutex_unlock (&lock->mutex);
796 g_static_rw_lock_free (GStaticRWLock* lock)
798 g_return_if_fail (lock);
802 g_cond_free (lock->read_cond);
803 lock->read_cond = NULL;
805 if (lock->write_cond)
807 g_cond_free (lock->write_cond);
808 lock->write_cond = NULL;
810 g_static_mutex_free (&lock->mutex);