Include windows.h and fix include order. (#394258, Kazuki Iwamoto)
[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 #include "glib.h"
38 #include "gthreadprivate.h"
39
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
44 #ifndef G_OS_WIN32
45 #include <windows.h>
46 #include <sys/time.h>
47 #include <time.h>
48 #endif /* G_OS_WIN32 */
49
50 #include <string.h>
51
52 #include "galias.h"
53
54 GQuark
55 g_thread_error_quark (void)
56 {
57   return g_quark_from_static_string ("g_thread_error");
58 }
59
60 /* Keep this in sync with GRealThread in gmain.c! */
61 typedef struct _GRealThread GRealThread;
62 struct  _GRealThread
63 {
64   GThread thread;
65   gpointer private_data;
66   GRealThread *next;
67   gpointer retval;
68   GSystemThread system_thread;
69 };
70
71 typedef struct _GStaticPrivateNode GStaticPrivateNode;
72 struct _GStaticPrivateNode
73 {
74   gpointer       data;
75   GDestroyNotify destroy;
76 };
77
78 static void    g_thread_cleanup (gpointer data);
79 static void    g_thread_fail (void);
80 static guint64 gettime (void);
81
82 /* Global variables */
83
84 static GSystemThread zero_thread; /* This is initialized to all zero */
85 gboolean g_thread_use_default_impl = TRUE;
86 gboolean g_threads_got_initialized = FALSE;
87
88 GThreadFunctions g_thread_functions_for_glib_use = {
89   (GMutex*(*)())g_thread_fail,                 /* mutex_new */
90   NULL,                                        /* mutex_lock */
91   NULL,                                        /* mutex_trylock */
92   NULL,                                        /* mutex_unlock */
93   NULL,                                        /* mutex_free */
94   (GCond*(*)())g_thread_fail,                  /* cond_new */
95   NULL,                                        /* cond_signal */
96   NULL,                                        /* cond_broadcast */
97   NULL,                                        /* cond_wait */
98   NULL,                                        /* cond_timed_wait  */
99   NULL,                                        /* cond_free */
100   (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
101   NULL,                                        /* private_get */
102   NULL,                                        /* private_set */
103   (void(*)(GThreadFunc, gpointer, gulong,
104            gboolean, gboolean, GThreadPriority,
105            gpointer, GError**))g_thread_fail,  /* thread_create */
106   NULL,                                        /* thread_yield */
107   NULL,                                        /* thread_join */
108   NULL,                                        /* thread_exit */
109   NULL,                                        /* thread_set_priority */
110   NULL,                                         /* thread_self */
111   NULL,                                         /* thread_equal */
112   gettime                                      /* gettime */
113 };
114
115 /* Local data */
116
117 static GMutex   *g_once_mutex = NULL;
118 static GCond    *g_once_cond = NULL;
119 static GPrivate *g_thread_specific_private = NULL;
120 static GRealThread *g_thread_all_threads = NULL;
121 static GSList   *g_thread_free_indeces = NULL;
122
123 G_LOCK_DEFINE_STATIC (g_thread);
124
125 #ifdef G_THREADS_ENABLED
126 /* This must be called only once, before any threads are created.
127  * It will only be called from g_thread_init() in -lgthread.
128  */
129 void
130 g_thread_init_glib (void)
131 {
132   /* We let the main thread (the one that calls g_thread_init) inherit
133    * the static_private data set before calling g_thread_init
134    */
135   GRealThread* main_thread = (GRealThread*) g_thread_self ();
136
137   /* mutex and cond creation works without g_threads_got_initialized */
138   g_once_mutex = g_mutex_new ();
139   g_once_cond = g_cond_new ();
140
141   /* we may only create mutex and cond in here */
142   _g_mem_thread_init_noprivate_nomessage ();
143
144   /* setup the basic threading system */
145   g_threads_got_initialized = TRUE;
146   g_thread_specific_private = g_private_new (g_thread_cleanup);
147   g_private_set (g_thread_specific_private, main_thread);
148   G_THREAD_UF (thread_self, (&main_thread->system_thread));
149
150   /* complete memory system initialization, g_private_*() works now */
151   _g_slice_thread_init_nomessage ();
152
153   /* accomplish log system initialization to enable messaging */
154   _g_messages_thread_init_nomessage ();
155
156   /* we may run full-fledged initializers from here */
157   _g_atomic_thread_init ();
158   _g_convert_thread_init ();
159   _g_rand_thread_init ();
160   _g_main_thread_init ();
161   _g_utils_thread_init ();
162 #ifdef G_OS_WIN32
163   _g_win32_thread_init ();
164 #endif
165 }
166 #endif /* G_THREADS_ENABLED */
167
168 gpointer
169 g_once_impl (GOnce       *once,
170              GThreadFunc  func,
171              gpointer     arg)
172 {
173   g_mutex_lock (g_once_mutex);
174
175   while (once->status == G_ONCE_STATUS_PROGRESS)
176     g_cond_wait (g_once_cond, g_once_mutex);
177
178   if (once->status != G_ONCE_STATUS_READY)
179     {
180       once->status = G_ONCE_STATUS_PROGRESS;
181       g_mutex_unlock (g_once_mutex);
182
183       once->retval = func (arg);
184
185       g_mutex_lock (g_once_mutex);
186       once->status = G_ONCE_STATUS_READY;
187       g_cond_broadcast (g_once_cond);
188     }
189
190   g_mutex_unlock (g_once_mutex);
191
192   return once->retval;
193 }
194
195 void
196 g_static_mutex_init (GStaticMutex *mutex)
197 {
198   static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
199
200   g_return_if_fail (mutex);
201
202   *mutex = init_mutex;
203 }
204
205 GMutex *
206 g_static_mutex_get_mutex_impl (GMutex** mutex)
207 {
208   if (!g_thread_supported ())
209     return NULL;
210
211   g_assert (g_once_mutex);
212
213   g_mutex_lock (g_once_mutex);
214
215   if (!(*mutex))
216     g_atomic_pointer_set (mutex, g_mutex_new());
217
218   g_mutex_unlock (g_once_mutex);
219
220   return *mutex;
221 }
222
223 void
224 g_static_mutex_free (GStaticMutex* mutex)
225 {
226   GMutex **runtime_mutex;
227
228   g_return_if_fail (mutex);
229
230   /* The runtime_mutex is the first (or only) member of GStaticMutex,
231    * see both versions (of glibconfig.h) in configure.in */
232   runtime_mutex = ((GMutex**)mutex);
233
234   if (*runtime_mutex)
235     g_mutex_free (*runtime_mutex);
236
237   *runtime_mutex = NULL;
238 }
239
240 void
241 g_static_rec_mutex_init (GStaticRecMutex *mutex)
242 {
243   static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
244
245   g_return_if_fail (mutex);
246
247   *mutex = init_mutex;
248 }
249
250 void
251 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
252 {
253   GSystemThread self;
254
255   g_return_if_fail (mutex);
256
257   if (!g_thread_supported ())
258     return;
259
260   G_THREAD_UF (thread_self, (&self));
261
262   if (g_system_thread_equal (self, mutex->owner))
263     {
264       mutex->depth++;
265       return;
266     }
267   g_static_mutex_lock (&mutex->mutex);
268   g_system_thread_assign (mutex->owner, self);
269   mutex->depth = 1;
270 }
271
272 gboolean
273 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
274 {
275   GSystemThread self;
276
277   g_return_val_if_fail (mutex, FALSE);
278
279   if (!g_thread_supported ())
280     return TRUE;
281
282   G_THREAD_UF (thread_self, (&self));
283
284   if (g_system_thread_equal (self, mutex->owner))
285     {
286       mutex->depth++;
287       return TRUE;
288     }
289
290   if (!g_static_mutex_trylock (&mutex->mutex))
291     return FALSE;
292
293   g_system_thread_assign (mutex->owner, self);
294   mutex->depth = 1;
295   return TRUE;
296 }
297
298 void
299 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
300 {
301   g_return_if_fail (mutex);
302
303   if (!g_thread_supported ())
304     return;
305
306   if (mutex->depth > 1)
307     {
308       mutex->depth--;
309       return;
310     }
311   g_system_thread_assign (mutex->owner, zero_thread);
312   g_static_mutex_unlock (&mutex->mutex);
313 }
314
315 void
316 g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
317                                 guint            depth)
318 {
319   GSystemThread self;
320   g_return_if_fail (mutex);
321
322   if (!g_thread_supported ())
323     return;
324
325   if (depth == 0)
326     return;
327
328   G_THREAD_UF (thread_self, (&self));
329
330   if (g_system_thread_equal (self, mutex->owner))
331     {
332       mutex->depth += depth;
333       return;
334     }
335   g_static_mutex_lock (&mutex->mutex);
336   g_system_thread_assign (mutex->owner, self);
337   mutex->depth = depth;
338 }
339
340 guint
341 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
342 {
343   guint depth;
344
345   g_return_val_if_fail (mutex, 0);
346
347   if (!g_thread_supported ())
348     return 1;
349
350   depth = mutex->depth;
351
352   g_system_thread_assign (mutex->owner, zero_thread);
353   mutex->depth = 0;
354   g_static_mutex_unlock (&mutex->mutex);
355
356   return depth;
357 }
358
359 void
360 g_static_rec_mutex_free (GStaticRecMutex *mutex)
361 {
362   g_return_if_fail (mutex);
363
364   g_static_mutex_free (&mutex->mutex);
365 }
366
367 void
368 g_static_private_init (GStaticPrivate *private_key)
369 {
370   private_key->index = 0;
371 }
372
373 gpointer
374 g_static_private_get (GStaticPrivate *private_key)
375 {
376   GRealThread *self = (GRealThread*) g_thread_self ();
377   GArray *array;
378
379   array = self->private_data;
380   if (!array)
381     return NULL;
382
383   if (!private_key->index)
384     return NULL;
385   else if (private_key->index <= array->len)
386     return g_array_index (array, GStaticPrivateNode,
387                           private_key->index - 1).data;
388   else
389     return NULL;
390 }
391
392 void
393 g_static_private_set (GStaticPrivate *private_key,
394                       gpointer        data,
395                       GDestroyNotify  notify)
396 {
397   GRealThread *self = (GRealThread*) g_thread_self ();
398   GArray *array;
399   static guint next_index = 0;
400   GStaticPrivateNode *node;
401
402   array = self->private_data;
403   if (!array)
404     {
405       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
406       self->private_data = array;
407     }
408
409   if (!private_key->index)
410     {
411       G_LOCK (g_thread);
412
413       if (!private_key->index)
414         {
415           if (g_thread_free_indeces)
416             {
417               private_key->index =
418                 GPOINTER_TO_UINT (g_thread_free_indeces->data);
419               g_thread_free_indeces =
420                 g_slist_delete_link (g_thread_free_indeces,
421                                      g_thread_free_indeces);
422             }
423           else
424             private_key->index = ++next_index;
425         }
426
427       G_UNLOCK (g_thread);
428     }
429
430   if (private_key->index > array->len)
431     g_array_set_size (array, private_key->index);
432
433   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
434   if (node->destroy)
435     {
436       gpointer ddata = node->data;
437       GDestroyNotify ddestroy = node->destroy;
438
439       node->data = data;
440       node->destroy = notify;
441
442       ddestroy (ddata);
443     }
444   else
445     {
446       node->data = data;
447       node->destroy = notify;
448     }
449 }
450
451 void
452 g_static_private_free (GStaticPrivate *private_key)
453 {
454   guint index = private_key->index;
455   GRealThread *thread;
456
457   if (!index)
458     return;
459
460   private_key->index = 0;
461
462   G_LOCK (g_thread);
463
464   thread = g_thread_all_threads;
465   while (thread)
466     {
467       GArray *array = thread->private_data;
468       thread = thread->next;
469
470       if (array && index <= array->len)
471         {
472           GStaticPrivateNode *node = &g_array_index (array,
473                                                      GStaticPrivateNode,
474                                                      index - 1);
475           gpointer ddata = node->data;
476           GDestroyNotify ddestroy = node->destroy;
477
478           node->data = NULL;
479           node->destroy = NULL;
480
481           if (ddestroy)
482             {
483               G_UNLOCK (g_thread);
484               ddestroy (ddata);
485               G_LOCK (g_thread);
486               }
487         }
488     }
489   g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
490                                            GUINT_TO_POINTER (index));
491   G_UNLOCK (g_thread);
492 }
493
494 static void
495 g_thread_cleanup (gpointer data)
496 {
497   if (data)
498     {
499       GRealThread* thread = data;
500       if (thread->private_data)
501         {
502           GArray* array = thread->private_data;
503           guint i;
504
505           for (i = 0; i < array->len; i++ )
506             {
507               GStaticPrivateNode *node =
508                 &g_array_index (array, GStaticPrivateNode, i);
509               if (node->destroy)
510                 node->destroy (node->data);
511             }
512           g_array_free (array, TRUE);
513         }
514
515       /* We only free the thread structure, if it isn't joinable. If
516          it is, the structure is freed in g_thread_join */
517       if (!thread->thread.joinable)
518         {
519           GRealThread *t, *p;
520
521           G_LOCK (g_thread);
522           for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
523             {
524               if (t == thread)
525                 {
526                   if (p)
527                     p->next = t->next;
528                   else
529                     g_thread_all_threads = t->next;
530                   break;
531                 }
532             }
533           G_UNLOCK (g_thread);
534
535           /* Just to make sure, this isn't used any more */
536           g_system_thread_assign (thread->system_thread, zero_thread);
537           g_free (thread);
538         }
539     }
540 }
541
542 static void
543 g_thread_fail (void)
544 {
545   g_error ("The thread system is not yet initialized.");
546 }
547
548 static guint64
549 gettime (void)
550 {
551 #ifdef G_OS_WIN32
552   guint64 v;
553
554   GetSystemTimeAsFileTime ((FILETIME *)&v);
555
556   return v;
557 #else
558   struct timeval tv;
559
560   gettimeofday (&tv, NULL);
561
562   return (guint64) tv.tv_sec * 1000000000 + tv.tv_usec * 1000; 
563 #endif
564 }
565
566 static gpointer
567 g_thread_create_proxy (gpointer data)
568 {
569   GRealThread* thread = data;
570
571   g_assert (data);
572
573   /* This has to happen before G_LOCK, as that might call g_thread_self */
574   g_private_set (g_thread_specific_private, data);
575
576   /* the lock makes sure, that thread->system_thread is written,
577      before thread->thread.func is called. See g_thread_create. */
578   G_LOCK (g_thread);
579   G_UNLOCK (g_thread);
580
581   thread->retval = thread->thread.func (thread->thread.data);
582
583   return NULL;
584 }
585
586 GThread*
587 g_thread_create_full (GThreadFunc                func,
588                       gpointer           data,
589                       gulong             stack_size,
590                       gboolean           joinable,
591                       gboolean           bound,
592                       GThreadPriority    priority,
593                       GError                **error)
594 {
595   GRealThread* result;
596   GError *local_error = NULL;
597   g_return_val_if_fail (func, NULL);
598   g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
599   g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
600
601   result = g_new0 (GRealThread, 1);
602
603   result->thread.joinable = joinable;
604   result->thread.priority = priority;
605   result->thread.func = func;
606   result->thread.data = data;
607   result->private_data = NULL;
608   G_LOCK (g_thread);
609   G_THREAD_UF (thread_create, (g_thread_create_proxy, result,
610                                stack_size, joinable, bound, priority,
611                                &result->system_thread, &local_error));
612   result->next = g_thread_all_threads;
613   g_thread_all_threads = result;
614   G_UNLOCK (g_thread);
615
616   if (local_error)
617     {
618       g_propagate_error (error, local_error);
619       g_free (result);
620       return NULL;
621     }
622
623   return (GThread*) result;
624 }
625
626 void
627 g_thread_exit (gpointer retval)
628 {
629   GRealThread* real = (GRealThread*) g_thread_self ();
630   real->retval = retval;
631   G_THREAD_CF (thread_exit, (void)0, ());
632 }
633
634 gpointer
635 g_thread_join (GThread* thread)
636 {
637   GRealThread* real = (GRealThread*) thread;
638   GRealThread *p, *t;
639   gpointer retval;
640
641   g_return_val_if_fail (thread, NULL);
642   g_return_val_if_fail (thread->joinable, NULL);
643   g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
644                                                 zero_thread), NULL);
645
646   G_THREAD_UF (thread_join, (&real->system_thread));
647
648   retval = real->retval;
649
650   G_LOCK (g_thread);
651   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
652     {
653       if (t == (GRealThread*) thread)
654         {
655           if (p)
656             p->next = t->next;
657           else
658             g_thread_all_threads = t->next;
659           break;
660         }
661     }
662   G_UNLOCK (g_thread);
663
664   /* Just to make sure, this isn't used any more */
665   thread->joinable = 0;
666   g_system_thread_assign (real->system_thread, zero_thread);
667
668   /* the thread structure for non-joinable threads is freed upon
669      thread end. We free the memory here. This will leave a loose end,
670      if a joinable thread is not joined. */
671
672   g_free (thread);
673
674   return retval;
675 }
676
677 void
678 g_thread_set_priority (GThread* thread,
679                        GThreadPriority priority)
680 {
681   GRealThread* real = (GRealThread*) thread;
682
683   g_return_if_fail (thread);
684   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
685   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
686   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
687
688   thread->priority = priority;
689
690   G_THREAD_CF (thread_set_priority, (void)0,
691                (&real->system_thread, priority));
692 }
693
694 GThread*
695 g_thread_self (void)
696 {
697   GRealThread* thread = g_private_get (g_thread_specific_private);
698
699   if (!thread)
700     {
701       /* If no thread data is available, provide and set one.  This
702          can happen for the main thread and for threads, that are not
703          created by GLib. */
704       thread = g_new0 (GRealThread, 1);
705       thread->thread.joinable = FALSE; /* This is a save guess */
706       thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
707                                                              just a guess */
708       thread->thread.func = NULL;
709       thread->thread.data = NULL;
710       thread->private_data = NULL;
711
712       if (g_thread_supported ())
713         G_THREAD_UF (thread_self, (&thread->system_thread));
714
715       g_private_set (g_thread_specific_private, thread);
716
717       G_LOCK (g_thread);
718       thread->next = g_thread_all_threads;
719       g_thread_all_threads = thread;
720       G_UNLOCK (g_thread);
721     }
722
723   return (GThread*)thread;
724 }
725
726 void
727 g_static_rw_lock_init (GStaticRWLock* lock)
728 {
729   static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
730
731   g_return_if_fail (lock);
732
733   *lock = init_lock;
734 }
735
736 inline static void
737 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
738 {
739   if (!*cond)
740       *cond = g_cond_new ();
741   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
742 }
743
744 inline static void
745 g_static_rw_lock_signal (GStaticRWLock* lock)
746 {
747   if (lock->want_to_write && lock->write_cond)
748     g_cond_signal (lock->write_cond);
749   else if (lock->want_to_read && lock->read_cond)
750     g_cond_broadcast (lock->read_cond);
751 }
752
753 void
754 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
755 {
756   g_return_if_fail (lock);
757
758   if (!g_threads_got_initialized)
759     return;
760
761   g_static_mutex_lock (&lock->mutex);
762   lock->want_to_read++;
763   while (lock->have_writer || lock->want_to_write)
764     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
765   lock->want_to_read--;
766   lock->read_counter++;
767   g_static_mutex_unlock (&lock->mutex);
768 }
769
770 gboolean
771 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
772 {
773   gboolean ret_val = FALSE;
774
775   g_return_val_if_fail (lock, FALSE);
776
777   if (!g_threads_got_initialized)
778     return TRUE;
779
780   g_static_mutex_lock (&lock->mutex);
781   if (!lock->have_writer && !lock->want_to_write)
782     {
783       lock->read_counter++;
784       ret_val = TRUE;
785     }
786   g_static_mutex_unlock (&lock->mutex);
787   return ret_val;
788 }
789
790 void
791 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
792 {
793   g_return_if_fail (lock);
794
795   if (!g_threads_got_initialized)
796     return;
797
798   g_static_mutex_lock (&lock->mutex);
799   lock->read_counter--;
800   if (lock->read_counter == 0)
801     g_static_rw_lock_signal (lock);
802   g_static_mutex_unlock (&lock->mutex);
803 }
804
805 void
806 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
807 {
808   g_return_if_fail (lock);
809
810   if (!g_threads_got_initialized)
811     return;
812
813   g_static_mutex_lock (&lock->mutex);
814   lock->want_to_write++;
815   while (lock->have_writer || lock->read_counter)
816     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
817   lock->want_to_write--;
818   lock->have_writer = TRUE;
819   g_static_mutex_unlock (&lock->mutex);
820 }
821
822 gboolean
823 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
824 {
825   gboolean ret_val = FALSE;
826
827   g_return_val_if_fail (lock, FALSE);
828
829   if (!g_threads_got_initialized)
830     return TRUE;
831
832   g_static_mutex_lock (&lock->mutex);
833   if (!lock->have_writer && !lock->read_counter)
834     {
835       lock->have_writer = TRUE;
836       ret_val = TRUE;
837     }
838   g_static_mutex_unlock (&lock->mutex);
839   return ret_val;
840 }
841
842 void
843 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
844 {
845   g_return_if_fail (lock);
846
847   if (!g_threads_got_initialized)
848     return;
849
850   g_static_mutex_lock (&lock->mutex);
851   lock->have_writer = FALSE;
852   g_static_rw_lock_signal (lock);
853   g_static_mutex_unlock (&lock->mutex);
854 }
855
856 void
857 g_static_rw_lock_free (GStaticRWLock* lock)
858 {
859   g_return_if_fail (lock);
860
861   if (lock->read_cond)
862     {
863       g_cond_free (lock->read_cond);
864       lock->read_cond = NULL;
865     }
866   if (lock->write_cond)
867     {
868       g_cond_free (lock->write_cond);
869       lock->write_cond = NULL;
870     }
871   g_static_mutex_free (&lock->mutex);
872 }
873
874 /**
875  * g_thread_foreach
876  * @thread_func: function to call for all GThread structures
877  * @user_data:   second argument to @thread_func
878  *
879  * Call @thread_func on all existing #GThread structures. Note that
880  * threads may decide to exit while @thread_func is running, so
881  * without intimate knowledge about the lifetime of foreign threads,
882  * @thread_func shouldn't access the GThread* pointer passed in as
883  * first argument. However, @thread_func will not be called for threads
884  * which are known to have exited already.
885  *
886  * Due to thread lifetime checks, this function has an execution complexity
887  * which is quadratic in the number of existing threads.
888  *
889  * Since: 2.10
890  */
891 void
892 g_thread_foreach (GFunc    thread_func,
893                   gpointer user_data)
894 {
895   GSList *slist = NULL;
896   GRealThread *thread;
897   g_return_if_fail (thread_func != NULL);
898   /* snapshot the list of threads for iteration */
899   G_LOCK (g_thread);
900   for (thread = g_thread_all_threads; thread; thread = thread->next)
901     slist = g_slist_prepend (slist, thread);
902   G_UNLOCK (g_thread);
903   /* walk the list, skipping non-existant threads */
904   while (slist)
905     {
906       GSList *node = slist;
907       slist = node->next;
908       /* check whether the current thread still exists */
909       G_LOCK (g_thread);
910       for (thread = g_thread_all_threads; thread; thread = thread->next)
911         if (thread == node->data)
912           break;
913       G_UNLOCK (g_thread);
914       if (thread)
915         thread_func (thread, user_data);
916       g_slist_free_1 (node);
917     }
918 }
919
920 #define __G_THREAD_C__
921 #include "galiasdef.c"