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