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