If a character can't be converted, don't replace it with a NUL byte, but
[glib.git] / glib / gthread.c
blobddee4f785e739bbd692f75768f3636ee54904a19
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/.
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 "gthreadprivate.h"
45 #include "galias.h"
47 GQuark
48 g_thread_error_quark (void)
50 return g_quark_from_static_string ("g_thread_error");
53 /* Keep this in sync with GRealThread in gmain.c! */
54 typedef struct _GRealThread GRealThread;
55 struct _GRealThread
57 GThread thread;
58 gpointer private_data;
59 GRealThread *next;
60 gpointer retval;
61 GSystemThread system_thread;
64 typedef struct _GStaticPrivateNode GStaticPrivateNode;
65 struct _GStaticPrivateNode
67 gpointer data;
68 GDestroyNotify destroy;
71 static void g_thread_cleanup (gpointer data);
72 static void g_thread_fail (void);
74 /* Global variables */
76 static GSystemThread zero_thread; /* This is initialized to all zero */
77 gboolean g_thread_use_default_impl = TRUE;
78 gboolean g_threads_got_initialized = FALSE;
80 GThreadFunctions g_thread_functions_for_glib_use = {
81 (GMutex*(*)())g_thread_fail, /* mutex_new */
82 NULL, /* mutex_lock */
83 NULL, /* mutex_trylock */
84 NULL, /* mutex_unlock */
85 NULL, /* mutex_free */
86 (GCond*(*)())g_thread_fail, /* cond_new */
87 NULL, /* cond_signal */
88 NULL, /* cond_broadcast */
89 NULL, /* cond_wait */
90 NULL, /* cond_timed_wait */
91 NULL, /* cond_free */
92 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
93 NULL, /* private_get */
94 NULL, /* private_set */
95 (void(*)(GThreadFunc, gpointer, gulong,
96 gboolean, gboolean, GThreadPriority,
97 gpointer, GError**))g_thread_fail, /* thread_create */
98 NULL, /* thread_yield */
99 NULL, /* thread_join */
100 NULL, /* thread_exit */
101 NULL, /* thread_set_priority */
102 NULL /* thread_self */
105 /* Local data */
107 static GMutex *g_once_mutex = NULL;
108 static GCond *g_once_cond = NULL;
109 static GPrivate *g_thread_specific_private = NULL;
110 static GRealThread *g_thread_all_threads = NULL;
111 static GSList *g_thread_free_indeces = NULL;
113 G_LOCK_DEFINE_STATIC (g_thread);
115 #ifdef G_THREADS_ENABLED
116 /* This must be called only once, before any threads are created.
117 * It will only be called from g_thread_init() in -lgthread.
119 void
120 g_thread_init_glib (void)
122 /* We let the main thread (the one that calls g_thread_init) inherit
123 * the static_private data set before calling g_thread_init
125 GRealThread* main_thread = (GRealThread*) g_thread_self ();
127 /* mutex and cond creation works without g_threads_got_initialized */
128 g_once_mutex = g_mutex_new ();
129 g_once_cond = g_cond_new ();
131 /* we may only create mutex and cond in here */
132 _g_mem_thread_init_noprivate_nomessage ();
134 /* setup the basic threading system */
135 g_threads_got_initialized = TRUE;
136 g_thread_specific_private = g_private_new (g_thread_cleanup);
137 g_private_set (g_thread_specific_private, main_thread);
138 G_THREAD_UF (thread_self, (&main_thread->system_thread));
140 /* complete memory system initialization, g_private_*() works now */
141 _g_slice_thread_init_nomessage ();
143 /* accomplish log system initialization to enable messaging */
144 _g_messages_thread_init_nomessage ();
146 /* we may run full-fledged initializers from here */
147 _g_atomic_thread_init ();
148 _g_convert_thread_init ();
149 _g_rand_thread_init ();
150 _g_main_thread_init ();
151 _g_utils_thread_init ();
152 #ifdef G_OS_WIN32
153 _g_win32_thread_init ();
154 #endif
156 #endif /* G_THREADS_ENABLED */
158 gpointer
159 g_once_impl (GOnce *once,
160 GThreadFunc func,
161 gpointer arg)
163 g_mutex_lock (g_once_mutex);
165 while (once->status == G_ONCE_STATUS_PROGRESS)
166 g_cond_wait (g_once_cond, g_once_mutex);
168 if (once->status != G_ONCE_STATUS_READY)
170 once->status = G_ONCE_STATUS_PROGRESS;
171 g_mutex_unlock (g_once_mutex);
173 once->retval = func (arg);
175 g_mutex_lock (g_once_mutex);
176 once->status = G_ONCE_STATUS_READY;
177 g_cond_broadcast (g_once_cond);
180 g_mutex_unlock (g_once_mutex);
182 return once->retval;
185 void
186 g_static_mutex_init (GStaticMutex *mutex)
188 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
190 g_return_if_fail (mutex);
192 *mutex = init_mutex;
195 GMutex *
196 g_static_mutex_get_mutex_impl (GMutex** mutex)
198 if (!g_thread_supported ())
199 return NULL;
201 g_assert (g_once_mutex);
203 g_mutex_lock (g_once_mutex);
205 if (!(*mutex))
206 g_atomic_pointer_set (mutex, g_mutex_new());
208 g_mutex_unlock (g_once_mutex);
210 return *mutex;
213 void
214 g_static_mutex_free (GStaticMutex* mutex)
216 GMutex **runtime_mutex;
218 g_return_if_fail (mutex);
220 /* The runtime_mutex is the first (or only) member of GStaticMutex,
221 * see both versions (of glibconfig.h) in configure.in */
222 runtime_mutex = ((GMutex**)mutex);
224 if (*runtime_mutex)
225 g_mutex_free (*runtime_mutex);
227 *runtime_mutex = NULL;
230 void
231 g_static_rec_mutex_init (GStaticRecMutex *mutex)
233 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
235 g_return_if_fail (mutex);
237 *mutex = init_mutex;
240 void
241 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
243 GSystemThread self;
245 g_return_if_fail (mutex);
247 if (!g_thread_supported ())
248 return;
250 G_THREAD_UF (thread_self, (&self));
252 if (g_system_thread_equal (self, mutex->owner))
254 mutex->depth++;
255 return;
257 g_static_mutex_lock (&mutex->mutex);
258 g_system_thread_assign (mutex->owner, self);
259 mutex->depth = 1;
262 gboolean
263 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
265 GSystemThread self;
267 g_return_val_if_fail (mutex, FALSE);
269 if (!g_thread_supported ())
270 return TRUE;
272 G_THREAD_UF (thread_self, (&self));
274 if (g_system_thread_equal (self, mutex->owner))
276 mutex->depth++;
277 return TRUE;
280 if (!g_static_mutex_trylock (&mutex->mutex))
281 return FALSE;
283 g_system_thread_assign (mutex->owner, self);
284 mutex->depth = 1;
285 return TRUE;
288 void
289 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
291 g_return_if_fail (mutex);
293 if (!g_thread_supported ())
294 return;
296 if (mutex->depth > 1)
298 mutex->depth--;
299 return;
301 g_system_thread_assign (mutex->owner, zero_thread);
302 g_static_mutex_unlock (&mutex->mutex);
305 void
306 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
307 guint depth)
309 GSystemThread self;
310 g_return_if_fail (mutex);
312 if (!g_thread_supported ())
313 return;
315 if (depth == 0)
316 return;
318 G_THREAD_UF (thread_self, (&self));
320 if (g_system_thread_equal (self, mutex->owner))
322 mutex->depth += depth;
323 return;
325 g_static_mutex_lock (&mutex->mutex);
326 g_system_thread_assign (mutex->owner, self);
327 mutex->depth = depth;
330 guint
331 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
333 guint depth;
335 g_return_val_if_fail (mutex, 0);
337 if (!g_thread_supported ())
338 return 1;
340 depth = mutex->depth;
342 g_system_thread_assign (mutex->owner, zero_thread);
343 mutex->depth = 0;
344 g_static_mutex_unlock (&mutex->mutex);
346 return depth;
349 void
350 g_static_rec_mutex_free (GStaticRecMutex *mutex)
352 g_return_if_fail (mutex);
354 g_static_mutex_free (&mutex->mutex);
357 void
358 g_static_private_init (GStaticPrivate *private_key)
360 private_key->index = 0;
363 gpointer
364 g_static_private_get (GStaticPrivate *private_key)
366 GRealThread *self = (GRealThread*) g_thread_self ();
367 GArray *array;
369 array = self->private_data;
370 if (!array)
371 return NULL;
373 if (!private_key->index)
374 return NULL;
375 else if (private_key->index <= array->len)
376 return g_array_index (array, GStaticPrivateNode,
377 private_key->index - 1).data;
378 else
379 return NULL;
382 void
383 g_static_private_set (GStaticPrivate *private_key,
384 gpointer data,
385 GDestroyNotify notify)
387 GRealThread *self = (GRealThread*) g_thread_self ();
388 GArray *array;
389 static guint next_index = 0;
390 GStaticPrivateNode *node;
392 array = self->private_data;
393 if (!array)
395 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
396 self->private_data = array;
399 if (!private_key->index)
401 G_LOCK (g_thread);
403 if (!private_key->index)
405 if (g_thread_free_indeces)
407 private_key->index =
408 GPOINTER_TO_UINT (g_thread_free_indeces->data);
409 g_thread_free_indeces =
410 g_slist_delete_link (g_thread_free_indeces,
411 g_thread_free_indeces);
413 else
414 private_key->index = ++next_index;
417 G_UNLOCK (g_thread);
420 if (private_key->index > array->len)
421 g_array_set_size (array, private_key->index);
423 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
424 if (node->destroy)
426 gpointer ddata = node->data;
427 GDestroyNotify ddestroy = node->destroy;
429 node->data = data;
430 node->destroy = notify;
432 ddestroy (ddata);
434 else
436 node->data = data;
437 node->destroy = notify;
441 void
442 g_static_private_free (GStaticPrivate *private_key)
444 guint index = private_key->index;
445 GRealThread *thread;
447 if (!index)
448 return;
450 private_key->index = 0;
452 G_LOCK (g_thread);
454 thread = g_thread_all_threads;
455 while (thread)
457 GArray *array = thread->private_data;
458 thread = thread->next;
460 if (array && index <= array->len)
462 GStaticPrivateNode *node = &g_array_index (array,
463 GStaticPrivateNode,
464 index - 1);
465 gpointer ddata = node->data;
466 GDestroyNotify ddestroy = node->destroy;
468 node->data = NULL;
469 node->destroy = NULL;
471 if (ddestroy)
473 G_UNLOCK (g_thread);
474 ddestroy (ddata);
475 G_LOCK (g_thread);
479 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
480 GUINT_TO_POINTER (index));
481 G_UNLOCK (g_thread);
484 static void
485 g_thread_cleanup (gpointer data)
487 if (data)
489 GRealThread* thread = data;
490 if (thread->private_data)
492 GArray* array = thread->private_data;
493 guint i;
495 for (i = 0; i < array->len; i++ )
497 GStaticPrivateNode *node =
498 &g_array_index (array, GStaticPrivateNode, i);
499 if (node->destroy)
500 node->destroy (node->data);
502 g_array_free (array, TRUE);
505 /* We only free the thread structure, if it isn't joinable. If
506 it is, the structure is freed in g_thread_join */
507 if (!thread->thread.joinable)
509 GRealThread *t, *p;
511 G_LOCK (g_thread);
512 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
514 if (t == thread)
516 if (p)
517 p->next = t->next;
518 else
519 g_thread_all_threads = t->next;
520 break;
523 G_UNLOCK (g_thread);
525 /* Just to make sure, this isn't used any more */
526 g_system_thread_assign (thread->system_thread, zero_thread);
527 g_free (thread);
532 static void
533 g_thread_fail (void)
535 g_error ("The thread system is not yet initialized.");
538 static gpointer
539 g_thread_create_proxy (gpointer data)
541 GRealThread* thread = data;
543 g_assert (data);
545 /* This has to happen before G_LOCK, as that might call g_thread_self */
546 g_private_set (g_thread_specific_private, data);
548 /* the lock makes sure, that thread->system_thread is written,
549 before thread->thread.func is called. See g_thread_create. */
550 G_LOCK (g_thread);
551 G_UNLOCK (g_thread);
553 thread->retval = thread->thread.func (thread->thread.data);
555 return NULL;
558 GThread*
559 g_thread_create_full (GThreadFunc func,
560 gpointer data,
561 gulong stack_size,
562 gboolean joinable,
563 gboolean bound,
564 GThreadPriority priority,
565 GError **error)
567 GRealThread* result;
568 GError *local_error = NULL;
569 g_return_val_if_fail (func, NULL);
570 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
571 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
573 result = g_new0 (GRealThread, 1);
575 result->thread.joinable = joinable;
576 result->thread.priority = priority;
577 result->thread.func = func;
578 result->thread.data = data;
579 result->private_data = NULL;
580 G_LOCK (g_thread);
581 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
582 stack_size, joinable, bound, priority,
583 &result->system_thread, &local_error));
584 result->next = g_thread_all_threads;
585 g_thread_all_threads = result;
586 G_UNLOCK (g_thread);
588 if (local_error)
590 g_propagate_error (error, local_error);
591 g_free (result);
592 return NULL;
595 return (GThread*) result;
598 void
599 g_thread_exit (gpointer retval)
601 GRealThread* real = (GRealThread*) g_thread_self ();
602 real->retval = retval;
603 G_THREAD_CF (thread_exit, (void)0, ());
606 gpointer
607 g_thread_join (GThread* thread)
609 GRealThread* real = (GRealThread*) thread;
610 GRealThread *p, *t;
611 gpointer retval;
613 g_return_val_if_fail (thread, NULL);
614 g_return_val_if_fail (thread->joinable, NULL);
615 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
616 zero_thread), NULL);
618 G_THREAD_UF (thread_join, (&real->system_thread));
620 retval = real->retval;
622 G_LOCK (g_thread);
623 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
625 if (t == (GRealThread*) thread)
627 if (p)
628 p->next = t->next;
629 else
630 g_thread_all_threads = t->next;
631 break;
634 G_UNLOCK (g_thread);
636 /* Just to make sure, this isn't used any more */
637 thread->joinable = 0;
638 g_system_thread_assign (real->system_thread, zero_thread);
640 /* the thread structure for non-joinable threads is freed upon
641 thread end. We free the memory here. This will leave a loose end,
642 if a joinable thread is not joined. */
644 g_free (thread);
646 return retval;
649 void
650 g_thread_set_priority (GThread* thread,
651 GThreadPriority priority)
653 GRealThread* real = (GRealThread*) thread;
655 g_return_if_fail (thread);
656 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
657 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
658 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
660 thread->priority = priority;
662 G_THREAD_CF (thread_set_priority, (void)0,
663 (&real->system_thread, priority));
666 GThread*
667 g_thread_self (void)
669 GRealThread* thread = g_private_get (g_thread_specific_private);
671 if (!thread)
673 /* If no thread data is available, provide and set one. This
674 can happen for the main thread and for threads, that are not
675 created by GLib. */
676 thread = g_new0 (GRealThread, 1);
677 thread->thread.joinable = FALSE; /* This is a save guess */
678 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
679 just a guess */
680 thread->thread.func = NULL;
681 thread->thread.data = NULL;
682 thread->private_data = NULL;
684 if (g_thread_supported ())
685 G_THREAD_UF (thread_self, (&thread->system_thread));
687 g_private_set (g_thread_specific_private, thread);
689 G_LOCK (g_thread);
690 thread->next = g_thread_all_threads;
691 g_thread_all_threads = thread;
692 G_UNLOCK (g_thread);
695 return (GThread*)thread;
698 void
699 g_static_rw_lock_init (GStaticRWLock* lock)
701 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
703 g_return_if_fail (lock);
705 *lock = init_lock;
708 inline static void
709 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
711 if (!*cond)
712 *cond = g_cond_new ();
713 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
716 inline static void
717 g_static_rw_lock_signal (GStaticRWLock* lock)
719 if (lock->want_to_write && lock->write_cond)
720 g_cond_signal (lock->write_cond);
721 else if (lock->want_to_read && lock->read_cond)
722 g_cond_broadcast (lock->read_cond);
725 void
726 g_static_rw_lock_reader_lock (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->want_to_read++;
735 while (lock->have_writer || lock->want_to_write)
736 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
737 lock->want_to_read--;
738 lock->read_counter++;
739 g_static_mutex_unlock (&lock->mutex);
742 gboolean
743 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
745 gboolean ret_val = FALSE;
747 g_return_val_if_fail (lock, FALSE);
749 if (!g_threads_got_initialized)
750 return TRUE;
752 g_static_mutex_lock (&lock->mutex);
753 if (!lock->have_writer && !lock->want_to_write)
755 lock->read_counter++;
756 ret_val = TRUE;
758 g_static_mutex_unlock (&lock->mutex);
759 return ret_val;
762 void
763 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
765 g_return_if_fail (lock);
767 if (!g_threads_got_initialized)
768 return;
770 g_static_mutex_lock (&lock->mutex);
771 lock->read_counter--;
772 if (lock->read_counter == 0)
773 g_static_rw_lock_signal (lock);
774 g_static_mutex_unlock (&lock->mutex);
777 void
778 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
780 g_return_if_fail (lock);
782 if (!g_threads_got_initialized)
783 return;
785 g_static_mutex_lock (&lock->mutex);
786 lock->want_to_write++;
787 while (lock->have_writer || lock->read_counter)
788 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
789 lock->want_to_write--;
790 lock->have_writer = TRUE;
791 g_static_mutex_unlock (&lock->mutex);
794 gboolean
795 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
797 gboolean ret_val = FALSE;
799 g_return_val_if_fail (lock, FALSE);
801 if (!g_threads_got_initialized)
802 return TRUE;
804 g_static_mutex_lock (&lock->mutex);
805 if (!lock->have_writer && !lock->read_counter)
807 lock->have_writer = TRUE;
808 ret_val = TRUE;
810 g_static_mutex_unlock (&lock->mutex);
811 return ret_val;
814 void
815 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
817 g_return_if_fail (lock);
819 if (!g_threads_got_initialized)
820 return;
822 g_static_mutex_lock (&lock->mutex);
823 lock->have_writer = FALSE;
824 g_static_rw_lock_signal (lock);
825 g_static_mutex_unlock (&lock->mutex);
828 void
829 g_static_rw_lock_free (GStaticRWLock* lock)
831 g_return_if_fail (lock);
833 if (lock->read_cond)
835 g_cond_free (lock->read_cond);
836 lock->read_cond = NULL;
838 if (lock->write_cond)
840 g_cond_free (lock->write_cond);
841 lock->write_cond = NULL;
843 g_static_mutex_free (&lock->mutex);
847 * g_thread_foreach
848 * @thread_func: function to call for all GThread structures
849 * @user_data: second argument to @thread_func
851 * Call @thread_func on all existing #GThread structures. Note that
852 * threads may decide to exit while @thread_func is running, so
853 * without intimate knowledge about the lifetime of foreign threads,
854 * @thread_func shouldn't access the GThread* pointer passed in as
855 * first argument. However, @thread_func will not be called for threads
856 * which are known to have exited already.
858 * Due to thread lifetime checks, this function has an execution complexity
859 * which is quadratic in the number of existing threads.
861 * Since: 2.10
863 void
864 g_thread_foreach (GFunc thread_func,
865 gpointer user_data)
867 GSList *slist = NULL;
868 GRealThread *thread;
869 g_return_if_fail (thread_func != NULL);
870 /* snapshot the list of threads for iteration */
871 G_LOCK (g_thread);
872 for (thread = g_thread_all_threads; thread; thread = thread->next)
873 slist = g_slist_prepend (slist, thread);
874 G_UNLOCK (g_thread);
875 /* walk the list, skipping non-existant threads */
876 while (slist)
878 GSList *node = slist;
879 slist = node->next;
880 /* check whether the current thread still exists */
881 G_LOCK (g_thread);
882 for (thread = g_thread_all_threads; thread; thread = thread->next)
883 if (thread == node->data)
884 break;
885 G_UNLOCK (g_thread);
886 if (thread)
887 thread_func (thread, user_data);
888 g_slist_free_1 (node);
892 #define __G_THREAD_C__
893 #include "galiasdef.c"