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