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