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