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 #endif /* G_THREAD_USE_PID_SURROGATE */
51 #include "gthreadinit.h"
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_once_mutex = NULL;
152 static GCond *g_once_cond = NULL;
153 static GPrivate *g_thread_specific_private = NULL;
154 static GSList *g_thread_all_threads = NULL;
155 static GSList *g_thread_free_indeces = NULL;
157 G_LOCK_DEFINE_STATIC (g_thread);
159 #ifdef G_THREADS_ENABLED
160 /* This must be called only once, before any threads are created.
161 * It will only be called from g_thread_init() in -lgthread.
164 g_thread_init_glib (void)
166 /* We let the main thread (the one that calls g_thread_init) inherit
167 * the static_private data set before calling g_thread_init
169 GRealThread* main_thread = (GRealThread*) g_thread_self ();
171 g_once_mutex = g_mutex_new ();
172 g_once_cond = g_cond_new ();
174 _g_convert_thread_init ();
175 _g_rand_thread_init ();
176 _g_main_thread_init ();
177 _g_mem_thread_init ();
178 _g_messages_thread_init ();
180 #ifdef G_THREAD_USE_PID_SURROGATE
181 priority_map[G_THREAD_PRIORITY_NORMAL] =
182 getpriority (PRIO_PROCESS, (getpid ()));
183 priority_map[G_THREAD_PRIORITY_LOW] =
184 MIN (20, priority_map[G_THREAD_PRIORITY_NORMAL] + 10);
185 priority_map[G_THREAD_PRIORITY_HIGH] =
186 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 10);
187 priority_map[G_THREAD_PRIORITY_URGENT] =
188 MAX (-20, priority_map[G_THREAD_PRIORITY_NORMAL] - 15);
189 #endif /* G_THREAD_USE_PID_SURROGATE */
191 g_threads_got_initialized = TRUE;
193 g_thread_specific_private = g_private_new (g_thread_cleanup);
194 g_private_set (g_thread_specific_private, main_thread);
195 G_THREAD_UF (thread_self, (&main_thread->system_thread));
197 _g_mem_thread_private_init ();
198 _g_messages_thread_private_init ();
201 #endif /* G_THREADS_ENABLED */
204 g_once_impl (GOnce *once,
208 g_mutex_lock (g_once_mutex);
210 while (once->status == G_ONCE_STATUS_PROGRESS)
211 g_cond_wait (g_once_cond, g_once_mutex);
213 if (once->status != G_ONCE_STATUS_READY)
215 once->status = G_ONCE_STATUS_PROGRESS;
216 g_mutex_unlock (g_once_mutex);
218 once->retval = func (arg);
220 g_mutex_lock (g_once_mutex);
221 once->status = G_ONCE_STATUS_READY;
222 g_cond_broadcast (g_once_cond);
225 g_mutex_unlock (g_once_mutex);
231 g_static_mutex_init (GStaticMutex *mutex)
233 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
235 g_return_if_fail (mutex);
241 g_static_mutex_get_mutex_impl (GMutex** mutex)
243 if (!g_thread_supported ())
246 g_assert (g_once_mutex);
248 g_mutex_lock (g_once_mutex);
252 GMutex *new_mutex = g_mutex_new ();
254 /* The following is a memory barrier to avoid the write
255 * to *new_mutex being reordered to after writing *mutex */
256 g_mutex_lock (new_mutex);
257 g_mutex_unlock (new_mutex);
262 g_mutex_unlock (g_once_mutex);
268 g_static_mutex_free (GStaticMutex* mutex)
270 GMutex **runtime_mutex;
272 g_return_if_fail (mutex);
274 /* The runtime_mutex is the first (or only) member of GStaticMutex,
275 * see both versions (of glibconfig.h) in configure.in */
276 runtime_mutex = ((GMutex**)mutex);
279 g_mutex_free (*runtime_mutex);
281 *runtime_mutex = NULL;
285 g_static_rec_mutex_init (GStaticRecMutex *mutex)
287 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
289 g_return_if_fail (mutex);
295 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
299 g_return_if_fail (mutex);
301 if (!g_thread_supported ())
304 G_THREAD_UF (thread_self, (&self));
306 if (g_system_thread_equal (self, mutex->owner))
311 g_static_mutex_lock (&mutex->mutex);
312 g_system_thread_assign (mutex->owner, self);
317 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
321 g_return_val_if_fail (mutex, FALSE);
323 if (!g_thread_supported ())
326 G_THREAD_UF (thread_self, (&self));
328 if (g_system_thread_equal (self, mutex->owner))
334 if (!g_static_mutex_trylock (&mutex->mutex))
337 g_system_thread_assign (mutex->owner, self);
343 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
345 g_return_if_fail (mutex);
347 if (!g_thread_supported ())
350 if (mutex->depth > 1)
355 g_system_thread_assign (mutex->owner, zero_thread);
356 g_static_mutex_unlock (&mutex->mutex);
360 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
364 g_return_if_fail (mutex);
366 if (!g_thread_supported ())
369 G_THREAD_UF (thread_self, (&self));
371 if (g_system_thread_equal (self, mutex->owner))
373 mutex->depth += depth;
376 g_static_mutex_lock (&mutex->mutex);
377 g_system_thread_assign (mutex->owner, self);
378 mutex->depth = depth;
382 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
386 g_return_val_if_fail (mutex, 0);
388 if (!g_thread_supported ())
391 depth = mutex->depth;
393 g_system_thread_assign (mutex->owner, zero_thread);
395 g_static_mutex_unlock (&mutex->mutex);
401 g_static_rec_mutex_free (GStaticRecMutex *mutex)
403 g_return_if_fail (mutex);
405 g_static_mutex_free (&mutex->mutex);
409 g_static_private_init (GStaticPrivate *private_key)
411 private_key->index = 0;
415 g_static_private_get (GStaticPrivate *private_key)
417 GRealThread *self = (GRealThread*) g_thread_self ();
420 array = self->private_data;
424 if (!private_key->index)
426 else if (private_key->index <= array->len)
427 return g_array_index (array, GStaticPrivateNode,
428 private_key->index - 1).data;
434 g_static_private_set (GStaticPrivate *private_key,
436 GDestroyNotify notify)
438 GRealThread *self = (GRealThread*) g_thread_self ();
440 static guint next_index = 0;
441 GStaticPrivateNode *node;
443 array = self->private_data;
446 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
447 self->private_data = array;
450 if (!private_key->index)
454 if (!private_key->index)
456 if (g_thread_free_indeces)
459 GPOINTER_TO_UINT (g_thread_free_indeces->data);
460 g_thread_free_indeces =
461 g_slist_delete_link (g_thread_free_indeces,
462 g_thread_free_indeces);
465 private_key->index = ++next_index;
471 if (private_key->index > array->len)
472 g_array_set_size (array, private_key->index);
474 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
477 gpointer ddata = node->data;
478 GDestroyNotify ddestroy = node->destroy;
481 node->destroy = notify;
488 node->destroy = notify;
493 g_static_private_free (GStaticPrivate *private_key)
495 guint index = private_key->index;
501 private_key->index = 0;
504 list = g_thread_all_threads;
507 GRealThread *thread = list->data;
508 GArray *array = thread->private_data;
511 if (array && index <= array->len)
513 GStaticPrivateNode *node = &g_array_index (array,
516 gpointer ddata = node->data;
517 GDestroyNotify ddestroy = node->destroy;
520 node->destroy = NULL;
530 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
531 GUINT_TO_POINTER (index));
536 g_thread_cleanup (gpointer data)
540 GRealThread* thread = data;
541 if (thread->private_data)
543 GArray* array = thread->private_data;
546 for (i = 0; i < array->len; i++ )
548 GStaticPrivateNode *node =
549 &g_array_index (array, GStaticPrivateNode, i);
551 node->destroy (node->data);
553 g_array_free (array, TRUE);
556 /* We only free the thread structure, if it isn't joinable. If
557 it is, the structure is freed in g_thread_join */
558 if (!thread->thread.joinable)
561 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
564 /* Just to make sure, this isn't used any more */
565 g_system_thread_assign (thread->system_thread, zero_thread);
574 g_error ("The thread system is not yet initialized.");
578 g_thread_create_proxy (gpointer data)
580 GRealThread* thread = data;
584 #ifdef G_THREAD_USE_PID_SURROGATE
585 thread->pid = getpid ();
586 #endif /* G_THREAD_USE_PID_SURROGATE */
588 /* This has to happen before G_LOCK, as that might call g_thread_self */
589 g_private_set (g_thread_specific_private, data);
591 /* the lock makes sure, that thread->system_thread is written,
592 before thread->thread.func is called. See g_thread_create. */
596 #ifdef G_THREAD_USE_PID_SURROGATE
597 if (g_thread_use_default_impl)
598 SET_PRIO (thread->pid, thread->thread.priority);
599 #endif /* G_THREAD_USE_PID_SURROGATE */
601 thread->retval = thread->thread.func (thread->thread.data);
607 g_thread_create_full (GThreadFunc func,
612 GThreadPriority priority,
616 GError *local_error = NULL;
617 g_return_val_if_fail (func, NULL);
618 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
619 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
621 result = g_new (GRealThread, 1);
623 result->thread.joinable = joinable;
624 result->thread.priority = priority;
625 result->thread.func = func;
626 result->thread.data = data;
627 result->private_data = NULL;
629 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
630 stack_size, joinable, bound, priority,
631 &result->system_thread, &local_error));
632 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
637 g_propagate_error (error, local_error);
642 return (GThread*) result;
646 g_thread_exit (gpointer retval)
648 GRealThread* real = (GRealThread*) g_thread_self ();
649 real->retval = retval;
650 G_THREAD_CF (thread_exit, (void)0, ());
654 g_thread_join (GThread* thread)
656 GRealThread* real = (GRealThread*) thread;
659 g_return_val_if_fail (thread, NULL);
660 g_return_val_if_fail (thread->joinable, NULL);
661 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
664 G_THREAD_UF (thread_join, (&real->system_thread));
666 retval = real->retval;
669 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
672 /* Just to make sure, this isn't used any more */
673 thread->joinable = 0;
674 g_system_thread_assign (real->system_thread, zero_thread);
676 /* the thread structure for non-joinable threads is freed upon
677 thread end. We free the memory here. This will leave a loose end,
678 if a joinable thread is not joined. */
686 g_thread_set_priority (GThread* thread,
687 GThreadPriority priority)
689 GRealThread* real = (GRealThread*) thread;
691 g_return_if_fail (thread);
692 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
693 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
694 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
696 thread->priority = priority;
698 #ifdef G_THREAD_USE_PID_SURROGATE
699 if (g_thread_use_default_impl)
700 SET_PRIO (real->pid, priority);
702 #endif /* G_THREAD_USE_PID_SURROGATE */
703 G_THREAD_CF (thread_set_priority, (void)0,
704 (&real->system_thread, priority));
710 GRealThread* thread = g_private_get (g_thread_specific_private);
714 /* If no thread data is available, provide and set one. This
715 can happen for the main thread and for threads, that are not
717 thread = g_new (GRealThread, 1);
718 thread->thread.joinable = FALSE; /* This is a save guess */
719 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
721 thread->thread.func = NULL;
722 thread->thread.data = NULL;
723 thread->private_data = NULL;
725 if (g_thread_supported ())
726 G_THREAD_UF (thread_self, (&thread->system_thread));
728 #ifdef G_THREAD_USE_PID_SURROGATE
729 thread->pid = getpid ();
730 #endif /* G_THREAD_USE_PID_SURROGATE */
732 g_private_set (g_thread_specific_private, thread);
735 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
739 return (GThread*)thread;
743 g_static_rw_lock_init (GStaticRWLock* lock)
745 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
747 g_return_if_fail (lock);
753 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
756 *cond = g_cond_new ();
757 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
761 g_static_rw_lock_signal (GStaticRWLock* lock)
763 if (lock->want_to_write && lock->write_cond)
764 g_cond_signal (lock->write_cond);
765 else if (lock->want_to_read && lock->read_cond)
766 g_cond_broadcast (lock->read_cond);
770 g_static_rw_lock_reader_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_read++;
779 while (lock->have_writer || lock->want_to_write)
780 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
781 lock->want_to_read--;
782 lock->read_counter++;
783 g_static_mutex_unlock (&lock->mutex);
787 g_static_rw_lock_reader_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->have_writer && !lock->want_to_write)
799 lock->read_counter++;
802 g_static_mutex_unlock (&lock->mutex);
807 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
809 g_return_if_fail (lock);
811 if (!g_threads_got_initialized)
814 g_static_mutex_lock (&lock->mutex);
815 lock->read_counter--;
816 if (lock->read_counter == 0)
817 g_static_rw_lock_signal (lock);
818 g_static_mutex_unlock (&lock->mutex);
822 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
824 g_return_if_fail (lock);
826 if (!g_threads_got_initialized)
829 g_static_mutex_lock (&lock->mutex);
830 lock->want_to_write++;
831 while (lock->have_writer || lock->read_counter)
832 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
833 lock->want_to_write--;
834 lock->have_writer = TRUE;
835 g_static_mutex_unlock (&lock->mutex);
839 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
841 gboolean ret_val = FALSE;
843 g_return_val_if_fail (lock, FALSE);
845 if (!g_threads_got_initialized)
848 g_static_mutex_lock (&lock->mutex);
849 if (!lock->have_writer && !lock->read_counter)
851 lock->have_writer = TRUE;
854 g_static_mutex_unlock (&lock->mutex);
859 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
861 g_return_if_fail (lock);
863 if (!g_threads_got_initialized)
866 g_static_mutex_lock (&lock->mutex);
867 lock->have_writer = FALSE;
868 g_static_rw_lock_signal (lock);
869 g_static_mutex_unlock (&lock->mutex);
873 g_static_rw_lock_free (GStaticRWLock* lock)
875 g_return_if_fail (lock);
879 g_cond_free (lock->read_cond);
880 lock->read_cond = NULL;
882 if (lock->write_cond)
884 g_cond_free (lock->write_cond);
885 lock->write_cond = NULL;
887 g_static_mutex_free (&lock->mutex);