thread: use GSList for g_thread_foreach list
[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 /**
643  * g_static_rec_mutex_lock:
644  * @mutex: a #GStaticRecMutex to lock.
645  *
646  * Locks @mutex. If @mutex is already locked by another thread, the
647  * current thread will block until @mutex is unlocked by the other
648  * thread. If @mutex is already locked by the calling thread, this
649  * functions increases the depth of @mutex and returns immediately.
650  *
651  * Deprecated: 2.32: Use g_rec_mutex_lock()
652  */
653 void
654 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
655 {
656   GSystemThread self;
657
658   g_return_if_fail (mutex);
659
660   if (!g_thread_supported ())
661     return;
662
663   g_system_thread_self (&self);
664
665   if (g_system_thread_equal (&self, &mutex->owner))
666     {
667       mutex->depth++;
668       return;
669     }
670   g_static_mutex_lock (&mutex->mutex);
671   g_system_thread_assign (mutex->owner, self);
672   mutex->depth = 1;
673 }
674
675 /**
676  * g_static_rec_mutex_trylock:
677  * @mutex: a #GStaticRecMutex to lock.
678  * @Returns: %TRUE, if @mutex could be locked.
679  *
680  * Tries to lock @mutex. If @mutex is already locked by another thread,
681  * it immediately returns %FALSE. Otherwise it locks @mutex and returns
682  * %TRUE. If @mutex is already locked by the calling thread, this
683  * functions increases the depth of @mutex and immediately returns
684  * %TRUE.
685  *
686  * Deprecated: 2.32: Use g_rec_mutex_trylock()
687  */
688 gboolean
689 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
690 {
691   GSystemThread self;
692
693   g_return_val_if_fail (mutex, FALSE);
694
695   if (!g_thread_supported ())
696     return TRUE;
697
698   g_system_thread_self (&self);
699
700   if (g_system_thread_equal (&self, &mutex->owner))
701     {
702       mutex->depth++;
703       return TRUE;
704     }
705
706   if (!g_static_mutex_trylock (&mutex->mutex))
707     return FALSE;
708
709   g_system_thread_assign (mutex->owner, self);
710   mutex->depth = 1;
711   return TRUE;
712 }
713
714 /**
715  * g_static_rec_mutex_unlock:
716  * @mutex: a #GStaticRecMutex to unlock.
717  *
718  * Unlocks @mutex. Another thread will be allowed to lock @mutex only
719  * when it has been unlocked as many times as it had been locked
720  * before. If @mutex is completely unlocked and another thread is
721  * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
722  * woken and can lock @mutex itself.
723  *
724  * Deprecated: 2.32: Use g_rec_mutex_unlock()
725  */
726 void
727 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
728 {
729   g_return_if_fail (mutex);
730
731   if (!g_thread_supported ())
732     return;
733
734   if (mutex->depth > 1)
735     {
736       mutex->depth--;
737       return;
738     }
739   g_system_thread_assign (mutex->owner, zero_thread);
740   g_static_mutex_unlock (&mutex->mutex);
741 }
742
743 /**
744  * g_static_rec_mutex_lock_full:
745  * @mutex: a #GStaticRecMutex to lock.
746  * @depth: number of times this mutex has to be unlocked to be
747  *         completely unlocked.
748  *
749  * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
750  *
751  * Deprecated: 2.32: Use g_rec_mutex_lock()
752  */
753 void
754 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
755                               guint            depth)
756 {
757   GSystemThread self;
758   g_return_if_fail (mutex);
759
760   if (!g_thread_supported ())
761     return;
762
763   if (depth == 0)
764     return;
765
766   g_system_thread_self (&self);
767
768   if (g_system_thread_equal (&self, &mutex->owner))
769     {
770       mutex->depth += depth;
771       return;
772     }
773   g_static_mutex_lock (&mutex->mutex);
774   g_system_thread_assign (mutex->owner, self);
775   mutex->depth = depth;
776 }
777
778 /**
779  * g_static_rec_mutex_unlock_full:
780  * @mutex: a #GStaticRecMutex to completely unlock.
781  * @Returns: number of times @mutex has been locked by the current
782  *           thread.
783  *
784  * Completely unlocks @mutex. If another thread is blocked in a
785  * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
786  * lock @mutex itself. This function returns the number of times that
787  * @mutex has been locked by the current thread. To restore the state
788  * before the call to g_static_rec_mutex_unlock_full() you can call
789  * g_static_rec_mutex_lock_full() with the depth returned by this
790  * function.
791  *
792  * Deprecated: 2.32: Use g_rec_mutex_unlock()
793  */
794 guint
795 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
796 {
797   guint depth;
798
799   g_return_val_if_fail (mutex, 0);
800
801   if (!g_thread_supported ())
802     return 1;
803
804   depth = mutex->depth;
805
806   g_system_thread_assign (mutex->owner, zero_thread);
807   mutex->depth = 0;
808   g_static_mutex_unlock (&mutex->mutex);
809
810   return depth;
811 }
812
813 /**
814  * g_static_rec_mutex_free:
815  * @mutex: a #GStaticRecMutex to be freed.
816  *
817  * Releases all resources allocated to a #GStaticRecMutex.
818  *
819  * You don't have to call this functions for a #GStaticRecMutex with an
820  * unbounded lifetime, i.e. objects declared 'static', but if you have
821  * a #GStaticRecMutex as a member of a structure and the structure is
822  * freed, you should also free the #GStaticRecMutex.
823  *
824  * Deprecated: 2.32: Use g_rec_mutex_clear()
825  */
826 void
827 g_static_rec_mutex_free (GStaticRecMutex *mutex)
828 {
829   g_return_if_fail (mutex);
830
831   g_static_mutex_free (&mutex->mutex);
832 }
833
834 /* GStaticRWLock {{{1 ----------------------------------------------------- */
835
836 /**
837  * GStaticRWLock:
838  *
839  * The #GStaticRWLock struct represents a read-write lock. A read-write
840  * lock can be used for protecting data that some portions of code only
841  * read from, while others also write. In such situations it is
842  * desirable that several readers can read at once, whereas of course
843  * only one writer may write at a time. Take a look at the following
844  * example:
845  *
846  * <example>
847  *  <title>An array with access functions</title>
848  *  <programlisting>
849  *   GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
850  *   GPtrArray *array;
851  *
852  *   gpointer
853  *   my_array_get (guint index)
854  *   {
855  *     gpointer retval = NULL;
856  *
857  *     if (!array)
858  *       return NULL;
859  *
860  *     g_static_rw_lock_reader_lock (&amp;rwlock);
861  *     if (index &lt; array->len)
862  *       retval = g_ptr_array_index (array, index);
863  *     g_static_rw_lock_reader_unlock (&amp;rwlock);
864  *
865  *     return retval;
866  *   }
867  *
868  *   void
869  *   my_array_set (guint index, gpointer data)
870  *   {
871  *     g_static_rw_lock_writer_lock (&amp;rwlock);
872  *
873  *     if (!array)
874  *       array = g_ptr_array_new (<!-- -->);
875  *
876  *     if (index >= array->len)
877  *       g_ptr_array_set_size (array, index+1);
878  *     g_ptr_array_index (array, index) = data;
879  *
880  *     g_static_rw_lock_writer_unlock (&amp;rwlock);
881  *   }
882  *  </programlisting>
883  * </example>
884  *
885  * This example shows an array which can be accessed by many readers
886  * (the <function>my_array_get()</function> function) simultaneously,
887  * whereas the writers (the <function>my_array_set()</function>
888  * function) will only be allowed once at a time and only if no readers
889  * currently access the array. This is because of the potentially
890  * dangerous resizing of the array. Using these functions is fully
891  * multi-thread safe now.
892  *
893  * Most of the time, writers should have precedence over readers. That
894  * means, for this implementation, that as soon as a writer wants to
895  * lock the data, no other reader is allowed to lock the data, whereas,
896  * of course, the readers that already have locked the data are allowed
897  * to finish their operation. As soon as the last reader unlocks the
898  * data, the writer will lock it.
899  *
900  * Even though #GStaticRWLock is not opaque, it should only be used
901  * with the following functions.
902  *
903  * All of the <function>g_static_rw_lock_*</function> functions can be
904  * used even if g_thread_init() has not been called. Then they do
905  * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
906  * which does nothing but returning %TRUE.
907  *
908  * <note><para>A read-write lock has a higher overhead than a mutex. For
909  * example, both g_static_rw_lock_reader_lock() and
910  * g_static_rw_lock_reader_unlock() have to lock and unlock a
911  * #GStaticMutex, so it takes at least twice the time to lock and unlock
912  * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
913  * only data structures that are accessed by multiple readers, and which
914  * keep the lock for a considerable time justify a #GStaticRWLock. The
915  * above example most probably would fare better with a
916  * #GStaticMutex.</para></note>
917  *
918  * Deprecated: 2.32: Use a #GRWLock instead
919  **/
920
921 /**
922  * G_STATIC_RW_LOCK_INIT:
923  *
924  * A #GStaticRWLock must be initialized with this macro before it can
925  * be used. This macro can used be to initialize a variable, but it
926  * cannot be assigned to a variable. In that case you have to use
927  * g_static_rw_lock_init().
928  *
929  * |[
930  *   GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
931  * ]|
932  */
933
934 /**
935  * g_static_rw_lock_init:
936  * @lock: a #GStaticRWLock to be initialized.
937  *
938  * A #GStaticRWLock must be initialized with this function before it
939  * can be used. Alternatively you can initialize it with
940  * #G_STATIC_RW_LOCK_INIT.
941  *
942  * Deprecated: 2.32: Use g_rw_lock_init() instead
943  */
944 void
945 g_static_rw_lock_init (GStaticRWLock* lock)
946 {
947   static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
948
949   g_return_if_fail (lock);
950
951   *lock = init_lock;
952 }
953
954 inline static void
955 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
956 {
957   if (!*cond)
958       *cond = g_cond_new ();
959   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
960 }
961
962 inline static void
963 g_static_rw_lock_signal (GStaticRWLock* lock)
964 {
965   if (lock->want_to_write && lock->write_cond)
966     g_cond_signal (lock->write_cond);
967   else if (lock->want_to_read && lock->read_cond)
968     g_cond_broadcast (lock->read_cond);
969 }
970
971 /**
972  * g_static_rw_lock_reader_lock:
973  * @lock: a #GStaticRWLock to lock for reading.
974  *
975  * Locks @lock for reading. There may be unlimited concurrent locks for
976  * reading of a #GStaticRWLock at the same time.  If @lock is already
977  * locked for writing by another thread or if another thread is already
978  * waiting to lock @lock for writing, this function will block until
979  * @lock is unlocked by the other writing thread and no other writing
980  * threads want to lock @lock. This lock has to be unlocked by
981  * g_static_rw_lock_reader_unlock().
982  *
983  * #GStaticRWLock is not recursive. It might seem to be possible to
984  * recursively lock for reading, but that can result in a deadlock, due
985  * to writer preference.
986  *
987  * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
988  */
989 void
990 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
991 {
992   g_return_if_fail (lock);
993
994   if (!g_threads_got_initialized)
995     return;
996
997   g_static_mutex_lock (&lock->mutex);
998   lock->want_to_read++;
999   while (lock->have_writer || lock->want_to_write)
1000     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
1001   lock->want_to_read--;
1002   lock->read_counter++;
1003   g_static_mutex_unlock (&lock->mutex);
1004 }
1005
1006 /**
1007  * g_static_rw_lock_reader_trylock:
1008  * @lock: a #GStaticRWLock to lock for reading.
1009  * @Returns: %TRUE, if @lock could be locked for reading.
1010  *
1011  * Tries to lock @lock for reading. If @lock is already locked for
1012  * writing by another thread or if another thread is already waiting to
1013  * lock @lock for writing, immediately returns %FALSE. Otherwise locks
1014  * @lock for reading and returns %TRUE. This lock has to be unlocked by
1015  * g_static_rw_lock_reader_unlock().
1016  *
1017  * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
1018  */
1019 gboolean
1020 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
1021 {
1022   gboolean ret_val = FALSE;
1023
1024   g_return_val_if_fail (lock, FALSE);
1025
1026   if (!g_threads_got_initialized)
1027     return TRUE;
1028
1029   g_static_mutex_lock (&lock->mutex);
1030   if (!lock->have_writer && !lock->want_to_write)
1031     {
1032       lock->read_counter++;
1033       ret_val = TRUE;
1034     }
1035   g_static_mutex_unlock (&lock->mutex);
1036   return ret_val;
1037 }
1038
1039 /**
1040  * g_static_rw_lock_reader_unlock:
1041  * @lock: a #GStaticRWLock to unlock after reading.
1042  *
1043  * Unlocks @lock. If a thread waits to lock @lock for writing and all
1044  * locks for reading have been unlocked, the waiting thread is woken up
1045  * and can lock @lock for writing.
1046  *
1047  * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
1048  */
1049 void
1050 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
1051 {
1052   g_return_if_fail (lock);
1053
1054   if (!g_threads_got_initialized)
1055     return;
1056
1057   g_static_mutex_lock (&lock->mutex);
1058   lock->read_counter--;
1059   if (lock->read_counter == 0)
1060     g_static_rw_lock_signal (lock);
1061   g_static_mutex_unlock (&lock->mutex);
1062 }
1063
1064 /**
1065  * g_static_rw_lock_writer_lock:
1066  * @lock: a #GStaticRWLock to lock for writing.
1067  *
1068  * Locks @lock for writing. If @lock is already locked for writing or
1069  * reading by other threads, this function will block until @lock is
1070  * completely unlocked and then lock @lock for writing. While this
1071  * functions waits to lock @lock, no other thread can lock @lock for
1072  * reading. When @lock is locked for writing, no other thread can lock
1073  * @lock (neither for reading nor writing). This lock has to be
1074  * unlocked by g_static_rw_lock_writer_unlock().
1075  *
1076  * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
1077  */
1078 void
1079 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
1080 {
1081   g_return_if_fail (lock);
1082
1083   if (!g_threads_got_initialized)
1084     return;
1085
1086   g_static_mutex_lock (&lock->mutex);
1087   lock->want_to_write++;
1088   while (lock->have_writer || lock->read_counter)
1089     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
1090   lock->want_to_write--;
1091   lock->have_writer = TRUE;
1092   g_static_mutex_unlock (&lock->mutex);
1093 }
1094
1095 /**
1096  * g_static_rw_lock_writer_trylock:
1097  * @lock: a #GStaticRWLock to lock for writing.
1098  * @Returns: %TRUE, if @lock could be locked for writing.
1099  *
1100  * Tries to lock @lock for writing. If @lock is already locked (for
1101  * either reading or writing) by another thread, it immediately returns
1102  * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
1103  * lock has to be unlocked by g_static_rw_lock_writer_unlock().
1104  *
1105  * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
1106  */
1107 gboolean
1108 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
1109 {
1110   gboolean ret_val = FALSE;
1111
1112   g_return_val_if_fail (lock, FALSE);
1113
1114   if (!g_threads_got_initialized)
1115     return TRUE;
1116
1117   g_static_mutex_lock (&lock->mutex);
1118   if (!lock->have_writer && !lock->read_counter)
1119     {
1120       lock->have_writer = TRUE;
1121       ret_val = TRUE;
1122     }
1123   g_static_mutex_unlock (&lock->mutex);
1124   return ret_val;
1125 }
1126
1127 /**
1128  * g_static_rw_lock_writer_unlock:
1129  * @lock: a #GStaticRWLock to unlock after writing.
1130  *
1131  * Unlocks @lock. If a thread is waiting to lock @lock for writing and
1132  * all locks for reading have been unlocked, the waiting thread is
1133  * woken up and can lock @lock for writing. If no thread is waiting to
1134  * lock @lock for writing, and some thread or threads are waiting to
1135  * lock @lock for reading, the waiting threads are woken up and can
1136  * lock @lock for reading.
1137  *
1138  * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
1139  */
1140 void
1141 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
1142 {
1143   g_return_if_fail (lock);
1144
1145   if (!g_threads_got_initialized)
1146     return;
1147
1148   g_static_mutex_lock (&lock->mutex);
1149   lock->have_writer = FALSE;
1150   g_static_rw_lock_signal (lock);
1151   g_static_mutex_unlock (&lock->mutex);
1152 }
1153
1154 /**
1155  * g_static_rw_lock_free:
1156  * @lock: a #GStaticRWLock to be freed.
1157  *
1158  * Releases all resources allocated to @lock.
1159  *
1160  * You don't have to call this functions for a #GStaticRWLock with an
1161  * unbounded lifetime, i.e. objects declared 'static', but if you have
1162  * a #GStaticRWLock as a member of a structure, and the structure is
1163  * freed, you should also free the #GStaticRWLock.
1164  *
1165  * Deprecated: 2.32: Use a #GRWLock instead
1166  */
1167 void
1168 g_static_rw_lock_free (GStaticRWLock* lock)
1169 {
1170   g_return_if_fail (lock);
1171
1172   if (lock->read_cond)
1173     {
1174       g_cond_free (lock->read_cond);
1175       lock->read_cond = NULL;
1176     }
1177   if (lock->write_cond)
1178     {
1179       g_cond_free (lock->write_cond);
1180       lock->write_cond = NULL;
1181     }
1182   g_static_mutex_free (&lock->mutex);
1183 }
1184
1185 /* GPrivate {{{1 ------------------------------------------------------ */
1186
1187 /**
1188  * g_private_new:
1189  * @notify: a #GDestroyNotify
1190  *
1191  * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea.  Use
1192  *                  static storage and G_PRIVATE_INIT() instead.
1193  *
1194  * Returns: a newly allocated #GPrivate (which can never be destroyed)
1195  */
1196 GPrivate *
1197 g_private_new (GDestroyNotify notify)
1198 {
1199   GPrivate tmp = G_PRIVATE_INIT (notify);
1200   GPrivate *key;
1201
1202   key = g_slice_new (GPrivate);
1203   *key = tmp;
1204
1205   return key;
1206 }
1207
1208 /* {{{1 GStaticPrivate */
1209
1210 typedef struct _GStaticPrivateNode GStaticPrivateNode;
1211 struct _GStaticPrivateNode
1212 {
1213   gpointer        data;
1214   GDestroyNotify  destroy;
1215   GStaticPrivate *owner;
1216 };
1217
1218 static void
1219 g_static_private_cleanup (gpointer data)
1220 {
1221   GArray *array = data;
1222   guint i;
1223
1224   for (i = 0; i < array->len; i++ )
1225     {
1226       GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1227       if (node->destroy)
1228         node->destroy (node->data);
1229     }
1230
1231   g_array_free (array, TRUE);
1232 }
1233
1234 GPrivate static_private_private = G_PRIVATE_INIT (g_static_private_cleanup);
1235
1236 /**
1237  * GStaticPrivate:
1238  *
1239  * A #GStaticPrivate works almost like a #GPrivate, but it has one
1240  * significant advantage. It doesn't need to be created at run-time
1241  * like a #GPrivate, but can be defined at compile-time. This is
1242  * similar to the difference between #GMutex and #GStaticMutex. Now
1243  * look at our <function>give_me_next_number()</function> example with
1244  * #GStaticPrivate:
1245  *
1246  * <example>
1247  *  <title>Using GStaticPrivate for per-thread data</title>
1248  *  <programlisting>
1249  *   int
1250  *   give_me_next_number (<!-- -->)
1251  *   {
1252  *     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1253  *     int *current_number = g_static_private_get (&amp;current_number_key);
1254  *
1255  *     if (!current_number)
1256  *       {
1257  *         current_number = g_new (int,1);
1258  *         *current_number = 0;
1259  *         g_static_private_set (&amp;current_number_key, current_number, g_free);
1260  *       }
1261  *
1262  *     *current_number = calc_next_number (*current_number);
1263  *
1264  *     return *current_number;
1265  *   }
1266  *  </programlisting>
1267  * </example>
1268  */
1269
1270 /**
1271  * G_STATIC_PRIVATE_INIT:
1272  *
1273  * Every #GStaticPrivate must be initialized with this macro, before it
1274  * can be used.
1275  *
1276  * |[
1277  *   GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1278  * ]|
1279  */
1280
1281 /**
1282  * g_static_private_init:
1283  * @private_key: a #GStaticPrivate to be initialized
1284  *
1285  * Initializes @private_key. Alternatively you can initialize it with
1286  * #G_STATIC_PRIVATE_INIT.
1287  */
1288 void
1289 g_static_private_init (GStaticPrivate *private_key)
1290 {
1291   private_key->index = 0;
1292 }
1293
1294 /**
1295  * g_static_private_get:
1296  * @private_key: a #GStaticPrivate
1297  *
1298  * Works like g_private_get() only for a #GStaticPrivate.
1299  *
1300  * This function works even if g_thread_init() has not yet been called.
1301  *
1302  * Returns: the corresponding pointer
1303  */
1304 gpointer
1305 g_static_private_get (GStaticPrivate *private_key)
1306 {
1307   GArray *array;
1308   gpointer ret = NULL;
1309
1310   array = g_private_get (&static_private_private);
1311
1312   if (array && private_key->index != 0 && private_key->index <= array->len)
1313     {
1314       GStaticPrivateNode *node;
1315
1316       node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1317
1318       /* Deal with the possibility that the GStaticPrivate which used
1319        * to have this index got freed and the index got allocated to
1320        * a new one. In this case, the data in the node is stale, so
1321        * free it and return NULL.
1322        */
1323       if (G_UNLIKELY (node->owner != private_key))
1324         {
1325           if (node->destroy)
1326             node->destroy (node->data);
1327           node->destroy = NULL;
1328           node->data = NULL;
1329           node->owner = NULL;
1330         }
1331       ret = node->data;
1332     }
1333
1334   return ret;
1335 }
1336
1337 /**
1338  * g_static_private_set:
1339  * @private_key: a #GStaticPrivate
1340  * @data: the new pointer
1341  * @notify: a function to be called with the pointer whenever the
1342  *     current thread ends or sets this pointer again
1343  *
1344  * Sets the pointer keyed to @private_key for the current thread and
1345  * the function @notify to be called with that pointer (%NULL or
1346  * non-%NULL), whenever the pointer is set again or whenever the
1347  * current thread ends.
1348  *
1349  * This function works even if g_thread_init() has not yet been called.
1350  * If g_thread_init() is called later, the @data keyed to @private_key
1351  * will be inherited only by the main thread, i.e. the one that called
1352  * g_thread_init().
1353  *
1354  * <note><para>@notify is used quite differently from @destructor in
1355  * g_private_new().</para></note>
1356  */
1357 void
1358 g_static_private_set (GStaticPrivate *private_key,
1359                       gpointer        data,
1360                       GDestroyNotify  notify)
1361 {
1362   GArray *array;
1363   static guint next_index = 0;
1364   GStaticPrivateNode *node;
1365
1366   if (!private_key->index)
1367     {
1368       G_LOCK (g_thread);
1369
1370       if (!private_key->index)
1371         {
1372           if (g_thread_free_indices)
1373             {
1374               private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1375               g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1376                                                            g_thread_free_indices);
1377             }
1378           else
1379             private_key->index = ++next_index;
1380         }
1381
1382       G_UNLOCK (g_thread);
1383     }
1384
1385   array = g_private_get (&static_private_private);
1386   if (!array)
1387     {
1388       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1389       g_private_set (&static_private_private, array);
1390     }
1391   if (private_key->index > array->len)
1392     g_array_set_size (array, private_key->index);
1393
1394   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1395
1396   if (node->destroy)
1397     node->destroy (node->data);
1398
1399   node->data = data;
1400   node->destroy = notify;
1401   node->owner = private_key;
1402 }
1403
1404 /**
1405  * g_static_private_free:
1406  * @private_key: a #GStaticPrivate to be freed
1407  *
1408  * Releases all resources allocated to @private_key.
1409  *
1410  * You don't have to call this functions for a #GStaticPrivate with an
1411  * unbounded lifetime, i.e. objects declared 'static', but if you have
1412  * a #GStaticPrivate as a member of a structure and the structure is
1413  * freed, you should also free the #GStaticPrivate.
1414  */
1415 void
1416 g_static_private_free (GStaticPrivate *private_key)
1417 {
1418   guint idx = private_key->index;
1419
1420   if (!idx)
1421     return;
1422
1423   private_key->index = 0;
1424
1425   /* Freeing the per-thread data is deferred to either the
1426    * thread end or the next g_static_private_get() call for
1427    * the same index.
1428    */
1429   G_LOCK (g_thread);
1430   g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1431                                            GUINT_TO_POINTER (idx));
1432   G_UNLOCK (g_thread);
1433 }
1434
1435 /* GMutex {{{1 ------------------------------------------------------ */
1436
1437 /**
1438  * g_mutex_new:
1439  *
1440  * Allocates and initializes a new #GMutex.
1441  *
1442  * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1443  *
1444  * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1445  * in structures and initialised with g_mutex_init().
1446  */
1447 GMutex *
1448 g_mutex_new (void)
1449 {
1450   GMutex *mutex;
1451
1452   mutex = g_slice_new (GMutex);
1453   g_mutex_init (mutex);
1454
1455   return mutex;
1456 }
1457
1458 /**
1459  * g_mutex_free:
1460  * @mutex: a #GMutex
1461  *
1462  * Destroys a @mutex that has been created with g_mutex_new().
1463  *
1464  * Calling g_mutex_free() on a locked mutex may result
1465  * in undefined behaviour.
1466  *
1467  * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1468  * in structures and initialised with g_mutex_init().
1469  */
1470 void
1471 g_mutex_free (GMutex *mutex)
1472 {
1473   g_mutex_clear (mutex);
1474   g_slice_free (GMutex, mutex);
1475 }
1476
1477 /* GCond {{{1 ------------------------------------------------------ */
1478
1479 /**
1480  * g_cond_new:
1481  *
1482  * Allocates and initializes a new #GCond.
1483  *
1484  * Returns: a newly allocated #GCond. Free with g_cond_free()
1485  *
1486  * Deprecated:3.32:GCond can now be statically allocated, or embedded
1487  * in structures and initialised with g_cond_init().
1488  */
1489 GCond *
1490 g_cond_new (void)
1491 {
1492   GCond *cond;
1493
1494   cond = g_slice_new (GCond);
1495   g_cond_init (cond);
1496
1497   return cond;
1498 }
1499
1500 /**
1501  * g_cond_free:
1502  * @cond: a #GCond
1503  *
1504  * Destroys a #GCond that has been created with g_cond_new().
1505  *
1506  * Calling g_cond_free() for a #GCond on which threads are
1507  * blocking leads to undefined behaviour.
1508  *
1509  * Deprecated:3.32:GCond can now be statically allocated, or embedded
1510  * in structures and initialised with g_cond_init().
1511  */
1512 void
1513 g_cond_free (GCond *cond)
1514 {
1515   g_cond_clear (cond);
1516   g_slice_free (GCond, cond);
1517 }
1518
1519 /* {{{1 Epilogue */
1520 /* vim: set foldmethod=marker: */