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