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/.
42 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
43 # define g_system_thread_equal(thread1, thread2) \
44 (thread1.dummy_pointer == thread2.dummy_pointer)
45 # define g_system_thread_assign(dest, src) \
46 (dest.dummy_pointer = src.dummy_pointer)
47 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
48 # define g_system_thread_equal(thread1, thread2) \
49 (memcmp (&thread1, &thread2, GLIB_SIZEOF_SYSTEM_THREAD) == 0)
50 # define g_system_thread_assign(dest, src) \
51 (memcpy (&dest, &src, GLIB_SIZEOF_SYSTEM_THREAD))
52 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
55 g_thread_error_quark (void)
59 quark = g_quark_from_static_string ("g_thread_error");
63 typedef struct _GRealThread GRealThread;
69 gpointer private_data;
70 GSystemThread system_thread;
73 typedef struct _GStaticPrivateNode GStaticPrivateNode;
74 struct _GStaticPrivateNode
77 GDestroyNotify destroy;
80 static void g_thread_cleanup (gpointer data);
81 static void g_thread_fail (void);
83 /* Global variables */
85 static GSystemThread zero_thread; /* This is initialized to all zero */
86 gboolean g_thread_use_default_impl = TRUE;
87 gboolean g_threads_got_initialized = FALSE;
89 #if defined(G_OS_WIN32) && defined(__GNUC__)
92 GThreadFunctions g_thread_functions_for_glib_use = {
93 (GMutex*(*)())g_thread_fail, /* mutex_new */
94 NULL, /* mutex_lock */
95 NULL, /* mutex_trylock */
96 NULL, /* mutex_unlock */
97 NULL, /* mutex_free */
98 (GCond*(*)())g_thread_fail, /* cond_new */
99 NULL, /* cond_signal */
100 NULL, /* cond_broadcast */
101 NULL, /* cond_wait */
102 NULL, /* cond_timed_wait */
103 NULL, /* cond_free */
104 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
105 NULL, /* private_get */
106 NULL, /* private_set */
107 (void(*)(GThreadFunc, gpointer, gulong,
108 gboolean, gboolean, GThreadPriority,
109 gpointer, GError**))g_thread_fail, /* thread_create */
110 NULL, /* thread_yield */
111 NULL, /* thread_join */
112 NULL, /* thread_exit */
113 NULL, /* thread_set_priority */
114 NULL /* thread_self */
119 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
120 static GPrivate *g_thread_specific_private = NULL;
121 static GSList *g_thread_all_threads = NULL;
122 static GSList *g_thread_free_indeces = NULL;
124 G_LOCK_DEFINE_STATIC (g_thread);
126 /* This must be called only once, before any threads are created.
127 * It will only be called from g_thread_init() in -lgthread.
132 GRealThread* main_thread;
134 /* We let the main thread (the one that calls g_thread_init) inherit
135 * the data, that it set before calling g_thread_init
137 main_thread = (GRealThread*) g_thread_self ();
139 g_thread_specific_private = g_private_new (g_thread_cleanup);
140 G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
141 G_THREAD_UF (thread_self, (&main_thread->system_thread));
143 g_mutex_protect_static_mutex_allocation = g_mutex_new ();
147 g_static_mutex_init (GStaticMutex *mutex)
149 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
151 g_return_if_fail (mutex);
153 memcpy (mutex, &init_mutex, sizeof (GStaticMutex));
157 g_static_mutex_get_mutex_impl (GMutex** mutex)
159 if (!g_thread_supported ())
162 g_assert (g_mutex_protect_static_mutex_allocation);
164 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
167 *mutex = g_mutex_new ();
169 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
175 g_static_mutex_free (GStaticMutex* mutex)
177 GMutex **runtime_mutex;
179 g_return_if_fail (mutex);
181 /* The runtime_mutex is the first (or only) member of GStaticMutex,
182 * see both versions (of glibconfig.h) in configure.in */
183 runtime_mutex = ((GMutex**)mutex);
186 g_mutex_free (*runtime_mutex);
188 *runtime_mutex = NULL;
192 g_static_rec_mutex_init (GStaticRecMutex *mutex)
194 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
196 g_return_if_fail (mutex);
198 memcpy (mutex, &init_mutex, sizeof (GStaticRecMutex));
202 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
206 g_return_if_fail (mutex);
208 if (!g_thread_supported ())
211 G_THREAD_UF (thread_self, (&self));
213 if (g_system_thread_equal (self, mutex->owner))
218 g_static_mutex_lock (&mutex->mutex);
219 g_system_thread_assign (mutex->owner, self);
224 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
228 g_return_val_if_fail (mutex, FALSE);
230 if (!g_thread_supported ())
233 G_THREAD_UF (thread_self, (&self));
235 if (g_system_thread_equal (self, mutex->owner))
241 if (!g_static_mutex_trylock (&mutex->mutex))
244 g_system_thread_assign (mutex->owner, self);
250 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
252 g_return_if_fail (mutex);
254 if (!g_thread_supported ())
257 if (mutex->depth > 1)
262 g_system_thread_assign (mutex->owner, zero_thread);
263 g_static_mutex_unlock (&mutex->mutex);
267 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
270 g_return_if_fail (mutex);
272 if (!g_thread_supported ())
275 g_static_mutex_lock (&mutex->mutex);
276 G_THREAD_UF (thread_self, (&mutex->owner));
277 mutex->depth = depth;
281 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
285 g_return_val_if_fail (mutex, 0);
287 if (!g_thread_supported ())
290 depth = mutex->depth;
292 g_system_thread_assign (mutex->owner, zero_thread);
294 g_static_mutex_unlock (&mutex->mutex);
300 g_static_rec_mutex_free (GStaticRecMutex *mutex)
302 g_return_if_fail (mutex);
304 g_static_mutex_free (&mutex->mutex);
308 g_static_private_init (GStaticPrivate *private_key)
310 private_key->index = 0;
314 g_static_private_get (GStaticPrivate *private_key)
316 return g_static_private_get_for_thread (private_key, g_thread_self ());
320 g_static_private_get_for_thread (GStaticPrivate *private_key,
324 GRealThread *self = (GRealThread*) thread;
326 g_return_val_if_fail (thread, NULL);
328 array = self->private_data;
332 if (!private_key->index)
334 else if (private_key->index <= array->len)
335 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
341 g_static_private_set (GStaticPrivate *private_key,
343 GDestroyNotify notify)
345 g_static_private_set_for_thread (private_key, g_thread_self (),
350 g_static_private_set_for_thread (GStaticPrivate *private_key,
353 GDestroyNotify notify)
356 GRealThread *self =(GRealThread*) thread;
357 static guint next_index = 0;
358 GStaticPrivateNode *node;
360 g_return_if_fail (thread);
362 array = self->private_data;
365 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
366 self->private_data = array;
369 if (!private_key->index)
373 if (!private_key->index)
375 if (g_thread_free_indeces)
378 GPOINTER_TO_UINT (g_thread_free_indeces->data);
379 g_thread_free_indeces =
380 g_slist_delete_link (g_thread_free_indeces,
381 g_thread_free_indeces);
384 private_key->index = ++next_index;
390 if (private_key->index > array->len)
391 g_array_set_size (array, private_key->index);
393 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
396 gpointer ddata = node->data;
397 GDestroyNotify ddestroy = node->destroy;
400 node->destroy = notify;
407 node->destroy = notify;
412 g_static_private_free (GStaticPrivate *private_key)
414 GStaticPrivate copied_key;
417 copied_key.index = private_key->index;
418 private_key->index = 0;
420 if (!copied_key.index)
424 list = g_thread_all_threads;
427 GThread *thread = list->data;
431 g_static_private_set_for_thread (&copied_key, thread, NULL, NULL);
434 g_thread_free_indeces =
435 g_slist_prepend (g_thread_free_indeces,
436 GUINT_TO_POINTER (copied_key.index));
441 g_thread_cleanup (gpointer data)
445 GRealThread* thread = data;
446 if (thread->private_data)
448 GArray* array = thread->private_data;
451 for (i = 0; i < array->len; i++ )
453 GStaticPrivateNode *node =
454 &g_array_index (array, GStaticPrivateNode, i);
456 node->destroy (node->data);
458 g_array_free (array, TRUE);
460 /* We only free the thread structure, if it isn't joinable. If
461 it is, the structure is freed in g_thread_join */
462 if (!thread->thread.joinable)
465 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
468 /* Just to make sure, this isn't used any more */
469 g_system_thread_assign (thread->system_thread, zero_thread);
478 g_error ("The thread system is not yet initialized.");
482 g_thread_create_proxy (gpointer data)
484 GRealThread* thread = data;
488 /* This has to happen before G_LOCK, as that might call g_thread_self */
489 g_private_set (g_thread_specific_private, data);
491 /* the lock makes sure, that thread->system_thread is written,
492 before thread->func is called. See g_thread_create. */
496 thread->func (thread->arg);
500 g_thread_create (GThreadFunc thread_func,
505 GThreadPriority priority,
508 GRealThread* result = g_new (GRealThread, 1);
509 GError *local_error = NULL;
510 g_return_val_if_fail (thread_func, NULL);
511 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
512 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
514 result->thread.joinable = joinable;
515 result->thread.bound = bound;
516 result->thread.priority = priority;
517 result->func = thread_func;
519 result->private_data = NULL;
521 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
522 stack_size, joinable, bound, priority,
523 &result->system_thread, &local_error));
524 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
529 g_propagate_error (error, local_error);
534 return (GThread*) result;
538 g_thread_join (GThread* thread)
540 GRealThread* real = (GRealThread*) thread;
543 g_return_if_fail (thread);
544 g_return_if_fail (thread->joinable);
545 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
547 G_THREAD_UF (thread_join, (&real->system_thread));
550 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
553 /* Just to make sure, this isn't used any more */
554 thread->joinable = 0;
555 g_system_thread_assign (real->system_thread, zero_thread);
557 /* the thread structure for non-joinable threads is freed upon
558 thread end. We free the memory here. This will leave loose end,
559 if a joinable thread is not joined. */
565 g_thread_set_priority (GThread* thread,
566 GThreadPriority priority)
568 GRealThread* real = (GRealThread*) thread;
570 g_return_if_fail (thread);
571 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
572 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
573 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
575 thread->priority = priority;
576 G_THREAD_CF (thread_set_priority, (void)0, (&real->system_thread, priority));
582 GRealThread* thread = g_private_get (g_thread_specific_private);
586 /* If no thread data is available, provide and set one. This
587 can happen for the main thread and for threads, that are not
589 thread = g_new (GRealThread, 1);
590 thread->thread.joinable = FALSE; /* This is a save guess */
591 thread->thread.bound = TRUE; /* This isn't important at all */
592 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
596 thread->private_data = NULL;
598 if (g_thread_supported ())
599 G_THREAD_UF (thread_self, (&thread->system_thread));
601 g_private_set (g_thread_specific_private, thread);
604 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
608 return (GThread*)thread;
612 g_static_rw_lock_init (GStaticRWLock* lock)
614 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
616 g_return_if_fail (lock);
618 memcpy (lock, &init_lock, sizeof (GStaticRWLock));
622 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
625 *cond = g_cond_new ();
626 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
630 g_static_rw_lock_signal (GStaticRWLock* lock)
632 if (lock->want_to_write && lock->write_cond)
633 g_cond_signal (lock->write_cond);
634 else if (lock->read_cond)
635 g_cond_broadcast (lock->read_cond);
639 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
641 g_return_if_fail (lock);
643 if (!g_threads_got_initialized)
646 g_static_mutex_lock (&lock->mutex);
647 while (lock->write || lock->want_to_write)
648 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
649 lock->read_counter++;
650 g_static_mutex_unlock (&lock->mutex);
654 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
656 gboolean ret_val = FALSE;
658 g_return_val_if_fail (lock, FALSE);
660 if (!g_threads_got_initialized)
663 g_static_mutex_lock (&lock->mutex);
664 if (!lock->write && !lock->want_to_write)
666 lock->read_counter++;
669 g_static_mutex_unlock (&lock->mutex);
674 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
676 g_return_if_fail (lock);
678 if (!g_threads_got_initialized)
681 g_static_mutex_lock (&lock->mutex);
682 lock->read_counter--;
683 if (lock->read_counter == 0)
684 g_static_rw_lock_signal (lock);
685 g_static_mutex_unlock (&lock->mutex);
689 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
691 g_return_if_fail (lock);
693 if (!g_threads_got_initialized)
696 g_static_mutex_lock (&lock->mutex);
697 lock->want_to_write++;
698 while (lock->write || lock->read_counter)
699 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
700 lock->want_to_write--;
702 g_static_mutex_unlock (&lock->mutex);
706 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
708 gboolean ret_val = FALSE;
710 g_return_val_if_fail (lock, FALSE);
712 if (!g_threads_got_initialized)
715 g_static_mutex_lock (&lock->mutex);
716 if (!lock->write && !lock->read_counter)
721 g_static_mutex_unlock (&lock->mutex);
726 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
728 g_return_if_fail (lock);
730 if (!g_threads_got_initialized)
733 g_static_mutex_lock (&lock->mutex);
735 g_static_rw_lock_signal (lock);
736 g_static_mutex_unlock (&lock->mutex);
740 g_static_rw_lock_free (GStaticRWLock* lock)
742 g_return_if_fail (lock);
746 g_cond_free (lock->read_cond);
747 lock->read_cond = NULL;
749 if (lock->write_cond)
751 g_cond_free (lock->write_cond);
752 lock->write_cond = NULL;
754 g_static_mutex_free (&lock->mutex);