more atomic ops pointer cast fixes. this time it'll work with atomic op
[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   result->next = g_thread_all_threads;
666   g_thread_all_threads = result;
667   G_UNLOCK (g_thread);
668
669   if (local_error)
670     {
671       g_propagate_error (error, local_error);
672       g_free (result);
673       return NULL;
674     }
675
676   return (GThread*) result;
677 }
678
679 void
680 g_thread_exit (gpointer retval)
681 {
682   GRealThread* real = (GRealThread*) g_thread_self ();
683   real->retval = retval;
684   G_THREAD_CF (thread_exit, (void)0, ());
685 }
686
687 gpointer
688 g_thread_join (GThread* thread)
689 {
690   GRealThread* real = (GRealThread*) thread;
691   GRealThread *p, *t;
692   gpointer retval;
693
694   g_return_val_if_fail (thread, NULL);
695   g_return_val_if_fail (thread->joinable, NULL);
696   g_return_val_if_fail (!g_system_thread_equal (real->system_thread,
697                                                 zero_thread), NULL);
698
699   G_THREAD_UF (thread_join, (&real->system_thread));
700
701   retval = real->retval;
702
703   G_LOCK (g_thread);
704   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
705     {
706       if (t == (GRealThread*) thread)
707         {
708           if (p)
709             p->next = t->next;
710           else
711             g_thread_all_threads = t->next;
712           break;
713         }
714     }
715   G_UNLOCK (g_thread);
716
717   /* Just to make sure, this isn't used any more */
718   thread->joinable = 0;
719   g_system_thread_assign (real->system_thread, zero_thread);
720
721   /* the thread structure for non-joinable threads is freed upon
722      thread end. We free the memory here. This will leave a loose end,
723      if a joinable thread is not joined. */
724
725   g_free (thread);
726
727   return retval;
728 }
729
730 void
731 g_thread_set_priority (GThread* thread,
732                        GThreadPriority priority)
733 {
734   GRealThread* real = (GRealThread*) thread;
735
736   g_return_if_fail (thread);
737   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
738   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
739   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
740
741   thread->priority = priority;
742
743   G_THREAD_CF (thread_set_priority, (void)0,
744                (&real->system_thread, priority));
745 }
746
747 GThread*
748 g_thread_self (void)
749 {
750   GRealThread* thread = g_private_get (g_thread_specific_private);
751
752   if (!thread)
753     {
754       /* If no thread data is available, provide and set one.  This
755          can happen for the main thread and for threads, that are not
756          created by GLib. */
757       thread = g_new0 (GRealThread, 1);
758       thread->thread.joinable = FALSE; /* This is a save guess */
759       thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
760                                                              just a guess */
761       thread->thread.func = NULL;
762       thread->thread.data = NULL;
763       thread->private_data = NULL;
764
765       if (g_thread_supported ())
766         G_THREAD_UF (thread_self, (&thread->system_thread));
767
768       g_private_set (g_thread_specific_private, thread);
769
770       G_LOCK (g_thread);
771       thread->next = g_thread_all_threads;
772       g_thread_all_threads = thread;
773       G_UNLOCK (g_thread);
774     }
775
776   return (GThread*)thread;
777 }
778
779 void
780 g_static_rw_lock_init (GStaticRWLock* lock)
781 {
782   static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
783
784   g_return_if_fail (lock);
785
786   *lock = init_lock;
787 }
788
789 inline static void
790 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
791 {
792   if (!*cond)
793       *cond = g_cond_new ();
794   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
795 }
796
797 inline static void
798 g_static_rw_lock_signal (GStaticRWLock* lock)
799 {
800   if (lock->want_to_write && lock->write_cond)
801     g_cond_signal (lock->write_cond);
802   else if (lock->want_to_read && lock->read_cond)
803     g_cond_broadcast (lock->read_cond);
804 }
805
806 void
807 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
808 {
809   g_return_if_fail (lock);
810
811   if (!g_threads_got_initialized)
812     return;
813
814   g_static_mutex_lock (&lock->mutex);
815   lock->want_to_read++;
816   while (lock->have_writer || lock->want_to_write)
817     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
818   lock->want_to_read--;
819   lock->read_counter++;
820   g_static_mutex_unlock (&lock->mutex);
821 }
822
823 gboolean
824 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
825 {
826   gboolean ret_val = FALSE;
827
828   g_return_val_if_fail (lock, FALSE);
829
830   if (!g_threads_got_initialized)
831     return TRUE;
832
833   g_static_mutex_lock (&lock->mutex);
834   if (!lock->have_writer && !lock->want_to_write)
835     {
836       lock->read_counter++;
837       ret_val = TRUE;
838     }
839   g_static_mutex_unlock (&lock->mutex);
840   return ret_val;
841 }
842
843 void
844 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
845 {
846   g_return_if_fail (lock);
847
848   if (!g_threads_got_initialized)
849     return;
850
851   g_static_mutex_lock (&lock->mutex);
852   lock->read_counter--;
853   if (lock->read_counter == 0)
854     g_static_rw_lock_signal (lock);
855   g_static_mutex_unlock (&lock->mutex);
856 }
857
858 void
859 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
860 {
861   g_return_if_fail (lock);
862
863   if (!g_threads_got_initialized)
864     return;
865
866   g_static_mutex_lock (&lock->mutex);
867   lock->want_to_write++;
868   while (lock->have_writer || lock->read_counter)
869     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
870   lock->want_to_write--;
871   lock->have_writer = TRUE;
872   g_static_mutex_unlock (&lock->mutex);
873 }
874
875 gboolean
876 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
877 {
878   gboolean ret_val = FALSE;
879
880   g_return_val_if_fail (lock, FALSE);
881
882   if (!g_threads_got_initialized)
883     return TRUE;
884
885   g_static_mutex_lock (&lock->mutex);
886   if (!lock->have_writer && !lock->read_counter)
887     {
888       lock->have_writer = TRUE;
889       ret_val = TRUE;
890     }
891   g_static_mutex_unlock (&lock->mutex);
892   return ret_val;
893 }
894
895 void
896 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
897 {
898   g_return_if_fail (lock);
899
900   if (!g_threads_got_initialized)
901     return;
902
903   g_static_mutex_lock (&lock->mutex);
904   lock->have_writer = FALSE;
905   g_static_rw_lock_signal (lock);
906   g_static_mutex_unlock (&lock->mutex);
907 }
908
909 void
910 g_static_rw_lock_free (GStaticRWLock* lock)
911 {
912   g_return_if_fail (lock);
913
914   if (lock->read_cond)
915     {
916       g_cond_free (lock->read_cond);
917       lock->read_cond = NULL;
918     }
919   if (lock->write_cond)
920     {
921       g_cond_free (lock->write_cond);
922       lock->write_cond = NULL;
923     }
924   g_static_mutex_free (&lock->mutex);
925 }
926
927 /**
928  * g_thread_foreach
929  * @thread_func: function to call for all GThread structures
930  * @user_data:   second argument to @thread_func
931  *
932  * Call @thread_func on all existing #GThread structures. Note that
933  * threads may decide to exit while @thread_func is running, so
934  * without intimate knowledge about the lifetime of foreign threads,
935  * @thread_func shouldn't access the GThread* pointer passed in as
936  * first argument. However, @thread_func will not be called for threads
937  * which are known to have exited already.
938  *
939  * Due to thread lifetime checks, this function has an execution complexity
940  * which is quadratic in the number of existing threads.
941  *
942  * Since: 2.10
943  */
944 void
945 g_thread_foreach (GFunc    thread_func,
946                   gpointer user_data)
947 {
948   GSList *slist = NULL;
949   GRealThread *thread;
950   g_return_if_fail (thread_func != NULL);
951   /* snapshot the list of threads for iteration */
952   G_LOCK (g_thread);
953   for (thread = g_thread_all_threads; thread; thread = thread->next)
954     slist = g_slist_prepend (slist, thread);
955   G_UNLOCK (g_thread);
956   /* walk the list, skipping non-existant threads */
957   while (slist)
958     {
959       GSList *node = slist;
960       slist = node->next;
961       /* check whether the current thread still exists */
962       G_LOCK (g_thread);
963       for (thread = g_thread_all_threads; thread; thread = thread->next)
964         if (thread == node->data)
965           break;
966       G_UNLOCK (g_thread);
967       if (thread)
968         thread_func (thread, user_data);
969       g_slist_free_1 (node);
970     }
971 }
972
973 #define __G_THREAD_C__
974 #include "galiasdef.c"