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