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