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