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()
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 GMutex *g_thread_specific_mutex = NULL;
121 static GPrivate *g_thread_specific_private = NULL;
123 /* This must be called only once, before any threads are created.
124 * It will only be called from g_thread_init() in -lgthread.
129 GRealThread* main_thread;
131 /* We let the main thread (the one that calls g_thread_init) inherit
132 * the data, that it set before calling g_thread_init
134 main_thread = (GRealThread*) g_thread_self ();
136 g_thread_specific_private = g_private_new (g_thread_cleanup);
137 G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
138 G_THREAD_UF (thread_self, (&main_thread->system_thread));
140 g_mutex_protect_static_mutex_allocation = g_mutex_new();
141 g_thread_specific_mutex = g_mutex_new();
145 g_static_mutex_get_mutex_impl (GMutex** mutex)
147 if (!g_thread_supported ())
150 g_assert (g_mutex_protect_static_mutex_allocation);
152 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
155 *mutex = g_mutex_new();
157 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
163 g_static_mutex_init (GStaticMutex *mutex)
165 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
167 g_return_if_fail (mutex);
169 memcpy (mutex, &init_mutex, sizeof (GStaticMutex));
173 g_static_mutex_free (GStaticMutex* mutex)
175 GMutex **runtime_mutex;
177 g_return_if_fail (mutex);
179 /* The runtime_mutex is the first (or only) member of GStaticMutex,
180 * see both versions (of glibconfig.h) in configure.in */
181 runtime_mutex = ((GMutex**)mutex);
184 g_mutex_free (*runtime_mutex);
186 *runtime_mutex = NULL;
190 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
194 g_return_if_fail (mutex);
196 if (!g_thread_supported ())
199 G_THREAD_UF (thread_self, (&self));
201 if (g_system_thread_equal (self, mutex->owner))
206 g_static_mutex_lock (&mutex->mutex);
207 g_system_thread_assign (mutex->owner, self);
212 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
216 g_return_val_if_fail (mutex, FALSE);
218 if (!g_thread_supported ())
221 G_THREAD_UF (thread_self, (&self));
223 if (g_system_thread_equal (self, mutex->owner))
229 if (!g_static_mutex_trylock (&mutex->mutex))
232 g_system_thread_assign (mutex->owner, self);
238 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
240 g_return_if_fail (mutex);
242 if (!g_thread_supported ())
245 if (mutex->depth > 1)
250 g_system_thread_assign (mutex->owner, zero_thread);
251 g_static_mutex_unlock (&mutex->mutex);
255 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
258 g_return_if_fail (mutex);
260 if (!g_thread_supported ())
263 g_static_mutex_lock (&mutex->mutex);
264 G_THREAD_UF (thread_self, (&mutex->owner));
265 mutex->depth = depth;
269 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
273 g_return_val_if_fail (mutex, 0);
275 if (!g_thread_supported ())
278 depth = mutex->depth;
280 g_system_thread_assign (mutex->owner, zero_thread);
282 g_static_mutex_unlock (&mutex->mutex);
289 g_static_private_get (GStaticPrivate *private_key)
291 return g_static_private_get_for_thread (private_key, g_thread_self ());
295 g_static_private_get_for_thread (GStaticPrivate *private_key,
299 GRealThread *self = (GRealThread*) thread;
301 g_return_val_if_fail (thread, NULL);
303 array = self->private_data;
307 if (!private_key->index)
309 else if (private_key->index <= array->len)
310 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
316 g_static_private_set (GStaticPrivate *private_key,
318 GDestroyNotify notify)
320 g_static_private_set_for_thread (private_key, g_thread_self (),
325 g_static_private_set_for_thread (GStaticPrivate *private_key,
328 GDestroyNotify notify)
331 GRealThread *self =(GRealThread*) thread;
332 static guint next_index = 0;
333 GStaticPrivateNode *node;
335 g_return_if_fail (thread);
337 array = self->private_data;
340 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
341 self->private_data = array;
344 if (!private_key->index)
346 g_mutex_lock (g_thread_specific_mutex);
348 if (!private_key->index)
349 private_key->index = ++next_index;
351 g_mutex_unlock (g_thread_specific_mutex);
354 if (private_key->index > array->len)
355 g_array_set_size (array, private_key->index);
357 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
360 gpointer ddata = node->data;
361 GDestroyNotify ddestroy = node->destroy;
364 node->destroy = notify;
371 node->destroy = notify;
376 g_thread_cleanup (gpointer data)
380 GRealThread* thread = data;
381 if (thread->private_data)
383 GArray* array = thread->private_data;
386 for (i = 0; i < array->len; i++ )
388 GStaticPrivateNode *node =
389 &g_array_index (array, GStaticPrivateNode, i);
391 node->destroy (node->data);
393 g_array_free (array, TRUE);
395 /* We only free the thread structure, if it isn't joinable. If
396 it is, the structure is freed in g_thread_join */
397 if (!thread->thread.joinable)
399 /* Just to make sure, this isn't used any more */
400 g_system_thread_assign (thread->system_thread, zero_thread);
409 g_error ("The thread system is not yet initialized.");
412 G_LOCK_DEFINE_STATIC (g_thread_create);
415 g_thread_create_proxy (gpointer data)
417 GRealThread* thread = data;
421 /* This has to happen before G_LOCK, as that might call g_thread_self */
422 g_private_set (g_thread_specific_private, data);
424 /* the lock makes sure, that thread->system_thread is written,
425 before thread->func is called. See g_thread_create. */
426 G_LOCK (g_thread_create);
427 G_UNLOCK (g_thread_create);
429 thread->func (thread->arg);
433 g_thread_create (GThreadFunc thread_func,
438 GThreadPriority priority,
441 GRealThread* result = g_new (GRealThread, 1);
442 GError *local_error = NULL;
443 g_return_val_if_fail (thread_func, NULL);
444 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
445 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
447 result->thread.joinable = joinable;
448 result->thread.bound = bound;
449 result->thread.priority = priority;
450 result->func = thread_func;
452 result->private_data = NULL;
453 G_LOCK (g_thread_create);
454 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
455 stack_size, joinable, bound, priority,
456 &result->system_thread, &local_error));
457 G_UNLOCK (g_thread_create);
461 g_propagate_error (error, local_error);
466 return (GThread*) result;
470 g_thread_join (GThread* thread)
472 GRealThread* real = (GRealThread*) thread;
475 g_return_if_fail (thread);
476 g_return_if_fail (thread->joinable);
477 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
479 G_THREAD_UF (thread_join, (&real->system_thread));
481 /* Just to make sure, this isn't used any more */
482 thread->joinable = 0;
483 g_system_thread_assign (real->system_thread, zero_thread);
485 /* the thread structure for non-joinable threads is freed upon
486 thread end. We free the memory here. This will leave loose end,
487 if a joinable thread is not joined. */
493 g_thread_set_priority (GThread* thread,
494 GThreadPriority priority)
496 GRealThread* real = (GRealThread*) thread;
498 g_return_if_fail (thread);
499 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
500 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
501 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
503 thread->priority = priority;
504 G_THREAD_CF (thread_set_priority, (void)0, (&real->system_thread, priority));
510 GRealThread* thread = g_private_get (g_thread_specific_private);
514 /* If no thread data is available, provide and set one. This
515 can happen for the main thread and for threads, that are not
517 thread = g_new (GRealThread, 1);
518 thread->thread.joinable = FALSE; /* This is a save guess */
519 thread->thread.bound = TRUE; /* This isn't important at all */
520 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
524 thread->private_data = NULL;
526 if (g_thread_supported ())
527 G_THREAD_UF (thread_self, (&thread->system_thread));
529 g_private_set (g_thread_specific_private, thread);
532 return (GThread*)thread;
535 static void inline g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
538 *cond = g_cond_new ();
539 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
542 static void inline g_static_rw_lock_signal (GStaticRWLock* lock)
544 if (lock->want_to_write && lock->write_cond)
545 g_cond_signal (lock->write_cond);
546 else if (lock->read_cond)
547 g_cond_broadcast (lock->read_cond);
550 void g_static_rw_lock_reader_lock (GStaticRWLock* lock)
552 g_return_if_fail (lock);
554 if (!g_threads_got_initialized)
557 g_static_mutex_lock (&lock->mutex);
558 while (lock->write || lock->want_to_write)
559 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
560 lock->read_counter++;
561 g_static_mutex_unlock (&lock->mutex);
564 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
566 gboolean ret_val = FALSE;
568 g_return_val_if_fail (lock, FALSE);
570 if (!g_threads_got_initialized)
573 g_static_mutex_lock (&lock->mutex);
574 if (!lock->write && !lock->want_to_write)
576 lock->read_counter++;
579 g_static_mutex_unlock (&lock->mutex);
583 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
585 g_return_if_fail (lock);
587 if (!g_threads_got_initialized)
590 g_static_mutex_lock (&lock->mutex);
591 lock->read_counter--;
592 if (lock->read_counter == 0)
593 g_static_rw_lock_signal (lock);
594 g_static_mutex_unlock (&lock->mutex);
597 void g_static_rw_lock_writer_lock (GStaticRWLock* lock)
599 g_return_if_fail (lock);
601 if (!g_threads_got_initialized)
604 g_static_mutex_lock (&lock->mutex);
605 lock->want_to_write++;
606 while (lock->write || lock->read_counter)
607 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
608 lock->want_to_write--;
610 g_static_mutex_unlock (&lock->mutex);
613 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
615 gboolean ret_val = FALSE;
617 g_return_val_if_fail (lock, FALSE);
619 if (!g_threads_got_initialized)
622 g_static_mutex_lock (&lock->mutex);
623 if (!lock->write && !lock->read_counter)
628 g_static_mutex_unlock (&lock->mutex);
632 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
634 g_return_if_fail (lock);
636 if (!g_threads_got_initialized)
639 g_static_mutex_lock (&lock->mutex);
641 g_static_rw_lock_signal (lock);
642 g_static_mutex_unlock (&lock->mutex);
645 void g_static_rw_lock_free (GStaticRWLock* lock)
647 g_return_if_fail (lock);
650 g_cond_free (lock->read_cond);
651 if (lock->write_cond)
652 g_cond_free (lock->write_cond);