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