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