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