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 Library 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 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library 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-1999. 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/.
38 typedef struct _GRealThread GRealThread;
45 gpointer system_thread;
46 gpointer private_data;
49 typedef struct _GStaticPrivateNode GStaticPrivateNode;
51 struct _GStaticPrivateNode
54 GDestroyNotify destroy;
57 static void g_thread_cleanup (gpointer data);
58 static void g_thread_fail (void);
60 /* Global variables */
62 gboolean g_thread_use_default_impl = TRUE;
63 gboolean g_threads_got_initialized = FALSE;
65 GThreadFunctions g_thread_functions_for_glib_use = {
66 (GMutex*(*)())g_thread_fail, /* mutex_new */
67 NULL, /* mutex_lock */
68 NULL, /* mutex_trylock */
69 NULL, /* mutex_unlock */
70 NULL, /* mutex_free */
71 (GCond*(*)())g_thread_fail, /* cond_new */
72 NULL, /* cond_signal */
73 NULL, /* cond_broadcast */
75 NULL, /* cond_timed_wait */
77 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
78 NULL, /* private_get */
79 NULL, /* private_set */
80 (gpointer(*)(GThreadFunc, gpointer, gulong,
82 GThreadPriority))g_thread_fail, /* thread_create */
83 NULL, /* thread_yield */
84 NULL, /* thread_join */
85 NULL, /* thread_exit */
86 NULL, /* thread_set_priority */
87 NULL /* thread_self */
92 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
93 static GMutex *g_thread_specific_mutex = NULL;
94 static GPrivate *g_thread_specific_private = NULL;
96 /* This must be called only once, before any threads are created.
97 * It will only be called from g_thread_init() in -lgthread.
102 gpointer private_old;
104 /* We let the main thread (the one that calls g_thread_init) inherit
105 * the data, that it set before calling g_thread_init
107 private_old = g_thread_specific_private;
109 g_thread_specific_private = g_private_new (g_thread_cleanup);
111 /* we can not use g_private_set here, as g_threads_got_initialized is not
112 * yet set TRUE, whereas the private_set function is already set.
114 g_thread_functions_for_glib_use.private_set (g_thread_specific_private,
117 g_mutex_protect_static_mutex_allocation = g_mutex_new();
118 g_thread_specific_mutex = g_mutex_new();
123 g_static_mutex_get_mutex_impl (GMutex** mutex)
125 if (!g_thread_supported ())
128 g_assert (g_mutex_protect_static_mutex_allocation);
130 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
133 *mutex = g_mutex_new();
135 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
140 #ifndef g_static_rec_mutex_lock
141 /* That means, that g_static_rec_mutex_lock is not defined to be
142 * g_static_mutex_lock, we have to provide an implementation ourselves.
145 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
147 guint counter = GPOINTER_TO_UINT (g_static_private_get (&mutex->counter));
150 g_static_mutex_lock (&mutex->mutex);
153 g_static_private_set (&mutex->counter, GUINT_TO_POINTER (counter), NULL);
157 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
159 guint counter = GPOINTER_TO_UINT (g_static_private_get (&mutex->counter));
162 if (!g_static_mutex_trylock (&mutex->mutex)) return FALSE;
165 g_static_private_set (&mutex->counter, GUINT_TO_POINTER (counter), NULL);
170 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
172 guint counter = GPOINTER_TO_UINT (g_static_private_get (&mutex->counter));
175 g_static_mutex_unlock (&mutex->mutex);
178 g_static_private_set (&mutex->counter, GUINT_TO_POINTER (counter), NULL);
180 #endif /* g_static_rec_mutex_lock */
183 g_static_private_get (GStaticPrivate *private_key)
185 return g_static_private_get_for_thread (private_key, g_thread_self ());
189 g_static_private_get_for_thread (GStaticPrivate *private_key,
193 GRealThread *self = (GRealThread*) thread;
195 g_return_val_if_fail (thread, NULL);
197 array = self->private_data;
201 if (!private_key->index)
203 else if (private_key->index <= array->len)
204 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
210 g_static_private_set (GStaticPrivate *private_key,
212 GDestroyNotify notify)
214 g_static_private_set_for_thread (private_key, g_thread_self (),
219 g_static_private_set_for_thread (GStaticPrivate *private_key,
222 GDestroyNotify notify)
225 GRealThread *self =(GRealThread*) thread;
226 static guint next_index = 0;
227 GStaticPrivateNode *node;
229 g_return_if_fail (thread);
231 array = self->private_data;
234 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
235 self->private_data = array;
238 if (!private_key->index)
240 g_mutex_lock (g_thread_specific_mutex);
242 if (!private_key->index)
243 private_key->index = ++next_index;
245 g_mutex_unlock (g_thread_specific_mutex);
248 if (private_key->index > array->len)
249 g_array_set_size (array, private_key->index);
251 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
254 gpointer ddata = node->data;
255 GDestroyNotify ddestroy = node->destroy;
258 node->destroy = notify;
265 node->destroy = notify;
270 g_thread_cleanup (gpointer data)
274 GRealThread* thread = data;
275 if (thread->private_data)
277 GArray* array = thread->private_data;
280 for (i = 0; i < array->len; i++ )
282 GStaticPrivateNode *node =
283 &g_array_index (array, GStaticPrivateNode, i);
285 node->destroy (node->data);
287 g_array_free (array, TRUE);
289 /* We only free the thread structure, if it isn't joinable. If
290 it is, the structure is freed in g_thread_join */
291 if (!thread->thread.joinable)
293 /* Just to make sure, this isn't used any more */
294 thread->system_thread = NULL;
303 g_error ("The thread system is not yet initialized.");
306 G_LOCK_DEFINE_STATIC (g_thread_create);
309 g_thread_create_proxy (gpointer data)
311 GRealThread* thread = data;
315 /* the lock makes sure, that thread->system_thread is written,
316 before thread->func is called. See g_thread_create */
318 G_LOCK (g_thread_create);
319 g_private_set (g_thread_specific_private, data);
320 G_UNLOCK (g_thread_create);
322 thread->func (thread->arg);
326 g_thread_create (GThreadFunc thread_func,
331 GThreadPriority priority)
333 GRealThread* result = g_new0 (GRealThread,1);
335 g_return_val_if_fail (thread_func, NULL);
337 result->thread.joinable = joinable;
338 result->thread.bound = bound;
339 result->thread.priority = priority;
340 result->func = thread_func;
342 G_LOCK (g_thread_create);
343 result->system_thread = G_THREAD_UF (thread_create, (g_thread_create_proxy,
347 G_UNLOCK (g_thread_create);
348 return (GThread*) result;
352 g_thread_join (GThread* thread)
354 GRealThread* real = (GRealThread*) thread;
356 g_return_if_fail (thread);
357 g_return_if_fail (thread->joinable);
358 g_return_if_fail (real->system_thread);
360 G_THREAD_UF (thread_join, (real->system_thread));
362 /* Just to make sure, this isn't used any more */
363 thread->joinable = 0;
364 real->system_thread = NULL;
366 /* the thread structure for non-joinable threads is freed upon
367 thread end. We free the memory here. This will leave loose end,
368 if a joinable thread is not joined. */
374 g_thread_set_priority (GThread* thread,
375 GThreadPriority priority)
377 GRealThread* real = (GRealThread*) thread;
379 g_return_if_fail (thread);
380 g_return_if_fail (real->system_thread);
382 thread->priority = priority;
383 G_THREAD_CF (thread_set_priority, (void)0, (real->system_thread, priority));
389 GRealThread* thread = g_private_get (g_thread_specific_private);
393 /* If no thread data is available, provide and set one. This
394 can happen for the main thread and for threads, that are not
396 thread = g_new (GRealThread,1);
397 thread->thread.joinable = FALSE; /* This is a save guess */
398 thread->thread.bound = TRUE; /* This isn't important at all */
399 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
403 thread->system_thread = NULL;
404 thread->private_data = NULL;
405 g_private_set (g_thread_specific_private, thread);
408 if (g_thread_supported () && !thread->system_thread)
410 thread->system_thread = g_thread_functions_for_glib_use.thread_self();
413 return (GThread*)thread;
416 static void inline g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
419 *cond = g_cond_new ();
420 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
423 static void inline g_static_rw_lock_signal (GStaticRWLock* lock)
425 if (lock->want_to_write && lock->write_cond)
426 g_cond_signal (lock->write_cond);
427 else if (lock->read_cond)
428 g_cond_signal (lock->read_cond);
431 void g_static_rw_lock_reader_lock (GStaticRWLock* lock)
433 g_return_if_fail (lock);
435 if (!g_threads_got_initialized)
438 g_static_mutex_lock (&lock->mutex);
439 while (lock->write || lock->want_to_write)
440 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
441 lock->read_counter++;
442 g_static_mutex_unlock (&lock->mutex);
445 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
447 gboolean ret_val = FALSE;
449 g_return_val_if_fail (lock, FALSE);
451 if (!g_threads_got_initialized)
454 g_static_mutex_lock (&lock->mutex);
455 if (!lock->write && !lock->want_to_write)
457 lock->read_counter++;
460 g_static_mutex_unlock (&lock->mutex);
464 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
466 g_return_if_fail (lock);
468 if (!g_threads_got_initialized)
471 g_static_mutex_lock (&lock->mutex);
472 lock->read_counter--;
473 g_static_rw_lock_signal (lock);
474 g_static_mutex_unlock (&lock->mutex);
477 void g_static_rw_lock_writer_lock (GStaticRWLock* lock)
479 g_return_if_fail (lock);
481 if (!g_threads_got_initialized)
484 g_static_mutex_lock (&lock->mutex);
485 lock->want_to_write++;
486 while (lock->write || lock->read_counter)
487 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
488 lock->want_to_write--;
490 g_static_mutex_unlock (&lock->mutex);
493 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
495 gboolean ret_val = FALSE;
497 g_return_val_if_fail (lock, FALSE);
499 if (!g_threads_got_initialized)
502 g_static_mutex_lock (&lock->mutex);
503 if (!lock->write && !lock->read_counter)
508 g_static_mutex_unlock (&lock->mutex);
512 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
514 g_return_if_fail (lock);
516 if (!g_threads_got_initialized)
519 g_static_mutex_lock (&lock->mutex);
521 g_static_rw_lock_signal (lock);
522 g_static_mutex_unlock (&lock->mutex);
525 void g_static_rw_lock_free (GStaticRWLock* lock)
527 g_return_if_fail (lock);
530 g_cond_free (lock->read_cond);
531 if (lock->write_cond)
532 g_cond_free (lock->write_cond);