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_rec_mutex_lock (GStaticRecMutex* mutex)
167 g_return_if_fail (mutex);
169 if (!g_thread_supported ())
172 G_THREAD_UF (thread_self, (&self));
174 if (g_system_thread_equal (self, mutex->owner))
179 g_static_mutex_lock (&mutex->mutex);
180 g_system_thread_assign (mutex->owner, self);
185 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
189 g_return_val_if_fail (mutex, FALSE);
191 if (!g_thread_supported ())
194 G_THREAD_UF (thread_self, (&self));
196 if (g_system_thread_equal (self, mutex->owner))
202 if (!g_static_mutex_trylock (&mutex->mutex))
205 g_system_thread_assign (mutex->owner, self);
211 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
213 g_return_if_fail (mutex);
215 if (!g_thread_supported ())
218 if (mutex->depth > 1)
223 g_system_thread_assign (mutex->owner, zero_thread);
224 g_static_mutex_unlock (&mutex->mutex);
228 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
231 g_return_if_fail (mutex);
233 if (!g_thread_supported ())
236 g_static_mutex_lock (&mutex->mutex);
237 G_THREAD_UF (thread_self, (&mutex->owner));
238 mutex->depth = depth;
242 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
246 g_return_val_if_fail (mutex, 0);
248 if (!g_thread_supported ())
251 depth = mutex->depth;
253 g_system_thread_assign (mutex->owner, zero_thread);
255 g_static_mutex_unlock (&mutex->mutex);
262 g_static_private_get (GStaticPrivate *private_key)
264 return g_static_private_get_for_thread (private_key, g_thread_self ());
268 g_static_private_get_for_thread (GStaticPrivate *private_key,
272 GRealThread *self = (GRealThread*) thread;
274 g_return_val_if_fail (thread, NULL);
276 array = self->private_data;
280 if (!private_key->index)
282 else if (private_key->index <= array->len)
283 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
289 g_static_private_set (GStaticPrivate *private_key,
291 GDestroyNotify notify)
293 g_static_private_set_for_thread (private_key, g_thread_self (),
298 g_static_private_set_for_thread (GStaticPrivate *private_key,
301 GDestroyNotify notify)
304 GRealThread *self =(GRealThread*) thread;
305 static guint next_index = 0;
306 GStaticPrivateNode *node;
308 g_return_if_fail (thread);
310 array = self->private_data;
313 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
314 self->private_data = array;
317 if (!private_key->index)
319 g_mutex_lock (g_thread_specific_mutex);
321 if (!private_key->index)
322 private_key->index = ++next_index;
324 g_mutex_unlock (g_thread_specific_mutex);
327 if (private_key->index > array->len)
328 g_array_set_size (array, private_key->index);
330 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
333 gpointer ddata = node->data;
334 GDestroyNotify ddestroy = node->destroy;
337 node->destroy = notify;
344 node->destroy = notify;
349 g_thread_cleanup (gpointer data)
353 GRealThread* thread = data;
354 if (thread->private_data)
356 GArray* array = thread->private_data;
359 for (i = 0; i < array->len; i++ )
361 GStaticPrivateNode *node =
362 &g_array_index (array, GStaticPrivateNode, i);
364 node->destroy (node->data);
366 g_array_free (array, TRUE);
368 /* We only free the thread structure, if it isn't joinable. If
369 it is, the structure is freed in g_thread_join */
370 if (!thread->thread.joinable)
372 /* Just to make sure, this isn't used any more */
373 g_system_thread_assign (thread->system_thread, zero_thread);
382 g_error ("The thread system is not yet initialized.");
385 G_LOCK_DEFINE_STATIC (g_thread_create);
388 g_thread_create_proxy (gpointer data)
390 GRealThread* thread = data;
394 /* the lock makes sure, that thread->system_thread is written,
395 before thread->func is called. See g_thread_create */
397 G_LOCK (g_thread_create);
398 g_private_set (g_thread_specific_private, data);
399 G_UNLOCK (g_thread_create);
401 thread->func (thread->arg);
405 g_thread_create (GThreadFunc thread_func,
410 GThreadPriority priority,
413 GRealThread* result = g_new (GRealThread, 1);
414 GError *local_error = NULL;
415 g_return_val_if_fail (thread_func, NULL);
416 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
417 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
419 result->thread.joinable = joinable;
420 result->thread.bound = bound;
421 result->thread.priority = priority;
422 result->func = thread_func;
424 result->private_data = NULL;
425 G_LOCK (g_thread_create);
426 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
427 stack_size, joinable, bound, priority,
428 &result->system_thread, &local_error));
429 G_UNLOCK (g_thread_create);
433 g_propagate_error (error, local_error);
438 return (GThread*) result;
442 g_thread_join (GThread* thread)
444 GRealThread* real = (GRealThread*) thread;
447 g_return_if_fail (thread);
448 g_return_if_fail (thread->joinable);
449 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
451 G_THREAD_UF (thread_join, (&real->system_thread));
453 /* Just to make sure, this isn't used any more */
454 thread->joinable = 0;
455 g_system_thread_assign (real->system_thread, zero_thread);
457 /* the thread structure for non-joinable threads is freed upon
458 thread end. We free the memory here. This will leave loose end,
459 if a joinable thread is not joined. */
465 g_thread_set_priority (GThread* thread,
466 GThreadPriority priority)
468 GRealThread* real = (GRealThread*) thread;
470 g_return_if_fail (thread);
471 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
472 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
473 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
475 thread->priority = priority;
476 G_THREAD_CF (thread_set_priority, (void)0, (&real->system_thread, priority));
482 GRealThread* thread = g_private_get (g_thread_specific_private);
486 /* If no thread data is available, provide and set one. This
487 can happen for the main thread and for threads, that are not
489 thread = g_new (GRealThread, 1);
490 thread->thread.joinable = FALSE; /* This is a save guess */
491 thread->thread.bound = TRUE; /* This isn't important at all */
492 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
496 thread->private_data = NULL;
498 if (g_thread_supported ())
499 G_THREAD_UF (thread_self, (&thread->system_thread));
501 g_private_set (g_thread_specific_private, thread);
504 return (GThread*)thread;
507 static void inline g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
510 *cond = g_cond_new ();
511 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
514 static void inline g_static_rw_lock_signal (GStaticRWLock* lock)
516 if (lock->want_to_write && lock->write_cond)
517 g_cond_signal (lock->write_cond);
518 else if (lock->read_cond)
519 g_cond_signal (lock->read_cond);
522 void g_static_rw_lock_reader_lock (GStaticRWLock* lock)
524 g_return_if_fail (lock);
526 if (!g_threads_got_initialized)
529 g_static_mutex_lock (&lock->mutex);
530 while (lock->write || lock->want_to_write)
531 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
532 lock->read_counter++;
533 g_static_mutex_unlock (&lock->mutex);
536 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
538 gboolean ret_val = FALSE;
540 g_return_val_if_fail (lock, FALSE);
542 if (!g_threads_got_initialized)
545 g_static_mutex_lock (&lock->mutex);
546 if (!lock->write && !lock->want_to_write)
548 lock->read_counter++;
551 g_static_mutex_unlock (&lock->mutex);
555 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
557 g_return_if_fail (lock);
559 if (!g_threads_got_initialized)
562 g_static_mutex_lock (&lock->mutex);
563 lock->read_counter--;
564 g_static_rw_lock_signal (lock);
565 g_static_mutex_unlock (&lock->mutex);
568 void g_static_rw_lock_writer_lock (GStaticRWLock* lock)
570 g_return_if_fail (lock);
572 if (!g_threads_got_initialized)
575 g_static_mutex_lock (&lock->mutex);
576 lock->want_to_write++;
577 while (lock->write || lock->read_counter)
578 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
579 lock->want_to_write--;
581 g_static_mutex_unlock (&lock->mutex);
584 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
586 gboolean ret_val = FALSE;
588 g_return_val_if_fail (lock, FALSE);
590 if (!g_threads_got_initialized)
593 g_static_mutex_lock (&lock->mutex);
594 if (!lock->write && !lock->read_counter)
599 g_static_mutex_unlock (&lock->mutex);
603 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
605 g_return_if_fail (lock);
607 if (!g_threads_got_initialized)
610 g_static_mutex_lock (&lock->mutex);
612 g_static_rw_lock_signal (lock);
613 g_static_mutex_unlock (&lock->mutex);
616 void g_static_rw_lock_free (GStaticRWLock* lock)
618 g_return_if_fail (lock);
621 g_cond_free (lock->read_cond);
622 if (lock->write_cond)
623 g_cond_free (lock->write_cond);