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