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