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