Run _g_atomic_thread_init as the first of the full fledged initializers to
[platform/upstream/glib.git] / glib / gthread.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gthread.c: MT safety related functions
5  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
6  *                Owen Taylor
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /*
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/.
29  */
30
31 /*
32  * MT safe
33  */
34
35 #include "config.h"
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include <string.h>
42
43 #include "glib.h"
44 #include "gthreadprivate.h"
45 #include "galias.h"
46
47 GQuark
48 g_thread_error_quark (void)
49 {
50   return g_quark_from_static_string ("g_thread_error");
51 }
52
53 /* Keep this in sync with GRealThread in gmain.c! */
54 typedef struct _GRealThread GRealThread;
55 struct  _GRealThread
56 {
57   GThread thread;
58   gpointer private_data;
59   GRealThread *next;
60   gpointer retval;
61   GSystemThread system_thread;
62 };
63
64 typedef struct _GStaticPrivateNode GStaticPrivateNode;
65 struct _GStaticPrivateNode
66 {
67   gpointer       data;
68   GDestroyNotify destroy;
69 };
70
71 static void g_thread_cleanup (gpointer data);
72 static void g_thread_fail (void);
73
74 /* Global variables */
75
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;
79
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 */
103 };
104
105 /* Local data */
106
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;
112
113 G_LOCK_DEFINE_STATIC (g_thread);
114
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.
118  */
119 void
120 g_thread_init_glib (void)
121 {
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
124    */
125   GRealThread* main_thread = (GRealThread*) g_thread_self ();
126
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 ();
130
131   /* we may only create mutex and cond in here */
132   _g_mem_thread_init_noprivate_nomessage ();
133
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));
139
140   /* complete memory system initialization, g_private_*() works now */
141   _g_slice_thread_init_nomessage ();
142
143   /* accomplish log system initialization to enable messaging */
144   _g_messages_thread_init_nomessage ();
145
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
155 }
156 #endif /* G_THREADS_ENABLED */
157
158 gpointer
159 g_once_impl (GOnce       *once,
160              GThreadFunc  func,
161              gpointer     arg)
162 {
163   g_mutex_lock (g_once_mutex);
164
165   while (once->status == G_ONCE_STATUS_PROGRESS)
166     g_cond_wait (g_once_cond, g_once_mutex);
167
168   if (once->status != G_ONCE_STATUS_READY)
169     {
170       once->status = G_ONCE_STATUS_PROGRESS;
171       g_mutex_unlock (g_once_mutex);
172
173       once->retval = func (arg);
174
175       g_mutex_lock (g_once_mutex);
176       once->status = G_ONCE_STATUS_READY;
177       g_cond_broadcast (g_once_cond);
178     }
179
180   g_mutex_unlock (g_once_mutex);
181
182   return once->retval;
183 }
184
185 void
186 g_static_mutex_init (GStaticMutex *mutex)
187 {
188   static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
189
190   g_return_if_fail (mutex);
191
192   *mutex = init_mutex;
193 }
194
195 GMutex *
196 g_static_mutex_get_mutex_impl (GMutex** mutex)
197 {
198   if (!g_thread_supported ())
199     return NULL;
200
201   g_assert (g_once_mutex);
202
203   g_mutex_lock (g_once_mutex);
204
205   if (!(*mutex))
206     g_atomic_pointer_set (mutex, g_mutex_new());
207
208   g_mutex_unlock (g_once_mutex);
209
210   return *mutex;
211 }
212
213 void
214 g_static_mutex_free (GStaticMutex* mutex)
215 {
216   GMutex **runtime_mutex;
217
218   g_return_if_fail (mutex);
219
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);
223
224   if (*runtime_mutex)
225     g_mutex_free (*runtime_mutex);
226
227   *runtime_mutex = NULL;
228 }
229
230 void
231 g_static_rec_mutex_init (GStaticRecMutex *mutex)
232 {
233   static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
234
235   g_return_if_fail (mutex);
236
237   *mutex = init_mutex;
238 }
239
240 void
241 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
242 {
243   GSystemThread self;
244
245   g_return_if_fail (mutex);
246
247   if (!g_thread_supported ())
248     return;
249
250   G_THREAD_UF (thread_self, (&self));
251
252   if (g_system_thread_equal (self, mutex->owner))
253     {
254       mutex->depth++;
255       return;
256     }
257   g_static_mutex_lock (&mutex->mutex);
258   g_system_thread_assign (mutex->owner, self);
259   mutex->depth = 1;
260 }
261
262 gboolean
263 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
264 {
265   GSystemThread self;
266
267   g_return_val_if_fail (mutex, FALSE);
268
269   if (!g_thread_supported ())
270     return TRUE;
271
272   G_THREAD_UF (thread_self, (&self));
273
274   if (g_system_thread_equal (self, mutex->owner))
275     {
276       mutex->depth++;
277       return TRUE;
278     }
279
280   if (!g_static_mutex_trylock (&mutex->mutex))
281     return FALSE;
282
283   g_system_thread_assign (mutex->owner, self);
284   mutex->depth = 1;
285   return TRUE;
286 }
287
288 void
289 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
290 {
291   g_return_if_fail (mutex);
292
293   if (!g_thread_supported ())
294     return;
295
296   if (mutex->depth > 1)
297     {
298       mutex->depth--;
299       return;
300     }
301   g_system_thread_assign (mutex->owner, zero_thread);
302   g_static_mutex_unlock (&mutex->mutex);
303 }
304
305 void
306 g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
307                                 guint            depth)
308 {
309   GSystemThread self;
310   g_return_if_fail (mutex);
311
312   if (!g_thread_supported ())
313     return;
314
315   if (depth == 0)
316     return;
317
318   G_THREAD_UF (thread_self, (&self));
319
320   if (g_system_thread_equal (self, mutex->owner))
321     {
322       mutex->depth += depth;
323       return;
324     }
325   g_static_mutex_lock (&mutex->mutex);
326   g_system_thread_assign (mutex->owner, self);
327   mutex->depth = depth;
328 }
329
330 guint
331 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
332 {
333   guint depth;
334
335   g_return_val_if_fail (mutex, 0);
336
337   if (!g_thread_supported ())
338     return 1;
339
340   depth = mutex->depth;
341
342   g_system_thread_assign (mutex->owner, zero_thread);
343   mutex->depth = 0;
344   g_static_mutex_unlock (&mutex->mutex);
345
346   return depth;
347 }
348
349 void
350 g_static_rec_mutex_free (GStaticRecMutex *mutex)
351 {
352   g_return_if_fail (mutex);
353
354   g_static_mutex_free (&mutex->mutex);
355 }
356
357 void
358 g_static_private_init (GStaticPrivate *private_key)
359 {
360   private_key->index = 0;
361 }
362
363 gpointer
364 g_static_private_get (GStaticPrivate *private_key)
365 {
366   GRealThread *self = (GRealThread*) g_thread_self ();
367   GArray *array;
368
369   array = self->private_data;
370   if (!array)
371     return NULL;
372
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;
380 }
381
382 void
383 g_static_private_set (GStaticPrivate *private_key,
384                       gpointer        data,
385                       GDestroyNotify  notify)
386 {
387   GRealThread *self = (GRealThread*) g_thread_self ();
388   GArray *array;
389   static guint next_index = 0;
390   GStaticPrivateNode *node;
391
392   array = self->private_data;
393   if (!array)
394     {
395       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
396       self->private_data = array;
397     }
398
399   if (!private_key->index)
400     {
401       G_LOCK (g_thread);
402
403       if (!private_key->index)
404         {
405           if (g_thread_free_indeces)
406             {
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);
412             }
413           else
414             private_key->index = ++next_index;
415         }
416
417       G_UNLOCK (g_thread);
418     }
419
420   if (private_key->index > array->len)
421     g_array_set_size (array, private_key->index);
422
423   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
424   if (node->destroy)
425     {
426       gpointer ddata = node->data;
427       GDestroyNotify ddestroy = node->destroy;
428
429       node->data = data;
430       node->destroy = notify;
431
432       ddestroy (ddata);
433     }
434   else
435     {
436       node->data = data;
437       node->destroy = notify;
438     }
439 }
440
441 void
442 g_static_private_free (GStaticPrivate *private_key)
443 {
444   guint index = private_key->index;
445   GRealThread *thread;
446
447   if (!index)
448     return;
449
450   private_key->index = 0;
451
452   G_LOCK (g_thread);
453
454   thread = g_thread_all_threads;
455   while (thread)
456     {
457       GArray *array = thread->private_data;
458       thread = thread->next;
459
460       if (array && index <= array->len)
461         {
462           GStaticPrivateNode *node = &g_array_index (array,
463                                                      GStaticPrivateNode,
464                                                      index - 1);
465           gpointer ddata = node->data;
466           GDestroyNotify ddestroy = node->destroy;
467
468           node->data = NULL;
469           node->destroy = NULL;
470
471           if (ddestroy)
472             {
473               G_UNLOCK (g_thread);
474               ddestroy (ddata);
475               G_LOCK (g_thread);
476               }
477         }
478     }
479   g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
480                                            GUINT_TO_POINTER (index));
481   G_UNLOCK (g_thread);
482 }
483
484 static void
485 g_thread_cleanup (gpointer data)
486 {
487   if (data)
488     {
489       GRealThread* thread = data;
490       if (thread->private_data)
491         {
492           GArray* array = thread->private_data;
493           guint i;
494
495           for (i = 0; i < array->len; i++ )
496             {
497               GStaticPrivateNode *node =
498                 &g_array_index (array, GStaticPrivateNode, i);
499               if (node->destroy)
500                 node->destroy (node->data);
501             }
502           g_array_free (array, TRUE);
503         }
504
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)
508         {
509           GRealThread *t, *p;
510
511           G_LOCK (g_thread);
512           for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
513             {
514               if (t == thread)
515                 {
516                   if (p)
517                     p->next = t->next;
518                   else
519                     g_thread_all_threads = t->next;
520                   break;
521                 }
522             }
523           G_UNLOCK (g_thread);
524
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);
528         }
529     }
530 }
531
532 static void
533 g_thread_fail (void)
534 {
535   g_error ("The thread system is not yet initialized.");
536 }
537
538 static gpointer
539 g_thread_create_proxy (gpointer data)
540 {
541   GRealThread* thread = data;
542
543   g_assert (data);
544
545   /* This has to happen before G_LOCK, as that might call g_thread_self */
546   g_private_set (g_thread_specific_private, data);
547
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);
552
553   thread->retval = thread->thread.func (thread->thread.data);
554
555   return NULL;
556 }
557
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)
566 {
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);
572
573   result = g_new0 (GRealThread, 1);
574
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);
587
588   if (local_error)
589     {
590       g_propagate_error (error, local_error);
591       g_free (result);
592       return NULL;
593     }
594
595   return (GThread*) result;
596 }
597
598 void
599 g_thread_exit (gpointer retval)
600 {
601   GRealThread* real = (GRealThread*) g_thread_self ();
602   real->retval = retval;
603   G_THREAD_CF (thread_exit, (void)0, ());
604 }
605
606 gpointer
607 g_thread_join (GThread* thread)
608 {
609   GRealThread* real = (GRealThread*) thread;
610   GRealThread *p, *t;
611   gpointer retval;
612
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);
617
618   G_THREAD_UF (thread_join, (&real->system_thread));
619
620   retval = real->retval;
621
622   G_LOCK (g_thread);
623   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
624     {
625       if (t == (GRealThread*) thread)
626         {
627           if (p)
628             p->next = t->next;
629           else
630             g_thread_all_threads = t->next;
631           break;
632         }
633     }
634   G_UNLOCK (g_thread);
635
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);
639
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. */
643
644   g_free (thread);
645
646   return retval;
647 }
648
649 void
650 g_thread_set_priority (GThread* thread,
651                        GThreadPriority priority)
652 {
653   GRealThread* real = (GRealThread*) thread;
654
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);
659
660   thread->priority = priority;
661
662   G_THREAD_CF (thread_set_priority, (void)0,
663                (&real->system_thread, priority));
664 }
665
666 GThread*
667 g_thread_self (void)
668 {
669   GRealThread* thread = g_private_get (g_thread_specific_private);
670
671   if (!thread)
672     {
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;
683
684       if (g_thread_supported ())
685         G_THREAD_UF (thread_self, (&thread->system_thread));
686
687       g_private_set (g_thread_specific_private, thread);
688
689       G_LOCK (g_thread);
690       thread->next = g_thread_all_threads;
691       g_thread_all_threads = thread;
692       G_UNLOCK (g_thread);
693     }
694
695   return (GThread*)thread;
696 }
697
698 void
699 g_static_rw_lock_init (GStaticRWLock* lock)
700 {
701   static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
702
703   g_return_if_fail (lock);
704
705   *lock = init_lock;
706 }
707
708 inline static void
709 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
710 {
711   if (!*cond)
712       *cond = g_cond_new ();
713   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
714 }
715
716 inline static void
717 g_static_rw_lock_signal (GStaticRWLock* lock)
718 {
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);
723 }
724
725 void
726 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
727 {
728   g_return_if_fail (lock);
729
730   if (!g_threads_got_initialized)
731     return;
732
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);
740 }
741
742 gboolean
743 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
744 {
745   gboolean ret_val = FALSE;
746
747   g_return_val_if_fail (lock, FALSE);
748
749   if (!g_threads_got_initialized)
750     return TRUE;
751
752   g_static_mutex_lock (&lock->mutex);
753   if (!lock->have_writer && !lock->want_to_write)
754     {
755       lock->read_counter++;
756       ret_val = TRUE;
757     }
758   g_static_mutex_unlock (&lock->mutex);
759   return ret_val;
760 }
761
762 void
763 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
764 {
765   g_return_if_fail (lock);
766
767   if (!g_threads_got_initialized)
768     return;
769
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);
775 }
776
777 void
778 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
779 {
780   g_return_if_fail (lock);
781
782   if (!g_threads_got_initialized)
783     return;
784
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);
792 }
793
794 gboolean
795 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
796 {
797   gboolean ret_val = FALSE;
798
799   g_return_val_if_fail (lock, FALSE);
800
801   if (!g_threads_got_initialized)
802     return TRUE;
803
804   g_static_mutex_lock (&lock->mutex);
805   if (!lock->have_writer && !lock->read_counter)
806     {
807       lock->have_writer = TRUE;
808       ret_val = TRUE;
809     }
810   g_static_mutex_unlock (&lock->mutex);
811   return ret_val;
812 }
813
814 void
815 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
816 {
817   g_return_if_fail (lock);
818
819   if (!g_threads_got_initialized)
820     return;
821
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);
826 }
827
828 void
829 g_static_rw_lock_free (GStaticRWLock* lock)
830 {
831   g_return_if_fail (lock);
832
833   if (lock->read_cond)
834     {
835       g_cond_free (lock->read_cond);
836       lock->read_cond = NULL;
837     }
838   if (lock->write_cond)
839     {
840       g_cond_free (lock->write_cond);
841       lock->write_cond = NULL;
842     }
843   g_static_mutex_free (&lock->mutex);
844 }
845
846 /**
847  * g_thread_foreach
848  * @thread_func: function to call for all GThread structures
849  * @user_data:   second argument to @thread_func
850  *
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.
857  *
858  * Due to thread lifetime checks, this function has an execution complexity
859  * which is quadratic in the number of existing threads.
860  *
861  * Since: 2.10
862  */
863 void
864 g_thread_foreach (GFunc    thread_func,
865                   gpointer user_data)
866 {
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)
877     {
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);
889     }
890 }
891
892 #define __G_THREAD_C__
893 #include "galiasdef.c"