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/.
42 typedef struct _GRealThread GRealThread;
49 gpointer system_thread;
50 gpointer private_data;
53 typedef struct _GStaticPrivateNode GStaticPrivateNode;
55 struct _GStaticPrivateNode
58 GDestroyNotify destroy;
61 static void g_thread_cleanup (gpointer data);
62 static void g_thread_fail (void);
64 /* Global variables */
66 gboolean g_thread_use_default_impl = TRUE;
67 gboolean g_threads_got_initialized = FALSE;
69 #if defined(NATIVE_WIN32) && defined(__GNUC__)
72 GThreadFunctions g_thread_functions_for_glib_use = {
73 (GMutex*(*)())g_thread_fail, /* mutex_new */
74 NULL, /* mutex_lock */
75 NULL, /* mutex_trylock */
76 NULL, /* mutex_unlock */
77 NULL, /* mutex_free */
78 (GCond*(*)())g_thread_fail, /* cond_new */
79 NULL, /* cond_signal */
80 NULL, /* cond_broadcast */
82 NULL, /* cond_timed_wait */
84 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
85 NULL, /* private_get */
86 NULL, /* private_set */
87 (gpointer(*)(GThreadFunc, gpointer, gulong,
89 GThreadPriority))g_thread_fail, /* thread_create */
90 NULL, /* thread_yield */
91 NULL, /* thread_join */
92 NULL, /* thread_exit */
93 NULL, /* thread_set_priority */
94 NULL /* thread_self */
99 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
100 static GMutex *g_thread_specific_mutex = NULL;
101 static GPrivate *g_thread_specific_private = NULL;
103 /* This must be called only once, before any threads are created.
104 * It will only be called from g_thread_init() in -lgthread.
109 gpointer private_old;
111 /* We let the main thread (the one that calls g_thread_init) inherit
112 * the data, that it set before calling g_thread_init
114 private_old = g_thread_specific_private;
116 g_thread_specific_private = g_private_new (g_thread_cleanup);
118 /* we can not use g_private_set here, as g_threads_got_initialized is not
119 * yet set TRUE, whereas the private_set function is already set.
121 g_thread_functions_for_glib_use.private_set (g_thread_specific_private,
124 g_mutex_protect_static_mutex_allocation = g_mutex_new();
125 g_thread_specific_mutex = g_mutex_new();
130 g_static_mutex_get_mutex_impl (GMutex** mutex)
132 if (!g_thread_supported ())
135 g_assert (g_mutex_protect_static_mutex_allocation);
137 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
140 *mutex = g_mutex_new();
142 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
147 #ifndef g_static_rec_mutex_lock
148 /* That means, that g_static_rec_mutex_lock is not defined to be
149 * g_static_mutex_lock, we have to provide an implementation ourselves.
152 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
154 guint counter = GPOINTER_TO_UINT (g_static_private_get (&mutex->counter));
157 g_static_mutex_lock (&mutex->mutex);
160 g_static_private_set (&mutex->counter, GUINT_TO_POINTER (counter), NULL);
164 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
166 guint counter = GPOINTER_TO_UINT (g_static_private_get (&mutex->counter));
169 if (!g_static_mutex_trylock (&mutex->mutex)) return FALSE;
172 g_static_private_set (&mutex->counter, GUINT_TO_POINTER (counter), NULL);
177 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
179 guint counter = GPOINTER_TO_UINT (g_static_private_get (&mutex->counter));
182 g_static_mutex_unlock (&mutex->mutex);
185 g_static_private_set (&mutex->counter, GUINT_TO_POINTER (counter), NULL);
187 #endif /* g_static_rec_mutex_lock */
190 g_static_private_get (GStaticPrivate *private_key)
192 return g_static_private_get_for_thread (private_key, g_thread_self ());
196 g_static_private_get_for_thread (GStaticPrivate *private_key,
200 GRealThread *self = (GRealThread*) thread;
202 g_return_val_if_fail (thread, NULL);
204 array = self->private_data;
208 if (!private_key->index)
210 else if (private_key->index <= array->len)
211 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
217 g_static_private_set (GStaticPrivate *private_key,
219 GDestroyNotify notify)
221 g_static_private_set_for_thread (private_key, g_thread_self (),
226 g_static_private_set_for_thread (GStaticPrivate *private_key,
229 GDestroyNotify notify)
232 GRealThread *self =(GRealThread*) thread;
233 static guint next_index = 0;
234 GStaticPrivateNode *node;
236 g_return_if_fail (thread);
238 array = self->private_data;
241 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
242 self->private_data = array;
245 if (!private_key->index)
247 g_mutex_lock (g_thread_specific_mutex);
249 if (!private_key->index)
250 private_key->index = ++next_index;
252 g_mutex_unlock (g_thread_specific_mutex);
255 if (private_key->index > array->len)
256 g_array_set_size (array, private_key->index);
258 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
261 gpointer ddata = node->data;
262 GDestroyNotify ddestroy = node->destroy;
265 node->destroy = notify;
272 node->destroy = notify;
277 g_thread_cleanup (gpointer data)
281 GRealThread* thread = data;
282 if (thread->private_data)
284 GArray* array = thread->private_data;
287 for (i = 0; i < array->len; i++ )
289 GStaticPrivateNode *node =
290 &g_array_index (array, GStaticPrivateNode, i);
292 node->destroy (node->data);
294 g_array_free (array, TRUE);
296 /* We only free the thread structure, if it isn't joinable. If
297 it is, the structure is freed in g_thread_join */
298 if (!thread->thread.joinable)
300 /* Just to make sure, this isn't used any more */
301 thread->system_thread = NULL;
310 g_error ("The thread system is not yet initialized.");
313 G_LOCK_DEFINE_STATIC (g_thread_create);
316 g_thread_create_proxy (gpointer data)
318 GRealThread* thread = data;
322 /* the lock makes sure, that thread->system_thread is written,
323 before thread->func is called. See g_thread_create */
325 G_LOCK (g_thread_create);
326 g_private_set (g_thread_specific_private, data);
327 G_UNLOCK (g_thread_create);
329 thread->func (thread->arg);
333 g_thread_create (GThreadFunc thread_func,
338 GThreadPriority priority)
340 GRealThread* result = g_new0 (GRealThread,1);
342 g_return_val_if_fail (thread_func, NULL);
344 result->thread.joinable = joinable;
345 result->thread.bound = bound;
346 result->thread.priority = priority;
347 result->func = thread_func;
349 G_LOCK (g_thread_create);
350 result->system_thread = G_THREAD_UF (thread_create, (g_thread_create_proxy,
354 G_UNLOCK (g_thread_create);
355 return (GThread*) result;
359 g_thread_join (GThread* thread)
361 GRealThread* real = (GRealThread*) thread;
363 g_return_if_fail (thread);
364 g_return_if_fail (thread->joinable);
365 g_return_if_fail (real->system_thread);
367 G_THREAD_UF (thread_join, (real->system_thread));
369 /* Just to make sure, this isn't used any more */
370 thread->joinable = 0;
371 real->system_thread = NULL;
373 /* the thread structure for non-joinable threads is freed upon
374 thread end. We free the memory here. This will leave loose end,
375 if a joinable thread is not joined. */
381 g_thread_set_priority (GThread* thread,
382 GThreadPriority priority)
384 GRealThread* real = (GRealThread*) thread;
386 g_return_if_fail (thread);
387 g_return_if_fail (real->system_thread);
389 thread->priority = priority;
390 G_THREAD_CF (thread_set_priority, (void)0, (real->system_thread, priority));
396 GRealThread* thread = g_private_get (g_thread_specific_private);
400 /* If no thread data is available, provide and set one. This
401 can happen for the main thread and for threads, that are not
403 thread = g_new (GRealThread,1);
404 thread->thread.joinable = FALSE; /* This is a save guess */
405 thread->thread.bound = TRUE; /* This isn't important at all */
406 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
410 thread->system_thread = NULL;
411 thread->private_data = NULL;
412 g_private_set (g_thread_specific_private, thread);
415 if (g_thread_supported () && !thread->system_thread)
417 thread->system_thread = g_thread_functions_for_glib_use.thread_self();
420 return (GThread*)thread;
423 static void inline g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
426 *cond = g_cond_new ();
427 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
430 static void inline g_static_rw_lock_signal (GStaticRWLock* lock)
432 if (lock->want_to_write && lock->write_cond)
433 g_cond_signal (lock->write_cond);
434 else if (lock->read_cond)
435 g_cond_signal (lock->read_cond);
438 void g_static_rw_lock_reader_lock (GStaticRWLock* lock)
440 g_return_if_fail (lock);
442 if (!g_threads_got_initialized)
445 g_static_mutex_lock (&lock->mutex);
446 while (lock->write || lock->want_to_write)
447 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
448 lock->read_counter++;
449 g_static_mutex_unlock (&lock->mutex);
452 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
454 gboolean ret_val = FALSE;
456 g_return_val_if_fail (lock, FALSE);
458 if (!g_threads_got_initialized)
461 g_static_mutex_lock (&lock->mutex);
462 if (!lock->write && !lock->want_to_write)
464 lock->read_counter++;
467 g_static_mutex_unlock (&lock->mutex);
471 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
473 g_return_if_fail (lock);
475 if (!g_threads_got_initialized)
478 g_static_mutex_lock (&lock->mutex);
479 lock->read_counter--;
480 g_static_rw_lock_signal (lock);
481 g_static_mutex_unlock (&lock->mutex);
484 void g_static_rw_lock_writer_lock (GStaticRWLock* lock)
486 g_return_if_fail (lock);
488 if (!g_threads_got_initialized)
491 g_static_mutex_lock (&lock->mutex);
492 lock->want_to_write++;
493 while (lock->write || lock->read_counter)
494 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
495 lock->want_to_write--;
497 g_static_mutex_unlock (&lock->mutex);
500 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
502 gboolean ret_val = FALSE;
504 g_return_val_if_fail (lock, FALSE);
506 if (!g_threads_got_initialized)
509 g_static_mutex_lock (&lock->mutex);
510 if (!lock->write && !lock->read_counter)
515 g_static_mutex_unlock (&lock->mutex);
519 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
521 g_return_if_fail (lock);
523 if (!g_threads_got_initialized)
526 g_static_mutex_lock (&lock->mutex);
528 g_static_rw_lock_signal (lock);
529 g_static_mutex_unlock (&lock->mutex);
532 void g_static_rw_lock_free (GStaticRWLock* lock)
534 g_return_if_fail (lock);
537 g_cond_free (lock->read_cond);
538 if (lock->write_cond)
539 g_cond_free (lock->write_cond);