Added functions g_static_rec_mutex_init, g_static_rec_mutex_free,
[glib.git] / gthread.c
blob7ec6568de25a626e2e989319eac778e767beab93
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
6 * Owen Taylor
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/.
31 /*
32 * MT safe
35 #include "config.h"
36 #include "glib.h"
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
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 */
54 GQuark
55 g_thread_error_quark (void)
57 static GQuark quark;
58 if (!quark)
59 quark = g_quark_from_static_string ("g_thread_error");
60 return quark;
63 typedef struct _GRealThread GRealThread;
64 struct _GRealThread
66 GThread thread;
67 GThreadFunc func;
68 gpointer arg;
69 gpointer private_data;
70 GSystemThread system_thread;
73 typedef struct _GStaticPrivateNode GStaticPrivateNode;
74 struct _GStaticPrivateNode
76 gpointer data;
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__)
90 __declspec(dllexport)
91 #endif
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 */
117 /* Local data */
119 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
120 static GPrivate *g_thread_specific_private = NULL;
121 static GSList *g_thread_all_threads = NULL;
122 static GSList *g_thread_free_indeces = NULL;
124 G_LOCK_DEFINE_STATIC (g_thread);
126 /* This must be called only once, before any threads are created.
127 * It will only be called from g_thread_init() in -lgthread.
129 void
130 g_mutex_init (void)
132 GRealThread* main_thread;
134 /* We let the main thread (the one that calls g_thread_init) inherit
135 * the data, that it set before calling g_thread_init
137 main_thread = (GRealThread*) g_thread_self ();
139 g_thread_specific_private = g_private_new (g_thread_cleanup);
140 G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
141 G_THREAD_UF (thread_self, (&main_thread->system_thread));
143 g_mutex_protect_static_mutex_allocation = g_mutex_new ();
146 void
147 g_static_mutex_init (GStaticMutex *mutex)
149 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
151 g_return_if_fail (mutex);
153 memcpy (mutex, &init_mutex, sizeof (GStaticMutex));
156 GMutex *
157 g_static_mutex_get_mutex_impl (GMutex** mutex)
159 if (!g_thread_supported ())
160 return NULL;
162 g_assert (g_mutex_protect_static_mutex_allocation);
164 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
166 if (!(*mutex))
167 *mutex = g_mutex_new ();
169 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
171 return *mutex;
174 void
175 g_static_mutex_free (GStaticMutex* mutex)
177 GMutex **runtime_mutex;
179 g_return_if_fail (mutex);
181 /* The runtime_mutex is the first (or only) member of GStaticMutex,
182 * see both versions (of glibconfig.h) in configure.in */
183 runtime_mutex = ((GMutex**)mutex);
185 if (*runtime_mutex)
186 g_mutex_free (*runtime_mutex);
188 *runtime_mutex = NULL;
191 void
192 g_static_rec_mutex_init (GStaticRecMutex *mutex)
194 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
196 g_return_if_fail (mutex);
198 memcpy (mutex, &init_mutex, sizeof (GStaticRecMutex));
201 void
202 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
204 GSystemThread self;
206 g_return_if_fail (mutex);
208 if (!g_thread_supported ())
209 return;
211 G_THREAD_UF (thread_self, (&self));
213 if (g_system_thread_equal (self, mutex->owner))
215 mutex->depth++;
216 return;
218 g_static_mutex_lock (&mutex->mutex);
219 g_system_thread_assign (mutex->owner, self);
220 mutex->depth = 1;
223 gboolean
224 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
226 GSystemThread self;
228 g_return_val_if_fail (mutex, FALSE);
230 if (!g_thread_supported ())
231 return TRUE;
233 G_THREAD_UF (thread_self, (&self));
235 if (g_system_thread_equal (self, mutex->owner))
237 mutex->depth++;
238 return TRUE;
241 if (!g_static_mutex_trylock (&mutex->mutex))
242 return FALSE;
244 g_system_thread_assign (mutex->owner, self);
245 mutex->depth = 1;
246 return TRUE;
249 void
250 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
252 g_return_if_fail (mutex);
254 if (!g_thread_supported ())
255 return;
257 if (mutex->depth > 1)
259 mutex->depth--;
260 return;
262 g_system_thread_assign (mutex->owner, zero_thread);
263 g_static_mutex_unlock (&mutex->mutex);
266 void
267 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
268 guint depth)
270 g_return_if_fail (mutex);
272 if (!g_thread_supported ())
273 return;
275 g_static_mutex_lock (&mutex->mutex);
276 G_THREAD_UF (thread_self, (&mutex->owner));
277 mutex->depth = depth;
280 guint
281 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
283 gint depth;
285 g_return_val_if_fail (mutex, 0);
287 if (!g_thread_supported ())
288 return 1;
290 depth = mutex->depth;
292 g_system_thread_assign (mutex->owner, zero_thread);
293 mutex->depth = 0;
294 g_static_mutex_unlock (&mutex->mutex);
296 return depth;
299 void
300 g_static_rec_mutex_free (GStaticRecMutex *mutex)
302 g_return_if_fail (mutex);
304 g_static_mutex_free (&mutex->mutex);
307 void
308 g_static_private_init (GStaticPrivate *private_key)
310 private_key->index = 0;
313 gpointer
314 g_static_private_get (GStaticPrivate *private_key)
316 return g_static_private_get_for_thread (private_key, g_thread_self ());
319 gpointer
320 g_static_private_get_for_thread (GStaticPrivate *private_key,
321 GThread *thread)
323 GArray *array;
324 GRealThread *self = (GRealThread*) thread;
326 g_return_val_if_fail (thread, NULL);
328 array = self->private_data;
329 if (!array)
330 return NULL;
332 if (!private_key->index)
333 return NULL;
334 else if (private_key->index <= array->len)
335 return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
336 else
337 return NULL;
340 void
341 g_static_private_set (GStaticPrivate *private_key,
342 gpointer data,
343 GDestroyNotify notify)
345 g_static_private_set_for_thread (private_key, g_thread_self (),
346 data, notify);
349 void
350 g_static_private_set_for_thread (GStaticPrivate *private_key,
351 GThread *thread,
352 gpointer data,
353 GDestroyNotify notify)
355 GArray *array;
356 GRealThread *self =(GRealThread*) thread;
357 static guint next_index = 0;
358 GStaticPrivateNode *node;
360 g_return_if_fail (thread);
362 array = self->private_data;
363 if (!array)
365 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
366 self->private_data = array;
369 if (!private_key->index)
371 G_LOCK (g_thread);
373 if (!private_key->index)
375 if (g_thread_free_indeces)
377 private_key->index =
378 GPOINTER_TO_UINT (g_thread_free_indeces->data);
379 g_thread_free_indeces =
380 g_slist_delete_link (g_thread_free_indeces,
381 g_thread_free_indeces);
383 else
384 private_key->index = ++next_index;
387 G_UNLOCK (g_thread);
390 if (private_key->index > array->len)
391 g_array_set_size (array, private_key->index);
393 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
394 if (node->destroy)
396 gpointer ddata = node->data;
397 GDestroyNotify ddestroy = node->destroy;
399 node->data = data;
400 node->destroy = notify;
402 ddestroy (ddata);
404 else
406 node->data = data;
407 node->destroy = notify;
411 void
412 g_static_private_free (GStaticPrivate *private_key)
414 GStaticPrivate copied_key;
415 GSList *list;
417 copied_key.index = private_key->index;
418 private_key->index = 0;
420 if (!copied_key.index)
421 return;
423 G_LOCK (g_thread);
424 list = g_thread_all_threads;
425 while (list)
427 GThread *thread = list->data;
428 list = list->next;
430 G_UNLOCK (g_thread);
431 g_static_private_set_for_thread (&copied_key, thread, NULL, NULL);
432 G_LOCK (g_thread);
434 g_thread_free_indeces =
435 g_slist_prepend (g_thread_free_indeces,
436 GUINT_TO_POINTER (copied_key.index));
437 G_UNLOCK (g_thread);
440 static void
441 g_thread_cleanup (gpointer data)
443 if (data)
445 GRealThread* thread = data;
446 if (thread->private_data)
448 GArray* array = thread->private_data;
449 guint i;
451 for (i = 0; i < array->len; i++ )
453 GStaticPrivateNode *node =
454 &g_array_index (array, GStaticPrivateNode, i);
455 if (node->destroy)
456 node->destroy (node->data);
458 g_array_free (array, TRUE);
460 /* We only free the thread structure, if it isn't joinable. If
461 it is, the structure is freed in g_thread_join */
462 if (!thread->thread.joinable)
464 G_LOCK (g_thread);
465 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
466 G_UNLOCK (g_thread);
468 /* Just to make sure, this isn't used any more */
469 g_system_thread_assign (thread->system_thread, zero_thread);
470 g_free (thread);
475 static void
476 g_thread_fail (void)
478 g_error ("The thread system is not yet initialized.");
481 static void
482 g_thread_create_proxy (gpointer data)
484 GRealThread* thread = data;
486 g_assert (data);
488 /* This has to happen before G_LOCK, as that might call g_thread_self */
489 g_private_set (g_thread_specific_private, data);
491 /* the lock makes sure, that thread->system_thread is written,
492 before thread->func is called. See g_thread_create. */
493 G_LOCK (g_thread);
494 G_UNLOCK (g_thread);
496 thread->func (thread->arg);
499 GThread*
500 g_thread_create (GThreadFunc thread_func,
501 gpointer arg,
502 gulong stack_size,
503 gboolean joinable,
504 gboolean bound,
505 GThreadPriority priority,
506 GError **error)
508 GRealThread* result = g_new (GRealThread, 1);
509 GError *local_error = NULL;
510 g_return_val_if_fail (thread_func, NULL);
511 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
512 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
514 result->thread.joinable = joinable;
515 result->thread.bound = bound;
516 result->thread.priority = priority;
517 result->func = thread_func;
518 result->arg = arg;
519 result->private_data = NULL;
520 G_LOCK (g_thread);
521 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
522 stack_size, joinable, bound, priority,
523 &result->system_thread, &local_error));
524 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
525 G_UNLOCK (g_thread);
527 if (local_error)
529 g_propagate_error (error, local_error);
530 g_free (result);
531 return NULL;
534 return (GThread*) result;
537 void
538 g_thread_join (GThread* thread)
540 GRealThread* real = (GRealThread*) thread;
543 g_return_if_fail (thread);
544 g_return_if_fail (thread->joinable);
545 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
547 G_THREAD_UF (thread_join, (&real->system_thread));
549 G_LOCK (g_thread);
550 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
551 G_UNLOCK (g_thread);
553 /* Just to make sure, this isn't used any more */
554 thread->joinable = 0;
555 g_system_thread_assign (real->system_thread, zero_thread);
557 /* the thread structure for non-joinable threads is freed upon
558 thread end. We free the memory here. This will leave loose end,
559 if a joinable thread is not joined. */
561 g_free (thread);
564 void
565 g_thread_set_priority (GThread* thread,
566 GThreadPriority priority)
568 GRealThread* real = (GRealThread*) thread;
570 g_return_if_fail (thread);
571 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
572 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
573 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
575 thread->priority = priority;
576 G_THREAD_CF (thread_set_priority, (void)0, (&real->system_thread, priority));
579 GThread*
580 g_thread_self (void)
582 GRealThread* thread = g_private_get (g_thread_specific_private);
584 if (!thread)
586 /* If no thread data is available, provide and set one. This
587 can happen for the main thread and for threads, that are not
588 created by GLib. */
589 thread = g_new (GRealThread, 1);
590 thread->thread.joinable = FALSE; /* This is a save guess */
591 thread->thread.bound = TRUE; /* This isn't important at all */
592 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
593 just a guess */
594 thread->func = NULL;
595 thread->arg = NULL;
596 thread->private_data = NULL;
598 if (g_thread_supported ())
599 G_THREAD_UF (thread_self, (&thread->system_thread));
601 g_private_set (g_thread_specific_private, thread);
603 G_LOCK (g_thread);
604 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
605 G_UNLOCK (g_thread);
608 return (GThread*)thread;
611 void
612 g_static_rw_lock_init (GStaticRWLock* lock)
614 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
616 g_return_if_fail (lock);
618 memcpy (lock, &init_lock, sizeof (GStaticRWLock));
621 static void inline
622 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
624 if (!*cond)
625 *cond = g_cond_new ();
626 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
629 static void inline
630 g_static_rw_lock_signal (GStaticRWLock* lock)
632 if (lock->want_to_write && lock->write_cond)
633 g_cond_signal (lock->write_cond);
634 else if (lock->read_cond)
635 g_cond_broadcast (lock->read_cond);
638 void
639 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
641 g_return_if_fail (lock);
643 if (!g_threads_got_initialized)
644 return;
646 g_static_mutex_lock (&lock->mutex);
647 while (lock->write || lock->want_to_write)
648 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
649 lock->read_counter++;
650 g_static_mutex_unlock (&lock->mutex);
653 gboolean
654 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
656 gboolean ret_val = FALSE;
658 g_return_val_if_fail (lock, FALSE);
660 if (!g_threads_got_initialized)
661 return TRUE;
663 g_static_mutex_lock (&lock->mutex);
664 if (!lock->write && !lock->want_to_write)
666 lock->read_counter++;
667 ret_val = TRUE;
669 g_static_mutex_unlock (&lock->mutex);
670 return ret_val;
673 void
674 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
676 g_return_if_fail (lock);
678 if (!g_threads_got_initialized)
679 return;
681 g_static_mutex_lock (&lock->mutex);
682 lock->read_counter--;
683 if (lock->read_counter == 0)
684 g_static_rw_lock_signal (lock);
685 g_static_mutex_unlock (&lock->mutex);
688 void
689 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
691 g_return_if_fail (lock);
693 if (!g_threads_got_initialized)
694 return;
696 g_static_mutex_lock (&lock->mutex);
697 lock->want_to_write++;
698 while (lock->write || lock->read_counter)
699 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
700 lock->want_to_write--;
701 lock->write = TRUE;
702 g_static_mutex_unlock (&lock->mutex);
705 gboolean
706 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
708 gboolean ret_val = FALSE;
710 g_return_val_if_fail (lock, FALSE);
712 if (!g_threads_got_initialized)
713 return TRUE;
715 g_static_mutex_lock (&lock->mutex);
716 if (!lock->write && !lock->read_counter)
718 lock->write = TRUE;
719 ret_val = TRUE;
721 g_static_mutex_unlock (&lock->mutex);
722 return ret_val;
725 void
726 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
728 g_return_if_fail (lock);
730 if (!g_threads_got_initialized)
731 return;
733 g_static_mutex_lock (&lock->mutex);
734 lock->write = FALSE;
735 g_static_rw_lock_signal (lock);
736 g_static_mutex_unlock (&lock->mutex);
739 void
740 g_static_rw_lock_free (GStaticRWLock* lock)
742 g_return_if_fail (lock);
744 if (lock->read_cond)
746 g_cond_free (lock->read_cond);
747 lock->read_cond = NULL;
749 if (lock->write_cond)
751 g_cond_free (lock->write_cond);
752 lock->write_cond = NULL;
754 g_static_mutex_free (&lock->mutex);