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