Make this function thread-safe in the GLib style.
[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 "galias.h"
44 #include "glib.h"
45 #include "gthreadinit.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_win32_thread_init ();
156  
157   g_threads_got_initialized = TRUE;
158
159   g_thread_specific_private = g_private_new (g_thread_cleanup);
160   g_private_set (g_thread_specific_private, main_thread);
161   G_THREAD_UF (thread_self, (&main_thread->system_thread));
162
163   _g_mem_thread_private_init ();
164   _g_messages_thread_private_init ();
165
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 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     {
218       GMutex *new_mutex = g_mutex_new (); 
219       
220       /* The following is a memory barrier to avoid the write 
221        * to *new_mutex being reordered to after writing *mutex */
222       g_mutex_lock (new_mutex);
223       g_mutex_unlock (new_mutex);
224       
225       *mutex = new_mutex;
226     }
227
228   g_mutex_unlock (g_once_mutex);
229   
230   return *mutex;
231 }
232
233 void
234 g_static_mutex_free (GStaticMutex* mutex)
235 {
236   GMutex **runtime_mutex;
237   
238   g_return_if_fail (mutex);
239
240   /* The runtime_mutex is the first (or only) member of GStaticMutex,
241    * see both versions (of glibconfig.h) in configure.in */
242   runtime_mutex = ((GMutex**)mutex);
243   
244   if (*runtime_mutex)
245     g_mutex_free (*runtime_mutex);
246
247   *runtime_mutex = NULL;
248 }
249
250 void     
251 g_static_rec_mutex_init (GStaticRecMutex *mutex)
252 {
253   static GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
254   
255   g_return_if_fail (mutex);
256
257   *mutex = init_mutex;
258 }
259
260 void
261 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
262 {
263   GSystemThread self;
264
265   g_return_if_fail (mutex);
266
267   if (!g_thread_supported ())
268     return;
269
270   G_THREAD_UF (thread_self, (&self));
271
272   if (g_system_thread_equal (self, mutex->owner))
273     {
274       mutex->depth++;
275       return;
276     }
277   g_static_mutex_lock (&mutex->mutex);
278   g_system_thread_assign (mutex->owner, self);
279   mutex->depth = 1;
280 }
281
282 gboolean
283 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
284 {
285   GSystemThread self;
286
287   g_return_val_if_fail (mutex, FALSE);
288
289   if (!g_thread_supported ())
290     return TRUE;
291
292   G_THREAD_UF (thread_self, (&self));
293
294   if (g_system_thread_equal (self, mutex->owner))
295     {
296       mutex->depth++;
297       return TRUE;
298     }
299
300   if (!g_static_mutex_trylock (&mutex->mutex))
301     return FALSE;
302
303   g_system_thread_assign (mutex->owner, self);
304   mutex->depth = 1;
305   return TRUE;
306 }
307
308 void
309 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
310 {
311   g_return_if_fail (mutex);
312
313   if (!g_thread_supported ())
314     return;
315
316   if (mutex->depth > 1)
317     {
318       mutex->depth--;
319       return;
320     }
321   g_system_thread_assign (mutex->owner, zero_thread);
322   g_static_mutex_unlock (&mutex->mutex);  
323 }
324
325 void
326 g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
327                                 guint            depth)
328 {
329   GSystemThread self;
330   g_return_if_fail (mutex);
331
332   if (!g_thread_supported ())
333     return;
334
335   G_THREAD_UF (thread_self, (&self));
336
337   if (g_system_thread_equal (self, mutex->owner))
338     {
339       mutex->depth += depth;
340       return;
341     }
342   g_static_mutex_lock (&mutex->mutex);
343   g_system_thread_assign (mutex->owner, self);
344   mutex->depth = depth;
345 }
346
347 guint    
348 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
349 {
350   guint depth;
351
352   g_return_val_if_fail (mutex, 0);
353
354   if (!g_thread_supported ())
355     return 1;
356
357   depth = mutex->depth;
358
359   g_system_thread_assign (mutex->owner, zero_thread);
360   mutex->depth = 0;
361   g_static_mutex_unlock (&mutex->mutex);
362
363   return depth;
364 }
365
366 void
367 g_static_rec_mutex_free (GStaticRecMutex *mutex)
368 {
369   g_return_if_fail (mutex);
370
371   g_static_mutex_free (&mutex->mutex);
372 }
373
374 void     
375 g_static_private_init (GStaticPrivate *private_key)
376 {
377   private_key->index = 0;
378 }
379
380 gpointer
381 g_static_private_get (GStaticPrivate *private_key)
382 {
383   GRealThread *self = (GRealThread*) g_thread_self ();
384   GArray *array;
385
386   array = self->private_data;
387   if (!array)
388     return NULL;
389
390   if (!private_key->index)
391     return NULL;
392   else if (private_key->index <= array->len)
393     return g_array_index (array, GStaticPrivateNode, 
394                           private_key->index - 1).data;
395   else
396     return NULL;
397 }
398
399 void
400 g_static_private_set (GStaticPrivate *private_key, 
401                       gpointer        data,
402                       GDestroyNotify  notify)
403 {
404   GRealThread *self = (GRealThread*) g_thread_self ();
405   GArray *array;
406   static guint next_index = 0;
407   GStaticPrivateNode *node;
408
409   array = self->private_data;
410   if (!array)
411     {
412       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
413       self->private_data = array;
414     }
415
416   if (!private_key->index)
417     {
418       G_LOCK (g_thread);
419
420       if (!private_key->index)
421         {
422           if (g_thread_free_indeces)
423             {
424               private_key->index = 
425                 GPOINTER_TO_UINT (g_thread_free_indeces->data);
426               g_thread_free_indeces = 
427                 g_slist_delete_link (g_thread_free_indeces,
428                                      g_thread_free_indeces);
429             }
430           else
431             private_key->index = ++next_index;
432         }
433
434       G_UNLOCK (g_thread);
435     }
436
437   if (private_key->index > array->len)
438     g_array_set_size (array, private_key->index);
439
440   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
441   if (node->destroy)
442     {
443       gpointer ddata = node->data;
444       GDestroyNotify ddestroy = node->destroy;
445
446       node->data = data;
447       node->destroy = notify;
448
449       ddestroy (ddata);
450     }
451   else
452     {
453       node->data = data;
454       node->destroy = notify;
455     }
456 }
457
458 void     
459 g_static_private_free (GStaticPrivate *private_key)
460 {
461   guint index = private_key->index;
462   GSList *list;
463
464   if (!index)
465     return;
466   
467   private_key->index = 0;
468
469   G_LOCK (g_thread);
470   list =  g_thread_all_threads;
471   while (list)
472     {
473       GRealThread *thread = list->data;
474       GArray *array = thread->private_data;
475       list = list->next;
476
477       if (array && index <= array->len)
478         {
479           GStaticPrivateNode *node = &g_array_index (array, 
480                                                      GStaticPrivateNode, 
481                                                      index - 1);
482           gpointer ddata = node->data;
483           GDestroyNotify ddestroy = node->destroy;
484
485           node->data = NULL;
486           node->destroy = NULL;
487
488           if (ddestroy) 
489             {
490               G_UNLOCK (g_thread);
491               ddestroy (ddata);
492               G_LOCK (g_thread);
493               }
494         }
495     }
496   g_thread_free_indeces = g_slist_prepend (g_thread_free_indeces, 
497                                            GUINT_TO_POINTER (index));
498   G_UNLOCK (g_thread);
499 }
500
501 static void
502 g_thread_cleanup (gpointer data)
503 {
504   if (data)
505     {
506       GRealThread* thread = data;
507       if (thread->private_data)
508         {
509           GArray* array = thread->private_data;
510           guint i;
511           
512           for (i = 0; i < array->len; i++ )
513             {
514               GStaticPrivateNode *node = 
515                 &g_array_index (array, GStaticPrivateNode, i);
516               if (node->destroy)
517                 node->destroy (node->data);
518             }
519           g_array_free (array, TRUE);
520         }
521
522       /* We only free the thread structure, if it isn't joinable. If
523          it is, the structure is freed in g_thread_join */
524       if (!thread->thread.joinable)
525         {
526           G_LOCK (g_thread);
527           g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
528           G_UNLOCK (g_thread);
529           
530           /* Just to make sure, this isn't used any more */
531           g_system_thread_assign (thread->system_thread, zero_thread);
532           g_free (thread);
533         }
534     }
535 }
536
537 static void
538 g_thread_fail (void)
539 {
540   g_error ("The thread system is not yet initialized.");
541 }
542
543 static gpointer
544 g_thread_create_proxy (gpointer data)
545 {
546   GRealThread* thread = data;
547
548   g_assert (data);
549
550   /* This has to happen before G_LOCK, as that might call g_thread_self */
551   g_private_set (g_thread_specific_private, data);
552
553   /* the lock makes sure, that thread->system_thread is written,
554      before thread->thread.func is called. See g_thread_create. */
555   G_LOCK (g_thread);
556   G_UNLOCK (g_thread);
557  
558   thread->retval = thread->thread.func (thread->thread.data);
559
560   return NULL;
561 }
562
563 GThread* 
564 g_thread_create_full (GThreadFunc                func,
565                       gpointer           data,
566                       gulong             stack_size,
567                       gboolean           joinable,
568                       gboolean           bound,
569                       GThreadPriority    priority,
570                       GError                **error)
571 {
572   GRealThread* result;
573   GError *local_error = NULL;
574   g_return_val_if_fail (func, NULL);
575   g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
576   g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
577   
578   result = g_new (GRealThread, 1);
579
580   result->thread.joinable = joinable;
581   result->thread.priority = priority;
582   result->thread.func = func;
583   result->thread.data = data;
584   result->private_data = NULL; 
585   G_LOCK (g_thread);
586   G_THREAD_UF (thread_create, (g_thread_create_proxy, result, 
587                                stack_size, joinable, bound, priority,
588                                &result->system_thread, &local_error));
589   g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
590   G_UNLOCK (g_thread);
591
592   if (local_error)
593     {
594       g_propagate_error (error, local_error);
595       g_free (result);
596       return NULL;
597     }
598
599   return (GThread*) result;
600 }
601
602 void
603 g_thread_exit (gpointer retval)
604 {
605   GRealThread* real = (GRealThread*) g_thread_self ();
606   real->retval = retval;
607   G_THREAD_CF (thread_exit, (void)0, ());
608 }
609
610 gpointer
611 g_thread_join (GThread* thread)
612 {
613   GRealThread* real = (GRealThread*) thread;
614   gpointer retval;
615
616   g_return_val_if_fail (thread, NULL);
617   g_return_val_if_fail (thread->joinable, NULL);
618   g_return_val_if_fail (!g_system_thread_equal (real->system_thread, 
619                                                 zero_thread), NULL);
620
621   G_THREAD_UF (thread_join, (&real->system_thread));
622
623   retval = real->retval;
624
625   G_LOCK (g_thread);
626   g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
627   G_UNLOCK (g_thread);
628
629   /* Just to make sure, this isn't used any more */
630   thread->joinable = 0;
631   g_system_thread_assign (real->system_thread, zero_thread);
632
633   /* the thread structure for non-joinable threads is freed upon
634      thread end. We free the memory here. This will leave a loose end,
635      if a joinable thread is not joined. */
636
637   g_free (thread);
638
639   return retval;
640 }
641
642 void
643 g_thread_set_priority (GThread* thread, 
644                        GThreadPriority priority)
645 {
646   GRealThread* real = (GRealThread*) thread;
647
648   g_return_if_fail (thread);
649   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
650   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
651   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
652
653   thread->priority = priority;
654
655   G_THREAD_CF (thread_set_priority, (void)0, 
656                (&real->system_thread, priority));
657 }
658
659 GThread*
660 g_thread_self (void)
661 {
662   GRealThread* thread = g_private_get (g_thread_specific_private);
663
664   if (!thread)
665     {  
666       /* If no thread data is available, provide and set one.  This
667          can happen for the main thread and for threads, that are not
668          created by GLib. */
669       thread = g_new (GRealThread, 1);
670       thread->thread.joinable = FALSE; /* This is a save guess */
671       thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
672                                                              just a guess */
673       thread->thread.func = NULL;
674       thread->thread.data = NULL;
675       thread->private_data = NULL;
676
677       if (g_thread_supported ())
678         G_THREAD_UF (thread_self, (&thread->system_thread));
679
680       g_private_set (g_thread_specific_private, thread); 
681       
682       G_LOCK (g_thread);
683       g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
684       G_UNLOCK (g_thread);
685     }
686   
687   return (GThread*)thread;
688 }
689
690 void
691 g_static_rw_lock_init (GStaticRWLock* lock)
692 {
693   static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
694
695   g_return_if_fail (lock);
696
697   *lock = init_lock;
698 }
699
700 static void inline 
701 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
702 {
703   if (!*cond)
704       *cond = g_cond_new ();
705   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
706 }
707
708 static void inline 
709 g_static_rw_lock_signal (GStaticRWLock* lock)
710 {
711   if (lock->want_to_write && lock->write_cond)
712     g_cond_signal (lock->write_cond);
713   else if (lock->want_to_read && lock->read_cond)
714     g_cond_broadcast (lock->read_cond);
715 }
716
717 void 
718 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
719 {
720   g_return_if_fail (lock);
721
722   if (!g_threads_got_initialized)
723     return;
724
725   g_static_mutex_lock (&lock->mutex);
726   lock->want_to_read++;
727   while (lock->have_writer || lock->want_to_write) 
728     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
729   lock->want_to_read--;
730   lock->read_counter++;
731   g_static_mutex_unlock (&lock->mutex);
732 }
733
734 gboolean 
735 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
736 {
737   gboolean ret_val = FALSE;
738
739   g_return_val_if_fail (lock, FALSE);
740
741   if (!g_threads_got_initialized)
742     return TRUE;
743
744   g_static_mutex_lock (&lock->mutex);
745   if (!lock->have_writer && !lock->want_to_write)
746     {
747       lock->read_counter++;
748       ret_val = TRUE;
749     }
750   g_static_mutex_unlock (&lock->mutex);
751   return ret_val;
752 }
753
754 void 
755 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
756 {
757   g_return_if_fail (lock);
758
759   if (!g_threads_got_initialized)
760     return;
761
762   g_static_mutex_lock (&lock->mutex);
763   lock->read_counter--;
764   if (lock->read_counter == 0)
765     g_static_rw_lock_signal (lock);
766   g_static_mutex_unlock (&lock->mutex);
767 }
768
769 void 
770 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
771 {
772   g_return_if_fail (lock);
773
774   if (!g_threads_got_initialized)
775     return;
776
777   g_static_mutex_lock (&lock->mutex);
778   lock->want_to_write++;
779   while (lock->have_writer || lock->read_counter)
780     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
781   lock->want_to_write--;
782   lock->have_writer = TRUE;
783   g_static_mutex_unlock (&lock->mutex);
784 }
785
786 gboolean 
787 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
788 {
789   gboolean ret_val = FALSE;
790
791   g_return_val_if_fail (lock, FALSE);
792   
793   if (!g_threads_got_initialized)
794     return TRUE;
795
796   g_static_mutex_lock (&lock->mutex);
797   if (!lock->have_writer && !lock->read_counter)
798     {
799       lock->have_writer = TRUE;
800       ret_val = TRUE;
801     }
802   g_static_mutex_unlock (&lock->mutex);
803   return ret_val;
804 }
805
806 void 
807 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
808 {
809   g_return_if_fail (lock);
810   
811   if (!g_threads_got_initialized)
812     return;
813
814   g_static_mutex_lock (&lock->mutex);
815   lock->have_writer = FALSE; 
816   g_static_rw_lock_signal (lock);
817   g_static_mutex_unlock (&lock->mutex);
818 }
819
820 void 
821 g_static_rw_lock_free (GStaticRWLock* lock)
822 {
823   g_return_if_fail (lock);
824   
825   if (lock->read_cond)
826     {
827       g_cond_free (lock->read_cond);
828       lock->read_cond = NULL;
829     }
830   if (lock->write_cond)
831     {
832       g_cond_free (lock->write_cond);
833       lock->write_cond = NULL;
834     }
835   g_static_mutex_free (&lock->mutex);
836 }