4393e8dd7469876aab721ff84320c429370002fa
[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(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   GSystemThread self;
296   g_return_if_fail (mutex);
297
298   if (!g_thread_supported ())
299     return;
300
301   G_THREAD_UF (thread_self, (&self));
302
303   if (g_system_thread_equal (self, mutex->owner))
304     {
305       mutex->depth += depth;
306       return;
307     }
308   g_static_mutex_lock (&mutex->mutex);
309   g_system_thread_assign (mutex->owner, self);
310   mutex->depth = depth;
311 }
312
313 guint    
314 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
315 {
316   gint depth;
317
318   g_return_val_if_fail (mutex, 0);
319
320   if (!g_thread_supported ())
321     return 1;
322
323   depth = mutex->depth;
324
325   g_system_thread_assign (mutex->owner, zero_thread);
326   mutex->depth = 0;
327   g_static_mutex_unlock (&mutex->mutex);
328
329   return depth;
330 }
331
332 void
333 g_static_rec_mutex_free (GStaticRecMutex *mutex)
334 {
335   g_return_if_fail (mutex);
336
337   g_static_mutex_free (&mutex->mutex);
338 }
339
340 void     
341 g_static_private_init (GStaticPrivate *private_key)
342 {
343   private_key->index = 0;
344 }
345
346 gpointer
347 g_static_private_get (GStaticPrivate *private_key)
348 {
349   return g_static_private_get_for_thread (private_key, g_thread_self ());
350 }
351
352 gpointer
353 g_static_private_get_for_thread (GStaticPrivate *private_key,
354                                  GThread        *thread)
355 {
356   GArray *array;
357   GRealThread *self = (GRealThread*) thread;
358
359   g_return_val_if_fail (thread, NULL);
360
361   array = self->private_data;
362   if (!array)
363     return NULL;
364
365   if (!private_key->index)
366     return NULL;
367   else if (private_key->index <= array->len)
368     return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
369   else
370     return NULL;
371 }
372
373 void
374 g_static_private_set (GStaticPrivate *private_key, 
375                       gpointer        data,
376                       GDestroyNotify  notify)
377 {
378   g_static_private_set_for_thread (private_key, g_thread_self (), 
379                                    data, notify);
380 }
381
382 void
383 g_static_private_set_for_thread (GStaticPrivate *private_key, 
384                                  GThread        *thread,
385                                  gpointer        data,
386                                  GDestroyNotify  notify)
387 {
388   GArray *array;
389   GRealThread *self =(GRealThread*) thread;
390   static guint next_index = 0;
391   GStaticPrivateNode *node;
392
393   g_return_if_fail (thread);
394   
395   array = self->private_data;
396   if (!array)
397     {
398       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
399       self->private_data = array;
400     }
401
402   if (!private_key->index)
403     {
404       G_LOCK (g_thread);
405
406       if (!private_key->index)
407         {
408           if (g_thread_free_indeces)
409             {
410               private_key->index = 
411                 GPOINTER_TO_UINT (g_thread_free_indeces->data);
412               g_thread_free_indeces = 
413                 g_slist_delete_link (g_thread_free_indeces,
414                                      g_thread_free_indeces);
415             }
416           else
417             private_key->index = ++next_index;
418         }
419
420       G_UNLOCK (g_thread);
421     }
422
423   if (private_key->index > array->len)
424     g_array_set_size (array, private_key->index);
425
426   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
427   if (node->destroy)
428     {
429       gpointer ddata = node->data;
430       GDestroyNotify ddestroy = node->destroy;
431
432       node->data = data;
433       node->destroy = notify;
434
435       ddestroy (ddata);
436     }
437   else
438     {
439       node->data = data;
440       node->destroy = notify;
441     }
442 }
443
444 void     
445 g_static_private_free (GStaticPrivate *private_key)
446 {
447   GStaticPrivate copied_key;
448   GSList *list;
449
450   copied_key.index = private_key->index;
451   private_key->index = 0;
452
453   if (!copied_key.index)
454     return;
455
456   G_LOCK (g_thread);
457   list =  g_thread_all_threads;
458   while (list)
459     {
460       GThread *thread = list->data;
461       list = list->next;
462       
463       G_UNLOCK (g_thread);
464       g_static_private_set_for_thread (&copied_key, thread, NULL, NULL);
465       G_LOCK (g_thread);
466     }
467   g_thread_free_indeces = 
468     g_slist_prepend (g_thread_free_indeces, 
469                      GUINT_TO_POINTER (copied_key.index));
470   G_UNLOCK (g_thread);
471 }
472
473 static void
474 g_thread_cleanup (gpointer data)
475 {
476   if (data)
477     {
478       GRealThread* thread = data;
479       if (thread->private_data)
480         {
481           GArray* array = thread->private_data;
482           guint i;
483           
484           for (i = 0; i < array->len; i++ )
485             {
486               GStaticPrivateNode *node = 
487                 &g_array_index (array, GStaticPrivateNode, i);
488               if (node->destroy)
489                 node->destroy (node->data);
490             }
491           g_array_free (array, TRUE);
492         }
493       /* We only free the thread structure, if it isn't joinable. If
494          it is, the structure is freed in g_thread_join */
495       if (!thread->thread.joinable)
496         {
497           G_LOCK (g_thread);
498           g_thread_all_threads = g_slist_remove (g_thread_all_threads, data);
499           G_UNLOCK (g_thread);
500           
501           /* Just to make sure, this isn't used any more */
502           g_system_thread_assign (thread->system_thread, zero_thread);
503           g_free (thread);
504         }
505     }
506 }
507
508 static void
509 g_thread_fail (void)
510 {
511   g_error ("The thread system is not yet initialized.");
512 }
513
514 static void 
515 g_thread_create_proxy (gpointer data)
516 {
517   GRealThread* thread = data;
518
519   g_assert (data);
520
521 #ifdef G_THREAD_USE_PID_SURROGATE
522   thread->pid = getpid ();
523 #endif /* G_THREAD_USE_PID_SURROGATE */
524
525   /* This has to happen before G_LOCK, as that might call g_thread_self */
526   g_private_set (g_thread_specific_private, data);
527
528   /* the lock makes sure, that thread->system_thread is written,
529      before thread->func is called. See g_thread_create. */
530   G_LOCK (g_thread);
531   G_UNLOCK (g_thread);
532  
533 #ifdef G_THREAD_USE_PID_SURROGATE
534   if (g_thread_use_default_impl)
535     SET_PRIO (thread->pid, thread->thread.priority);
536 #endif /* G_THREAD_USE_PID_SURROGATE */
537
538   thread->func (thread->arg);
539 }
540
541 GThread* 
542 g_thread_create (GThreadFunc             thread_func,
543                  gpointer                arg,
544                  gulong                  stack_size,
545                  gboolean                joinable,
546                  gboolean                bound,
547                  GThreadPriority         priority,
548                  GError                **error)
549 {
550   GRealThread* result = g_new (GRealThread, 1);
551   GError *local_error = NULL;
552   g_return_val_if_fail (thread_func, NULL);
553   g_return_val_if_fail (priority >= G_THREAD_PRIORITY_LOW, NULL);
554   g_return_val_if_fail (priority <= G_THREAD_PRIORITY_URGENT, NULL);
555   
556   result->thread.joinable = joinable;
557   result->thread.bound = bound;
558   result->thread.priority = priority;
559   result->func = thread_func;
560   result->arg = arg;
561   result->private_data = NULL; 
562   G_LOCK (g_thread);
563   G_THREAD_UF (thread_create, (g_thread_create_proxy, result, 
564                                stack_size, joinable, bound, priority,
565                                &result->system_thread, &local_error));
566   g_thread_all_threads = g_slist_prepend (g_thread_all_threads, result);
567   G_UNLOCK (g_thread);
568
569   if (local_error)
570     {
571       g_propagate_error (error, local_error);
572       g_free (result);
573       return NULL;
574     }
575
576   return (GThread*) result;
577 }
578
579 void 
580 g_thread_join (GThread* thread)
581 {
582   GRealThread* real = (GRealThread*) thread;
583   
584
585   g_return_if_fail (thread);
586   g_return_if_fail (thread->joinable);
587   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
588
589   G_THREAD_UF (thread_join, (&real->system_thread));
590
591   G_LOCK (g_thread);
592   g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
593   G_UNLOCK (g_thread);
594
595   /* Just to make sure, this isn't used any more */
596   thread->joinable = 0;
597   g_system_thread_assign (real->system_thread, zero_thread);
598
599   /* the thread structure for non-joinable threads is freed upon
600      thread end. We free the memory here. This will leave loose end,
601      if a joinable thread is not joined. */
602
603   g_free (thread);
604 }
605
606 void
607 g_thread_set_priority (GThread* thread, 
608                        GThreadPriority priority)
609 {
610   GRealThread* real = (GRealThread*) thread;
611
612   g_return_if_fail (thread);
613   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
614   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
615   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
616
617   thread->priority = priority;
618
619 #ifdef G_THREAD_USE_PID_SURROGATE
620   if (g_thread_use_default_impl)
621     SET_PRIO (real->pid, priority);
622   else
623 #endif /* G_THREAD_USE_PID_SURROGATE */
624     G_THREAD_CF (thread_set_priority, (void)0, 
625                  (&real->system_thread, priority));
626 }
627
628 GThread*
629 g_thread_self (void)
630 {
631   GRealThread* thread = g_private_get (g_thread_specific_private);
632
633   if (!thread)
634     {  
635       /* If no thread data is available, provide and set one.  This
636          can happen for the main thread and for threads, that are not
637          created by GLib. */
638       thread = g_new (GRealThread, 1);
639       thread->thread.joinable = FALSE; /* This is a save guess */
640       thread->thread.bound = TRUE; /* This isn't important at all */
641       thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
642                                                              just a guess */
643       thread->func = NULL;
644       thread->arg = NULL;
645       thread->private_data = NULL;
646
647       if (g_thread_supported ())
648         G_THREAD_UF (thread_self, (&thread->system_thread));
649
650 #ifdef G_THREAD_USE_PID_SURROGATE
651       thread->pid = getpid ();
652 #endif /* G_THREAD_USE_PID_SURROGATE */
653       
654       g_private_set (g_thread_specific_private, thread); 
655       
656       G_LOCK (g_thread);
657       g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
658       G_UNLOCK (g_thread);
659     }
660   
661   return (GThread*)thread;
662 }
663
664 void
665 g_static_rw_lock_init (GStaticRWLock* lock)
666 {
667   static GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
668
669   g_return_if_fail (lock);
670
671   memcpy (lock, &init_lock, sizeof (GStaticRWLock));
672 }
673
674 static void inline 
675 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
676 {
677   if (!*cond)
678       *cond = g_cond_new ();
679   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
680 }
681
682 static void inline 
683 g_static_rw_lock_signal (GStaticRWLock* lock)
684 {
685   if (lock->want_to_write && lock->write_cond)
686     g_cond_signal (lock->write_cond);
687   else if (lock->read_cond)
688     g_cond_broadcast (lock->read_cond);
689 }
690
691 void 
692 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
693 {
694   g_return_if_fail (lock);
695
696   if (!g_threads_got_initialized)
697     return;
698
699   g_static_mutex_lock (&lock->mutex);
700   while (lock->write || lock->want_to_write) 
701     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
702   lock->read_counter++;
703   g_static_mutex_unlock (&lock->mutex);
704 }
705
706 gboolean 
707 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
708 {
709   gboolean ret_val = FALSE;
710
711   g_return_val_if_fail (lock, FALSE);
712
713   if (!g_threads_got_initialized)
714     return TRUE;
715
716   g_static_mutex_lock (&lock->mutex);
717   if (!lock->write && !lock->want_to_write)
718     {
719       lock->read_counter++;
720       ret_val = TRUE;
721     }
722   g_static_mutex_unlock (&lock->mutex);
723   return ret_val;
724 }
725
726 void 
727 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
728 {
729   g_return_if_fail (lock);
730
731   if (!g_threads_got_initialized)
732     return;
733
734   g_static_mutex_lock (&lock->mutex);
735   lock->read_counter--;
736   if (lock->read_counter == 0)
737     g_static_rw_lock_signal (lock);
738   g_static_mutex_unlock (&lock->mutex);
739 }
740
741 void 
742 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
743 {
744   g_return_if_fail (lock);
745
746   if (!g_threads_got_initialized)
747     return;
748
749   g_static_mutex_lock (&lock->mutex);
750   lock->want_to_write++;
751   while (lock->write || lock->read_counter)
752     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
753   lock->want_to_write--;
754   lock->write = TRUE;
755   g_static_mutex_unlock (&lock->mutex);
756 }
757
758 gboolean 
759 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
760 {
761   gboolean ret_val = FALSE;
762
763   g_return_val_if_fail (lock, FALSE);
764   
765   if (!g_threads_got_initialized)
766     return TRUE;
767
768   g_static_mutex_lock (&lock->mutex);
769   if (!lock->write && !lock->read_counter)
770     {
771       lock->write = TRUE;
772       ret_val = TRUE;
773     }
774   g_static_mutex_unlock (&lock->mutex);
775   return ret_val;
776 }
777
778 void 
779 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
780 {
781   g_return_if_fail (lock);
782   
783   if (!g_threads_got_initialized)
784     return;
785
786   g_static_mutex_lock (&lock->mutex);
787   lock->write = FALSE; 
788   g_static_rw_lock_signal (lock);
789   g_static_mutex_unlock (&lock->mutex);
790 }
791
792 void 
793 g_static_rw_lock_free (GStaticRWLock* lock)
794 {
795   g_return_if_fail (lock);
796   
797   if (lock->read_cond)
798     {
799       g_cond_free (lock->read_cond);
800       lock->read_cond = NULL;
801     }
802   if (lock->write_cond)
803     {
804       g_cond_free (lock->write_cond);
805       lock->write_cond = NULL;
806     }
807   g_static_mutex_free (&lock->mutex);
808 }