1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.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 #include <linux/unistd.h>
43 _syscall0(pid_t,gettid)
44 #endif /* G_THREAD_USE_PID_SURROGATE */
53 #include "gthreadinit.h"
55 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
56 # define g_system_thread_equal_simple(thread1, thread2) \
57 ((thread1).dummy_pointer == (thread2).dummy_pointer)
58 # define g_system_thread_assign(dest, src) \
59 ((dest).dummy_pointer = (src).dummy_pointer)
60 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
61 # define g_system_thread_equal_simple(thread1, thread2) \
62 (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
63 # define g_system_thread_assign(dest, src) \
64 (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
65 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
67 #define g_system_thread_equal(thread1, thread2) \
68 (g_thread_functions_for_glib_use.thread_equal ? \
69 g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
70 g_system_thread_equal_simple((thread1), (thread2)))
73 g_thread_error_quark (void)
77 quark = g_quark_from_static_string ("g_thread_error");
81 /* Keep this in sync with GRealThread in gmain.c! */
82 typedef struct _GRealThread GRealThread;
86 gpointer private_data;
88 GSystemThread system_thread;
89 #ifdef G_THREAD_USE_PID_SURROGATE
91 #endif /* G_THREAD_USE_PID_SURROGATE */
94 #ifdef G_THREAD_USE_PID_SURROGATE
95 static gint priority_map[4];
96 static gboolean prio_warned = FALSE;
97 # define SET_PRIO(tid, prio) G_STMT_START{ \
98 gint error = setpriority (PRIO_PROCESS, (tid), priority_map[prio]); \
99 if (error == -1 && errno == EACCES && !prio_warned) \
101 prio_warned = TRUE; \
102 g_warning ("Priorities can only be increased by root."); \
105 #endif /* G_THREAD_USE_PID_SURROGATE */
107 typedef struct _GStaticPrivateNode GStaticPrivateNode;
108 struct _GStaticPrivateNode
111 GDestroyNotify destroy;
114 static void g_thread_cleanup (gpointer data);
115 static void g_thread_fail (void);
117 /* Global variables */
119 static GSystemThread zero_thread; /* This is initialized to all zero */
120 gboolean g_thread_use_default_impl = TRUE;
121 gboolean g_threads_got_initialized = FALSE;
123 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
124 __declspec(dllexport)
126 GThreadFunctions g_thread_functions_for_glib_use = {
127 (GMutex*(*)())g_thread_fail, /* mutex_new */
128 NULL, /* mutex_lock */
129 NULL, /* mutex_trylock */
130 NULL, /* mutex_unlock */
131 NULL, /* mutex_free */
132 (GCond*(*)())g_thread_fail, /* cond_new */
133 NULL, /* cond_signal */
134 NULL, /* cond_broadcast */
135 NULL, /* cond_wait */
136 NULL, /* cond_timed_wait */
137 NULL, /* cond_free */
138 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
139 NULL, /* private_get */
140 NULL, /* private_set */
141 (void(*)(GThreadFunc, gpointer, gulong,
142 gboolean, gboolean, GThreadPriority,
143 gpointer, GError**))g_thread_fail, /* thread_create */
144 NULL, /* thread_yield */
145 NULL, /* thread_join */
146 NULL, /* thread_exit */
147 NULL, /* thread_set_priority */
148 NULL /* thread_self */
153 static GMutex *g_once_mutex = NULL;
154 static GCond *g_once_cond = NULL;
155 static GPrivate *g_thread_specific_private = NULL;
156 static GSList *g_thread_all_threads = NULL;
157 static GSList *g_thread_free_indeces = NULL;
159 G_LOCK_DEFINE_STATIC (g_thread);
161 #ifdef G_THREADS_ENABLED
162 /* This must be called only once, before any threads are created.
163 * It will only be called from g_thread_init() in -lgthread.
166 g_thread_init_glib (void)
168 /* We let the main thread (the one that calls g_thread_init) inherit
169 * the static_private data set before calling g_thread_init
171 GRealThread* main_thread = (GRealThread*) g_thread_self ();
173 g_once_mutex = g_mutex_new ();
174 g_once_cond = g_cond_new ();
176 _g_convert_thread_init ();
177 _g_rand_thread_init ();
178 _g_main_thread_init ();
179 _g_mem_thread_init ();
180 _g_messages_thread_init ();
182 #ifdef G_THREAD_USE_PID_SURROGATE
183 priority_map[G_THREAD_PRIORITY_NORMAL] =
184 getpriority (PRIO_PROCESS, (gettid ()));
185 priority_map[G_THREAD_PRIORITY_LOW] =
186 MIN (20, priority_map[G_THREAD_PRIORITY_NORMAL] + 10);
187 priority_map[G_THREAD_PRIORITY_HIGH] =
188 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 10);
189 priority_map[G_THREAD_PRIORITY_URGENT] =
190 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 15);
191 #endif /* G_THREAD_USE_PID_SURROGATE */
193 g_threads_got_initialized = TRUE;
195 g_thread_specific_private = g_private_new (g_thread_cleanup);
196 g_private_set (g_thread_specific_private, main_thread);
197 G_THREAD_UF (thread_self, (&main_thread->system_thread));
199 _g_mem_thread_private_init ();
200 _g_messages_thread_private_init ();
203 #endif /* G_THREADS_ENABLED */
206 g_once_impl (GOnce *once,
210 g_mutex_lock (g_once_mutex);
212 while (once->status == G_ONCE_STATUS_PROGRESS)
213 g_cond_wait (g_once_cond, g_once_mutex);
215 if (once->status != G_ONCE_STATUS_READY)
217 once->status = G_ONCE_STATUS_PROGRESS;
218 g_mutex_unlock (g_once_mutex);
220 once->retval = func (arg);
222 g_mutex_lock (g_once_mutex);
223 once->status = G_ONCE_STATUS_READY;
224 g_cond_broadcast (g_once_cond);
227 g_mutex_unlock (g_once_mutex);
233 g_static_mutex_init (GStaticMutex *mutex)
235 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
237 g_return_if_fail (mutex);
243 g_static_mutex_get_mutex_impl (GMutex** mutex)
245 if (!g_thread_supported ())
248 g_assert (g_once_mutex);
250 g_mutex_lock (g_once_mutex);
254 GMutex *new_mutex = g_mutex_new ();
256 /* The following is a memory barrier to avoid the write
257 * to *new_mutex being reordered to after writing *mutex */
258 g_mutex_lock (new_mutex);
259 g_mutex_unlock (new_mutex);
264 g_mutex_unlock (g_once_mutex);
270 g_static_mutex_free (GStaticMutex* mutex)
272 GMutex **runtime_mutex;
274 g_return_if_fail (mutex);
276 /* The runtime_mutex is the first (or only) member of GStaticMutex,
277 * see both versions (of glibconfig.h) in configure.in */
278 runtime_mutex = ((GMutex**)mutex);
281 g_mutex_free (*runtime_mutex);
283 *runtime_mutex = NULL;
287 g_static_rec_mutex_init (GStaticRecMutex *mutex)
289 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
291 g_return_if_fail (mutex);
297 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
301 g_return_if_fail (mutex);
303 if (!g_thread_supported ())
306 G_THREAD_UF (thread_self, (&self));
308 if (g_system_thread_equal (self, mutex->owner))
313 g_static_mutex_lock (&mutex->mutex);
314 g_system_thread_assign (mutex->owner, self);
319 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
323 g_return_val_if_fail (mutex, FALSE);
325 if (!g_thread_supported ())
328 G_THREAD_UF (thread_self, (&self));
330 if (g_system_thread_equal (self, mutex->owner))
336 if (!g_static_mutex_trylock (&mutex->mutex))
339 g_system_thread_assign (mutex->owner, self);
345 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
347 g_return_if_fail (mutex);
349 if (!g_thread_supported ())
352 if (mutex->depth > 1)
357 g_system_thread_assign (mutex->owner, zero_thread);
358 g_static_mutex_unlock (&mutex->mutex);
362 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
366 g_return_if_fail (mutex);
368 if (!g_thread_supported ())
371 G_THREAD_UF (thread_self, (&self));
373 if (g_system_thread_equal (self, mutex->owner))
375 mutex->depth += depth;
378 g_static_mutex_lock (&mutex->mutex);
379 g_system_thread_assign (mutex->owner, self);
380 mutex->depth = depth;
384 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
388 g_return_val_if_fail (mutex, 0);
390 if (!g_thread_supported ())
393 depth = mutex->depth;
395 g_system_thread_assign (mutex->owner, zero_thread);
397 g_static_mutex_unlock (&mutex->mutex);
403 g_static_rec_mutex_free (GStaticRecMutex *mutex)
405 g_return_if_fail (mutex);
407 g_static_mutex_free (&mutex->mutex);
411 g_static_private_init (GStaticPrivate *private_key)
413 private_key->index = 0;
417 g_static_private_get (GStaticPrivate *private_key)
419 GRealThread *self = (GRealThread*) g_thread_self ();
422 array = self->private_data;
426 if (!private_key->index)
428 else if (private_key->index <= array->len)
429 return g_array_index (array, GStaticPrivateNode,
430 private_key->index - 1).data;
436 g_static_private_set (GStaticPrivate *private_key,
438 GDestroyNotify notify)
440 GRealThread *self = (GRealThread*) g_thread_self ();
442 static guint next_index = 0;
443 GStaticPrivateNode *node;
445 array = self->private_data;
448 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
449 self->private_data = array;
452 if (!private_key->index)
456 if (!private_key->index)
458 if (g_thread_free_indeces)
461 GPOINTER_TO_UINT (g_thread_free_indeces->data);
462 g_thread_free_indeces =
463 g_slist_delete_link (g_thread_free_indeces,
464 g_thread_free_indeces);
467 private_key->index = ++next_index;
473 if (private_key->index > array->len)
474 g_array_set_size (array, private_key->index);
476 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
479 gpointer ddata = node->data;
480 GDestroyNotify ddestroy = node->destroy;
483 node->destroy = notify;
490 node->destroy = notify;
495 g_static_private_free (GStaticPrivate *private_key)
497 guint index = private_key->index;
503 private_key->index = 0;
506 list = g_thread_all_threads;
509 GRealThread *thread = list->data;
510 GArray *array = thread->private_data;
513 if (array && index <= array->len)
515 GStaticPrivateNode *node = &g_array_index (array,
518 gpointer ddata = node->data;
519 GDestroyNotify ddestroy = node->destroy;
522 node->destroy = NULL;
532 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
533 GUINT_TO_POINTER (index));
538 g_thread_cleanup (gpointer data)
542 GRealThread* thread = data;
543 if (thread->private_data)
545 GArray* array = thread->private_data;
548 for (i = 0; i < array->len; i++ )
550 GStaticPrivateNode *node =
551 &g_array_index (array, GStaticPrivateNode, i);
553 node->destroy (node->data);
555 g_array_free (array, TRUE);
558 /* We only free the thread structure, if it isn't joinable. If
559 it is, the structure is freed in g_thread_join */
560 if (!thread->thread.joinable)
563 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
566 /* Just to make sure, this isn't used any more */
567 g_system_thread_assign (thread->system_thread, zero_thread);
576 g_error ("The thread system is not yet initialized.");
580 g_thread_create_proxy (gpointer data)
582 GRealThread* thread = data;
586 #ifdef G_THREAD_USE_PID_SURROGATE
587 thread->tid = gettid ();
588 #endif /* G_THREAD_USE_PID_SURROGATE */
590 /* This has to happen before G_LOCK, as that might call g_thread_self */
591 g_private_set (g_thread_specific_private, data);
593 /* the lock makes sure, that thread->system_thread is written,
594 before thread->thread.func is called. See g_thread_create. */
598 #ifdef G_THREAD_USE_PID_SURROGATE
599 if (g_thread_use_default_impl)
600 SET_PRIO (thread->tid, thread->thread.priority);
601 #endif /* G_THREAD_USE_PID_SURROGATE */
603 thread->retval = thread->thread.func (thread->thread.data);
609 g_thread_create_full (GThreadFunc func,
614 GThreadPriority priority,
618 GError *local_error = NULL;
619 g_return_val_if_fail (func, NULL);
620 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
621 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
623 result = g_new (GRealThread, 1);
625 result->thread.joinable = joinable;
626 result->thread.priority = priority;
627 result->thread.func = func;
628 result->thread.data = data;
629 result->private_data = NULL;
631 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
632 stack_size, joinable, bound, priority,
633 &result->system_thread, &local_error));
634 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
639 g_propagate_error (error, local_error);
644 return (GThread*) result;
648 g_thread_exit (gpointer retval)
650 GRealThread* real = (GRealThread*) g_thread_self ();
651 real->retval = retval;
652 G_THREAD_CF (thread_exit, (void)0, ());
656 g_thread_join (GThread* thread)
658 GRealThread* real = (GRealThread*) thread;
661 g_return_val_if_fail (thread, NULL);
662 g_return_val_if_fail (thread->joinable, NULL);
663 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
666 G_THREAD_UF (thread_join, (&real->system_thread));
668 retval = real->retval;
671 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
674 /* Just to make sure, this isn't used any more */
675 thread->joinable = 0;
676 g_system_thread_assign (real->system_thread, zero_thread);
678 /* the thread structure for non-joinable threads is freed upon
679 thread end. We free the memory here. This will leave a loose end,
680 if a joinable thread is not joined. */
688 g_thread_set_priority (GThread* thread,
689 GThreadPriority priority)
691 GRealThread* real = (GRealThread*) thread;
693 g_return_if_fail (thread);
694 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
695 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
696 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
698 thread->priority = priority;
700 #ifdef G_THREAD_USE_PID_SURROGATE
701 if (g_thread_use_default_impl)
702 SET_PRIO (real->tid, priority);
704 #endif /* G_THREAD_USE_PID_SURROGATE */
705 G_THREAD_CF (thread_set_priority, (void)0,
706 (&real->system_thread, priority));
712 GRealThread* thread = g_private_get (g_thread_specific_private);
716 /* If no thread data is available, provide and set one. This
717 can happen for the main thread and for threads, that are not
719 thread = g_new (GRealThread, 1);
720 thread->thread.joinable = FALSE; /* This is a save guess */
721 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
723 thread->thread.func = NULL;
724 thread->thread.data = NULL;
725 thread->private_data = NULL;
727 if (g_thread_supported ())
728 G_THREAD_UF (thread_self, (&thread->system_thread));
730 #ifdef G_THREAD_USE_PID_SURROGATE
731 thread->tid = gettid ();
732 #endif /* G_THREAD_USE_PID_SURROGATE */
734 g_private_set (g_thread_specific_private, thread);
737 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
741 return (GThread*)thread;
745 g_static_rw_lock_init (GStaticRWLock* lock)
747 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
749 g_return_if_fail (lock);
755 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
758 *cond = g_cond_new ();
759 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
763 g_static_rw_lock_signal (GStaticRWLock* lock)
765 if (lock->want_to_write && lock->write_cond)
766 g_cond_signal (lock->write_cond);
767 else if (lock->want_to_read && lock->read_cond)
768 g_cond_broadcast (lock->read_cond);
772 g_static_rw_lock_reader_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_read++;
781 while (lock->have_writer || lock->want_to_write)
782 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
783 lock->want_to_read--;
784 lock->read_counter++;
785 g_static_mutex_unlock (&lock->mutex);
789 g_static_rw_lock_reader_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->have_writer && !lock->want_to_write)
801 lock->read_counter++;
804 g_static_mutex_unlock (&lock->mutex);
809 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
811 g_return_if_fail (lock);
813 if (!g_threads_got_initialized)
816 g_static_mutex_lock (&lock->mutex);
817 lock->read_counter--;
818 if (lock->read_counter == 0)
819 g_static_rw_lock_signal (lock);
820 g_static_mutex_unlock (&lock->mutex);
824 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
826 g_return_if_fail (lock);
828 if (!g_threads_got_initialized)
831 g_static_mutex_lock (&lock->mutex);
832 lock->want_to_write++;
833 while (lock->have_writer || lock->read_counter)
834 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
835 lock->want_to_write--;
836 lock->have_writer = TRUE;
837 g_static_mutex_unlock (&lock->mutex);
841 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
843 gboolean ret_val = FALSE;
845 g_return_val_if_fail (lock, FALSE);
847 if (!g_threads_got_initialized)
850 g_static_mutex_lock (&lock->mutex);
851 if (!lock->have_writer && !lock->read_counter)
853 lock->have_writer = TRUE;
856 g_static_mutex_unlock (&lock->mutex);
861 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
863 g_return_if_fail (lock);
865 if (!g_threads_got_initialized)
868 g_static_mutex_lock (&lock->mutex);
869 lock->have_writer = FALSE;
870 g_static_rw_lock_signal (lock);
871 g_static_mutex_unlock (&lock->mutex);
875 g_static_rw_lock_free (GStaticRWLock* lock)
877 g_return_if_fail (lock);
881 g_cond_free (lock->read_cond);
882 lock->read_cond = NULL;
884 if (lock->write_cond)
886 g_cond_free (lock->write_cond);
887 lock->write_cond = NULL;
889 g_static_mutex_free (&lock->mutex);