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