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