Fix errors in the recently moved time calculations. (#395203, Chris
[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 #ifndef G_OS_WIN32
42 #include <sys/time.h>
43 #include <time.h>
44 #endif /* G_OS_WIN32 */
45
46 #include <string.h>
47
48 #include "glib.h"
49 #include "gthreadprivate.h"
50 #include "galias.h"
51
52 GQuark
53 g_thread_error_quark (void)
54 {
55   return g_quark_from_static_string ("g_thread_error");
56 }
57
58 /* Keep this in sync with GRealThread in gmain.c! */
59 typedef struct _GRealThread GRealThread;
60 struct  _GRealThread
61 {
62   GThread thread;
63   gpointer private_data;
64   GRealThread *next;
65   gpointer retval;
66   GSystemThread system_thread;
67 };
68
69 typedef struct _GStaticPrivateNode GStaticPrivateNode;
70 struct _GStaticPrivateNode
71 {
72   gpointer       data;
73   GDestroyNotify destroy;
74 };
75
76 static void    g_thread_cleanup (gpointer data);
77 static void    g_thread_fail (void);
78 static guint64 gettime (void);
79
80 /* Global variables */
81
82 static GSystemThread zero_thread; /* This is initialized to all zero */
83 gboolean g_thread_use_default_impl = TRUE;
84 gboolean g_threads_got_initialized = FALSE;
85
86 GThreadFunctions g_thread_functions_for_glib_use = {
87   (GMutex*(*)())g_thread_fail,                 /* mutex_new */
88   NULL,                                        /* mutex_lock */
89   NULL,                                        /* mutex_trylock */
90   NULL,                                        /* mutex_unlock */
91   NULL,                                        /* mutex_free */
92   (GCond*(*)())g_thread_fail,                  /* cond_new */
93   NULL,                                        /* cond_signal */
94   NULL,                                        /* cond_broadcast */
95   NULL,                                        /* cond_wait */
96   NULL,                                        /* cond_timed_wait  */
97   NULL,                                        /* cond_free */
98   (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
99   NULL,                                        /* private_get */
100   NULL,                                        /* private_set */
101   (void(*)(GThreadFunc, gpointer, gulong,
102            gboolean, gboolean, GThreadPriority,
103            gpointer, GError**))g_thread_fail,  /* thread_create */
104   NULL,                                        /* thread_yield */
105   NULL,                                        /* thread_join */
106   NULL,                                        /* thread_exit */
107   NULL,                                        /* thread_set_priority */
108   NULL,                                         /* thread_self */
109   NULL,                                         /* thread_equal */
110   gettime                                      /* gettime */
111 };
112
113 /* Local data */
114
115 static GMutex   *g_once_mutex = NULL;
116 static GCond    *g_once_cond = NULL;
117 static GPrivate *g_thread_specific_private = NULL;
118 static GRealThread *g_thread_all_threads = NULL;
119 static GSList   *g_thread_free_indeces = NULL;
120
121 G_LOCK_DEFINE_STATIC (g_thread);
122
123 #ifdef G_THREADS_ENABLED
124 /* This must be called only once, before any threads are created.
125  * It will only be called from g_thread_init() in -lgthread.
126  */
127 void
128 g_thread_init_glib (void)
129 {
130   /* We let the main thread (the one that calls g_thread_init) inherit
131    * the static_private data set before calling g_thread_init
132    */
133   GRealThread* main_thread = (GRealThread*) g_thread_self ();
134
135   /* mutex and cond creation works without g_threads_got_initialized */
136   g_once_mutex = g_mutex_new ();
137   g_once_cond = g_cond_new ();
138
139   /* we may only create mutex and cond in here */
140   _g_mem_thread_init_noprivate_nomessage ();
141
142   /* setup the basic threading system */
143   g_threads_got_initialized = TRUE;
144   g_thread_specific_private = g_private_new (g_thread_cleanup);
145   g_private_set (g_thread_specific_private, main_thread);
146   G_THREAD_UF (thread_self, (&main_thread->system_thread));
147
148   /* complete memory system initialization, g_private_*() works now */
149   _g_slice_thread_init_nomessage ();
150
151   /* accomplish log system initialization to enable messaging */
152   _g_messages_thread_init_nomessage ();
153
154   /* we may run full-fledged initializers from here */
155   _g_atomic_thread_init ();
156   _g_convert_thread_init ();
157   _g_rand_thread_init ();
158   _g_main_thread_init ();
159   _g_utils_thread_init ();
160 #ifdef G_OS_WIN32
161   _g_win32_thread_init ();
162 #endif
163 }
164 #endif /* G_THREADS_ENABLED */
165
166 gpointer
167 g_once_impl (GOnce       *once,
168              GThreadFunc  func,
169              gpointer     arg)
170 {
171   g_mutex_lock (g_once_mutex);
172
173   while (once->status == G_ONCE_STATUS_PROGRESS)
174     g_cond_wait (g_once_cond, g_once_mutex);
175
176   if (once->status != G_ONCE_STATUS_READY)
177     {
178       once->status = G_ONCE_STATUS_PROGRESS;
179       g_mutex_unlock (g_once_mutex);
180
181       once->retval = func (arg);
182
183       g_mutex_lock (g_once_mutex);
184       once->status = G_ONCE_STATUS_READY;
185       g_cond_broadcast (g_once_cond);
186     }
187
188   g_mutex_unlock (g_once_mutex);
189
190   return once->retval;
191 }
192
193 void
194 g_static_mutex_init (GStaticMutex *mutex)
195 {
196   static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
197
198   g_return_if_fail (mutex);
199
200   *mutex = init_mutex;
201 }
202
203 GMutex *
204 g_static_mutex_get_mutex_impl (GMutex** mutex)
205 {
206   if (!g_thread_supported ())
207     return NULL;
208
209   g_assert (g_once_mutex);
210
211   g_mutex_lock (g_once_mutex);
212
213   if (!(*mutex))
214     g_atomic_pointer_set (mutex, g_mutex_new());
215
216   g_mutex_unlock (g_once_mutex);
217
218   return *mutex;
219 }
220
221 void
222 g_static_mutex_free (GStaticMutex* mutex)
223 {
224   GMutex **runtime_mutex;
225
226   g_return_if_fail (mutex);
227
228   /* The runtime_mutex is the first (or only) member of GStaticMutex,
229    * see both versions (of glibconfig.h) in configure.in */
230   runtime_mutex = ((GMutex**)mutex);
231
232   if (*runtime_mutex)
233     g_mutex_free (*runtime_mutex);
234
235   *runtime_mutex = NULL;
236 }
237
238 void
239 g_static_rec_mutex_init (GStaticRecMutex *mutex)
240 {
241   static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
242
243   g_return_if_fail (mutex);
244
245   *mutex = init_mutex;
246 }
247
248 void
249 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
250 {
251   GSystemThread self;
252
253   g_return_if_fail (mutex);
254
255   if (!g_thread_supported ())
256     return;
257
258   G_THREAD_UF (thread_self, (&self));
259
260   if (g_system_thread_equal (self, mutex->owner))
261     {
262       mutex->depth++;
263       return;
264     }
265   g_static_mutex_lock (&mutex->mutex);
266   g_system_thread_assign (mutex->owner, self);
267   mutex->depth = 1;
268 }
269
270 gboolean
271 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
272 {
273   GSystemThread self;
274
275   g_return_val_if_fail (mutex, FALSE);
276
277   if (!g_thread_supported ())
278     return TRUE;
279
280   G_THREAD_UF (thread_self, (&self));
281
282   if (g_system_thread_equal (self, mutex->owner))
283     {
284       mutex->depth++;
285       return TRUE;
286     }
287
288   if (!g_static_mutex_trylock (&mutex->mutex))
289     return FALSE;
290
291   g_system_thread_assign (mutex->owner, self);
292   mutex->depth = 1;
293   return TRUE;
294 }
295
296 void
297 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
298 {
299   g_return_if_fail (mutex);
300
301   if (!g_thread_supported ())
302     return;
303
304   if (mutex->depth > 1)
305     {
306       mutex->depth--;
307       return;
308     }
309   g_system_thread_assign (mutex->owner, zero_thread);
310   g_static_mutex_unlock (&mutex->mutex);
311 }
312
313 void
314 g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
315                                 guint            depth)
316 {
317   GSystemThread self;
318   g_return_if_fail (mutex);
319
320   if (!g_thread_supported ())
321     return;
322
323   if (depth == 0)
324     return;
325
326   G_THREAD_UF (thread_self, (&self));
327
328   if (g_system_thread_equal (self, mutex->owner))
329     {
330       mutex->depth += depth;
331       return;
332     }
333   g_static_mutex_lock (&mutex->mutex);
334   g_system_thread_assign (mutex->owner, self);
335   mutex->depth = depth;
336 }
337
338 guint
339 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
340 {
341   guint depth;
342
343   g_return_val_if_fail (mutex, 0);
344
345   if (!g_thread_supported ())
346     return 1;
347
348   depth = mutex->depth;
349
350   g_system_thread_assign (mutex->owner, zero_thread);
351   mutex->depth = 0;
352   g_static_mutex_unlock (&mutex->mutex);
353
354   return depth;
355 }
356
357 void
358 g_static_rec_mutex_free (GStaticRecMutex *mutex)
359 {
360   g_return_if_fail (mutex);
361
362   g_static_mutex_free (&mutex->mutex);
363 }
364
365 void
366 g_static_private_init (GStaticPrivate *private_key)
367 {
368   private_key->index = 0;
369 }
370
371 gpointer
372 g_static_private_get (GStaticPrivate *private_key)
373 {
374   GRealThread *self = (GRealThread*) g_thread_self ();
375   GArray *array;
376
377   array = self->private_data;
378   if (!array)
379     return NULL;
380
381   if (!private_key->index)
382     return NULL;
383   else if (private_key->index <= array->len)
384     return g_array_index (array, GStaticPrivateNode,
385                           private_key->index - 1).data;
386   else
387     return NULL;
388 }
389
390 void
391 g_static_private_set (GStaticPrivate *private_key,
392                       gpointer        data,
393                       GDestroyNotify  notify)
394 {
395   GRealThread *self = (GRealThread*) g_thread_self ();
396   GArray *array;
397   static guint next_index = 0;
398   GStaticPrivateNode *node;
399
400   array = self->private_data;
401   if (!array)
402     {
403       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
404       self->private_data = array;
405     }
406
407   if (!private_key->index)
408     {
409       G_LOCK (g_thread);
410
411       if (!private_key->index)
412         {
413           if (g_thread_free_indeces)
414             {
415               private_key->index =
416                 GPOINTER_TO_UINT (g_thread_free_indeces->data);
417               g_thread_free_indeces =
418                 g_slist_delete_link (g_thread_free_indeces,
419                                      g_thread_free_indeces);
420             }
421           else
422             private_key->index = ++next_index;
423         }
424
425       G_UNLOCK (g_thread);
426     }
427
428   if (private_key->index > array->len)
429     g_array_set_size (array, private_key->index);
430
431   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
432   if (node->destroy)
433     {
434       gpointer ddata = node->data;
435       GDestroyNotify ddestroy = node->destroy;
436
437       node->data = data;
438       node->destroy = notify;
439
440       ddestroy (ddata);
441     }
442   else
443     {
444       node->data = data;
445       node->destroy = notify;
446     }
447 }
448
449 void
450 g_static_private_free (GStaticPrivate *private_key)
451 {
452   guint index = private_key->index;
453   GRealThread *thread;
454
455   if (!index)
456     return;
457
458   private_key->index = 0;
459
460   G_LOCK (g_thread);
461
462   thread = g_thread_all_threads;
463   while (thread)
464     {
465       GArray *array = thread->private_data;
466       thread = thread->next;
467
468       if (array && index <= array->len)
469         {
470           GStaticPrivateNode *node = &g_array_index (array,
471                                                      GStaticPrivateNode,
472                                                      index - 1);
473           gpointer ddata = node->data;
474           GDestroyNotify ddestroy = node->destroy;
475
476           node->data = NULL;
477           node->destroy = NULL;
478
479           if (ddestroy)
480             {
481               G_UNLOCK (g_thread);
482               ddestroy (ddata);
483               G_LOCK (g_thread);
484               }
485         }
486     }
487   g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces,
488                                            GUINT_TO_POINTER (index));
489   G_UNLOCK (g_thread);
490 }
491
492 static void
493 g_thread_cleanup (gpointer data)
494 {
495   if (data)
496     {
497       GRealThread* thread = data;
498       if (thread->private_data)
499         {
500           GArray* array = thread->private_data;
501           guint i;
502
503           for (i = 0; i < array->len; i++ )
504             {
505               GStaticPrivateNode *node =
506                 &g_array_index (array, GStaticPrivateNode, i);
507               if (node->destroy)
508                 node->destroy (node->data);
509             }
510           g_array_free (array, TRUE);
511         }
512
513       /* We only free the thread structure, if it isn't joinable. If
514          it is, the structure is freed in g_thread_join */
515       if (!thread->thread.joinable)
516         {
517           GRealThread *t, *p;
518
519           G_LOCK (g_thread);
520           for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
521             {
522               if (t == thread)
523                 {
524                   if (p)
525                     p->next = t->next;
526                   else
527                     g_thread_all_threads = t->next;
528                   break;
529                 }
530             }
531           G_UNLOCK (g_thread);
532
533           /* Just to make sure, this isn't used any more */
534           g_system_thread_assign (thread->system_thread, zero_thread);
535           g_free (thread);
536         }
537     }
538 }
539
540 static void
541 g_thread_fail (void)
542 {
543   g_error ("The thread system is not yet initialized.");
544 }
545
546 static guint64
547 gettime (void)
548 {
549 #ifdef G_OS_WIN32
550   guint64 v;
551
552   GetSystemTimeAsFileTime ((FILETIME *)&v);
553
554   return v;
555 #else
556   struct timeval tv;
557
558   gettimeofday (&tv, NULL);
559
560   return (guint64) tv.tv_sec * 1000000000 + tv.tv_usec * 1000; 
561 #endif
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"