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