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