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