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