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