Several docs cleanups
[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 #include "gmessages.h"
27 #include "gslice.h"
28 #include "gmain.h"
29 #include "gthread.h"
30 #include "gthreadprivate.h"
31 #include "deprecated/gthread.h"
32
33 #include "gutils.h"
34
35 /* {{{1 Documentation */
36
37 /**
38  * SECTION:threads-deprecated
39  * @title: Deprecated thread API
40  * @short_description: old thread APIs (for reference only)
41  * @see_also: #GThread
42  *
43  * These APIs are deprecated.  You should not use them in new code.
44  * This section remains only to assist with understanding code that was
45  * written to use these APIs at some point in the past.
46  **/
47
48 /**
49  * GThreadPriority:
50  * @G_THREAD_PRIORITY_LOW: a priority lower than normal
51  * @G_THREAD_PRIORITY_NORMAL: the default priority
52  * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
53  * @G_THREAD_PRIORITY_URGENT: the highest priority
54  *
55  * Deprecated:2.32: Thread priorities no longer have any effect.
56  */
57
58 /**
59  * GThreadFunctions:
60  * @mutex_new: virtual function pointer for g_mutex_new()
61  * @mutex_lock: virtual function pointer for g_mutex_lock()
62  * @mutex_trylock: virtual function pointer for g_mutex_trylock()
63  * @mutex_unlock: virtual function pointer for g_mutex_unlock()
64  * @mutex_free: virtual function pointer for g_mutex_free()
65  * @cond_new: virtual function pointer for g_cond_new()
66  * @cond_signal: virtual function pointer for g_cond_signal()
67  * @cond_broadcast: virtual function pointer for g_cond_broadcast()
68  * @cond_wait: virtual function pointer for g_cond_wait()
69  * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
70  * @cond_free: virtual function pointer for g_cond_free()
71  * @private_new: virtual function pointer for g_private_new()
72  * @private_get: virtual function pointer for g_private_get()
73  * @private_set: virtual function pointer for g_private_set()
74  * @thread_create: virtual function pointer for g_thread_create()
75  * @thread_yield: virtual function pointer for g_thread_yield()
76  * @thread_join: virtual function pointer for g_thread_join()
77  * @thread_exit: virtual function pointer for g_thread_exit()
78  * @thread_set_priority: virtual function pointer for
79  *                       g_thread_set_priority()
80  * @thread_self: virtual function pointer for g_thread_self()
81  * @thread_equal: used internally by recursive mutex locks and by some
82  *                assertion checks
83  *
84  * This function table is no longer used by g_thread_init()
85  * to initialize the thread system.
86  */
87
88 /**
89  * G_THREADS_IMPL_POSIX:
90  *
91  * This macro is defined if POSIX style threads are used.
92  *
93  * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
94  *                 Use G_OS_WIN32 to detect Windows.
95  */
96
97 /**
98  * G_THREADS_IMPL_WIN32:
99  *
100  * This macro is defined if Windows style threads are used.
101  *
102  * Deprecated:2.32:Use G_OS_WIN32 to detect Windows.
103  */
104
105
106 /* {{{1 Exported Variables */
107
108 gboolean g_thread_use_default_impl = TRUE;
109
110 GThreadFunctions g_thread_functions_for_glib_use =
111 {
112   g_mutex_new,
113   g_mutex_lock,
114   g_mutex_trylock,
115   g_mutex_unlock,
116   g_mutex_free,
117   g_cond_new,
118   g_cond_signal,
119   g_cond_broadcast,
120   g_cond_wait,
121   g_cond_timed_wait,
122   g_cond_free,
123   g_private_new,
124   g_private_get,
125   g_private_set,
126   NULL,
127   g_thread_yield,
128   NULL,
129   NULL,
130   NULL,
131   NULL,
132   NULL,
133 };
134
135 static guint64
136 gettime (void)
137 {
138   return g_get_monotonic_time () * 1000;
139 }
140
141 guint64 (*g_thread_gettime) (void) = gettime;
142
143 /* Initialisation {{{1 ---------------------------------------------------- */
144 gboolean         g_threads_got_initialized = TRUE;
145 GSystemThread    zero_thread; /* This is initialized to all zero */
146
147
148 /**
149  * g_thread_init:
150  * @vtable: a function table of type #GThreadFunctions, that provides
151  *     the entry points to the thread system to be used. Since 2.32,
152  *     this parameter is ignored and should always be %NULL
153  *
154  * If you use GLib from more than one thread, you must initialize the
155  * thread system by calling g_thread_init().
156  *
157  * Since version 2.24, calling g_thread_init() multiple times is allowed,
158  * but nothing happens except for the first call.
159  *
160  * Since version 2.32, GLib does not support custom thread implementations
161  * anymore and the @vtable parameter is ignored and you should pass %NULL.
162  *
163  * <note><para>g_thread_init() must not be called directly or indirectly
164  * in a callback from GLib. Also no mutexes may be currently locked while
165  * calling g_thread_init().</para></note>
166  *
167  * <note><para>To use g_thread_init() in your program, you have to link
168  * with the libraries that the command <command>pkg-config --libs
169  * gthread-2.0</command> outputs. This is not the case for all the
170  * other thread-related functions of GLib. Those can be used without
171  * having to link with the thread libraries.</para></note>
172  */
173
174 /**
175  * g_thread_get_initialized:
176  *
177  * Indicates if g_thread_init() has been called.
178  *
179  * Returns: %TRUE if threads have been initialized.
180  *
181  * Since: 2.20
182  */
183 gboolean
184 g_thread_get_initialized (void)
185 {
186   return g_thread_supported ();
187 }
188
189 /* We need this for ABI compatibility */
190 void g_thread_init_glib (void) { }
191
192 /* Internal variables {{{1 */
193
194 static GRealThread *g_thread_all_threads = NULL;
195 static GSList      *g_thread_free_indices = NULL;
196
197 /* Protects g_thread_all_threads and g_thread_free_indices */
198 G_LOCK_DEFINE_STATIC (g_thread);
199
200 /* Misc. GThread functions {{{1 */
201
202 /**
203  * g_thread_set_priority:
204  * @thread: a #GThread.
205  * @priority: ignored
206  *
207  * This function does nothing.
208  *
209  * Deprecated:2.32: Thread priorities no longer have any effect.
210  */
211 void
212 g_thread_set_priority (GThread         *thread,
213                        GThreadPriority  priority)
214 {
215 }
216
217 /**
218  * g_thread_create:
219  * @func: a function to execute in the new thread
220  * @data: an argument to supply to the new thread
221  * @joinable: should this thread be joinable?
222  * @error: return location for error, or %NULL
223  *
224  * This function creates a new thread.
225  *
226  * If @joinable is %TRUE, you can wait for this threads termination
227  * calling g_thread_join(). Otherwise the thread will just disappear
228  * when it terminates.
229  *
230  * The new thread executes the function @func with the argument @data.
231  * If the thread was created successfully, it is returned.
232  *
233  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
234  * The error is set, if and only if the function returns %NULL.
235  *
236  * Returns: the new #GThread on success
237  *
238  * Deprecated:2.32: Use g_thread_new() instead
239  */
240 GThread *
241 g_thread_create (GThreadFunc   func,
242                  gpointer      data,
243                  gboolean      joinable,
244                  GError      **error)
245 {
246   return g_thread_new_internal (NULL, func, data, joinable, 0, TRUE, error);
247 }
248
249 /**
250  * g_thread_create_full:
251  * @func: a function to execute in the new thread.
252  * @data: an argument to supply to the new thread.
253  * @stack_size: a stack size for the new thread.
254  * @joinable: should this thread be joinable?
255  * @bound: ignored
256  * @priority: ignored
257  * @error: return location for error.
258  * @Returns: the new #GThread on success.
259  *
260  * This function creates a new thread.
261  *
262  * Deprecated:2.32: The @bound and @priority arguments are now ignored.
263  * Use g_thread_new() or g_thread_new_full() instead.
264  */
265 GThread *
266 g_thread_create_full (GThreadFunc       func,
267                       gpointer          data,
268                       gulong            stack_size,
269                       gboolean          joinable,
270                       gboolean          bound,
271                       GThreadPriority   priority,
272                       GError          **error)
273 {
274   return g_thread_new_internal (NULL, func, data, joinable, stack_size, TRUE, error);
275 }
276
277 /**
278  * g_thread_foreach:
279  * @thread_func: function to call for all #GThread structures
280  * @user_data: second argument to @thread_func
281  *
282  * Call @thread_func on all #GThreads that have been
283  * created with g_thread_create().
284  *
285  * Note that threads may decide to exit while @thread_func is
286  * running, so without intimate knowledge about the lifetime of
287  * foreign threads, @thread_func shouldn't access the GThread*
288  * pointer passed in as first argument. However, @thread_func will
289  * not be called for threads which are known to have exited already.
290  *
291  * Due to thread lifetime checks, this function has an execution complexity
292  * which is quadratic in the number of existing threads.
293  *
294  * Since: 2.10
295  *
296  * Deprecated:2.32: There aren't many things you can do with a #GThread,
297  *     except comparing it with one that was returned from g_thread_create().
298  *     There are better ways to find out if your thread is still alive.
299  */
300 void
301 g_thread_foreach (GFunc    thread_func,
302                   gpointer user_data)
303 {
304   GSList *slist = NULL;
305   GRealThread *thread;
306   g_return_if_fail (thread_func != NULL);
307   /* snapshot the list of threads for iteration */
308   G_LOCK (g_thread);
309   for (thread = g_thread_all_threads; thread; thread = thread->next)
310     slist = g_slist_prepend (slist, thread);
311   G_UNLOCK (g_thread);
312   /* walk the list, skipping non-existent threads */
313   while (slist)
314     {
315       GSList *node = slist;
316       slist = node->next;
317       /* check whether the current thread still exists */
318       G_LOCK (g_thread);
319       for (thread = g_thread_all_threads; thread; thread = thread->next)
320         if (thread == node->data)
321           break;
322       G_UNLOCK (g_thread);
323       if (thread)
324         thread_func (thread, user_data);
325       g_slist_free_1 (node);
326     }
327 }
328
329 void
330 g_enumerable_thread_add (GRealThread *thread)
331 {
332   G_LOCK (g_thread);
333   thread->next = g_thread_all_threads;
334   g_thread_all_threads = thread;
335   G_UNLOCK (g_thread);
336 }
337
338 void
339 g_enumerable_thread_remove (GRealThread *thread)
340 {
341   GRealThread *t, *p;
342
343   G_LOCK (g_thread);
344   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
345     {
346       if (t == thread)
347         {
348           if (p)
349             p->next = t->next;
350           else
351             g_thread_all_threads = t->next;
352           break;
353         }
354     }
355   G_UNLOCK (g_thread);
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 (GMutex** mutex)
488 {
489   GMutex *result;
490
491   if (!g_thread_supported ())
492     return NULL;
493
494   result = g_atomic_pointer_get (mutex);
495
496   if (!result)
497     {
498       g_mutex_lock (&g_once_mutex);
499
500       result = *mutex;
501       if (!result)
502         {
503           result = g_mutex_new ();
504           g_atomic_pointer_set (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 /**
1219  * GStaticPrivate:
1220  *
1221  * A #GStaticPrivate works almost like a #GPrivate, but it has one
1222  * significant advantage. It doesn't need to be created at run-time
1223  * like a #GPrivate, but can be defined at compile-time. This is
1224  * similar to the difference between #GMutex and #GStaticMutex. Now
1225  * look at our <function>give_me_next_number()</function> example with
1226  * #GStaticPrivate:
1227  *
1228  * <example>
1229  *  <title>Using GStaticPrivate for per-thread data</title>
1230  *  <programlisting>
1231  *   int
1232  *   give_me_next_number (<!-- -->)
1233  *   {
1234  *     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1235  *     int *current_number = g_static_private_get (&amp;current_number_key);
1236  *
1237  *     if (!current_number)
1238  *       {
1239  *         current_number = g_new (int,1);
1240  *         *current_number = 0;
1241  *         g_static_private_set (&amp;current_number_key, current_number, g_free);
1242  *       }
1243  *
1244  *     *current_number = calc_next_number (*current_number);
1245  *
1246  *     return *current_number;
1247  *   }
1248  *  </programlisting>
1249  * </example>
1250  */
1251
1252 /**
1253  * G_STATIC_PRIVATE_INIT:
1254  *
1255  * Every #GStaticPrivate must be initialized with this macro, before it
1256  * can be used.
1257  *
1258  * |[
1259  *   GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1260  * ]|
1261  */
1262
1263 /**
1264  * g_static_private_init:
1265  * @private_key: a #GStaticPrivate to be initialized
1266  *
1267  * Initializes @private_key. Alternatively you can initialize it with
1268  * #G_STATIC_PRIVATE_INIT.
1269  */
1270 void
1271 g_static_private_init (GStaticPrivate *private_key)
1272 {
1273   private_key->index = 0;
1274 }
1275
1276 /**
1277  * g_static_private_get:
1278  * @private_key: a #GStaticPrivate
1279  *
1280  * Works like g_private_get() only for a #GStaticPrivate.
1281  *
1282  * This function works even if g_thread_init() has not yet been called.
1283  *
1284  * Returns: the corresponding pointer
1285  */
1286 gpointer
1287 g_static_private_get (GStaticPrivate *private_key)
1288 {
1289   GRealThread *self = (GRealThread*) g_thread_self ();
1290   GArray *array;
1291   gpointer ret = NULL;
1292   array = self->private_data;
1293
1294   if (array && private_key->index != 0 && private_key->index <= array->len)
1295     {
1296       GStaticPrivateNode *node;
1297
1298       node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1299
1300       /* Deal with the possibility that the GStaticPrivate which used
1301        * to have this index got freed and the index got allocated to
1302        * a new one. In this case, the data in the node is stale, so
1303        * free it and return NULL.
1304        */
1305       if (G_UNLIKELY (node->owner != private_key))
1306         {
1307           if (node->destroy)
1308             node->destroy (node->data);
1309           node->destroy = NULL;
1310           node->data = NULL;
1311           node->owner = NULL;
1312         }
1313       ret = node->data;
1314     }
1315
1316   return ret;
1317 }
1318
1319 /**
1320  * g_static_private_set:
1321  * @private_key: a #GStaticPrivate
1322  * @data: the new pointer
1323  * @notify: a function to be called with the pointer whenever the
1324  *     current thread ends or sets this pointer again
1325  *
1326  * Sets the pointer keyed to @private_key for the current thread and
1327  * the function @notify to be called with that pointer (%NULL or
1328  * non-%NULL), whenever the pointer is set again or whenever the
1329  * current thread ends.
1330  *
1331  * This function works even if g_thread_init() has not yet been called.
1332  * If g_thread_init() is called later, the @data keyed to @private_key
1333  * will be inherited only by the main thread, i.e. the one that called
1334  * g_thread_init().
1335  *
1336  * <note><para>@notify is used quite differently from @destructor in
1337  * g_private_new().</para></note>
1338  */
1339 void
1340 g_static_private_set (GStaticPrivate *private_key,
1341                       gpointer        data,
1342                       GDestroyNotify  notify)
1343 {
1344   GRealThread *self = (GRealThread*) g_thread_self ();
1345   GArray *array;
1346   static guint next_index = 0;
1347   GStaticPrivateNode *node;
1348
1349   if (!private_key->index)
1350     {
1351       G_LOCK (g_thread);
1352
1353       if (!private_key->index)
1354         {
1355           if (g_thread_free_indices)
1356             {
1357               private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1358               g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1359                                                            g_thread_free_indices);
1360             }
1361           else
1362             private_key->index = ++next_index;
1363         }
1364
1365       G_UNLOCK (g_thread);
1366     }
1367
1368   array = self->private_data;
1369   if (!array)
1370     {
1371       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1372       self->private_data = array;
1373     }
1374   if (private_key->index > array->len)
1375     g_array_set_size (array, private_key->index);
1376
1377   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1378
1379   if (node->destroy)
1380     node->destroy (node->data);
1381
1382   node->data = data;
1383   node->destroy = notify;
1384   node->owner = private_key;
1385 }
1386
1387 /**
1388  * g_static_private_free:
1389  * @private_key: a #GStaticPrivate to be freed
1390  *
1391  * Releases all resources allocated to @private_key.
1392  *
1393  * You don't have to call this functions for a #GStaticPrivate with an
1394  * unbounded lifetime, i.e. objects declared 'static', but if you have
1395  * a #GStaticPrivate as a member of a structure and the structure is
1396  * freed, you should also free the #GStaticPrivate.
1397  */
1398 void
1399 g_static_private_free (GStaticPrivate *private_key)
1400 {
1401   guint idx = private_key->index;
1402
1403   if (!idx)
1404     return;
1405
1406   private_key->index = 0;
1407
1408   /* Freeing the per-thread data is deferred to either the
1409    * thread end or the next g_static_private_get() call for
1410    * the same index.
1411    */
1412   G_LOCK (g_thread);
1413   g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1414                                            GUINT_TO_POINTER (idx));
1415   G_UNLOCK (g_thread);
1416 }
1417
1418 void
1419 g_static_private_cleanup (GRealThread *thread)
1420 {
1421   GArray *array;
1422
1423   array = thread->private_data;
1424   thread->private_data = NULL;
1425
1426   if (array)
1427     {
1428       guint i;
1429
1430       for (i = 0; i < array->len; i++ )
1431         {
1432           GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1433           if (node->destroy)
1434             node->destroy (node->data);
1435         }
1436       g_array_free (array, TRUE);
1437     }
1438 }
1439
1440 /* GMutex {{{1 ------------------------------------------------------ */
1441
1442 /**
1443  * g_mutex_new:
1444  *
1445  * Allocates and initializes a new #GMutex.
1446  *
1447  * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1448  *
1449  * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1450  * in structures and initialised with g_mutex_init().
1451  */
1452 GMutex *
1453 g_mutex_new (void)
1454 {
1455   GMutex *mutex;
1456
1457   mutex = g_slice_new (GMutex);
1458   g_mutex_init (mutex);
1459
1460   return mutex;
1461 }
1462
1463 /**
1464  * g_mutex_free:
1465  * @mutex: a #GMutex
1466  *
1467  * Destroys a @mutex that has been created with g_mutex_new().
1468  *
1469  * Calling g_mutex_free() on a locked mutex may result
1470  * in undefined behaviour.
1471  *
1472  * Deprecated:3.32:GMutex can now be statically allocated, or embedded
1473  * in structures and initialised with g_mutex_init().
1474  */
1475 void
1476 g_mutex_free (GMutex *mutex)
1477 {
1478   g_mutex_clear (mutex);
1479   g_slice_free (GMutex, mutex);
1480 }
1481
1482 /* GCond {{{1 ------------------------------------------------------ */
1483
1484 /**
1485  * g_cond_new:
1486  *
1487  * Allocates and initializes a new #GCond.
1488  *
1489  * Returns: a newly allocated #GCond. Free with g_cond_free()
1490  *
1491  * Deprecated:3.32:GCond can now be statically allocated, or embedded
1492  * in structures and initialised with g_cond_init().
1493  */
1494 GCond *
1495 g_cond_new (void)
1496 {
1497   GCond *cond;
1498
1499   cond = g_slice_new (GCond);
1500   g_cond_init (cond);
1501
1502   return cond;
1503 }
1504
1505 /**
1506  * g_cond_free:
1507  * @cond: a #GCond
1508  *
1509  * Destroys a #GCond that has been created with g_cond_new().
1510  *
1511  * Calling g_cond_free() for a #GCond on which threads are
1512  * blocking leads to undefined behaviour.
1513  *
1514  * Deprecated:3.32:GCond can now be statically allocated, or embedded
1515  * in structures and initialised with g_cond_init().
1516  */
1517 void
1518 g_cond_free (GCond *cond)
1519 {
1520   g_cond_clear (cond);
1521   g_slice_free (GCond, cond);
1522 }
1523
1524 /* {{{1 Epilogue */
1525 /* vim: set foldmethod=marker: */