Apply fix for CVE-2008-2371 to fix a heap-based buffer overflow.
[glib.git] / glib / gthread.c
blobe186b9f23741f106d28b72a010d28613b7c52e49
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 /* implement gthread.h's inline functions */
36 #define G_IMPLEMENT_INLINES 1
37 #define __G_THREAD_C__
39 #include "config.h"
41 #include "glib.h"
42 #include "gthreadprivate.h"
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
48 #ifndef G_OS_WIN32
49 #include <sys/time.h>
50 #include <time.h>
51 #else
52 #include <windows.h>
53 #endif /* G_OS_WIN32 */
55 #include <string.h>
57 #include "galias.h"
59 GQuark
60 g_thread_error_quark (void)
62 return g_quark_from_static_string ("g_thread_error");
65 /* Keep this in sync with GRealThread in gmain.c! */
66 typedef struct _GRealThread GRealThread;
67 struct _GRealThread
69 GThread thread;
70 gpointer private_data;
71 GRealThread *next;
72 gpointer retval;
73 GSystemThread system_thread;
76 typedef struct _GStaticPrivateNode GStaticPrivateNode;
77 struct _GStaticPrivateNode
79 gpointer data;
80 GDestroyNotify destroy;
83 static void g_thread_cleanup (gpointer data);
84 static void g_thread_fail (void);
85 static guint64 gettime (void);
87 guint64 (*g_thread_gettime) (void) = gettime;
89 /* Global variables */
91 static GSystemThread zero_thread; /* This is initialized to all zero */
92 gboolean g_thread_use_default_impl = TRUE;
93 gboolean g_threads_got_initialized = FALSE;
95 GThreadFunctions g_thread_functions_for_glib_use = {
96 (GMutex*(*)())g_thread_fail, /* mutex_new */
97 NULL, /* mutex_lock */
98 NULL, /* mutex_trylock */
99 NULL, /* mutex_unlock */
100 NULL, /* mutex_free */
101 (GCond*(*)())g_thread_fail, /* cond_new */
102 NULL, /* cond_signal */
103 NULL, /* cond_broadcast */
104 NULL, /* cond_wait */
105 NULL, /* cond_timed_wait */
106 NULL, /* cond_free */
107 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
108 NULL, /* private_get */
109 NULL, /* private_set */
110 (void(*)(GThreadFunc, gpointer, gulong,
111 gboolean, gboolean, GThreadPriority,
112 gpointer, GError**))g_thread_fail, /* thread_create */
113 NULL, /* thread_yield */
114 NULL, /* thread_join */
115 NULL, /* thread_exit */
116 NULL, /* thread_set_priority */
117 NULL, /* thread_self */
118 NULL /* thread_equal */
121 /* Local data */
123 static GMutex *g_once_mutex = NULL;
124 static GCond *g_once_cond = NULL;
125 static GPrivate *g_thread_specific_private = NULL;
126 static GRealThread *g_thread_all_threads = NULL;
127 static GSList *g_thread_free_indeces = NULL;
128 static GSList* g_once_init_list = NULL;
130 G_LOCK_DEFINE_STATIC (g_thread);
132 #ifdef G_THREADS_ENABLED
133 /* This must be called only once, before any threads are created.
134 * It will only be called from g_thread_init() in -lgthread.
136 void
137 g_thread_init_glib (void)
139 /* We let the main thread (the one that calls g_thread_init) inherit
140 * the static_private data set before calling g_thread_init
142 GRealThread* main_thread = (GRealThread*) g_thread_self ();
144 /* mutex and cond creation works without g_threads_got_initialized */
145 g_once_mutex = g_mutex_new ();
146 g_once_cond = g_cond_new ();
148 /* we may only create mutex and cond in here */
149 _g_mem_thread_init_noprivate_nomessage ();
151 /* setup the basic threading system */
152 g_threads_got_initialized = TRUE;
153 g_thread_specific_private = g_private_new (g_thread_cleanup);
154 g_private_set (g_thread_specific_private, main_thread);
155 G_THREAD_UF (thread_self, (&main_thread->system_thread));
157 /* complete memory system initialization, g_private_*() works now */
158 _g_slice_thread_init_nomessage ();
160 /* accomplish log system initialization to enable messaging */
161 _g_messages_thread_init_nomessage ();
163 /* we may run full-fledged initializers from here */
164 _g_atomic_thread_init ();
165 _g_convert_thread_init ();
166 _g_rand_thread_init ();
167 _g_main_thread_init ();
168 _g_utils_thread_init ();
169 #ifdef G_OS_WIN32
170 _g_win32_thread_init ();
171 #endif
173 #endif /* G_THREADS_ENABLED */
175 gpointer
176 g_once_impl (GOnce *once,
177 GThreadFunc func,
178 gpointer arg)
180 g_mutex_lock (g_once_mutex);
182 while (once->status == G_ONCE_STATUS_PROGRESS)
183 g_cond_wait (g_once_cond, g_once_mutex);
185 if (once->status != G_ONCE_STATUS_READY)
187 once->status = G_ONCE_STATUS_PROGRESS;
188 g_mutex_unlock (g_once_mutex);
190 once->retval = func (arg);
192 g_mutex_lock (g_once_mutex);
193 once->status = G_ONCE_STATUS_READY;
194 g_cond_broadcast (g_once_cond);
197 g_mutex_unlock (g_once_mutex);
199 return once->retval;
202 gboolean
203 g_once_init_enter_impl (volatile gsize *value_location)
205 gboolean need_init = FALSE;
206 g_mutex_lock (g_once_mutex);
207 if (g_atomic_pointer_get ((void**) value_location) == NULL)
209 if (!g_slist_find (g_once_init_list, (void*) value_location))
211 need_init = TRUE;
212 g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
214 else
216 g_cond_wait (g_once_cond, g_once_mutex);
217 while (g_slist_find (g_once_init_list, (void*) value_location));
219 g_mutex_unlock (g_once_mutex);
220 return need_init;
223 void
224 g_once_init_leave (volatile gsize *value_location,
225 gsize initialization_value)
227 g_return_if_fail (g_atomic_pointer_get ((void**) value_location) == NULL);
228 g_return_if_fail (initialization_value != 0);
229 g_return_if_fail (g_once_init_list != NULL);
231 g_atomic_pointer_set ((void**) value_location, (void*) initialization_value);
232 g_mutex_lock (g_once_mutex);
233 g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
234 g_cond_broadcast (g_once_cond);
235 g_mutex_unlock (g_once_mutex);
238 void
239 g_static_mutex_init (GStaticMutex *mutex)
241 static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
243 g_return_if_fail (mutex);
245 *mutex = init_mutex;
248 GMutex *
249 g_static_mutex_get_mutex_impl (GMutex** mutex)
251 if (!g_thread_supported ())
252 return NULL;
254 g_assert (g_once_mutex);
256 g_mutex_lock (g_once_mutex);
258 if (!(*mutex))
259 g_atomic_pointer_set ((void**) mutex, g_mutex_new());
261 g_mutex_unlock (g_once_mutex);
263 return *mutex;
266 void
267 g_static_mutex_free (GStaticMutex* mutex)
269 GMutex **runtime_mutex;
271 g_return_if_fail (mutex);
273 /* The runtime_mutex is the first (or only) member of GStaticMutex,
274 * see both versions (of glibconfig.h) in configure.in. Note, that
275 * this variable is NULL, if g_thread_init() hasn't been called or
276 * if we're using the default thread implementation and it provides
277 * static mutexes. */
278 runtime_mutex = ((GMutex**)mutex);
280 if (*runtime_mutex)
281 g_mutex_free (*runtime_mutex);
283 *runtime_mutex = NULL;
286 void
287 g_static_rec_mutex_init (GStaticRecMutex *mutex)
289 static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
291 g_return_if_fail (mutex);
293 *mutex = init_mutex;
296 void
297 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
299 GSystemThread self;
301 g_return_if_fail (mutex);
303 if (!g_thread_supported ())
304 return;
306 G_THREAD_UF (thread_self, (&self));
308 if (g_system_thread_equal (self, mutex->owner))
310 mutex->depth++;
311 return;
313 g_static_mutex_lock (&mutex->mutex);
314 g_system_thread_assign (mutex->owner, self);
315 mutex->depth = 1;
318 gboolean
319 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
321 GSystemThread self;
323 g_return_val_if_fail (mutex, FALSE);
325 if (!g_thread_supported ())
326 return TRUE;
328 G_THREAD_UF (thread_self, (&self));
330 if (g_system_thread_equal (self, mutex->owner))
332 mutex->depth++;
333 return TRUE;
336 if (!g_static_mutex_trylock (&mutex->mutex))
337 return FALSE;
339 g_system_thread_assign (mutex->owner, self);
340 mutex->depth = 1;
341 return TRUE;
344 void
345 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
347 g_return_if_fail (mutex);
349 if (!g_thread_supported ())
350 return;
352 if (mutex->depth > 1)
354 mutex->depth--;
355 return;
357 g_system_thread_assign (mutex->owner, zero_thread);
358 g_static_mutex_unlock (&mutex->mutex);
361 void
362 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
363 guint depth)
365 GSystemThread self;
366 g_return_if_fail (mutex);
368 if (!g_thread_supported ())
369 return;
371 if (depth == 0)
372 return;
374 G_THREAD_UF (thread_self, (&self));
376 if (g_system_thread_equal (self, mutex->owner))
378 mutex->depth += depth;
379 return;
381 g_static_mutex_lock (&mutex->mutex);
382 g_system_thread_assign (mutex->owner, self);
383 mutex->depth = depth;
386 guint
387 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
389 guint depth;
391 g_return_val_if_fail (mutex, 0);
393 if (!g_thread_supported ())
394 return 1;
396 depth = mutex->depth;
398 g_system_thread_assign (mutex->owner, zero_thread);
399 mutex->depth = 0;
400 g_static_mutex_unlock (&mutex->mutex);
402 return depth;
405 void
406 g_static_rec_mutex_free (GStaticRecMutex *mutex)
408 g_return_if_fail (mutex);
410 g_static_mutex_free (&mutex->mutex);
413 void
414 g_static_private_init (GStaticPrivate *private_key)
416 private_key->index = 0;
419 gpointer
420 g_static_private_get (GStaticPrivate *private_key)
422 GRealThread *self = (GRealThread*) g_thread_self ();
423 GArray *array;
425 array = self->private_data;
426 if (!array)
427 return NULL;
429 if (!private_key->index)
430 return NULL;
431 else if (private_key->index <= array->len)
432 return g_array_index (array, GStaticPrivateNode,
433 private_key->index - 1).data;
434 else
435 return NULL;
438 void
439 g_static_private_set (GStaticPrivate *private_key,
440 gpointer data,
441 GDestroyNotify notify)
443 GRealThread *self = (GRealThread*) g_thread_self ();
444 GArray *array;
445 static guint next_index = 0;
446 GStaticPrivateNode *node;
448 array = self->private_data;
449 if (!array)
451 array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
452 self->private_data = array;
455 if (!private_key->index)
457 G_LOCK (g_thread);
459 if (!private_key->index)
461 if (g_thread_free_indeces)
463 private_key->index =
464 GPOINTER_TO_UINT (g_thread_free_indeces->data);
465 g_thread_free_indeces =
466 g_slist_delete_link (g_thread_free_indeces,
467 g_thread_free_indeces);
469 else
470 private_key->index = ++next_index;
473 G_UNLOCK (g_thread);
476 if (private_key->index > array->len)
477 g_array_set_size (array, private_key->index);
479 node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
480 if (node->destroy)
482 gpointer ddata = node->data;
483 GDestroyNotify ddestroy = node->destroy;
485 node->data = data;
486 node->destroy = notify;
488 ddestroy (ddata);
490 else
492 node->data = data;
493 node->destroy = notify;
497 void
498 g_static_private_free (GStaticPrivate *private_key)
500 guint index = private_key->index;
501 GRealThread *thread;
503 if (!index)
504 return;
506 private_key->index = 0;
508 G_LOCK (g_thread);
510 thread = g_thread_all_threads;
511 while (thread)
513 GArray *array = thread->private_data;
514 thread = thread->next;
516 if (array && index <= array->len)
518 GStaticPrivateNode *node = &g_array_index (array,
519 GStaticPrivateNode,
520 index - 1);
521 gpointer ddata = node->data;
522 GDestroyNotify ddestroy = node->destroy;
524 node->data = NULL;
525 node->destroy = NULL;
527 if (ddestroy)
529 G_UNLOCK (g_thread);
530 ddestroy (ddata);
531 G_LOCK (g_thread);
535 g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
536 GUINT_TO_POINTER (index));
537 G_UNLOCK (g_thread);
540 static void
541 g_thread_cleanup (gpointer data)
543 if (data)
545 GRealThread* thread = data;
546 if (thread->private_data)
548 GArray* array = thread->private_data;
549 guint i;
551 for (i = 0; i < array->len; i++ )
553 GStaticPrivateNode *node =
554 &g_array_index (array, GStaticPrivateNode, i);
555 if (node->destroy)
556 node->destroy (node->data);
558 g_array_free (array, TRUE);
561 /* We only free the thread structure, if it isn't joinable. If
562 it is, the structure is freed in g_thread_join */
563 if (!thread->thread.joinable)
565 GRealThread *t, *p;
567 G_LOCK (g_thread);
568 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
570 if (t == thread)
572 if (p)
573 p->next = t->next;
574 else
575 g_thread_all_threads = t->next;
576 break;
579 G_UNLOCK (g_thread);
581 /* Just to make sure, this isn't used any more */
582 g_system_thread_assign (thread->system_thread, zero_thread);
583 g_free (thread);
588 static void
589 g_thread_fail (void)
591 g_error ("The thread system is not yet initialized.");
594 #define G_NSEC_PER_SEC 1000000000
596 static guint64
597 gettime (void)
599 #ifdef G_OS_WIN32
600 guint64 v;
602 /* Returns 100s of nanoseconds since start of 1601 */
603 GetSystemTimeAsFileTime ((FILETIME *)&v);
605 /* Offset to Unix epoch */
606 v -= G_GINT64_CONSTANT (116444736000000000);
607 /* Convert to nanoseconds */
608 v *= 100;
610 return v;
611 #else
612 struct timeval tv;
614 gettimeofday (&tv, NULL);
616 return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
617 #endif
620 static gpointer
621 g_thread_create_proxy (gpointer data)
623 GRealThread* thread = data;
625 g_assert (data);
627 /* This has to happen before G_LOCK, as that might call g_thread_self */
628 g_private_set (g_thread_specific_private, data);
630 /* the lock makes sure, that thread->system_thread is written,
631 before thread->thread.func is called. See g_thread_create. */
632 G_LOCK (g_thread);
633 G_UNLOCK (g_thread);
635 thread->retval = thread->thread.func (thread->thread.data);
637 return NULL;
640 GThread*
641 g_thread_create_full (GThreadFunc func,
642 gpointer data,
643 gulong stack_size,
644 gboolean joinable,
645 gboolean bound,
646 GThreadPriority priority,
647 GError **error)
649 GRealThread* result;
650 GError *local_error = NULL;
651 g_return_val_if_fail (func, NULL);
652 g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
653 g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
655 result = g_new0 (GRealThread, 1);
657 result->thread.joinable = joinable;
658 result->thread.priority = priority;
659 result->thread.func = func;
660 result->thread.data = data;
661 result->private_data = NULL;
662 G_LOCK (g_thread);
663 G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
664 stack_size, joinable, bound, priority,
665 &result->system_thread, &local_error));
666 if (!local_error)
668 result->next = g_thread_all_threads;
669 g_thread_all_threads = result;
671 G_UNLOCK (g_thread);
673 if (local_error)
675 g_propagate_error (error, local_error);
676 g_free (result);
677 return NULL;
680 return (GThread*) result;
683 void
684 g_thread_exit (gpointer retval)
686 GRealThread* real = (GRealThread*) g_thread_self ();
687 real->retval = retval;
688 G_THREAD_CF (thread_exit, (void)0, ());
691 gpointer
692 g_thread_join (GThread* thread)
694 GRealThread* real = (GRealThread*) thread;
695 GRealThread *p, *t;
696 gpointer retval;
698 g_return_val_if_fail (thread, NULL);
699 g_return_val_if_fail (thread->joinable, NULL);
700 g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
701 zero_thread), NULL);
703 G_THREAD_UF (thread_join, (&real->system_thread));
705 retval = real->retval;
707 G_LOCK (g_thread);
708 for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
710 if (t == (GRealThread*) thread)
712 if (p)
713 p->next = t->next;
714 else
715 g_thread_all_threads = t->next;
716 break;
719 G_UNLOCK (g_thread);
721 /* Just to make sure, this isn't used any more */
722 thread->joinable = 0;
723 g_system_thread_assign (real->system_thread, zero_thread);
725 /* the thread structure for non-joinable threads is freed upon
726 thread end. We free the memory here. This will leave a loose end,
727 if a joinable thread is not joined. */
729 g_free (thread);
731 return retval;
734 void
735 g_thread_set_priority (GThread* thread,
736 GThreadPriority priority)
738 GRealThread* real = (GRealThread*) thread;
740 g_return_if_fail (thread);
741 g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
742 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
743 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
745 thread->priority = priority;
747 G_THREAD_CF (thread_set_priority, (void)0,
748 (&real->system_thread, priority));
751 GThread*
752 g_thread_self (void)
754 GRealThread* thread = g_private_get (g_thread_specific_private);
756 if (!thread)
758 /* If no thread data is available, provide and set one. This
759 can happen for the main thread and for threads, that are not
760 created by GLib. */
761 thread = g_new0 (GRealThread, 1);
762 thread->thread.joinable = FALSE; /* This is a save guess */
763 thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
764 just a guess */
765 thread->thread.func = NULL;
766 thread->thread.data = NULL;
767 thread->private_data = NULL;
769 if (g_thread_supported ())
770 G_THREAD_UF (thread_self, (&thread->system_thread));
772 g_private_set (g_thread_specific_private, thread);
774 G_LOCK (g_thread);
775 thread->next = g_thread_all_threads;
776 g_thread_all_threads = thread;
777 G_UNLOCK (g_thread);
780 return (GThread*)thread;
783 void
784 g_static_rw_lock_init (GStaticRWLock* lock)
786 static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
788 g_return_if_fail (lock);
790 *lock = init_lock;
793 inline static void
794 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
796 if (!*cond)
797 *cond = g_cond_new ();
798 g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
801 inline static void
802 g_static_rw_lock_signal (GStaticRWLock* lock)
804 if (lock->want_to_write && lock->write_cond)
805 g_cond_signal (lock->write_cond);
806 else if (lock->want_to_read && lock->read_cond)
807 g_cond_broadcast (lock->read_cond);
810 void
811 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
813 g_return_if_fail (lock);
815 if (!g_threads_got_initialized)
816 return;
818 g_static_mutex_lock (&lock->mutex);
819 lock->want_to_read++;
820 while (lock->have_writer || lock->want_to_write)
821 g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
822 lock->want_to_read--;
823 lock->read_counter++;
824 g_static_mutex_unlock (&lock->mutex);
827 gboolean
828 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
830 gboolean ret_val = FALSE;
832 g_return_val_if_fail (lock, FALSE);
834 if (!g_threads_got_initialized)
835 return TRUE;
837 g_static_mutex_lock (&lock->mutex);
838 if (!lock->have_writer && !lock->want_to_write)
840 lock->read_counter++;
841 ret_val = TRUE;
843 g_static_mutex_unlock (&lock->mutex);
844 return ret_val;
847 void
848 g_static_rw_lock_reader_unlock (GStaticRWLock* lock)
850 g_return_if_fail (lock);
852 if (!g_threads_got_initialized)
853 return;
855 g_static_mutex_lock (&lock->mutex);
856 lock->read_counter--;
857 if (lock->read_counter == 0)
858 g_static_rw_lock_signal (lock);
859 g_static_mutex_unlock (&lock->mutex);
862 void
863 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
865 g_return_if_fail (lock);
867 if (!g_threads_got_initialized)
868 return;
870 g_static_mutex_lock (&lock->mutex);
871 lock->want_to_write++;
872 while (lock->have_writer || lock->read_counter)
873 g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
874 lock->want_to_write--;
875 lock->have_writer = TRUE;
876 g_static_mutex_unlock (&lock->mutex);
879 gboolean
880 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
882 gboolean ret_val = FALSE;
884 g_return_val_if_fail (lock, FALSE);
886 if (!g_threads_got_initialized)
887 return TRUE;
889 g_static_mutex_lock (&lock->mutex);
890 if (!lock->have_writer && !lock->read_counter)
892 lock->have_writer = TRUE;
893 ret_val = TRUE;
895 g_static_mutex_unlock (&lock->mutex);
896 return ret_val;
899 void
900 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
902 g_return_if_fail (lock);
904 if (!g_threads_got_initialized)
905 return;
907 g_static_mutex_lock (&lock->mutex);
908 lock->have_writer = FALSE;
909 g_static_rw_lock_signal (lock);
910 g_static_mutex_unlock (&lock->mutex);
913 void
914 g_static_rw_lock_free (GStaticRWLock* lock)
916 g_return_if_fail (lock);
918 if (lock->read_cond)
920 g_cond_free (lock->read_cond);
921 lock->read_cond = NULL;
923 if (lock->write_cond)
925 g_cond_free (lock->write_cond);
926 lock->write_cond = NULL;
928 g_static_mutex_free (&lock->mutex);
932 * g_thread_foreach
933 * @thread_func: function to call for all GThread structures
934 * @user_data: second argument to @thread_func
936 * Call @thread_func on all existing #GThread structures. Note that
937 * threads may decide to exit while @thread_func is running, so
938 * without intimate knowledge about the lifetime of foreign threads,
939 * @thread_func shouldn't access the GThread* pointer passed in as
940 * first argument. However, @thread_func will not be called for threads
941 * which are known to have exited already.
943 * Due to thread lifetime checks, this function has an execution complexity
944 * which is quadratic in the number of existing threads.
946 * Since: 2.10
948 void
949 g_thread_foreach (GFunc thread_func,
950 gpointer user_data)
952 GSList *slist = NULL;
953 GRealThread *thread;
954 g_return_if_fail (thread_func != NULL);
955 /* snapshot the list of threads for iteration */
956 G_LOCK (g_thread);
957 for (thread = g_thread_all_threads; thread; thread = thread->next)
958 slist = g_slist_prepend (slist, thread);
959 G_UNLOCK (g_thread);
960 /* walk the list, skipping non-existant threads */
961 while (slist)
963 GSList *node = slist;
964 slist = node->next;
965 /* check whether the current thread still exists */
966 G_LOCK (g_thread);
967 for (thread = g_thread_all_threads; thread; thread = thread->next)
968 if (thread == node->data)
969 break;
970 G_UNLOCK (g_thread);
971 if (thread)
972 thread_func (thread, user_data);
973 g_slist_free_1 (node);
977 #define __G_THREAD_C__
978 #include "galiasdef.c"