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