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