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