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_free (GStaticMutex* mutex)
165 GMutex **runtime_mutex;
167 g_return_if_fail (mutex);
169 /* The runtime_mutex is the first (or only) member of GStaticMutex,
170 * see both versions (of glibconfig.h) in configure.in */
171 runtime_mutex = ((GMutex**)mutex);
174 g_mutex_free (*runtime_mutex);
176 *runtime_mutex = NULL;
180 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
184 g_return_if_fail (mutex);
186 if (!g_thread_supported ())
189 G_THREAD_UF (thread_self, (&self));
191 if (g_system_thread_equal (self, mutex->owner))
196 g_static_mutex_lock (&mutex->mutex);
197 g_system_thread_assign (mutex->owner, self);
202 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
206 g_return_val_if_fail (mutex, FALSE);
208 if (!g_thread_supported ())
211 G_THREAD_UF (thread_self, (&self));
213 if (g_system_thread_equal (self, mutex->owner))
219 if (!g_static_mutex_trylock (&mutex->mutex))
222 g_system_thread_assign (mutex->owner, self);
228 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
230 g_return_if_fail (mutex);
232 if (!g_thread_supported ())
235 if (mutex->depth > 1)
240 g_system_thread_assign (mutex->owner, zero_thread);
241 g_static_mutex_unlock (&mutex->mutex);
245 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
248 g_return_if_fail (mutex);
250 if (!g_thread_supported ())
253 g_static_mutex_lock (&mutex->mutex);
254 G_THREAD_UF (thread_self, (&mutex->owner));
255 mutex->depth = depth;
259 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
263 g_return_val_if_fail (mutex, 0);
265 if (!g_thread_supported ())
268 depth = mutex->depth;
270 g_system_thread_assign (mutex->owner, zero_thread);
272 g_static_mutex_unlock (&mutex->mutex);
279 g_static_private_get (GStaticPrivate *private_key)
281 return g_static_private_get_for_thread (private_key, g_thread_self ());
285 g_static_private_get_for_thread (GStaticPrivate *private_key,
289 GRealThread *self = (GRealThread*) thread;
291 g_return_val_if_fail (thread, NULL);
293 array = self->private_data;
297 if (!private_key->index)
299 else if (private_key->index <= array->len)
300 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
306 g_static_private_set (GStaticPrivate *private_key,
308 GDestroyNotify notify)
310 g_static_private_set_for_thread (private_key, g_thread_self (),
315 g_static_private_set_for_thread (GStaticPrivate *private_key,
318 GDestroyNotify notify)
321 GRealThread *self =(GRealThread*) thread;
322 static guint next_index = 0;
323 GStaticPrivateNode *node;
325 g_return_if_fail (thread);
327 array = self->private_data;
330 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
331 self->private_data = array;
334 if (!private_key->index)
336 g_mutex_lock (g_thread_specific_mutex);
338 if (!private_key->index)
339 private_key->index = ++next_index;
341 g_mutex_unlock (g_thread_specific_mutex);
344 if (private_key->index > array->len)
345 g_array_set_size (array, private_key->index);
347 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
350 gpointer ddata = node->data;
351 GDestroyNotify ddestroy = node->destroy;
354 node->destroy = notify;
361 node->destroy = notify;
366 g_thread_cleanup (gpointer data)
370 GRealThread* thread = data;
371 if (thread->private_data)
373 GArray* array = thread->private_data;
376 for (i = 0; i < array->len; i++ )
378 GStaticPrivateNode *node =
379 &g_array_index (array, GStaticPrivateNode, i);
381 node->destroy (node->data);
383 g_array_free (array, TRUE);
385 /* We only free the thread structure, if it isn't joinable. If
386 it is, the structure is freed in g_thread_join */
387 if (!thread->thread.joinable)
389 /* Just to make sure, this isn't used any more */
390 g_system_thread_assign (thread->system_thread, zero_thread);
399 g_error ("The thread system is not yet initialized.");
402 G_LOCK_DEFINE_STATIC (g_thread_create);
405 g_thread_create_proxy (gpointer data)
407 GRealThread* thread = data;
411 /* This has to happen before G_LOCK, as that might call g_thread_self */
412 g_private_set (g_thread_specific_private, data);
414 /* the lock makes sure, that thread->system_thread is written,
415 before thread->func is called. See g_thread_create. */
416 G_LOCK (g_thread_create);
417 G_UNLOCK (g_thread_create);
419 thread->func (thread->arg);
423 g_thread_create (GThreadFunc thread_func,
428 GThreadPriority priority,
431 GRealThread* result = g_new (GRealThread, 1);
432 GError *local_error = NULL;
433 g_return_val_if_fail (thread_func, NULL);
434 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
435 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
437 result->thread.joinable = joinable;
438 result->thread.bound = bound;
439 result->thread.priority = priority;
440 result->func = thread_func;
442 result->private_data = NULL;
443 G_LOCK (g_thread_create);
444 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
445 stack_size, joinable, bound, priority,
446 &result->system_thread, &local_error));
447 G_UNLOCK (g_thread_create);
451 g_propagate_error (error, local_error);
456 return (GThread*) result;
460 g_thread_join (GThread* thread)
462 GRealThread* real = (GRealThread*) thread;
465 g_return_if_fail (thread);
466 g_return_if_fail (thread->joinable);
467 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
469 G_THREAD_UF (thread_join, (&real->system_thread));
471 /* Just to make sure, this isn't used any more */
472 thread->joinable = 0;
473 g_system_thread_assign (real->system_thread, zero_thread);
475 /* the thread structure for non-joinable threads is freed upon
476 thread end. We free the memory here. This will leave loose end,
477 if a joinable thread is not joined. */
483 g_thread_set_priority (GThread* thread,
484 GThreadPriority priority)
486 GRealThread* real = (GRealThread*) thread;
488 g_return_if_fail (thread);
489 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
490 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
491 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
493 thread->priority = priority;
494 G_THREAD_CF (thread_set_priority, (void)0, (&real->system_thread, priority));
500 GRealThread* thread = g_private_get (g_thread_specific_private);
504 /* If no thread data is available, provide and set one. This
505 can happen for the main thread and for threads, that are not
507 thread = g_new (GRealThread, 1);
508 thread->thread.joinable = FALSE; /* This is a save guess */
509 thread->thread.bound = TRUE; /* This isn't important at all */
510 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
514 thread->private_data = NULL;
516 if (g_thread_supported ())
517 G_THREAD_UF (thread_self, (&thread->system_thread));
519 g_private_set (g_thread_specific_private, thread);
522 return (GThread*)thread;
525 static void inline g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
528 *cond = g_cond_new ();
529 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
532 static void inline g_static_rw_lock_signal (GStaticRWLock* lock)
534 if (lock->want_to_write && lock->write_cond)
535 g_cond_signal (lock->write_cond);
536 else if (lock->read_cond)
537 g_cond_broadcast (lock->read_cond);
540 void g_static_rw_lock_reader_lock (GStaticRWLock* lock)
542 g_return_if_fail (lock);
544 if (!g_threads_got_initialized)
547 g_static_mutex_lock (&lock->mutex);
548 while (lock->write || lock->want_to_write)
549 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
550 lock->read_counter++;
551 g_static_mutex_unlock (&lock->mutex);
554 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
556 gboolean ret_val = FALSE;
558 g_return_val_if_fail (lock, FALSE);
560 if (!g_threads_got_initialized)
563 g_static_mutex_lock (&lock->mutex);
564 if (!lock->write && !lock->want_to_write)
566 lock->read_counter++;
569 g_static_mutex_unlock (&lock->mutex);
573 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
575 g_return_if_fail (lock);
577 if (!g_threads_got_initialized)
580 g_static_mutex_lock (&lock->mutex);
581 lock->read_counter--;
582 if (lock->read_counter == 0)
583 g_static_rw_lock_signal (lock);
584 g_static_mutex_unlock (&lock->mutex);
587 void g_static_rw_lock_writer_lock (GStaticRWLock* lock)
589 g_return_if_fail (lock);
591 if (!g_threads_got_initialized)
594 g_static_mutex_lock (&lock->mutex);
595 lock->want_to_write++;
596 while (lock->write || lock->read_counter)
597 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
598 lock->want_to_write--;
600 g_static_mutex_unlock (&lock->mutex);
603 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
605 gboolean ret_val = FALSE;
607 g_return_val_if_fail (lock, FALSE);
609 if (!g_threads_got_initialized)
612 g_static_mutex_lock (&lock->mutex);
613 if (!lock->write && !lock->read_counter)
618 g_static_mutex_unlock (&lock->mutex);
622 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
624 g_return_if_fail (lock);
626 if (!g_threads_got_initialized)
629 g_static_mutex_lock (&lock->mutex);
631 g_static_rw_lock_signal (lock);
632 g_static_mutex_unlock (&lock->mutex);
635 void g_static_rw_lock_free (GStaticRWLock* lock)
637 g_return_if_fail (lock);
640 g_cond_free (lock->read_cond);
641 if (lock->write_cond)
642 g_cond_free (lock->write_cond);