2c9dad0c9359c933a76b334d4ddf2c91f027d671
[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 /* Misc. GThread functions {{{1 */
113
114 /**
115  * g_thread_set_priority:
116  * @thread: a #GThread.
117  * @priority: ignored
118  *
119  * This function does nothing.
120  *
121  * Deprecated:2.32: Thread priorities no longer have any effect.
122  */
123 void
124 g_thread_set_priority (GThread         *thread,
125                        GThreadPriority  priority)
126 {
127 }
128
129 /**
130  * g_thread_create_full:
131  * @func: a function to execute in the new thread.
132  * @data: an argument to supply to the new thread.
133  * @stack_size: a stack size for the new thread.
134  * @joinable: should this thread be joinable?
135  * @bound: ignored
136  * @priority: ignored
137  * @error: return location for error.
138  * @Returns: the new #GThread on success.
139  *
140  * This function creates a new thread.
141  *
142  * Deprecated:2.32: The @bound and @priority arguments are now ignored.
143  * Use g_thread_create() or g_thread_create_with_stack_size() instead.
144  */
145 GThread *
146 g_thread_create_full (GThreadFunc       func,
147                       gpointer          data,
148                       gulong            stack_size,
149                       gboolean          joinable,
150                       gboolean          bound,
151                       GThreadPriority   priority,
152                       GError          **error)
153 {
154   return g_thread_create_with_stack_size (func, data, joinable, stack_size, error);
155 }
156
157 /* GStaticMutex {{{1 ------------------------------------------------------ */
158
159 /**
160  * GStaticMutex:
161  *
162  * A #GStaticMutex works like a #GMutex.
163  *
164  * Prior to GLib 2.32, GStaticMutex had the significant advantage
165  * that it doesn't need to be created at run-time, but can be defined
166  * at compile-time. Since 2.32, #GMutex can be statically allocated
167  * as well, and GStaticMutex has been deprecated.
168  *
169  * Here is a version of our give_me_next_number() example using
170  * a GStaticMutex.
171  *
172  * <example>
173  *  <title>
174  *   Using <structname>GStaticMutex</structname>
175  *   to simplify thread-safe programming
176  *  </title>
177  *  <programlisting>
178  *   int
179  *   give_me_next_number (void)
180  *   {
181  *     static int current_number = 0;
182  *     int ret_val;
183  *     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
184  *
185  *     g_static_mutex_lock (&amp;mutex);
186  *     ret_val = current_number = calc_next_number (current_number);
187  *     g_static_mutex_unlock (&amp;mutex);
188  *
189  *     return ret_val;
190  *   }
191  *  </programlisting>
192  * </example>
193  *
194  * Sometimes you would like to dynamically create a mutex. If you don't
195  * want to require prior calling to g_thread_init(), because your code
196  * should also be usable in non-threaded programs, you are not able to
197  * use g_mutex_new() and thus #GMutex, as that requires a prior call to
198  * g_thread_init(). In theses cases you can also use a #GStaticMutex.
199  * It must be initialized with g_static_mutex_init() before using it
200  * and freed with with g_static_mutex_free() when not needed anymore to
201  * free up any allocated resources.
202  *
203  * Even though #GStaticMutex is not opaque, it should only be used with
204  * the following functions, as it is defined differently on different
205  * platforms.
206  *
207  * All of the <function>g_static_mutex_*</function> functions apart
208  * from <function>g_static_mutex_get_mutex</function> can also be used
209  * even if g_thread_init() has not yet been called. Then they do
210  * nothing, apart from <function>g_static_mutex_trylock</function>,
211  * which does nothing but returning %TRUE.
212  *
213  * <note><para>All of the <function>g_static_mutex_*</function>
214  * functions are actually macros. Apart from taking their addresses, you
215  * can however use them as if they were functions.</para></note>
216  **/
217
218 /**
219  * G_STATIC_MUTEX_INIT:
220  *
221  * A #GStaticMutex must be initialized with this macro, before it can
222  * be used. This macro can used be to initialize a variable, but it
223  * cannot be assigned to a variable. In that case you have to use
224  * g_static_mutex_init().
225  *
226  * |[
227  * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
228  * ]|
229  **/
230
231 /**
232  * g_static_mutex_init:
233  * @mutex: a #GStaticMutex to be initialized.
234  *
235  * Initializes @mutex.
236  * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
237  *
238  * Deprecated: 2.32: Use g_mutex_init()
239  */
240 void
241 g_static_mutex_init (GStaticMutex *mutex)
242 {
243   static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
244
245   g_return_if_fail (mutex);
246
247   *mutex = init_mutex;
248 }
249
250 /* IMPLEMENTATION NOTE:
251  *
252  * On some platforms a GStaticMutex is actually a normal GMutex stored
253  * inside of a structure instead of being allocated dynamically.  We can
254  * only do this for platforms on which we know, in advance, how to
255  * allocate (size) and initialise (value) that memory.
256  *
257  * On other platforms, a GStaticMutex is nothing more than a pointer to
258  * a GMutex.  In that case, the first access we make to the static mutex
259  * must first allocate the normal GMutex and store it into the pointer.
260  *
261  * configure.ac writes macros into glibconfig.h to determine if
262  * g_static_mutex_get_mutex() accesses the structure in memory directly
263  * (on platforms where we are able to do that) or if it ends up here,
264  * where we may have to allocate the GMutex before returning it.
265  */
266
267 /**
268  * g_static_mutex_get_mutex:
269  * @mutex: a #GStaticMutex.
270  * @Returns: the #GMutex corresponding to @mutex.
271  *
272  * For some operations (like g_cond_wait()) you must have a #GMutex
273  * instead of a #GStaticMutex. This function will return the
274  * corresponding #GMutex for @mutex.
275  *
276  * Deprecated: 2.32: Just use a #GMutex
277  */
278 GMutex *
279 g_static_mutex_get_mutex_impl (GMutex** mutex)
280 {
281   GMutex *result;
282
283   if (!g_thread_supported ())
284     return NULL;
285
286   result = g_atomic_pointer_get (mutex);
287
288   if (!result)
289     {
290       g_mutex_lock (&g_once_mutex);
291
292       result = *mutex;
293       if (!result)
294         {
295           result = g_mutex_new ();
296           g_atomic_pointer_set (mutex, result);
297         }
298
299       g_mutex_unlock (&g_once_mutex);
300     }
301
302   return result;
303 }
304
305 /* IMPLEMENTATION NOTE:
306  *
307  * g_static_mutex_lock(), g_static_mutex_trylock() and
308  * g_static_mutex_unlock() are all preprocessor macros that wrap the
309  * corresponding g_mutex_*() function around a call to
310  * g_static_mutex_get_mutex().
311  */
312
313 /**
314  * g_static_mutex_lock:
315  * @mutex: a #GStaticMutex.
316  *
317  * Works like g_mutex_lock(), but for a #GStaticMutex.
318  *
319  * Deprecated: 2.32: Use g_mutex_lock()
320  */
321
322 /**
323  * g_static_mutex_trylock:
324  * @mutex: a #GStaticMutex.
325  * @Returns: %TRUE, if the #GStaticMutex could be locked.
326  *
327  * Works like g_mutex_trylock(), but for a #GStaticMutex.
328  *
329  * Deprecated: 2.32: Use g_mutex_trylock()
330  */
331
332 /**
333  * g_static_mutex_unlock:
334  * @mutex: a #GStaticMutex.
335  *
336  * Works like g_mutex_unlock(), but for a #GStaticMutex.
337  *
338  * Deprecated: 2.32: Use g_mutex_unlock()
339  */
340
341 /**
342  * g_static_mutex_free:
343  * @mutex: a #GStaticMutex to be freed.
344  *
345  * Releases all resources allocated to @mutex.
346  *
347  * You don't have to call this functions for a #GStaticMutex with an
348  * unbounded lifetime, i.e. objects declared 'static', but if you have
349  * a #GStaticMutex as a member of a structure and the structure is
350  * freed, you should also free the #GStaticMutex.
351  *
352  * <note><para>Calling g_static_mutex_free() on a locked mutex may
353  * result in undefined behaviour.</para></note>
354  *
355  * Deprecated: 2.32: Use g_mutex_free()
356  */
357 void
358 g_static_mutex_free (GStaticMutex* mutex)
359 {
360   GMutex **runtime_mutex;
361
362   g_return_if_fail (mutex);
363
364   /* The runtime_mutex is the first (or only) member of GStaticMutex,
365    * see both versions (of glibconfig.h) in configure.ac. Note, that
366    * this variable is NULL, if g_thread_init() hasn't been called or
367    * if we're using the default thread implementation and it provides
368    * static mutexes. */
369   runtime_mutex = ((GMutex**)mutex);
370
371   if (*runtime_mutex)
372     g_mutex_free (*runtime_mutex);
373
374   *runtime_mutex = NULL;
375 }
376
377 /* {{{1 GStaticRecMutex */
378
379 /**
380  * GStaticRecMutex:
381  *
382  * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
383  * multiple times by one thread. If you enter it n times, you have to
384  * unlock it n times again to let other threads lock it. An exception
385  * is the function g_static_rec_mutex_unlock_full(): that allows you to
386  * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
387  * number of times this mutex was locked). The depth can later be used
388  * to restore the state of the #GStaticRecMutex by calling
389  * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
390  * been deprecated in favor of #GRecMutex.
391  *
392  * Even though #GStaticRecMutex is not opaque, it should only be used
393  * with the following functions.
394  *
395  * All of the <function>g_static_rec_mutex_*</function> functions can
396  * be used even if g_thread_init() has not been called. Then they do
397  * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
398  * which does nothing but returning %TRUE.
399  **/
400
401 /**
402  * G_STATIC_REC_MUTEX_INIT:
403  *
404  * A #GStaticRecMutex must be initialized with this macro before it can
405  * be used. This macro can used be to initialize a variable, but it
406  * cannot be assigned to a variable. In that case you have to use
407  * g_static_rec_mutex_init().
408  *
409  * |[
410  *   GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
411  * ]|
412  */
413
414 /**
415  * g_static_rec_mutex_init:
416  * @mutex: a #GStaticRecMutex to be initialized.
417  *
418  * A #GStaticRecMutex must be initialized with this function before it
419  * can be used. Alternatively you can initialize it with
420  * #G_STATIC_REC_MUTEX_INIT.
421  *
422  * Deprecated: 2.32: Use g_rec_mutex_init()
423  */
424 void
425 g_static_rec_mutex_init (GStaticRecMutex *mutex)
426 {
427   static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
428
429   g_return_if_fail (mutex);
430
431   *mutex = init_mutex;
432 }
433
434 /**
435  * g_static_rec_mutex_lock:
436  * @mutex: a #GStaticRecMutex to lock.
437  *
438  * Locks @mutex. If @mutex is already locked by another thread, the
439  * current thread will block until @mutex is unlocked by the other
440  * thread. If @mutex is already locked by the calling thread, this
441  * functions increases the depth of @mutex and returns immediately.
442  *
443  * Deprecated: 2.32: Use g_rec_mutex_lock()
444  */
445 void
446 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
447 {
448   GSystemThread self;
449
450   g_return_if_fail (mutex);
451
452   if (!g_thread_supported ())
453     return;
454
455   g_system_thread_self (&self);
456
457   if (g_system_thread_equal (&self, &mutex->owner))
458     {
459       mutex->depth++;
460       return;
461     }
462   g_static_mutex_lock (&mutex->mutex);
463   g_system_thread_assign (mutex->owner, self);
464   mutex->depth = 1;
465 }
466
467 /**
468  * g_static_rec_mutex_trylock:
469  * @mutex: a #GStaticRecMutex to lock.
470  * @Returns: %TRUE, if @mutex could be locked.
471  *
472  * Tries to lock @mutex. If @mutex is already locked by another thread,
473  * it immediately returns %FALSE. Otherwise it locks @mutex and returns
474  * %TRUE. If @mutex is already locked by the calling thread, this
475  * functions increases the depth of @mutex and immediately returns
476  * %TRUE.
477  *
478  * Deprecated: 2.32: Use g_rec_mutex_trylock()
479  */
480 gboolean
481 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
482 {
483   GSystemThread self;
484
485   g_return_val_if_fail (mutex, FALSE);
486
487   if (!g_thread_supported ())
488     return TRUE;
489
490   g_system_thread_self (&self);
491
492   if (g_system_thread_equal (&self, &mutex->owner))
493     {
494       mutex->depth++;
495       return TRUE;
496     }
497
498   if (!g_static_mutex_trylock (&mutex->mutex))
499     return FALSE;
500
501   g_system_thread_assign (mutex->owner, self);
502   mutex->depth = 1;
503   return TRUE;
504 }
505
506 /**
507  * g_static_rec_mutex_unlock:
508  * @mutex: a #GStaticRecMutex to unlock.
509  *
510  * Unlocks @mutex. Another thread will be allowed to lock @mutex only
511  * when it has been unlocked as many times as it had been locked
512  * before. If @mutex is completely unlocked and another thread is
513  * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
514  * woken and can lock @mutex itself.
515  *
516  * Deprecated: 2.32: Use g_rec_mutex_unlock()
517  */
518 void
519 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
520 {
521   g_return_if_fail (mutex);
522
523   if (!g_thread_supported ())
524     return;
525
526   if (mutex->depth > 1)
527     {
528       mutex->depth--;
529       return;
530     }
531   g_system_thread_assign (mutex->owner, zero_thread);
532   g_static_mutex_unlock (&mutex->mutex);
533 }
534
535 /**
536  * g_static_rec_mutex_lock_full:
537  * @mutex: a #GStaticRecMutex to lock.
538  * @depth: number of times this mutex has to be unlocked to be
539  *         completely unlocked.
540  *
541  * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
542  *
543  * Deprecated: 2.32: Use g_rec_mutex_lock()
544  */
545 void
546 g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
547                               guint            depth)
548 {
549   GSystemThread self;
550   g_return_if_fail (mutex);
551
552   if (!g_thread_supported ())
553     return;
554
555   if (depth == 0)
556     return;
557
558   g_system_thread_self (&self);
559
560   if (g_system_thread_equal (&self, &mutex->owner))
561     {
562       mutex->depth += depth;
563       return;
564     }
565   g_static_mutex_lock (&mutex->mutex);
566   g_system_thread_assign (mutex->owner, self);
567   mutex->depth = depth;
568 }
569
570 /**
571  * g_static_rec_mutex_unlock_full:
572  * @mutex: a #GStaticRecMutex to completely unlock.
573  * @Returns: number of times @mutex has been locked by the current
574  *           thread.
575  *
576  * Completely unlocks @mutex. If another thread is blocked in a
577  * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
578  * lock @mutex itself. This function returns the number of times that
579  * @mutex has been locked by the current thread. To restore the state
580  * before the call to g_static_rec_mutex_unlock_full() you can call
581  * g_static_rec_mutex_lock_full() with the depth returned by this
582  * function.
583  *
584  * Deprecated: 2.32: Use g_rec_mutex_unlock()
585  */
586 guint
587 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
588 {
589   guint depth;
590
591   g_return_val_if_fail (mutex, 0);
592
593   if (!g_thread_supported ())
594     return 1;
595
596   depth = mutex->depth;
597
598   g_system_thread_assign (mutex->owner, zero_thread);
599   mutex->depth = 0;
600   g_static_mutex_unlock (&mutex->mutex);
601
602   return depth;
603 }
604
605 /**
606  * g_static_rec_mutex_free:
607  * @mutex: a #GStaticRecMutex to be freed.
608  *
609  * Releases all resources allocated to a #GStaticRecMutex.
610  *
611  * You don't have to call this functions for a #GStaticRecMutex with an
612  * unbounded lifetime, i.e. objects declared 'static', but if you have
613  * a #GStaticRecMutex as a member of a structure and the structure is
614  * freed, you should also free the #GStaticRecMutex.
615  *
616  * Deprecated: 2.32: Use g_rec_mutex_clear()
617  */
618 void
619 g_static_rec_mutex_free (GStaticRecMutex *mutex)
620 {
621   g_return_if_fail (mutex);
622
623   g_static_mutex_free (&mutex->mutex);
624 }
625
626 /* GStaticRWLock {{{1 ----------------------------------------------------- */
627
628 /**
629  * GStaticRWLock:
630  *
631  * The #GStaticRWLock struct represents a read-write lock. A read-write
632  * lock can be used for protecting data that some portions of code only
633  * read from, while others also write. In such situations it is
634  * desirable that several readers can read at once, whereas of course
635  * only one writer may write at a time. Take a look at the following
636  * example:
637  *
638  * <example>
639  *  <title>An array with access functions</title>
640  *  <programlisting>
641  *   GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
642  *   GPtrArray *array;
643  *
644  *   gpointer
645  *   my_array_get (guint index)
646  *   {
647  *     gpointer retval = NULL;
648  *
649  *     if (!array)
650  *       return NULL;
651  *
652  *     g_static_rw_lock_reader_lock (&amp;rwlock);
653  *     if (index &lt; array->len)
654  *       retval = g_ptr_array_index (array, index);
655  *     g_static_rw_lock_reader_unlock (&amp;rwlock);
656  *
657  *     return retval;
658  *   }
659  *
660  *   void
661  *   my_array_set (guint index, gpointer data)
662  *   {
663  *     g_static_rw_lock_writer_lock (&amp;rwlock);
664  *
665  *     if (!array)
666  *       array = g_ptr_array_new (<!-- -->);
667  *
668  *     if (index >= array->len)
669  *       g_ptr_array_set_size (array, index+1);
670  *     g_ptr_array_index (array, index) = data;
671  *
672  *     g_static_rw_lock_writer_unlock (&amp;rwlock);
673  *   }
674  *  </programlisting>
675  * </example>
676  *
677  * This example shows an array which can be accessed by many readers
678  * (the <function>my_array_get()</function> function) simultaneously,
679  * whereas the writers (the <function>my_array_set()</function>
680  * function) will only be allowed once at a time and only if no readers
681  * currently access the array. This is because of the potentially
682  * dangerous resizing of the array. Using these functions is fully
683  * multi-thread safe now.
684  *
685  * Most of the time, writers should have precedence over readers. That
686  * means, for this implementation, that as soon as a writer wants to
687  * lock the data, no other reader is allowed to lock the data, whereas,
688  * of course, the readers that already have locked the data are allowed
689  * to finish their operation. As soon as the last reader unlocks the
690  * data, the writer will lock it.
691  *
692  * Even though #GStaticRWLock is not opaque, it should only be used
693  * with the following functions.
694  *
695  * All of the <function>g_static_rw_lock_*</function> functions can be
696  * used even if g_thread_init() has not been called. Then they do
697  * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
698  * which does nothing but returning %TRUE.
699  *
700  * <note><para>A read-write lock has a higher overhead than a mutex. For
701  * example, both g_static_rw_lock_reader_lock() and
702  * g_static_rw_lock_reader_unlock() have to lock and unlock a
703  * #GStaticMutex, so it takes at least twice the time to lock and unlock
704  * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
705  * only data structures that are accessed by multiple readers, and which
706  * keep the lock for a considerable time justify a #GStaticRWLock. The
707  * above example most probably would fare better with a
708  * #GStaticMutex.</para></note>
709  *
710  * Deprecated: 2.32: Use a #GRWLock instead
711  **/
712
713 /**
714  * G_STATIC_RW_LOCK_INIT:
715  *
716  * A #GStaticRWLock must be initialized with this macro before it can
717  * be used. This macro can used be to initialize a variable, but it
718  * cannot be assigned to a variable. In that case you have to use
719  * g_static_rw_lock_init().
720  *
721  * |[
722  *   GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
723  * ]|
724  */
725
726 /**
727  * g_static_rw_lock_init:
728  * @lock: a #GStaticRWLock to be initialized.
729  *
730  * A #GStaticRWLock must be initialized with this function before it
731  * can be used. Alternatively you can initialize it with
732  * #G_STATIC_RW_LOCK_INIT.
733  *
734  * Deprecated: 2.32: Use g_rw_lock_init() instead
735  */
736 void
737 g_static_rw_lock_init (GStaticRWLock* lock)
738 {
739   static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
740
741   g_return_if_fail (lock);
742
743   *lock = init_lock;
744 }
745
746 inline static void
747 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
748 {
749   if (!*cond)
750       *cond = g_cond_new ();
751   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
752 }
753
754 inline static void
755 g_static_rw_lock_signal (GStaticRWLock* lock)
756 {
757   if (lock->want_to_write && lock->write_cond)
758     g_cond_signal (lock->write_cond);
759   else if (lock->want_to_read && lock->read_cond)
760     g_cond_broadcast (lock->read_cond);
761 }
762
763 /**
764  * g_static_rw_lock_reader_lock:
765  * @lock: a #GStaticRWLock to lock for reading.
766  *
767  * Locks @lock for reading. There may be unlimited concurrent locks for
768  * reading of a #GStaticRWLock at the same time.  If @lock is already
769  * locked for writing by another thread or if another thread is already
770  * waiting to lock @lock for writing, this function will block until
771  * @lock is unlocked by the other writing thread and no other writing
772  * threads want to lock @lock. This lock has to be unlocked by
773  * g_static_rw_lock_reader_unlock().
774  *
775  * #GStaticRWLock is not recursive. It might seem to be possible to
776  * recursively lock for reading, but that can result in a deadlock, due
777  * to writer preference.
778  *
779  * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
780  */
781 void
782 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
783 {
784   g_return_if_fail (lock);
785
786   if (!g_threads_got_initialized)
787     return;
788
789   g_static_mutex_lock (&lock->mutex);
790   lock->want_to_read++;
791   while (lock->have_writer || lock->want_to_write)
792     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
793   lock->want_to_read--;
794   lock->read_counter++;
795   g_static_mutex_unlock (&lock->mutex);
796 }
797
798 /**
799  * g_static_rw_lock_reader_trylock:
800  * @lock: a #GStaticRWLock to lock for reading.
801  * @Returns: %TRUE, if @lock could be locked for reading.
802  *
803  * Tries to lock @lock for reading. If @lock is already locked for
804  * writing by another thread or if another thread is already waiting to
805  * lock @lock for writing, immediately returns %FALSE. Otherwise locks
806  * @lock for reading and returns %TRUE. This lock has to be unlocked by
807  * g_static_rw_lock_reader_unlock().
808  *
809  * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
810  */
811 gboolean
812 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
813 {
814   gboolean ret_val = FALSE;
815
816   g_return_val_if_fail (lock, FALSE);
817
818   if (!g_threads_got_initialized)
819     return TRUE;
820
821   g_static_mutex_lock (&lock->mutex);
822   if (!lock->have_writer && !lock->want_to_write)
823     {
824       lock->read_counter++;
825       ret_val = TRUE;
826     }
827   g_static_mutex_unlock (&lock->mutex);
828   return ret_val;
829 }
830
831 /**
832  * g_static_rw_lock_reader_unlock:
833  * @lock: a #GStaticRWLock to unlock after reading.
834  *
835  * Unlocks @lock. If a thread waits to lock @lock for writing and all
836  * locks for reading have been unlocked, the waiting thread is woken up
837  * and can lock @lock for writing.
838  *
839  * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
840  */
841 void
842 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
843 {
844   g_return_if_fail (lock);
845
846   if (!g_threads_got_initialized)
847     return;
848
849   g_static_mutex_lock (&lock->mutex);
850   lock->read_counter--;
851   if (lock->read_counter == 0)
852     g_static_rw_lock_signal (lock);
853   g_static_mutex_unlock (&lock->mutex);
854 }
855
856 /**
857  * g_static_rw_lock_writer_lock:
858  * @lock: a #GStaticRWLock to lock for writing.
859  *
860  * Locks @lock for writing. If @lock is already locked for writing or
861  * reading by other threads, this function will block until @lock is
862  * completely unlocked and then lock @lock for writing. While this
863  * functions waits to lock @lock, no other thread can lock @lock for
864  * reading. When @lock is locked for writing, no other thread can lock
865  * @lock (neither for reading nor writing). This lock has to be
866  * unlocked by g_static_rw_lock_writer_unlock().
867  *
868  * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
869  */
870 void
871 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
872 {
873   g_return_if_fail (lock);
874
875   if (!g_threads_got_initialized)
876     return;
877
878   g_static_mutex_lock (&lock->mutex);
879   lock->want_to_write++;
880   while (lock->have_writer || lock->read_counter)
881     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
882   lock->want_to_write--;
883   lock->have_writer = TRUE;
884   g_static_mutex_unlock (&lock->mutex);
885 }
886
887 /**
888  * g_static_rw_lock_writer_trylock:
889  * @lock: a #GStaticRWLock to lock for writing.
890  * @Returns: %TRUE, if @lock could be locked for writing.
891  *
892  * Tries to lock @lock for writing. If @lock is already locked (for
893  * either reading or writing) by another thread, it immediately returns
894  * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
895  * lock has to be unlocked by g_static_rw_lock_writer_unlock().
896  *
897  * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
898  */
899 gboolean
900 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
901 {
902   gboolean ret_val = FALSE;
903
904   g_return_val_if_fail (lock, FALSE);
905
906   if (!g_threads_got_initialized)
907     return TRUE;
908
909   g_static_mutex_lock (&lock->mutex);
910   if (!lock->have_writer && !lock->read_counter)
911     {
912       lock->have_writer = TRUE;
913       ret_val = TRUE;
914     }
915   g_static_mutex_unlock (&lock->mutex);
916   return ret_val;
917 }
918
919 /**
920  * g_static_rw_lock_writer_unlock:
921  * @lock: a #GStaticRWLock to unlock after writing.
922  *
923  * Unlocks @lock. If a thread is waiting to lock @lock for writing and
924  * all locks for reading have been unlocked, the waiting thread is
925  * woken up and can lock @lock for writing. If no thread is waiting to
926  * lock @lock for writing, and some thread or threads are waiting to
927  * lock @lock for reading, the waiting threads are woken up and can
928  * lock @lock for reading.
929  *
930  * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
931  */
932 void
933 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
934 {
935   g_return_if_fail (lock);
936
937   if (!g_threads_got_initialized)
938     return;
939
940   g_static_mutex_lock (&lock->mutex);
941   lock->have_writer = FALSE;
942   g_static_rw_lock_signal (lock);
943   g_static_mutex_unlock (&lock->mutex);
944 }
945
946 /**
947  * g_static_rw_lock_free:
948  * @lock: a #GStaticRWLock to be freed.
949  *
950  * Releases all resources allocated to @lock.
951  *
952  * You don't have to call this functions for a #GStaticRWLock with an
953  * unbounded lifetime, i.e. objects declared 'static', but if you have
954  * a #GStaticRWLock as a member of a structure, and the structure is
955  * freed, you should also free the #GStaticRWLock.
956  *
957  * Deprecated: 2.32: Use a #GRWLock instead
958  */
959 void
960 g_static_rw_lock_free (GStaticRWLock* lock)
961 {
962   g_return_if_fail (lock);
963
964   if (lock->read_cond)
965     {
966       g_cond_free (lock->read_cond);
967       lock->read_cond = NULL;
968     }
969   if (lock->write_cond)
970     {
971       g_cond_free (lock->write_cond);
972       lock->write_cond = NULL;
973     }
974   g_static_mutex_free (&lock->mutex);
975 }
976
977 /* GPrivate {{{1 ------------------------------------------------------ */
978
979 /**
980  * g_private_new:
981  * @notify: a #GDestroyNotify
982  *
983  * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea.  Use
984  *                  static storage and G_PRIVATE_INIT() instead.
985  *
986  * Returns: a newly allocated #GPrivate (which can never be destroyed)
987  */
988 GPrivate *
989 g_private_new (GDestroyNotify notify)
990 {
991   GPrivate tmp = G_PRIVATE_INIT (notify);
992   GPrivate *key;
993
994   key = g_slice_new (GPrivate);
995   *key = tmp;
996
997   return key;
998 }
999
1000 /* Epilogue {{{1 */
1001 /* vim: set foldmethod=marker: */