Move short_month_names and long_month_names to bss.
[platform/upstream/glib.git] / glib / gthread.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gthread.c: MT safety related functions
5  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
6  *                Owen Taylor
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /*
25  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
26  * file for a list of people on the GLib Team.  See the ChangeLog
27  * files for a list of changes.  These files are distributed with
28  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
29  */
30
31 /* 
32  * MT safe
33  */
34
35 #include "config.h"
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include <string.h>
42
43 #include "glib.h"
44 #include "gthreadinit.h"
45 #include "galias.h"
46
47 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
48 # define g_system_thread_equal_simple(thread1, thread2)                 \
49    ((thread1).dummy_pointer == (thread2).dummy_pointer)
50 # define g_system_thread_assign(dest, src)                              \
51    ((dest).dummy_pointer = (src).dummy_pointer)
52 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
53 # define g_system_thread_equal_simple(thread1, thread2)                 \
54    (memcmp (&(thread1), &(thread2), GLIB_SIZEOF_SYSTEM_THREAD) == 0)
55 # define g_system_thread_assign(dest, src)                              \
56    (memcpy (&(dest), &(src), GLIB_SIZEOF_SYSTEM_THREAD))
57 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
58
59 #define g_system_thread_equal(thread1, thread2)                         \
60   (g_thread_functions_for_glib_use.thread_equal ?                       \
61    g_thread_functions_for_glib_use.thread_equal (&(thread1), &(thread2)) :\
62    g_system_thread_equal_simple((thread1), (thread2)))
63
64 GQuark 
65 g_thread_error_quark (void)
66 {
67   return g_quark_from_static_string ("g_thread_error");
68 }
69
70 /* Keep this in sync with GRealThread in gmain.c! */
71 typedef struct _GRealThread GRealThread;
72 struct  _GRealThread
73 {
74   GThread thread;
75   gpointer private_data;
76   GRealThread *next;
77   gpointer retval;
78   GSystemThread system_thread;
79 };
80
81 typedef struct _GStaticPrivateNode GStaticPrivateNode;
82 struct _GStaticPrivateNode
83 {
84   gpointer       data;
85   GDestroyNotify destroy;
86 };
87
88 static void g_thread_cleanup (gpointer data);
89 static void g_thread_fail (void);
90
91 /* Global variables */
92
93 static GSystemThread zero_thread; /* This is initialized to all zero */
94 gboolean g_thread_use_default_impl = TRUE;
95 gboolean g_threads_got_initialized = FALSE;
96
97 GThreadFunctions g_thread_functions_for_glib_use = {
98   (GMutex*(*)())g_thread_fail,                 /* mutex_new */
99   NULL,                                        /* mutex_lock */
100   NULL,                                        /* mutex_trylock */
101   NULL,                                        /* mutex_unlock */
102   NULL,                                        /* mutex_free */
103   (GCond*(*)())g_thread_fail,                  /* cond_new */
104   NULL,                                        /* cond_signal */
105   NULL,                                        /* cond_broadcast */
106   NULL,                                        /* cond_wait */
107   NULL,                                        /* cond_timed_wait  */
108   NULL,                                        /* cond_free */
109   (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
110   NULL,                                        /* private_get */
111   NULL,                                        /* private_set */
112   (void(*)(GThreadFunc, gpointer, gulong, 
113            gboolean, gboolean, GThreadPriority, 
114            gpointer, GError**))g_thread_fail,  /* thread_create */
115   NULL,                                        /* thread_yield */
116   NULL,                                        /* thread_join */
117   NULL,                                        /* thread_exit */
118   NULL,                                        /* thread_set_priority */
119   NULL                                         /* thread_self */
120 }; 
121
122 /* Local data */
123
124 static GMutex   *g_once_mutex = NULL;
125 static GCond    *g_once_cond = NULL;
126 static GPrivate *g_thread_specific_private = NULL;
127 static GRealThread *g_thread_all_threads = NULL;
128 static GSList   *g_thread_free_indeces = 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_convert_thread_init ();
165   _g_rand_thread_init ();
166   _g_main_thread_init ();
167   _g_atomic_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 void 
203 g_static_mutex_init (GStaticMutex *mutex)
204 {
205   static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
206
207   g_return_if_fail (mutex);
208
209   *mutex = init_mutex;
210 }
211
212 GMutex *
213 g_static_mutex_get_mutex_impl (GMutex** mutex)
214 {
215   if (!g_thread_supported ())
216     return NULL;
217
218   g_assert (g_once_mutex);
219
220   g_mutex_lock (g_once_mutex);
221
222   if (!(*mutex)) 
223     {
224       GMutex *new_mutex = g_mutex_new (); 
225       
226       /* The following is a memory barrier to avoid the write 
227        * to *new_mutex being reordered to after writing *mutex */
228       g_mutex_lock (new_mutex);
229       g_mutex_unlock (new_mutex);
230       
231       *mutex = new_mutex;
232     }
233
234   g_mutex_unlock (g_once_mutex);
235   
236   return *mutex;
237 }
238
239 void
240 g_static_mutex_free (GStaticMutex* mutex)
241 {
242   GMutex **runtime_mutex;
243   
244   g_return_if_fail (mutex);
245
246   /* The runtime_mutex is the first (or only) member of GStaticMutex,
247    * see both versions (of glibconfig.h) in configure.in */
248   runtime_mutex = ((GMutex**)mutex);
249   
250   if (*runtime_mutex)
251     g_mutex_free (*runtime_mutex);
252
253   *runtime_mutex = NULL;
254 }
255
256 void     
257 g_static_rec_mutex_init (GStaticRecMutex *mutex)
258 {
259   static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
260   
261   g_return_if_fail (mutex);
262
263   *mutex = init_mutex;
264 }
265
266 void
267 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
268 {
269   GSystemThread self;
270
271   g_return_if_fail (mutex);
272
273   if (!g_thread_supported ())
274     return;
275
276   G_THREAD_UF (thread_self, (&self));
277
278   if (g_system_thread_equal (self, mutex->owner))
279     {
280       mutex->depth++;
281       return;
282     }
283   g_static_mutex_lock (&mutex->mutex);
284   g_system_thread_assign (mutex->owner, self);
285   mutex->depth = 1;
286 }
287
288 gboolean
289 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
290 {
291   GSystemThread self;
292
293   g_return_val_if_fail (mutex, FALSE);
294
295   if (!g_thread_supported ())
296     return TRUE;
297
298   G_THREAD_UF (thread_self, (&self));
299
300   if (g_system_thread_equal (self, mutex->owner))
301     {
302       mutex->depth++;
303       return TRUE;
304     }
305
306   if (!g_static_mutex_trylock (&mutex->mutex))
307     return FALSE;
308
309   g_system_thread_assign (mutex->owner, self);
310   mutex->depth = 1;
311   return TRUE;
312 }
313
314 void
315 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
316 {
317   g_return_if_fail (mutex);
318
319   if (!g_thread_supported ())
320     return;
321
322   if (mutex->depth > 1)
323     {
324       mutex->depth--;
325       return;
326     }
327   g_system_thread_assign (mutex->owner, zero_thread);
328   g_static_mutex_unlock (&mutex->mutex);  
329 }
330
331 void
332 g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
333                                 guint            depth)
334 {
335   GSystemThread self;
336   g_return_if_fail (mutex);
337
338   if (!g_thread_supported ())
339     return;
340
341   if (depth == 0)
342     return;
343
344   G_THREAD_UF (thread_self, (&self));
345
346   if (g_system_thread_equal (self, mutex->owner))
347     {
348       mutex->depth += depth;
349       return;
350     }
351   g_static_mutex_lock (&mutex->mutex);
352   g_system_thread_assign (mutex->owner, self);
353   mutex->depth = depth;
354 }
355
356 guint    
357 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
358 {
359   guint depth;
360
361   g_return_val_if_fail (mutex, 0);
362
363   if (!g_thread_supported ())
364     return 1;
365
366   depth = mutex->depth;
367
368   g_system_thread_assign (mutex->owner, zero_thread);
369   mutex->depth = 0;
370   g_static_mutex_unlock (&mutex->mutex);
371
372   return depth;
373 }
374
375 void
376 g_static_rec_mutex_free (GStaticRecMutex *mutex)
377 {
378   g_return_if_fail (mutex);
379
380   g_static_mutex_free (&mutex->mutex);
381 }
382
383 void     
384 g_static_private_init (GStaticPrivate *private_key)
385 {
386   private_key->index = 0;
387 }
388
389 gpointer
390 g_static_private_get (GStaticPrivate *private_key)
391 {
392   GRealThread *self = (GRealThread*) g_thread_self ();
393   GArray *array;
394
395   array = self->private_data;
396   if (!array)
397     return NULL;
398
399   if (!private_key->index)
400     return NULL;
401   else if (private_key->index <= array->len)
402     return g_array_index (array, GStaticPrivateNode, 
403                           private_key->index - 1).data;
404   else
405     return NULL;
406 }
407
408 void
409 g_static_private_set (GStaticPrivate *private_key, 
410                       gpointer        data,
411                       GDestroyNotify  notify)
412 {
413   GRealThread *self = (GRealThread*) g_thread_self ();
414   GArray *array;
415   static guint next_index = 0;
416   GStaticPrivateNode *node;
417
418   array = self->private_data;
419   if (!array)
420     {
421       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
422       self->private_data = array;
423     }
424
425   if (!private_key->index)
426     {
427       G_LOCK (g_thread);
428
429       if (!private_key->index)
430         {
431           if (g_thread_free_indeces)
432             {
433               private_key->index = 
434                 GPOINTER_TO_UINT (g_thread_free_indeces->data);
435               g_thread_free_indeces = 
436                 g_slist_delete_link (g_thread_free_indeces,
437                                      g_thread_free_indeces);
438             }
439           else
440             private_key->index = ++next_index;
441         }
442
443       G_UNLOCK (g_thread);
444     }
445
446   if (private_key->index > array->len)
447     g_array_set_size (array, private_key->index);
448
449   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
450   if (node->destroy)
451     {
452       gpointer ddata = node->data;
453       GDestroyNotify ddestroy = node->destroy;
454
455       node->data = data;
456       node->destroy = notify;
457
458       ddestroy (ddata);
459     }
460   else
461     {
462       node->data = data;
463       node->destroy = notify;
464     }
465 }
466
467 void     
468 g_static_private_free (GStaticPrivate *private_key)
469 {
470   guint index = private_key->index;
471   GRealThread *thread;
472
473   if (!index)
474     return;
475   
476   private_key->index = 0;
477
478   G_LOCK (g_thread);
479   
480   thread = g_thread_all_threads;
481   while (thread)
482     {
483       GArray *array = thread->private_data;
484       thread = thread->next;
485
486       if (array && index <= array->len)
487         {
488           GStaticPrivateNode *node = &g_array_index (array, 
489                                                      GStaticPrivateNode, 
490                                                      index - 1);
491           gpointer ddata = node->data;
492           GDestroyNotify ddestroy = node->destroy;
493
494           node->data = NULL;
495           node->destroy = NULL;
496
497           if (ddestroy) 
498             {
499               G_UNLOCK (g_thread);
500               ddestroy (ddata);
501               G_LOCK (g_thread);
502               }
503         }
504     }
505   g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces, 
506                                            GUINT_TO_POINTER (index));
507   G_UNLOCK (g_thread);
508 }
509
510 static void
511 g_thread_cleanup (gpointer data)
512 {
513   if (data)
514     {
515       GRealThread* thread = data;
516       if (thread->private_data)
517         {
518           GArray* array = thread->private_data;
519           guint i;
520           
521           for (i = 0; i < array->len; i++ )
522             {
523               GStaticPrivateNode *node = 
524                 &g_array_index (array, GStaticPrivateNode, i);
525               if (node->destroy)
526                 node->destroy (node->data);
527             }
528           g_array_free (array, TRUE);
529         }
530
531       /* We only free the thread structure, if it isn't joinable. If
532          it is, the structure is freed in g_thread_join */
533       if (!thread->thread.joinable)
534         {
535           GRealThread *t, *p;
536
537           G_LOCK (g_thread);
538           for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
539             {
540               if (t == thread)
541                 {
542                   if (p)
543                     p->next = t->next;
544                   else
545                     g_thread_all_threads = t->next;
546                   break;
547                 }
548             }
549           G_UNLOCK (g_thread);
550           
551           /* Just to make sure, this isn't used any more */
552           g_system_thread_assign (thread->system_thread, zero_thread);
553           g_free (thread);
554         }
555     }
556 }
557
558 static void
559 g_thread_fail (void)
560 {
561   g_error ("The thread system is not yet initialized.");
562 }
563
564 static gpointer
565 g_thread_create_proxy (gpointer data)
566 {
567   GRealThread* thread = data;
568
569   g_assert (data);
570
571   /* This has to happen before G_LOCK, as that might call g_thread_self */
572   g_private_set (g_thread_specific_private, data);
573
574   /* the lock makes sure, that thread->system_thread is written,
575      before thread->thread.func is called. See g_thread_create. */
576   G_LOCK (g_thread);
577   G_UNLOCK (g_thread);
578  
579   thread->retval = thread->thread.func (thread->thread.data);
580
581   return NULL;
582 }
583
584 GThread* 
585 g_thread_create_full (GThreadFunc                func,
586                       gpointer           data,
587                       gulong             stack_size,
588                       gboolean           joinable,
589                       gboolean           bound,
590                       GThreadPriority    priority,
591                       GError                **error)
592 {
593   GRealThread* result;
594   GError *local_error = NULL;
595   g_return_val_if_fail (func, NULL);
596   g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
597   g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
598   
599   result = g_new0 (GRealThread, 1);
600
601   result->thread.joinable = joinable;
602   result->thread.priority = priority;
603   result->thread.func = func;
604   result->thread.data = data;
605   result->private_data = NULL; 
606   G_LOCK (g_thread);
607   G_THREAD_UF (thread_create, (g_thread_create_proxy, result, 
608                                stack_size, joinable, bound, priority,
609                                &result->system_thread, &local_error));
610   result->next = g_thread_all_threads;
611   g_thread_all_threads = result;
612   G_UNLOCK (g_thread);
613
614   if (local_error)
615     {
616       g_propagate_error (error, local_error);
617       g_free (result);
618       return NULL;
619     }
620
621   return (GThread*) result;
622 }
623
624 void
625 g_thread_exit (gpointer retval)
626 {
627   GRealThread* real = (GRealThread*) g_thread_self ();
628   real->retval = retval;
629   G_THREAD_CF (thread_exit, (void)0, ());
630 }
631
632 gpointer
633 g_thread_join (GThread* thread)
634 {
635   GRealThread* real = (GRealThread*) thread;
636   GRealThread *p, *t;
637   gpointer retval;
638
639   g_return_val_if_fail (thread, NULL);
640   g_return_val_if_fail (thread->joinable, NULL);
641   g_return_val_if_fail (!g_system_thread_equal (real->system_thread, 
642                                                 zero_thread), NULL);
643
644   G_THREAD_UF (thread_join, (&real->system_thread));
645
646   retval = real->retval;
647
648   G_LOCK (g_thread);
649   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
650     {
651       if (t == (GRealThread*) thread)
652         {
653           if (p)
654             p->next = t->next;
655           else
656             g_thread_all_threads = t->next;
657           break;
658         }
659     }
660   G_UNLOCK (g_thread);
661
662   /* Just to make sure, this isn't used any more */
663   thread->joinable = 0;
664   g_system_thread_assign (real->system_thread, zero_thread);
665
666   /* the thread structure for non-joinable threads is freed upon
667      thread end. We free the memory here. This will leave a loose end,
668      if a joinable thread is not joined. */
669
670   g_free (thread);
671
672   return retval;
673 }
674
675 void
676 g_thread_set_priority (GThread* thread, 
677                        GThreadPriority priority)
678 {
679   GRealThread* real = (GRealThread*) thread;
680
681   g_return_if_fail (thread);
682   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
683   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
684   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
685
686   thread->priority = priority;
687
688   G_THREAD_CF (thread_set_priority, (void)0, 
689                (&real->system_thread, priority));
690 }
691
692 GThread*
693 g_thread_self (void)
694 {
695   GRealThread* thread = g_private_get (g_thread_specific_private);
696
697   if (!thread)
698     {  
699       /* If no thread data is available, provide and set one.  This
700          can happen for the main thread and for threads, that are not
701          created by GLib. */
702       thread = g_new0 (GRealThread, 1);
703       thread->thread.joinable = FALSE; /* This is a save guess */
704       thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
705                                                              just a guess */
706       thread->thread.func = NULL;
707       thread->thread.data = NULL;
708       thread->private_data = NULL;
709
710       if (g_thread_supported ())
711         G_THREAD_UF (thread_self, (&thread->system_thread));
712
713       g_private_set (g_thread_specific_private, thread); 
714       
715       G_LOCK (g_thread);
716       thread->next = g_thread_all_threads;
717       g_thread_all_threads = thread;
718       G_UNLOCK (g_thread);
719     }
720   
721   return (GThread*)thread;
722 }
723
724 void
725 g_static_rw_lock_init (GStaticRWLock* lock)
726 {
727   static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
728
729   g_return_if_fail (lock);
730
731   *lock = init_lock;
732 }
733
734 inline static void 
735 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
736 {
737   if (!*cond)
738       *cond = g_cond_new ();
739   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
740 }
741
742 inline static void 
743 g_static_rw_lock_signal (GStaticRWLock* lock)
744 {
745   if (lock->want_to_write && lock->write_cond)
746     g_cond_signal (lock->write_cond);
747   else if (lock->want_to_read && lock->read_cond)
748     g_cond_broadcast (lock->read_cond);
749 }
750
751 void 
752 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
753 {
754   g_return_if_fail (lock);
755
756   if (!g_threads_got_initialized)
757     return;
758
759   g_static_mutex_lock (&lock->mutex);
760   lock->want_to_read++;
761   while (lock->have_writer || lock->want_to_write) 
762     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
763   lock->want_to_read--;
764   lock->read_counter++;
765   g_static_mutex_unlock (&lock->mutex);
766 }
767
768 gboolean 
769 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
770 {
771   gboolean ret_val = FALSE;
772
773   g_return_val_if_fail (lock, FALSE);
774
775   if (!g_threads_got_initialized)
776     return TRUE;
777
778   g_static_mutex_lock (&lock->mutex);
779   if (!lock->have_writer && !lock->want_to_write)
780     {
781       lock->read_counter++;
782       ret_val = TRUE;
783     }
784   g_static_mutex_unlock (&lock->mutex);
785   return ret_val;
786 }
787
788 void 
789 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
790 {
791   g_return_if_fail (lock);
792
793   if (!g_threads_got_initialized)
794     return;
795
796   g_static_mutex_lock (&lock->mutex);
797   lock->read_counter--;
798   if (lock->read_counter == 0)
799     g_static_rw_lock_signal (lock);
800   g_static_mutex_unlock (&lock->mutex);
801 }
802
803 void 
804 g_static_rw_lock_writer_lock (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->want_to_write++;
813   while (lock->have_writer || lock->read_counter)
814     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
815   lock->want_to_write--;
816   lock->have_writer = TRUE;
817   g_static_mutex_unlock (&lock->mutex);
818 }
819
820 gboolean 
821 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
822 {
823   gboolean ret_val = FALSE;
824
825   g_return_val_if_fail (lock, FALSE);
826   
827   if (!g_threads_got_initialized)
828     return TRUE;
829
830   g_static_mutex_lock (&lock->mutex);
831   if (!lock->have_writer && !lock->read_counter)
832     {
833       lock->have_writer = TRUE;
834       ret_val = TRUE;
835     }
836   g_static_mutex_unlock (&lock->mutex);
837   return ret_val;
838 }
839
840 void 
841 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
842 {
843   g_return_if_fail (lock);
844   
845   if (!g_threads_got_initialized)
846     return;
847
848   g_static_mutex_lock (&lock->mutex);
849   lock->have_writer = FALSE; 
850   g_static_rw_lock_signal (lock);
851   g_static_mutex_unlock (&lock->mutex);
852 }
853
854 void 
855 g_static_rw_lock_free (GStaticRWLock* lock)
856 {
857   g_return_if_fail (lock);
858   
859   if (lock->read_cond)
860     {
861       g_cond_free (lock->read_cond);
862       lock->read_cond = NULL;
863     }
864   if (lock->write_cond)
865     {
866       g_cond_free (lock->write_cond);
867       lock->write_cond = NULL;
868     }
869   g_static_mutex_free (&lock->mutex);
870 }
871
872 /**
873  * g_thread_foreach
874  * @thread_func: function to call for all GThread structures
875  * @user_data:   second argument to @thread_func
876  *
877  * Call @thread_func on all existing #GThread structures. Note that
878  * threads may decide to exit while @thread_func is running, so
879  * without intimate knowledge about the lifetime of foreign threads,
880  * @thread_func shouldn't access the GThread* pointer passed in as
881  * first argument. However, @thread_func will not be called for threads
882  * which are known to have exited already.
883  *
884  * Due to thread lifetime checks, this function has an execution complexity
885  * which is quadratic in the number of existing threads.
886  *
887  * Since: 2.10
888  */
889 void
890 g_thread_foreach (GFunc    thread_func,
891                   gpointer user_data)
892 {
893   GSList *slist = NULL;
894   GRealThread *thread;
895   g_return_if_fail (thread_func != NULL);
896   /* snapshot the list of threads for iteration */
897   G_LOCK (g_thread);
898   for (thread = g_thread_all_threads; thread; thread = thread->next)
899     slist = g_slist_prepend (slist, thread);
900   G_UNLOCK (g_thread);
901   /* walk the list, skipping non-existant threads */
902   while (slist)
903     {
904       GSList *node = slist;
905       slist = node->next;
906       /* check whether the current thread still exists */
907       G_LOCK (g_thread);
908       for (thread = g_thread_all_threads; thread; thread = thread->next)
909         if (thread == node->data)
910           break;
911       G_UNLOCK (g_thread);
912       if (thread)
913         thread_func (thread, user_data);
914       g_slist_free_1 (node);
915     }
916 }
917
918 #define __G_THREAD_C__
919 #include "galiasdef.c"