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