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