Mention 64bit integer types.
[glib.git] / glib / gthread.c
blobe1a10804c1f919be15614573f45fb3712b427d27
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.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"
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
41 #include <string.h>
43 #include "glib.h"
44 #include "gthreadinit.h"
45 #include "galias.h"
47 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
48 # define g_system_thread_equal_simple(thread1, thread2) \
49 ((thread1).dummy_pointer == (thread2).dummy_pointer)
50 # define g_system_thread_assign(dest, src) \
51 ((dest).dummy_pointer = (src).dummy_pointer)
52 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
53 # define g_system_thread_equal_simple(thread1, thread2) \
54 (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
55 # define g_system_thread_assign(dest, src) \
56 (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
57 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
59 #define g_system_thread_equal(thread1, thread2) \
60 (g_thread_functions_for_glib_use.thread_equal ? \
61 g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
62 g_system_thread_equal_simple((thread1), (thread2)))
64 GQuark
65 g_thread_error_quark (void)
67 static GQuark quark;
68 if (!quark)
69 quark = g_quark_from_static_string ("g_thread_error");
70 return quark;
73 /* Keep this in sync with GRealThread in gmain.c! */
74 typedef struct _GRealThread GRealThread;
75 struct _GRealThread
77 GThread thread;
78 gpointer private_data;
79 gpointer retval;
80 GSystemThread system_thread;
83 typedef struct _GStaticPrivateNode GStaticPrivateNode;
84 struct _GStaticPrivateNode
86 gpointer data;
87 GDestroyNotify destroy;
90 static void g_thread_cleanup (gpointer data);
91 static void g_thread_fail (void);
93 /* Global variables */
95 static GSystemThread zero_thread; /* This is initialized to all zero */
96 gboolean g_thread_use_default_impl = TRUE;
97 gboolean g_threads_got_initialized = FALSE;
99 GThreadFunctions g_thread_functions_for_glib_use = {
100 (GMutex*(*)())g_thread_fail, /* mutex_new */
101 NULL, /* mutex_lock */
102 NULL, /* mutex_trylock */
103 NULL, /* mutex_unlock */
104 NULL, /* mutex_free */
105 (GCond*(*)())g_thread_fail, /* cond_new */
106 NULL, /* cond_signal */
107 NULL, /* cond_broadcast */
108 NULL, /* cond_wait */
109 NULL, /* cond_timed_wait */
110 NULL, /* cond_free */
111 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
112 NULL, /* private_get */
113 NULL, /* private_set */
114 (void(*)(GThreadFunc, gpointer, gulong,
115 gboolean, gboolean, GThreadPriority,
116 gpointer, GError**))g_thread_fail, /* thread_create */
117 NULL, /* thread_yield */
118 NULL, /* thread_join */
119 NULL, /* thread_exit */
120 NULL, /* thread_set_priority */
121 NULL /* thread_self */
124 /* Local data */
126 static GMutex *g_once_mutex = NULL;
127 static GCond *g_once_cond = NULL;
128 static GPrivate *g_thread_specific_private = NULL;
129 static GSList *g_thread_all_threads = NULL;
130 static GSList *g_thread_free_indeces = NULL;
132 G_LOCK_DEFINE_STATIC (g_thread);
134 #ifdef G_THREADS_ENABLED
135 /* This must be called only once, before any threads are created.
136 * It will only be called from g_thread_init() in -lgthread.
138 void
139 g_thread_init_glib (void)
141 /* We let the main thread (the one that calls g_thread_init) inherit
142 * the static_private data set before calling g_thread_init
144 GRealThread* main_thread = (GRealThread*) g_thread_self ();
146 g_once_mutex = g_mutex_new ();
147 g_once_cond = g_cond_new ();
149 _g_convert_thread_init ();
150 _g_rand_thread_init ();
151 _g_main_thread_init ();
152 _g_mem_thread_init ();
153 _g_messages_thread_init ();
154 _g_atomic_thread_init ();
155 _g_utils_thread_init ();
156 #ifdef G_OS_WIN32
157 _g_win32_thread_init ();
158 #endif
160 g_threads_got_initialized = TRUE;
162 g_thread_specific_private = g_private_new (g_thread_cleanup);
163 g_private_set (g_thread_specific_private, main_thread);
164 G_THREAD_UF (thread_self, (&main_thread->system_thread));
166 _g_mem_thread_private_init ();
167 _g_messages_thread_private_init ();
170 #endif /* G_THREADS_ENABLED */
172 gpointer
173 g_once_impl (GOnce *once,
174 GThreadFunc func,
175 gpointer arg)
177 g_mutex_lock (g_once_mutex);
179 while (once->status == G_ONCE_STATUS_PROGRESS)
180 g_cond_wait (g_once_cond, g_once_mutex);
182 if (once->status != G_ONCE_STATUS_READY)
184 once->status = G_ONCE_STATUS_PROGRESS;
185 g_mutex_unlock (g_once_mutex);
187 once->retval = func (arg);
189 g_mutex_lock (g_once_mutex);
190 once->status = G_ONCE_STATUS_READY;
191 g_cond_broadcast (g_once_cond);
194 g_mutex_unlock (g_once_mutex);
196 return once->retval;
199 void
200 g_static_mutex_init (GStaticMutex *mutex)
202 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
204 g_return_if_fail (mutex);
206 *mutex = init_mutex;
209 GMutex *
210 g_static_mutex_get_mutex_impl (GMutex** mutex)
212 if (!g_thread_supported ())
213 return NULL;
215 g_assert (g_once_mutex);
217 g_mutex_lock (g_once_mutex);
219 if (!(*mutex))
221 GMutex *new_mutex = g_mutex_new ();
223 /* The following is a memory barrier to avoid the write
224 * to *new_mutex being reordered to after writing *mutex */
225 g_mutex_lock (new_mutex);
226 g_mutex_unlock (new_mutex);
228 *mutex = new_mutex;
231 g_mutex_unlock (g_once_mutex);
233 return *mutex;
236 void
237 g_static_mutex_free (GStaticMutex* mutex)
239 GMutex **runtime_mutex;
241 g_return_if_fail (mutex);
243 /* The runtime_mutex is the first (or only) member of GStaticMutex,
244 * see both versions (of glibconfig.h) in configure.in */
245 runtime_mutex = ((GMutex**)mutex);
247 if (*runtime_mutex)
248 g_mutex_free (*runtime_mutex);
250 *runtime_mutex = NULL;
253 void
254 g_static_rec_mutex_init (GStaticRecMutex *mutex)
256 static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
258 g_return_if_fail (mutex);
260 *mutex = init_mutex;
263 void
264 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
266 GSystemThread self;
268 g_return_if_fail (mutex);
270 if (!g_thread_supported ())
271 return;
273 G_THREAD_UF (thread_self, (&self));
275 if (g_system_thread_equal (self, mutex->owner))
277 mutex->depth++;
278 return;
280 g_static_mutex_lock (&mutex->mutex);
281 g_system_thread_assign (mutex->owner, self);
282 mutex->depth = 1;
285 gboolean
286 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
288 GSystemThread self;
290 g_return_val_if_fail (mutex, FALSE);
292 if (!g_thread_supported ())
293 return TRUE;
295 G_THREAD_UF (thread_self, (&self));
297 if (g_system_thread_equal (self, mutex->owner))
299 mutex->depth++;
300 return TRUE;
303 if (!g_static_mutex_trylock (&mutex->mutex))
304 return FALSE;
306 g_system_thread_assign (mutex->owner, self);
307 mutex->depth = 1;
308 return TRUE;
311 void
312 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
314 g_return_if_fail (mutex);
316 if (!g_thread_supported ())
317 return;
319 if (mutex->depth > 1)
321 mutex->depth--;
322 return;
324 g_system_thread_assign (mutex->owner, zero_thread);
325 g_static_mutex_unlock (&mutex->mutex);
328 void
329 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
330 guint depth)
332 GSystemThread self;
333 g_return_if_fail (mutex);
335 if (!g_thread_supported ())
336 return;
338 G_THREAD_UF (thread_self, (&self));
340 if (g_system_thread_equal (self, mutex->owner))
342 mutex->depth += depth;
343 return;
345 g_static_mutex_lock (&mutex->mutex);
346 g_system_thread_assign (mutex->owner, self);
347 mutex->depth = depth;
350 guint
351 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
353 guint depth;
355 g_return_val_if_fail (mutex, 0);
357 if (!g_thread_supported ())
358 return 1;
360 depth = mutex->depth;
362 g_system_thread_assign (mutex->owner, zero_thread);
363 mutex->depth = 0;
364 g_static_mutex_unlock (&mutex->mutex);
366 return depth;
369 void
370 g_static_rec_mutex_free (GStaticRecMutex *mutex)
372 g_return_if_fail (mutex);
374 g_static_mutex_free (&mutex->mutex);
377 void
378 g_static_private_init (GStaticPrivate *private_key)
380 private_key->index = 0;
383 gpointer
384 g_static_private_get (GStaticPrivate *private_key)
386 GRealThread *self = (GRealThread*) g_thread_self ();
387 GArray *array;
389 array = self->private_data;
390 if (!array)
391 return NULL;
393 if (!private_key->index)
394 return NULL;
395 else if (private_key->index <= array->len)
396 return g_array_index (array, GStaticPrivateNode,
397 private_key->index - 1).data;
398 else
399 return NULL;
402 void
403 g_static_private_set (GStaticPrivate *private_key,
404 gpointer data,
405 GDestroyNotify notify)
407 GRealThread *self = (GRealThread*) g_thread_self ();
408 GArray *array;
409 static guint next_index = 0;
410 GStaticPrivateNode *node;
412 array = self->private_data;
413 if (!array)
415 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
416 self->private_data = array;
419 if (!private_key->index)
421 G_LOCK (g_thread);
423 if (!private_key->index)
425 if (g_thread_free_indeces)
427 private_key->index =
428 GPOINTER_TO_UINT (g_thread_free_indeces->data);
429 g_thread_free_indeces =
430 g_slist_delete_link (g_thread_free_indeces,
431 g_thread_free_indeces);
433 else
434 private_key->index = ++next_index;
437 G_UNLOCK (g_thread);
440 if (private_key->index > array->len)
441 g_array_set_size (array, private_key->index);
443 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
444 if (node->destroy)
446 gpointer ddata = node->data;
447 GDestroyNotify ddestroy = node->destroy;
449 node->data = data;
450 node->destroy = notify;
452 ddestroy (ddata);
454 else
456 node->data = data;
457 node->destroy = notify;
461 void
462 g_static_private_free (GStaticPrivate *private_key)
464 guint index = private_key->index;
465 GSList *list;
467 if (!index)
468 return;
470 private_key->index = 0;
472 G_LOCK (g_thread);
473 list = g_thread_all_threads;
474 while (list)
476 GRealThread *thread = list->data;
477 GArray *array = thread->private_data;
478 list = list->next;
480 if (array && index <= array->len)
482 GStaticPrivateNode *node = &g_array_index (array,
483 GStaticPrivateNode,
484 index - 1);
485 gpointer ddata = node->data;
486 GDestroyNotify ddestroy = node->destroy;
488 node->data = NULL;
489 node->destroy = NULL;
491 if (ddestroy)
493 G_UNLOCK (g_thread);
494 ddestroy (ddata);
495 G_LOCK (g_thread);
499 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
500 GUINT_TO_POINTER (index));
501 G_UNLOCK (g_thread);
504 static void
505 g_thread_cleanup (gpointer data)
507 if (data)
509 GRealThread* thread = data;
510 if (thread->private_data)
512 GArray* array = thread->private_data;
513 guint i;
515 for (i = 0; i < array->len; i++ )
517 GStaticPrivateNode *node =
518 &g_array_index (array, GStaticPrivateNode, i);
519 if (node->destroy)
520 node->destroy (node->data);
522 g_array_free (array, TRUE);
525 /* We only free the thread structure, if it isn't joinable. If
526 it is, the structure is freed in g_thread_join */
527 if (!thread->thread.joinable)
529 G_LOCK (g_thread);
530 g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
531 G_UNLOCK (g_thread);
533 /* Just to make sure, this isn't used any more */
534 g_system_thread_assign (thread->system_thread, zero_thread);
535 g_free (thread);
540 static void
541 g_thread_fail (void)
543 g_error ("The thread system is not yet initialized.");
546 static gpointer
547 g_thread_create_proxy (gpointer data)
549 GRealThread* thread = data;
551 g_assert (data);
553 /* This has to happen before G_LOCK, as that might call g_thread_self */
554 g_private_set (g_thread_specific_private, data);
556 /* the lock makes sure, that thread->system_thread is written,
557 before thread->thread.func is called. See g_thread_create. */
558 G_LOCK (g_thread);
559 G_UNLOCK (g_thread);
561 thread->retval = thread->thread.func (thread->thread.data);
563 return NULL;
566 GThread*
567 g_thread_create_full (GThreadFunc func,
568 gpointer data,
569 gulong stack_size,
570 gboolean joinable,
571 gboolean bound,
572 GThreadPriority priority,
573 GError **error)
575 GRealThread* result;
576 GError *local_error = NULL;
577 g_return_val_if_fail (func, NULL);
578 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
579 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
581 result = g_new (GRealThread, 1);
583 result->thread.joinable = joinable;
584 result->thread.priority = priority;
585 result->thread.func = func;
586 result->thread.data = data;
587 result->private_data = NULL;
588 G_LOCK (g_thread);
589 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
590 stack_size, joinable, bound, priority,
591 &result->system_thread, &local_error));
592 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
593 G_UNLOCK (g_thread);
595 if (local_error)
597 g_propagate_error (error, local_error);
598 g_free (result);
599 return NULL;
602 return (GThread*) result;
605 void
606 g_thread_exit (gpointer retval)
608 GRealThread* real = (GRealThread*) g_thread_self ();
609 real->retval = retval;
610 G_THREAD_CF (thread_exit, (void)0, ());
613 gpointer
614 g_thread_join (GThread* thread)
616 GRealThread* real = (GRealThread*) thread;
617 gpointer retval;
619 g_return_val_if_fail (thread, NULL);
620 g_return_val_if_fail (thread->joinable, NULL);
621 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
622 zero_thread), NULL);
624 G_THREAD_UF (thread_join, (&real->system_thread));
626 retval = real->retval;
628 G_LOCK (g_thread);
629 g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
630 G_UNLOCK (g_thread);
632 /* Just to make sure, this isn't used any more */
633 thread->joinable = 0;
634 g_system_thread_assign (real->system_thread, zero_thread);
636 /* the thread structure for non-joinable threads is freed upon
637 thread end. We free the memory here. This will leave a loose end,
638 if a joinable thread is not joined. */
640 g_free (thread);
642 return retval;
645 void
646 g_thread_set_priority (GThread* thread,
647 GThreadPriority priority)
649 GRealThread* real = (GRealThread*) thread;
651 g_return_if_fail (thread);
652 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
653 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
654 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
656 thread->priority = priority;
658 G_THREAD_CF (thread_set_priority, (void)0,
659 (&real->system_thread, priority));
662 GThread*
663 g_thread_self (void)
665 GRealThread* thread = g_private_get (g_thread_specific_private);
667 if (!thread)
669 /* If no thread data is available, provide and set one. This
670 can happen for the main thread and for threads, that are not
671 created by GLib. */
672 thread = g_new (GRealThread, 1);
673 thread->thread.joinable = FALSE; /* This is a save guess */
674 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
675 just a guess */
676 thread->thread.func = NULL;
677 thread->thread.data = NULL;
678 thread->private_data = NULL;
680 if (g_thread_supported ())
681 G_THREAD_UF (thread_self, (&thread->system_thread));
683 g_private_set (g_thread_specific_private, thread);
685 G_LOCK (g_thread);
686 g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
687 G_UNLOCK (g_thread);
690 return (GThread*)thread;
693 void
694 g_static_rw_lock_init (GStaticRWLock* lock)
696 static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
698 g_return_if_fail (lock);
700 *lock = init_lock;
703 static void inline
704 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
706 if (!*cond)
707 *cond = g_cond_new ();
708 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
711 static void inline
712 g_static_rw_lock_signal (GStaticRWLock* lock)
714 if (lock->want_to_write && lock->write_cond)
715 g_cond_signal (lock->write_cond);
716 else if (lock->want_to_read && lock->read_cond)
717 g_cond_broadcast (lock->read_cond);
720 void
721 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
723 g_return_if_fail (lock);
725 if (!g_threads_got_initialized)
726 return;
728 g_static_mutex_lock (&lock->mutex);
729 lock->want_to_read++;
730 while (lock->have_writer || lock->want_to_write)
731 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
732 lock->want_to_read--;
733 lock->read_counter++;
734 g_static_mutex_unlock (&lock->mutex);
737 gboolean
738 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
740 gboolean ret_val = FALSE;
742 g_return_val_if_fail (lock, FALSE);
744 if (!g_threads_got_initialized)
745 return TRUE;
747 g_static_mutex_lock (&lock->mutex);
748 if (!lock->have_writer && !lock->want_to_write)
750 lock->read_counter++;
751 ret_val = TRUE;
753 g_static_mutex_unlock (&lock->mutex);
754 return ret_val;
757 void
758 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
760 g_return_if_fail (lock);
762 if (!g_threads_got_initialized)
763 return;
765 g_static_mutex_lock (&lock->mutex);
766 lock->read_counter--;
767 if (lock->read_counter == 0)
768 g_static_rw_lock_signal (lock);
769 g_static_mutex_unlock (&lock->mutex);
772 void
773 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
775 g_return_if_fail (lock);
777 if (!g_threads_got_initialized)
778 return;
780 g_static_mutex_lock (&lock->mutex);
781 lock->want_to_write++;
782 while (lock->have_writer || lock->read_counter)
783 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
784 lock->want_to_write--;
785 lock->have_writer = TRUE;
786 g_static_mutex_unlock (&lock->mutex);
789 gboolean
790 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
792 gboolean ret_val = FALSE;
794 g_return_val_if_fail (lock, FALSE);
796 if (!g_threads_got_initialized)
797 return TRUE;
799 g_static_mutex_lock (&lock->mutex);
800 if (!lock->have_writer && !lock->read_counter)
802 lock->have_writer = TRUE;
803 ret_val = TRUE;
805 g_static_mutex_unlock (&lock->mutex);
806 return ret_val;
809 void
810 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
812 g_return_if_fail (lock);
814 if (!g_threads_got_initialized)
815 return;
817 g_static_mutex_lock (&lock->mutex);
818 lock->have_writer = FALSE;
819 g_static_rw_lock_signal (lock);
820 g_static_mutex_unlock (&lock->mutex);
823 void
824 g_static_rw_lock_free (GStaticRWLock* lock)
826 g_return_if_fail (lock);
828 if (lock->read_cond)
830 g_cond_free (lock->read_cond);
831 lock->read_cond = NULL;
833 if (lock->write_cond)
835 g_cond_free (lock->write_cond);
836 lock->write_cond = NULL;
838 g_static_mutex_free (&lock->mutex);
841 #define __G_THREAD_C__
842 #include "galiasdef.c"