Cleanup thread documentation
[platform/upstream/glib.git] / glib / deprecated / gthread-deprecated.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gthread.c: MT safety related functions
5  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
6  *                Owen Taylor
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include "config.h"
25
26 #include "gmessages.h"
27 #include "gslice.h"
28 #include "gmain.h"
29 #include "gthread.h"
30 #include "gthreadprivate.h"
31 #include "deprecated/gthread.h"
32
33 #include "gutils.h"
34
35 /* {{{1 Documentation */
36
37 /**
38  * GThreadPriority:
39  * @G_THREAD_PRIORITY_LOW: a priority lower than normal
40  * @G_THREAD_PRIORITY_NORMAL: the default priority
41  * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
42  * @G_THREAD_PRIORITY_URGENT: the highest priority
43  *
44  * Deprecated:2.32: Thread priorities no longer have any effect.
45  */
46
47 /**
48  * GThreadFunctions:
49  * @mutex_new: virtual function pointer for g_mutex_new()
50  * @mutex_lock: virtual function pointer for g_mutex_lock()
51  * @mutex_trylock: virtual function pointer for g_mutex_trylock()
52  * @mutex_unlock: virtual function pointer for g_mutex_unlock()
53  * @mutex_free: virtual function pointer for g_mutex_free()
54  * @cond_new: virtual function pointer for g_cond_new()
55  * @cond_signal: virtual function pointer for g_cond_signal()
56  * @cond_broadcast: virtual function pointer for g_cond_broadcast()
57  * @cond_wait: virtual function pointer for g_cond_wait()
58  * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
59  * @cond_free: virtual function pointer for g_cond_free()
60  * @private_new: virtual function pointer for g_private_new()
61  * @private_get: virtual function pointer for g_private_get()
62  * @private_set: virtual function pointer for g_private_set()
63  * @thread_create: virtual function pointer for g_thread_create()
64  * @thread_yield: virtual function pointer for g_thread_yield()
65  * @thread_join: virtual function pointer for g_thread_join()
66  * @thread_exit: virtual function pointer for g_thread_exit()
67  * @thread_set_priority: virtual function pointer for
68  *                       g_thread_set_priority()
69  * @thread_self: virtual function pointer for g_thread_self()
70  * @thread_equal: used internally by recursive mutex locks and by some
71  *                assertion checks
72  *
73  * This function table is no longer used by g_thread_init()
74  * to initialize the thread system.
75  */
76
77 /**
78  * G_THREADS_IMPL_POSIX:
79  *
80  * This macro is defined if POSIX style threads are used.
81  *
82  * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
83  *                 Use G_OS_WIN32 to detect Windows.
84  */
85
86 /**
87  * G_THREADS_IMPL_WIN32:
88  *
89  * This macro is defined if Windows style threads are used.
90  *
91  * Deprecated:2.32:Use G_OS_WIN32 to detect Windows.
92  */
93
94
95 /* {{{1 Exported Variables */
96
97 gboolean g_thread_use_default_impl = TRUE;
98
99 GThreadFunctions g_thread_functions_for_glib_use =
100 {
101   g_mutex_new,
102   g_mutex_lock,
103   g_mutex_trylock,
104   g_mutex_unlock,
105   g_mutex_free,
106   g_cond_new,
107   g_cond_signal,
108   g_cond_broadcast,
109   g_cond_wait,
110   g_cond_timed_wait,
111   g_cond_free,
112   g_private_new,
113   g_private_get,
114   g_private_set,
115   NULL,
116   g_thread_yield,
117   NULL,
118   NULL,
119   NULL,
120   NULL,
121   NULL,
122 };
123
124 static guint64
125 gettime (void)
126 {
127   return g_get_monotonic_time () * 1000;
128 }
129
130 guint64 (*g_thread_gettime) (void) = gettime;
131
132 /* Initialisation {{{1 ---------------------------------------------------- */
133 gboolean         g_threads_got_initialized = TRUE;
134 GSystemThread    zero_thread; /* This is initialized to all zero */
135
136
137 /**
138  * g_thread_init:
139  * @vtable: a function table of type #GThreadFunctions, that provides
140  *     the entry points to the thread system to be used. Since 2.32,
141  *     this parameter is ignored and should always be %NULL
142  *
143  * If you use GLib from more than one thread, you must initialize the
144  * thread system by calling g_thread_init().
145  *
146  * Since version 2.24, calling g_thread_init() multiple times is allowed,
147  * but nothing happens except for the first call.
148  *
149  * Since version 2.32, GLib does not support custom thread implementations
150  * anymore and the @vtable parameter is ignored and you should pass %NULL.
151  *
152  * <note><para>g_thread_init() must not be called directly or indirectly
153  * in a callback from GLib. Also no mutexes may be currently locked while
154  * calling g_thread_init().</para></note>
155  *
156  * <note><para>To use g_thread_init() in your program, you have to link
157  * with the libraries that the command <command>pkg-config --libs
158  * gthread-2.0</command> outputs. This is not the case for all the
159  * other thread-related functions of GLib. Those can be used without
160  * having to link with the thread libraries.</para></note>
161  */
162
163 /**
164  * g_thread_get_initialized:
165  *
166  * Indicates if g_thread_init() has been called.
167  *
168  * Returns: %TRUE if threads have been initialized.
169  *
170  * Since: 2.20
171  */
172 gboolean
173 g_thread_get_initialized (void)
174 {
175   return g_thread_supported ();
176 }
177
178 /* We need this for ABI compatibility */
179 void g_thread_init_glib (void) { }
180
181 /* Internal variables {{{1 */
182
183 static GRealThread *g_thread_all_threads = NULL;
184 static GSList      *g_thread_free_indices = NULL;
185
186 /* Protects g_thread_all_threads and g_thread_free_indices */
187 G_LOCK_DEFINE_STATIC (g_thread);
188
189 /* Misc. GThread functions {{{1 */
190
191 /**
192  * g_thread_set_priority:
193  * @thread: a #GThread.
194  * @priority: ignored
195  *
196  * This function does nothing.
197  *
198  * Deprecated:2.32: Thread priorities no longer have any effect.
199  */
200 void
201 g_thread_set_priority (GThread         *thread,
202                        GThreadPriority  priority)
203 {
204 }
205
206 /**
207  * g_thread_create:
208  * @func: a function to execute in the new thread
209  * @data: an argument to supply to the new thread
210  * @joinable: should this thread be joinable?
211  * @error: return location for error, or %NULL
212  *
213  * This function creates a new thread.
214  *
215  * If @joinable is %TRUE, you can wait for this threads termination
216  * calling g_thread_join(). Otherwise the thread will just disappear
217  * when it terminates.
218  *
219  * The new thread executes the function @func with the argument @data.
220  * If the thread was created successfully, it is returned.
221  *
222  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
223  * The error is set, if and only if the function returns %NULL.
224  *
225  * Returns: the new #GThread on success
226  *
227  * Deprecated:2.32: Use g_thread_new() instead
228  */
229 GThread *
230 g_thread_create (GThreadFunc   func,
231                  gpointer      data,
232                  gboolean      joinable,
233                  GError      **error)
234 {
235   return g_thread_new_internal (NULL, func, data, joinable, 0, TRUE, error);
236 }
237
238 /**
239  * g_thread_create_full:
240  * @func: a function to execute in the new thread.
241  * @data: an argument to supply to the new thread.
242  * @stack_size: a stack size for the new thread.
243  * @joinable: should this thread be joinable?
244  * @bound: ignored
245  * @priority: ignored
246  * @error: return location for error.
247  * @Returns: the new #GThread on success.
248  *
249  * This function creates a new thread.
250  *
251  * Deprecated:2.32: The @bound and @priority arguments are now ignored.
252  * Use g_thread_new() or g_thread_new_full() instead.
253  */
254 GThread *
255 g_thread_create_full (GThreadFunc       func,
256                       gpointer          data,
257                       gulong            stack_size,
258                       gboolean          joinable,
259                       gboolean          bound,
260                       GThreadPriority   priority,
261                       GError          **error)
262 {
263   return g_thread_new_internal (NULL, func, data, joinable, stack_size, TRUE, error);
264 }
265
266 /**
267  * g_thread_foreach:
268  * @thread_func: function to call for all #GThread structures
269  * @user_data: second argument to @thread_func
270  *
271  * Call @thread_func on all #GThreads that have been
272  * created with g_thread_create().
273  *
274  * Note that threads may decide to exit while @thread_func is
275  * running, so without intimate knowledge about the lifetime of
276  * foreign threads, @thread_func shouldn't access the GThread*
277  * pointer passed in as first argument. However, @thread_func will
278  * not be called for threads which are known to have exited already.
279  *
280  * Due to thread lifetime checks, this function has an execution complexity
281  * which is quadratic in the number of existing threads.
282  *
283  * Since: 2.10
284  *
285  * Deprecated:2.32: There aren't many things you can do with a #GThread,
286  *     except comparing it with one that was returned from g_thread_create().
287  *     There are better ways to find out if your thread is still alive.
288  */
289 void
290 g_thread_foreach (GFunc    thread_func,
291                   gpointer user_data)
292 {
293   GSList *slist = NULL;
294   GRealThread *thread;
295   g_return_if_fail (thread_func != NULL);
296   /* snapshot the list of threads for iteration */
297   G_LOCK (g_thread);
298   for (thread = g_thread_all_threads; thread; thread = thread->next)
299     slist = g_slist_prepend (slist, thread);
300   G_UNLOCK (g_thread);
301   /* walk the list, skipping non-existent threads */
302   while (slist)
303     {
304       GSList *node = slist;
305       slist = node->next;
306       /* check whether the current thread still exists */
307       G_LOCK (g_thread);
308       for (thread = g_thread_all_threads; thread; thread = thread->next)
309         if (thread == node->data)
310           break;
311       G_UNLOCK (g_thread);
312       if (thread)
313         thread_func (thread, user_data);
314       g_slist_free_1 (node);
315     }
316 }
317
318 void
319 g_enumerable_thread_add (GRealThread *thread)
320 {
321   G_LOCK (g_thread);
322   thread->next = g_thread_all_threads;
323   g_thread_all_threads = thread;
324   G_UNLOCK (g_thread);
325 }
326
327 void
328 g_enumerable_thread_remove (GRealThread *thread)
329 {
330   GRealThread *t, *p;
331
332   G_LOCK (g_thread);
333   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
334     {
335       if (t == thread)
336         {
337           if (p)
338             p->next = t->next;
339           else
340             g_thread_all_threads = t->next;
341           break;
342         }
343     }
344   G_UNLOCK (g_thread);
345 }
346
347 /* GOnce {{{1 ------------------------------------------------------------- */
348 gboolean
349 g_once_init_enter_impl (volatile gsize *location)
350 {
351   return (g_once_init_enter) (location);
352 }
353
354 /* GStaticMutex {{{1 ------------------------------------------------------ */
355
356 /**
357  * GStaticMutex:
358  *
359  * A #GStaticMutex works like a #GMutex.
360  *
361  * Prior to GLib 2.32, GStaticMutex had the significant advantage
362  * that it doesn't need to be created at run-time, but can be defined
363  * at compile-time. Since 2.32, #GMutex can be statically allocated
364  * as well, and GStaticMutex has been deprecated.
365  *
366  * Here is a version of our give_me_next_number() example using
367  * a GStaticMutex.
368  *
369  * <example>
370  *  <title>
371  *   Using <structname>GStaticMutex</structname>
372  *   to simplify thread-safe programming
373  *  </title>
374  *  <programlisting>
375  *   int
376  *   give_me_next_number (void)
377  *   {
378  *     static int current_number = 0;
379  *     int ret_val;
380  *     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
381  *
382  *     g_static_mutex_lock (&amp;mutex);
383  *     ret_val = current_number = calc_next_number (current_number);
384  *     g_static_mutex_unlock (&amp;mutex);
385  *
386  *     return ret_val;
387  *   }
388  *  </programlisting>
389  * </example>
390  *
391  * Sometimes you would like to dynamically create a mutex. If you don't
392  * want to require prior calling to g_thread_init(), because your code
393  * should also be usable in non-threaded programs, you are not able to
394  * use g_mutex_new() and thus #GMutex, as that requires a prior call to
395  * g_thread_init(). In theses cases you can also use a #GStaticMutex.
396  * It must be initialized with g_static_mutex_init() before using it
397  * and freed with with g_static_mutex_free() when not needed anymore to
398  * free up any allocated resources.
399  *
400  * Even though #GStaticMutex is not opaque, it should only be used with
401  * the following functions, as it is defined differently on different
402  * platforms.
403  *
404  * All of the <function>g_static_mutex_*</function> functions apart
405  * from <function>g_static_mutex_get_mutex</function> can also be used
406  * even if g_thread_init() has not yet been called. Then they do
407  * nothing, apart from <function>g_static_mutex_trylock</function>,
408  * which does nothing but returning %TRUE.
409  *
410  * <note><para>All of the <function>g_static_mutex_*</function>
411  * functions are actually macros. Apart from taking their addresses, you
412  * can however use them as if they were functions.</para></note>
413  **/
414
415 /**
416  * G_STATIC_MUTEX_INIT:
417  *
418  * A #GStaticMutex must be initialized with this macro, before it can
419  * be used. This macro can used be to initialize a variable, but it
420  * cannot be assigned to a variable. In that case you have to use
421  * g_static_mutex_init().
422  *
423  * |[
424  * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
425  * ]|
426  **/
427
428 /**
429  * g_static_mutex_init:
430  * @mutex: a #GStaticMutex to be initialized.
431  *
432  * Initializes @mutex.
433  * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
434  *
435  * Deprecated: 2.32: Use g_mutex_init()
436  */
437 void
438 g_static_mutex_init (GStaticMutex *mutex)
439 {
440   static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
441
442   g_return_if_fail (mutex);
443
444   *mutex = init_mutex;
445 }
446
447 /* IMPLEMENTATION NOTE:
448  *
449  * On some platforms a GStaticMutex is actually a normal GMutex stored
450  * inside of a structure instead of being allocated dynamically.  We can
451  * only do this for platforms on which we know, in advance, how to
452  * allocate (size) and initialise (value) that memory.
453  *
454  * On other platforms, a GStaticMutex is nothing more than a pointer to
455  * a GMutex.  In that case, the first access we make to the static mutex
456  * must first allocate the normal GMutex and store it into the pointer.
457  *
458  * configure.ac writes macros into glibconfig.h to determine if
459  * g_static_mutex_get_mutex() accesses the structure in memory directly
460  * (on platforms where we are able to do that) or if it ends up here,
461  * where we may have to allocate the GMutex before returning it.
462  */
463
464 /**
465  * g_static_mutex_get_mutex:
466  * @mutex: a #GStaticMutex.
467  * @Returns: the #GMutex corresponding to @mutex.
468  *
469  * For some operations (like g_cond_wait()) you must have a #GMutex
470  * instead of a #GStaticMutex. This function will return the
471  * corresponding #GMutex for @mutex.
472  *
473  * Deprecated: 2.32: Just use a #GMutex
474  */
475 GMutex *
476 g_static_mutex_get_mutex_impl (GMutex** mutex)
477 {
478   GMutex *result;
479
480   if (!g_thread_supported ())
481     return NULL;
482
483   result = g_atomic_pointer_get (mutex);
484
485   if (!result)
486     {
487       g_mutex_lock (&g_once_mutex);
488
489       result = *mutex;
490       if (!result)
491         {
492           result = g_mutex_new ();
493           g_atomic_pointer_set (mutex, result);
494         }
495
496       g_mutex_unlock (&g_once_mutex);
497     }
498
499   return result;
500 }
501
502 /* IMPLEMENTATION NOTE:
503  *
504  * g_static_mutex_lock(), g_static_mutex_trylock() and
505  * g_static_mutex_unlock() are all preprocessor macros that wrap the
506  * corresponding g_mutex_*() function around a call to
507  * g_static_mutex_get_mutex().
508  */
509
510 /**
511  * g_static_mutex_lock:
512  * @mutex: a #GStaticMutex.
513  *
514  * Works like g_mutex_lock(), but for a #GStaticMutex.
515  *
516  * Deprecated: 2.32: Use g_mutex_lock()
517  */
518
519 /**
520  * g_static_mutex_trylock:
521  * @mutex: a #GStaticMutex.
522  * @Returns: %TRUE, if the #GStaticMutex could be locked.
523  *
524  * Works like g_mutex_trylock(), but for a #GStaticMutex.
525  *
526  * Deprecated: 2.32: Use g_mutex_trylock()
527  */
528
529 /**
530  * g_static_mutex_unlock:
531  * @mutex: a #GStaticMutex.
532  *
533  * Works like g_mutex_unlock(), but for a #GStaticMutex.
534  *
535  * Deprecated: 2.32: Use g_mutex_unlock()
536  */
537
538 /**
539  * g_static_mutex_free:
540  * @mutex: a #GStaticMutex to be freed.
541  *
542  * Releases all resources allocated to @mutex.
543  *
544  * You don't have to call this functions for a #GStaticMutex with an
545  * unbounded lifetime, i.e. objects declared 'static', but if you have
546  * a #GStaticMutex as a member of a structure and the structure is
547  * freed, you should also free the #GStaticMutex.
548  *
549  * <note><para>Calling g_static_mutex_free() on a locked mutex may
550  * result in undefined behaviour.</para></note>
551  *
552  * Deprecated: 2.32: Use g_mutex_free()
553  */
554 void
555 g_static_mutex_free (GStaticMutex* mutex)
556 {
557   GMutex **runtime_mutex;
558
559   g_return_if_fail (mutex);
560
561   /* The runtime_mutex is the first (or only) member of GStaticMutex,
562    * see both versions (of glibconfig.h) in configure.ac. Note, that
563    * this variable is NULL, if g_thread_init() hasn't been called or
564    * if we're using the default thread implementation and it provides
565    * static mutexes. */
566   runtime_mutex = ((GMutex**)mutex);
567
568   if (*runtime_mutex)
569     g_mutex_free (*runtime_mutex);
570
571   *runtime_mutex = NULL;
572 }
573
574 /* {{{1 GStaticRecMutex */
575
576 /**
577  * GStaticRecMutex:
578  *
579  * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
580  * multiple times by one thread. If you enter it n times, you have to
581  * unlock it n times again to let other threads lock it. An exception
582  * is the function g_static_rec_mutex_unlock_full(): that allows you to
583  * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
584  * number of times this mutex was locked). The depth can later be used
585  * to restore the state of the #GStaticRecMutex by calling
586  * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
587  * been deprecated in favor of #GRecMutex.
588  *
589  * Even though #GStaticRecMutex is not opaque, it should only be used
590  * with the following functions.
591  *
592  * All of the <function>g_static_rec_mutex_*</function> functions can
593  * be used even if g_thread_init() has not been called. Then they do
594  * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
595  * which does nothing but returning %TRUE.
596  **/
597
598 /**
599  * G_STATIC_REC_MUTEX_INIT:
600  *
601  * A #GStaticRecMutex must be initialized with this macro before it can
602  * be used. This macro can used be to initialize a variable, but it
603  * cannot be assigned to a variable. In that case you have to use
604  * g_static_rec_mutex_init().
605  *
606  * |[
607  *   GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
608  * ]|
609  */
610
611 /**
612  * g_static_rec_mutex_init:
613  * @mutex: a #GStaticRecMutex to be initialized.
614  *
615  * A #GStaticRecMutex must be initialized with this function before it
616  * can be used. Alternatively you can initialize it with
617  * #G_STATIC_REC_MUTEX_INIT.
618  *
619  * Deprecated: 2.32: Use g_rec_mutex_init()
620  */
621 void
622 g_static_rec_mutex_init (GStaticRecMutex *mutex)
623 {
624   static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
625
626   g_return_if_fail (mutex);
627
628   *mutex = init_mutex;
629 }
630
631 /**
632  * g_static_rec_mutex_lock:
633  * @mutex: a #GStaticRecMutex to lock.
634  *
635  * Locks @mutex. If @mutex is already locked by another thread, the
636  * current thread will block until @mutex is unlocked by the other
637  * thread. If @mutex is already locked by the calling thread, this
638  * functions increases the depth of @mutex and returns immediately.
639  *
640  * Deprecated: 2.32: Use g_rec_mutex_lock()
641  */
642 void
643 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
644 {
645   GSystemThread self;
646
647   g_return_if_fail (mutex);
648
649   if (!g_thread_supported ())
650     return;
651
652   g_system_thread_self (&self);
653
654   if (g_system_thread_equal (&self, &mutex->owner))
655     {
656       mutex->depth++;
657       return;
658     }
659   g_static_mutex_lock (&mutex->mutex);
660   g_system_thread_assign (mutex->owner, self);
661   mutex->depth = 1;
662 }
663
664 /**
665  * g_static_rec_mutex_trylock:
666  * @mutex: a #GStaticRecMutex to lock.
667  * @Returns: %TRUE, if @mutex could be locked.
668  *
669  * Tries to lock @mutex. If @mutex is already locked by another thread,
670  * it immediately returns %FALSE. Otherwise it locks @mutex and returns
671  * %TRUE. If @mutex is already locked by the calling thread, this
672  * functions increases the depth of @mutex and immediately returns
673  * %TRUE.
674  *
675  * Deprecated: 2.32: Use g_rec_mutex_trylock()
676  */
677 gboolean
678 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
679 {
680   GSystemThread self;
681
682   g_return_val_if_fail (mutex, FALSE);
683
684   if (!g_thread_supported ())
685     return TRUE;
686
687   g_system_thread_self (&self);
688
689   if (g_system_thread_equal (&self, &mutex->owner))
690     {
691       mutex->depth++;
692       return TRUE;
693     }
694
695   if (!g_static_mutex_trylock (&mutex->mutex))
696     return FALSE;
697
698   g_system_thread_assign (mutex->owner, self);
699   mutex->depth = 1;
700   return TRUE;
701 }
702
703 /**
704  * g_static_rec_mutex_unlock:
705  * @mutex: a #GStaticRecMutex to unlock.
706  *
707  * Unlocks @mutex. Another thread will be allowed to lock @mutex only
708  * when it has been unlocked as many times as it had been locked
709  * before. If @mutex is completely unlocked and another thread is
710  * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
711  * woken and can lock @mutex itself.
712  *
713  * Deprecated: 2.32: Use g_rec_mutex_unlock()
714  */
715 void
716 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
717 {
718   g_return_if_fail (mutex);
719
720   if (!g_thread_supported ())
721     return;
722
723   if (mutex->depth > 1)
724     {
725       mutex->depth--;
726       return;
727     }
728   g_system_thread_assign (mutex->owner, zero_thread);
729   g_static_mutex_unlock (&mutex->mutex);
730 }
731
732 /**
733  * g_static_rec_mutex_lock_full:
734  * @mutex: a #GStaticRecMutex to lock.
735  * @depth: number of times this mutex has to be unlocked to be
736  *         completely unlocked.
737  *
738  * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
739  *
740  * Deprecated: 2.32: Use g_rec_mutex_lock()
741  */
742 void
743 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
744                               guint            depth)
745 {
746   GSystemThread self;
747   g_return_if_fail (mutex);
748
749   if (!g_thread_supported ())
750     return;
751
752   if (depth == 0)
753     return;
754
755   g_system_thread_self (&self);
756
757   if (g_system_thread_equal (&self, &mutex->owner))
758     {
759       mutex->depth += depth;
760       return;
761     }
762   g_static_mutex_lock (&mutex->mutex);
763   g_system_thread_assign (mutex->owner, self);
764   mutex->depth = depth;
765 }
766
767 /**
768  * g_static_rec_mutex_unlock_full:
769  * @mutex: a #GStaticRecMutex to completely unlock.
770  * @Returns: number of times @mutex has been locked by the current
771  *           thread.
772  *
773  * Completely unlocks @mutex. If another thread is blocked in a
774  * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
775  * lock @mutex itself. This function returns the number of times that
776  * @mutex has been locked by the current thread. To restore the state
777  * before the call to g_static_rec_mutex_unlock_full() you can call
778  * g_static_rec_mutex_lock_full() with the depth returned by this
779  * function.
780  *
781  * Deprecated: 2.32: Use g_rec_mutex_unlock()
782  */
783 guint
784 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
785 {
786   guint depth;
787
788   g_return_val_if_fail (mutex, 0);
789
790   if (!g_thread_supported ())
791     return 1;
792
793   depth = mutex->depth;
794
795   g_system_thread_assign (mutex->owner, zero_thread);
796   mutex->depth = 0;
797   g_static_mutex_unlock (&mutex->mutex);
798
799   return depth;
800 }
801
802 /**
803  * g_static_rec_mutex_free:
804  * @mutex: a #GStaticRecMutex to be freed.
805  *
806  * Releases all resources allocated to a #GStaticRecMutex.
807  *
808  * You don't have to call this functions for a #GStaticRecMutex with an
809  * unbounded lifetime, i.e. objects declared 'static', but if you have
810  * a #GStaticRecMutex as a member of a structure and the structure is
811  * freed, you should also free the #GStaticRecMutex.
812  *
813  * Deprecated: 2.32: Use g_rec_mutex_clear()
814  */
815 void
816 g_static_rec_mutex_free (GStaticRecMutex *mutex)
817 {
818   g_return_if_fail (mutex);
819
820   g_static_mutex_free (&mutex->mutex);
821 }
822
823 /* GStaticRWLock {{{1 ----------------------------------------------------- */
824
825 /**
826  * GStaticRWLock:
827  *
828  * The #GStaticRWLock struct represents a read-write lock. A read-write
829  * lock can be used for protecting data that some portions of code only
830  * read from, while others also write. In such situations it is
831  * desirable that several readers can read at once, whereas of course
832  * only one writer may write at a time. Take a look at the following
833  * example:
834  *
835  * <example>
836  *  <title>An array with access functions</title>
837  *  <programlisting>
838  *   GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
839  *   GPtrArray *array;
840  *
841  *   gpointer
842  *   my_array_get (guint index)
843  *   {
844  *     gpointer retval = NULL;
845  *
846  *     if (!array)
847  *       return NULL;
848  *
849  *     g_static_rw_lock_reader_lock (&amp;rwlock);
850  *     if (index &lt; array->len)
851  *       retval = g_ptr_array_index (array, index);
852  *     g_static_rw_lock_reader_unlock (&amp;rwlock);
853  *
854  *     return retval;
855  *   }
856  *
857  *   void
858  *   my_array_set (guint index, gpointer data)
859  *   {
860  *     g_static_rw_lock_writer_lock (&amp;rwlock);
861  *
862  *     if (!array)
863  *       array = g_ptr_array_new (<!-- -->);
864  *
865  *     if (index >= array->len)
866  *       g_ptr_array_set_size (array, index+1);
867  *     g_ptr_array_index (array, index) = data;
868  *
869  *     g_static_rw_lock_writer_unlock (&amp;rwlock);
870  *   }
871  *  </programlisting>
872  * </example>
873  *
874  * This example shows an array which can be accessed by many readers
875  * (the <function>my_array_get()</function> function) simultaneously,
876  * whereas the writers (the <function>my_array_set()</function>
877  * function) will only be allowed once at a time and only if no readers
878  * currently access the array. This is because of the potentially
879  * dangerous resizing of the array. Using these functions is fully
880  * multi-thread safe now.
881  *
882  * Most of the time, writers should have precedence over readers. That
883  * means, for this implementation, that as soon as a writer wants to
884  * lock the data, no other reader is allowed to lock the data, whereas,
885  * of course, the readers that already have locked the data are allowed
886  * to finish their operation. As soon as the last reader unlocks the
887  * data, the writer will lock it.
888  *
889  * Even though #GStaticRWLock is not opaque, it should only be used
890  * with the following functions.
891  *
892  * All of the <function>g_static_rw_lock_*</function> functions can be
893  * used even if g_thread_init() has not been called. Then they do
894  * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
895  * which does nothing but returning %TRUE.
896  *
897  * <note><para>A read-write lock has a higher overhead than a mutex. For
898  * example, both g_static_rw_lock_reader_lock() and
899  * g_static_rw_lock_reader_unlock() have to lock and unlock a
900  * #GStaticMutex, so it takes at least twice the time to lock and unlock
901  * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
902  * only data structures that are accessed by multiple readers, and which
903  * keep the lock for a considerable time justify a #GStaticRWLock. The
904  * above example most probably would fare better with a
905  * #GStaticMutex.</para></note>
906  *
907  * Deprecated: 2.32: Use a #GRWLock instead
908  **/
909
910 /**
911  * G_STATIC_RW_LOCK_INIT:
912  *
913  * A #GStaticRWLock must be initialized with this macro before it can
914  * be used. This macro can used be to initialize a variable, but it
915  * cannot be assigned to a variable. In that case you have to use
916  * g_static_rw_lock_init().
917  *
918  * |[
919  *   GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
920  * ]|
921  */
922
923 /**
924  * g_static_rw_lock_init:
925  * @lock: a #GStaticRWLock to be initialized.
926  *
927  * A #GStaticRWLock must be initialized with this function before it
928  * can be used. Alternatively you can initialize it with
929  * #G_STATIC_RW_LOCK_INIT.
930  *
931  * Deprecated: 2.32: Use g_rw_lock_init() instead
932  */
933 void
934 g_static_rw_lock_init (GStaticRWLock* lock)
935 {
936   static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
937
938   g_return_if_fail (lock);
939
940   *lock = init_lock;
941 }
942
943 inline static void
944 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
945 {
946   if (!*cond)
947       *cond = g_cond_new ();
948   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
949 }
950
951 inline static void
952 g_static_rw_lock_signal (GStaticRWLock* lock)
953 {
954   if (lock->want_to_write && lock->write_cond)
955     g_cond_signal (lock->write_cond);
956   else if (lock->want_to_read && lock->read_cond)
957     g_cond_broadcast (lock->read_cond);
958 }
959
960 /**
961  * g_static_rw_lock_reader_lock:
962  * @lock: a #GStaticRWLock to lock for reading.
963  *
964  * Locks @lock for reading. There may be unlimited concurrent locks for
965  * reading of a #GStaticRWLock at the same time.  If @lock is already
966  * locked for writing by another thread or if another thread is already
967  * waiting to lock @lock for writing, this function will block until
968  * @lock is unlocked by the other writing thread and no other writing
969  * threads want to lock @lock. This lock has to be unlocked by
970  * g_static_rw_lock_reader_unlock().
971  *
972  * #GStaticRWLock is not recursive. It might seem to be possible to
973  * recursively lock for reading, but that can result in a deadlock, due
974  * to writer preference.
975  *
976  * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
977  */
978 void
979 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
980 {
981   g_return_if_fail (lock);
982
983   if (!g_threads_got_initialized)
984     return;
985
986   g_static_mutex_lock (&lock->mutex);
987   lock->want_to_read++;
988   while (lock->have_writer || lock->want_to_write)
989     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
990   lock->want_to_read--;
991   lock->read_counter++;
992   g_static_mutex_unlock (&lock->mutex);
993 }
994
995 /**
996  * g_static_rw_lock_reader_trylock:
997  * @lock: a #GStaticRWLock to lock for reading.
998  * @Returns: %TRUE, if @lock could be locked for reading.
999  *
1000  * Tries to lock @lock for reading. If @lock is already locked for
1001  * writing by another thread or if another thread is already waiting to
1002  * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1003  * @lock for reading and returns %TRUE. This lock has to be unlocked by
1004  * g_static_rw_lock_reader_unlock().
1005  *
1006  * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
1007  */
1008 gboolean
1009 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1010 {
1011   gboolean ret_val = FALSE;
1012
1013   g_return_val_if_fail (lock, FALSE);
1014
1015   if (!g_threads_got_initialized)
1016     return TRUE;
1017
1018   g_static_mutex_lock (&lock->mutex);
1019   if (!lock->have_writer && !lock->want_to_write)
1020     {
1021       lock->read_counter++;
1022       ret_val = TRUE;
1023     }
1024   g_static_mutex_unlock (&lock->mutex);
1025   return ret_val;
1026 }
1027
1028 /**
1029  * g_static_rw_lock_reader_unlock:
1030  * @lock: a #GStaticRWLock to unlock after reading.
1031  *
1032  * Unlocks @lock. If a thread waits to lock @lock for writing and all
1033  * locks for reading have been unlocked, the waiting thread is woken up
1034  * and can lock @lock for writing.
1035  *
1036  * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1037  */
1038 void
1039 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
1040 {
1041   g_return_if_fail (lock);
1042
1043   if (!g_threads_got_initialized)
1044     return;
1045
1046   g_static_mutex_lock (&lock->mutex);
1047   lock->read_counter--;
1048   if (lock->read_counter == 0)
1049     g_static_rw_lock_signal (lock);
1050   g_static_mutex_unlock (&lock->mutex);
1051 }
1052
1053 /**
1054  * g_static_rw_lock_writer_lock:
1055  * @lock: a #GStaticRWLock to lock for writing.
1056  *
1057  * Locks @lock for writing. If @lock is already locked for writing or
1058  * reading by other threads, this function will block until @lock is
1059  * completely unlocked and then lock @lock for writing. While this
1060  * functions waits to lock @lock, no other thread can lock @lock for
1061  * reading. When @lock is locked for writing, no other thread can lock
1062  * @lock (neither for reading nor writing). This lock has to be
1063  * unlocked by g_static_rw_lock_writer_unlock().
1064  *
1065  * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1066  */
1067 void
1068 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1069 {
1070   g_return_if_fail (lock);
1071
1072   if (!g_threads_got_initialized)
1073     return;
1074
1075   g_static_mutex_lock (&lock->mutex);
1076   lock->want_to_write++;
1077   while (lock->have_writer || lock->read_counter)
1078     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1079   lock->want_to_write--;
1080   lock->have_writer = TRUE;
1081   g_static_mutex_unlock (&lock->mutex);
1082 }
1083
1084 /**
1085  * g_static_rw_lock_writer_trylock:
1086  * @lock: a #GStaticRWLock to lock for writing.
1087  * @Returns: %TRUE, if @lock could be locked for writing.
1088  *
1089  * Tries to lock @lock for writing. If @lock is already locked (for
1090  * either reading or writing) by another thread, it immediately returns
1091  * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1092  * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1093  *
1094  * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1095  */
1096 gboolean
1097 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1098 {
1099   gboolean ret_val = FALSE;
1100
1101   g_return_val_if_fail (lock, FALSE);
1102
1103   if (!g_threads_got_initialized)
1104     return TRUE;
1105
1106   g_static_mutex_lock (&lock->mutex);
1107   if (!lock->have_writer && !lock->read_counter)
1108     {
1109       lock->have_writer = TRUE;
1110       ret_val = TRUE;
1111     }
1112   g_static_mutex_unlock (&lock->mutex);
1113   return ret_val;
1114 }
1115
1116 /**
1117  * g_static_rw_lock_writer_unlock:
1118  * @lock: a #GStaticRWLock to unlock after writing.
1119  *
1120  * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1121  * all locks for reading have been unlocked, the waiting thread is
1122  * woken up and can lock @lock for writing. If no thread is waiting to
1123  * lock @lock for writing, and some thread or threads are waiting to
1124  * lock @lock for reading, the waiting threads are woken up and can
1125  * lock @lock for reading.
1126  *
1127  * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1128  */
1129 void
1130 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1131 {
1132   g_return_if_fail (lock);
1133
1134   if (!g_threads_got_initialized)
1135     return;
1136
1137   g_static_mutex_lock (&lock->mutex);
1138   lock->have_writer = FALSE;
1139   g_static_rw_lock_signal (lock);
1140   g_static_mutex_unlock (&lock->mutex);
1141 }
1142
1143 /**
1144  * g_static_rw_lock_free:
1145  * @lock: a #GStaticRWLock to be freed.
1146  *
1147  * Releases all resources allocated to @lock.
1148  *
1149  * You don't have to call this functions for a #GStaticRWLock with an
1150  * unbounded lifetime, i.e. objects declared 'static', but if you have
1151  * a #GStaticRWLock as a member of a structure, and the structure is
1152  * freed, you should also free the #GStaticRWLock.
1153  *
1154  * Deprecated: 2.32: Use a #GRWLock instead
1155  */
1156 void
1157 g_static_rw_lock_free (GStaticRWLock* lock)
1158 {
1159   g_return_if_fail (lock);
1160
1161   if (lock->read_cond)
1162     {
1163       g_cond_free (lock->read_cond);
1164       lock->read_cond = NULL;
1165     }
1166   if (lock->write_cond)
1167     {
1168       g_cond_free (lock->write_cond);
1169       lock->write_cond = NULL;
1170     }
1171   g_static_mutex_free (&lock->mutex);
1172 }
1173
1174 /* GPrivate {{{1 ------------------------------------------------------ */
1175
1176 /**
1177  * g_private_new:
1178  * @notify: a #GDestroyNotify
1179  *
1180  * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea.  Use
1181  *                  static storage and G_PRIVATE_INIT() instead.
1182  *
1183  * Returns: a newly allocated #GPrivate (which can never be destroyed)
1184  */
1185 GPrivate *
1186 g_private_new (GDestroyNotify notify)
1187 {
1188   GPrivate tmp = G_PRIVATE_INIT (notify);
1189   GPrivate *key;
1190
1191   key = g_slice_new (GPrivate);
1192   *key = tmp;
1193
1194   return key;
1195 }
1196
1197 /* {{{1 GStaticPrivate */
1198
1199 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1200 struct _GStaticPrivateNode
1201 {
1202   gpointer        data;
1203   GDestroyNotify  destroy;
1204   GStaticPrivate *owner;
1205 };
1206
1207 /**
1208  * GStaticPrivate:
1209  *
1210  * A #GStaticPrivate works almost like a #GPrivate, but it has one
1211  * significant advantage. It doesn't need to be created at run-time
1212  * like a #GPrivate, but can be defined at compile-time. This is
1213  * similar to the difference between #GMutex and #GStaticMutex. Now
1214  * look at our <function>give_me_next_number()</function> example with
1215  * #GStaticPrivate:
1216  *
1217  * <example>
1218  *  <title>Using GStaticPrivate for per-thread data</title>
1219  *  <programlisting>
1220  *   int
1221  *   give_me_next_number (<!-- -->)
1222  *   {
1223  *     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1224  *     int *current_number = g_static_private_get (&amp;current_number_key);
1225  *
1226  *     if (!current_number)
1227  *       {
1228  *         current_number = g_new (int,1);
1229  *         *current_number = 0;
1230  *         g_static_private_set (&amp;current_number_key, current_number, g_free);
1231  *       }
1232  *
1233  *     *current_number = calc_next_number (*current_number);
1234  *
1235  *     return *current_number;
1236  *   }
1237  *  </programlisting>
1238  * </example>
1239  */
1240
1241 /**
1242  * G_STATIC_PRIVATE_INIT:
1243  *
1244  * Every #GStaticPrivate must be initialized with this macro, before it
1245  * can be used.
1246  *
1247  * |[
1248  *   GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1249  * ]|
1250  */
1251
1252 /**
1253  * g_static_private_init:
1254  * @private_key: a #GStaticPrivate to be initialized
1255  *
1256  * Initializes @private_key. Alternatively you can initialize it with
1257  * #G_STATIC_PRIVATE_INIT.
1258  */
1259 void
1260 g_static_private_init (GStaticPrivate *private_key)
1261 {
1262   private_key->index = 0;
1263 }
1264
1265 /**
1266  * g_static_private_get:
1267  * @private_key: a #GStaticPrivate
1268  *
1269  * Works like g_private_get() only for a #GStaticPrivate.
1270  *
1271  * This function works even if g_thread_init() has not yet been called.
1272  *
1273  * Returns: the corresponding pointer
1274  */
1275 gpointer
1276 g_static_private_get (GStaticPrivate *private_key)
1277 {
1278   GRealThread *self = (GRealThread*) g_thread_self ();
1279   GArray *array;
1280   gpointer ret = NULL;
1281   array = self->private_data;
1282
1283   if (array && private_key->index != 0 && private_key->index <= array->len)
1284     {
1285       GStaticPrivateNode *node;
1286
1287       node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1288
1289       /* Deal with the possibility that the GStaticPrivate which used
1290        * to have this index got freed and the index got allocated to
1291        * a new one. In this case, the data in the node is stale, so
1292        * free it and return NULL.
1293        */
1294       if (G_UNLIKELY (node->owner != private_key))
1295         {
1296           if (node->destroy)
1297             node->destroy (node->data);
1298           node->destroy = NULL;
1299           node->data = NULL;
1300           node->owner = NULL;
1301         }
1302       ret = node->data;
1303     }
1304
1305   return ret;
1306 }
1307
1308 /**
1309  * g_static_private_set:
1310  * @private_key: a #GStaticPrivate
1311  * @data: the new pointer
1312  * @notify: a function to be called with the pointer whenever the
1313  *     current thread ends or sets this pointer again
1314  *
1315  * Sets the pointer keyed to @private_key for the current thread and
1316  * the function @notify to be called with that pointer (%NULL or
1317  * non-%NULL), whenever the pointer is set again or whenever the
1318  * current thread ends.
1319  *
1320  * This function works even if g_thread_init() has not yet been called.
1321  * If g_thread_init() is called later, the @data keyed to @private_key
1322  * will be inherited only by the main thread, i.e. the one that called
1323  * g_thread_init().
1324  *
1325  * <note><para>@notify is used quite differently from @destructor in
1326  * g_private_new().</para></note>
1327  */
1328 void
1329 g_static_private_set (GStaticPrivate *private_key,
1330                       gpointer        data,
1331                       GDestroyNotify  notify)
1332 {
1333   GRealThread *self = (GRealThread*) g_thread_self ();
1334   GArray *array;
1335   static guint next_index = 0;
1336   GStaticPrivateNode *node;
1337
1338   if (!private_key->index)
1339     {
1340       G_LOCK (g_thread);
1341
1342       if (!private_key->index)
1343         {
1344           if (g_thread_free_indices)
1345             {
1346               private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1347               g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1348                                                            g_thread_free_indices);
1349             }
1350           else
1351             private_key->index = ++next_index;
1352         }
1353
1354       G_UNLOCK (g_thread);
1355     }
1356
1357   array = self->private_data;
1358   if (!array)
1359     {
1360       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1361       self->private_data = array;
1362     }
1363   if (private_key->index > array->len)
1364     g_array_set_size (array, private_key->index);
1365
1366   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1367
1368   if (node->destroy)
1369     node->destroy (node->data);
1370
1371   node->data = data;
1372   node->destroy = notify;
1373   node->owner = private_key;
1374 }
1375
1376 /**
1377  * g_static_private_free:
1378  * @private_key: a #GStaticPrivate to be freed
1379  *
1380  * Releases all resources allocated to @private_key.
1381  *
1382  * You don't have to call this functions for a #GStaticPrivate with an
1383  * unbounded lifetime, i.e. objects declared 'static', but if you have
1384  * a #GStaticPrivate as a member of a structure and the structure is
1385  * freed, you should also free the #GStaticPrivate.
1386  */
1387 void
1388 g_static_private_free (GStaticPrivate *private_key)
1389 {
1390   guint idx = private_key->index;
1391
1392   if (!idx)
1393     return;
1394
1395   private_key->index = 0;
1396
1397   /* Freeing the per-thread data is deferred to either the
1398    * thread end or the next g_static_private_get() call for
1399    * the same index.
1400    */
1401   G_LOCK (g_thread);
1402   g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1403                                            GUINT_TO_POINTER (idx));
1404   G_UNLOCK (g_thread);
1405 }
1406
1407 void
1408 g_static_private_cleanup (GRealThread *thread)
1409 {
1410   GArray *array;
1411
1412   array = thread->private_data;
1413   thread->private_data = NULL;
1414
1415   if (array)
1416     {
1417       guint i;
1418
1419       for (i = 0; i < array->len; i++ )
1420         {
1421           GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1422           if (node->destroy)
1423             node->destroy (node->data);
1424         }
1425       g_array_free (array, TRUE);
1426     }
1427 }
1428
1429 /* GMutex {{{1 ------------------------------------------------------ */
1430
1431 /**
1432  * g_mutex_new:
1433  *
1434  * Allocates and initializes a new #GMutex.
1435  *
1436  * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1437  *
1438  * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1439  * in structures and initialised with g_mutex_init().
1440  */
1441 GMutex *
1442 g_mutex_new (void)
1443 {
1444   GMutex *mutex;
1445
1446   mutex = g_slice_new (GMutex);
1447   g_mutex_init (mutex);
1448
1449   return mutex;
1450 }
1451
1452 /**
1453  * g_mutex_free:
1454  * @mutex: a #GMutex
1455  *
1456  * Destroys a @mutex that has been created with g_mutex_new().
1457  *
1458  * Calling g_mutex_free() on a locked mutex may result
1459  * in undefined behaviour.
1460  *
1461  * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1462  * in structures and initialised with g_mutex_init().
1463  */
1464 void
1465 g_mutex_free (GMutex *mutex)
1466 {
1467   g_mutex_clear (mutex);
1468   g_slice_free (GMutex, mutex);
1469 }
1470
1471 /* GCond {{{1 ------------------------------------------------------ */
1472
1473 /**
1474  * g_cond_new:
1475  *
1476  * Allocates and initializes a new #GCond.
1477  *
1478  * Returns: a newly allocated #GCond. Free with g_cond_free()
1479  *
1480  * Deprecated:3.32:GCond can now be statically allocated, or embedded
1481  * in structures and initialised with g_cond_init().
1482  */
1483 GCond *
1484 g_cond_new (void)
1485 {
1486   GCond *cond;
1487
1488   cond = g_slice_new (GCond);
1489   g_cond_init (cond);
1490
1491   return cond;
1492 }
1493
1494 /**
1495  * g_cond_free:
1496  * @cond: a #GCond
1497  *
1498  * Destroys a #GCond that has been created with g_cond_new().
1499  *
1500  * Calling g_cond_free() for a #GCond on which threads are
1501  * blocking leads to undefined behaviour.
1502  *
1503  * Deprecated:3.32:GCond can now be statically allocated, or embedded
1504  * in structures and initialised with g_cond_init().
1505  */
1506 void
1507 g_cond_free (GCond *cond)
1508 {
1509   g_cond_clear (cond);
1510   g_slice_free (GCond, cond);
1511 }
1512
1513 /* {{{1 Epilogue */
1514 /* vim: set foldmethod=marker: */