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