Delay allocation until after all g_return_val_if_fail ().
[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   guint 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;
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 = g_new (GRealThread, 1);
559
560   result->thread.joinable = joinable;
561   result->thread.priority = priority;
562   result->thread.func = func;
563   result->thread.data = data;
564   result->private_data = NULL; 
565   G_LOCK (g_thread);
566   G_THREAD_UF (thread_create, (g_thread_create_proxy, result, 
567                                stack_size, joinable, bound, priority,
568                                &result->system_thread, &local_error));
569   g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
570   G_UNLOCK (g_thread);
571
572   if (local_error)
573     {
574       g_propagate_error (error, local_error);
575       g_free (result);
576       return NULL;
577     }
578
579   return (GThread*) result;
580 }
581
582 void
583 g_thread_exit (gpointer retval)
584 {
585   GRealThread* real = (GRealThread*) g_thread_self ();
586   real->retval = retval;
587   G_THREAD_CF (thread_exit, (void)0, ());
588 }
589
590 gpointer
591 g_thread_join (GThread* thread)
592 {
593   GRealThread* real = (GRealThread*) thread;
594   gpointer retval;
595
596   g_return_val_if_fail (thread, NULL);
597   g_return_val_if_fail (thread->joinable, NULL);
598   g_return_val_if_fail (!g_system_thread_equal (real->system_thread, 
599                                                 zero_thread), NULL);
600
601   G_THREAD_UF (thread_join, (&real->system_thread));
602
603   retval = real->retval;
604
605   G_LOCK (g_thread);
606   g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
607   G_UNLOCK (g_thread);
608
609   /* Just to make sure, this isn't used any more */
610   thread->joinable = 0;
611   g_system_thread_assign (real->system_thread, zero_thread);
612
613   /* the thread structure for non-joinable threads is freed upon
614      thread end. We free the memory here. This will leave a loose end,
615      if a joinable thread is not joined. */
616
617   g_free (thread);
618
619   return retval;
620 }
621
622 void
623 g_thread_set_priority (GThread* thread, 
624                        GThreadPriority priority)
625 {
626   GRealThread* real = (GRealThread*) thread;
627
628   g_return_if_fail (thread);
629   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
630   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
631   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
632
633   thread->priority = priority;
634
635 #ifdef G_THREAD_USE_PID_SURROGATE
636   if (g_thread_use_default_impl)
637     SET_PRIO (real->pid, priority);
638   else
639 #endif /* G_THREAD_USE_PID_SURROGATE */
640     G_THREAD_CF (thread_set_priority, (void)0, 
641                  (&real->system_thread, priority));
642 }
643
644 GThread*
645 g_thread_self (void)
646 {
647   GRealThread* thread = g_private_get (g_thread_specific_private);
648
649   if (!thread)
650     {  
651       /* If no thread data is available, provide and set one.  This
652          can happen for the main thread and for threads, that are not
653          created by GLib. */
654       thread = g_new (GRealThread, 1);
655       thread->thread.joinable = FALSE; /* This is a save guess */
656       thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
657                                                              just a guess */
658       thread->thread.func = NULL;
659       thread->thread.data = NULL;
660       thread->private_data = 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->want_to_read && 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   lock->want_to_read++;
716   while (lock->write || lock->want_to_write) 
717     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
718   lock->want_to_read--;
719   lock->read_counter++;
720   g_static_mutex_unlock (&lock->mutex);
721 }
722
723 gboolean 
724 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
725 {
726   gboolean ret_val = FALSE;
727
728   g_return_val_if_fail (lock, FALSE);
729
730   if (!g_threads_got_initialized)
731     return TRUE;
732
733   g_static_mutex_lock (&lock->mutex);
734   if (!lock->write && !lock->want_to_write)
735     {
736       lock->read_counter++;
737       ret_val = TRUE;
738     }
739   g_static_mutex_unlock (&lock->mutex);
740   return ret_val;
741 }
742
743 void 
744 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
745 {
746   g_return_if_fail (lock);
747
748   if (!g_threads_got_initialized)
749     return;
750
751   g_static_mutex_lock (&lock->mutex);
752   lock->read_counter--;
753   if (lock->read_counter == 0)
754     g_static_rw_lock_signal (lock);
755   g_static_mutex_unlock (&lock->mutex);
756 }
757
758 void 
759 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
760 {
761   g_return_if_fail (lock);
762
763   if (!g_threads_got_initialized)
764     return;
765
766   g_static_mutex_lock (&lock->mutex);
767   lock->want_to_write++;
768   while (lock->write || lock->read_counter)
769     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
770   lock->want_to_write--;
771   lock->write = TRUE;
772   g_static_mutex_unlock (&lock->mutex);
773 }
774
775 gboolean 
776 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
777 {
778   gboolean ret_val = FALSE;
779
780   g_return_val_if_fail (lock, FALSE);
781   
782   if (!g_threads_got_initialized)
783     return TRUE;
784
785   g_static_mutex_lock (&lock->mutex);
786   if (!lock->write && !lock->read_counter)
787     {
788       lock->write = TRUE;
789       ret_val = TRUE;
790     }
791   g_static_mutex_unlock (&lock->mutex);
792   return ret_val;
793 }
794
795 void 
796 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
797 {
798   g_return_if_fail (lock);
799   
800   if (!g_threads_got_initialized)
801     return;
802
803   g_static_mutex_lock (&lock->mutex);
804   lock->write = FALSE; 
805   g_static_rw_lock_signal (lock);
806   g_static_mutex_unlock (&lock->mutex);
807 }
808
809 void 
810 g_static_rw_lock_free (GStaticRWLock* lock)
811 {
812   g_return_if_fail (lock);
813   
814   if (lock->read_cond)
815     {
816       g_cond_free (lock->read_cond);
817       lock->read_cond = NULL;
818     }
819   if (lock->write_cond)
820     {
821       g_cond_free (lock->write_cond);
822       lock->write_cond = NULL;
823     }
824   g_static_mutex_free (&lock->mutex);
825 }