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