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