GThread: more doc updates
[platform/upstream/glib.git] / glib / gthread.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 /* {{{1 Prelude */
25 /* Prelude {{{1 ----------------------------------------------------------- */
26
27 /*
28  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
29  * file for a list of people on the GLib Team.  See the ChangeLog
30  * files for a list of changes.  These files are distributed with
31  * GLib at ftp://ftp.gtk.org/pub/gtk/.
32  */
33
34 /*
35  * MT safe
36  */
37
38 /* implement gthread.h's inline functions */
39 #define G_IMPLEMENT_INLINES 1
40 #define __G_THREAD_C__
41
42 #include "config.h"
43
44 #include "deprecated/gthread.h"
45 #include "gthreadprivate.h"
46 #include "gslice.h"
47 #include "gmain.h"
48
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52
53 #ifndef G_OS_WIN32
54 #include <sys/time.h>
55 #include <time.h>
56 #else
57 #include <windows.h>
58 #endif /* G_OS_WIN32 */
59
60 #include <string.h>
61
62 #include "garray.h"
63 #include "gbitlock.h"
64 #include "gslist.h"
65 #include "gtestutils.h"
66 #include "gtimer.h"
67
68 /**
69  * SECTION:threads
70  * @title: Threads
71  * @short_description: portable support for threads, mutexes, locks,
72  *     conditions and thread private data
73  * @see_also: #GThreadPool, #GAsyncQueue
74  *
75  * Threads act almost like processes, but unlike processes all threads
76  * of one process share the same memory. This is good, as it provides
77  * easy communication between the involved threads via this shared
78  * memory, and it is bad, because strange things (so called
79  * "Heisenbugs") might happen if the program is not carefully designed.
80  * In particular, due to the concurrent nature of threads, no
81  * assumptions on the order of execution of code running in different
82  * threads can be made, unless order is explicitly forced by the
83  * programmer through synchronization primitives.
84  *
85  * The aim of the thread-related functions in GLib is to provide a
86  * portable means for writing multi-threaded software. There are
87  * primitives for mutexes to protect the access to portions of memory
88  * (#GMutex, #GRecMutex and #GRWLock). There is a facility to use
89  * individual bits for locks (g_bit_lock()). There are primitives
90  * for condition variables to allow synchronization of threads (#GCond).
91  * There are primitives for thread-private data - data that every thread
92  * has a private instance of (#GPrivate, #GStaticPrivate). There are
93  * facilities for one-time initialization (#GOnce, g_once_init_enter()).
94  * Finally there are primitives to create and manage threads (#GThread).
95  *
96  * The threading system is initialized with g_thread_init(), which
97  * takes an optional custom thread implementation or %NULL for the
98  * default implementation. If you want to call g_thread_init() with a
99  * non-%NULL argument this must be done before executing any other GLib
100  * functions (except g_mem_set_vtable()). This is a requirement even if
101  * no threads are in fact ever created by the process.
102  *
103  * Calling g_thread_init() with a %NULL argument is somewhat more
104  * relaxed. You may call any other glib functions in the main thread
105  * before g_thread_init() as long as g_thread_init() is not called from
106  * a glib callback, or with any locks held. However, many libraries
107  * above glib does not support late initialization of threads, so doing
108  * this should be avoided if possible.
109  *
110  * Please note that since version 2.24 the GObject initialization
111  * function g_type_init() initializes threads (with a %NULL argument),
112  * so most applications, including those using GTK+ will run with
113  * threads enabled. If you want a special thread implementation, make
114  * sure you call g_thread_init() before g_type_init() is called.
115  *
116  * After calling g_thread_init(), GLib is completely thread safe (all
117  * global data is automatically locked), but individual data structure
118  * instances are not automatically locked for performance reasons. So,
119  * for example you must coordinate accesses to the same #GHashTable
120  * from multiple threads. The two notable exceptions from this rule
121  * are #GMainLoop and #GAsyncQueue, which <emphasis>are</emphasis>
122  * threadsafe and need no further application-level locking to be
123  * accessed from multiple threads.
124  */
125
126 /**
127  * G_THREADS_IMPL_POSIX:
128  *
129  * This macro is defined if POSIX style threads are used.
130  */
131
132 /**
133  * G_THREADS_IMPL_WIN32:
134  *
135  * This macro is defined if Windows style threads are used.
136  */
137
138 /**
139  * G_THREADS_ENABLED:
140  *
141  * This macro is defined, for backward compatibility, to indicate that
142  * GLib has been compiled with thread support. As of GLib 2.28, it is
143  * always defined.
144  **/
145
146 /* G_LOCK Documentation {{{1 ---------------------------------------------- */
147
148 /* IMPLEMENTATION NOTE:
149  *
150  * G_LOCK_DEFINE and friends are convenience macros defined in
151  * gthread.h.  Their documentation lives here.
152  */
153
154 /**
155  * G_LOCK_DEFINE:
156  * @name: the name of the lock.
157  *
158  * The %G_LOCK_* macros provide a convenient interface to #GStaticMutex
159  * with the advantage that they will expand to nothing in programs
160  * compiled against a thread-disabled GLib, saving code and memory
161  * there. #G_LOCK_DEFINE defines a lock. It can appear anywhere
162  * variable definitions may appear in programs, i.e. in the first block
163  * of a function or outside of functions. The @name parameter will be
164  * mangled to get the name of the #GStaticMutex. This means that you
165  * can use names of existing variables as the parameter - e.g. the name
166  * of the variable you intent to protect with the lock. Look at our
167  * <function>give_me_next_number()</function> example using the
168  * %G_LOCK_* macros:
169  *
170  * <example>
171  *  <title>Using the %G_LOCK_* convenience macros</title>
172  *  <programlisting>
173  *   G_LOCK_DEFINE (current_number);
174  *
175  *   int
176  *   give_me_next_number (void)
177  *   {
178  *     static int current_number = 0;
179  *     int ret_val;
180  *
181  *     G_LOCK (current_number);
182  *     ret_val = current_number = calc_next_number (current_number);
183  *     G_UNLOCK (current_number);
184  *
185  *     return ret_val;
186  *   }
187  *  </programlisting>
188  * </example>
189  **/
190
191 /**
192  * G_LOCK_DEFINE_STATIC:
193  * @name: the name of the lock.
194  *
195  * This works like #G_LOCK_DEFINE, but it creates a static object.
196  **/
197
198 /**
199  * G_LOCK_EXTERN:
200  * @name: the name of the lock.
201  *
202  * This declares a lock, that is defined with #G_LOCK_DEFINE in another
203  * module.
204  **/
205
206 /**
207  * G_LOCK:
208  * @name: the name of the lock.
209  *
210  * Works like g_mutex_lock(), but for a lock defined with
211  * #G_LOCK_DEFINE.
212  **/
213
214 /**
215  * G_TRYLOCK:
216  * @name: the name of the lock.
217  * @Returns: %TRUE, if the lock could be locked.
218  *
219  * Works like g_mutex_trylock(), but for a lock defined with
220  * #G_LOCK_DEFINE.
221  **/
222
223 /**
224  * G_UNLOCK:
225  * @name: the name of the lock.
226  *
227  * Works like g_mutex_unlock(), but for a lock defined with
228  * #G_LOCK_DEFINE.
229  **/
230
231 /* GThreadError {{{1 ------------------------------------------------------- */
232 /**
233  * GThreadError:
234  * @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
235  *                        shortage. Try again later.
236  *
237  * Possible errors of thread related functions.
238  **/
239
240 /**
241  * G_THREAD_ERROR:
242  *
243  * The error domain of the GLib thread subsystem.
244  **/
245 GQuark
246 g_thread_error_quark (void)
247 {
248   return g_quark_from_static_string ("g_thread_error");
249 }
250
251 /* Miscellaneous Structures {{{1 ------------------------------------------ */
252 typedef struct _GRealThread GRealThread;
253 struct  _GRealThread
254 {
255   GThread thread;
256   /* Bit 0 protects private_data. To avoid deadlocks, do not block while
257    * holding this (particularly on the g_thread lock). */
258   volatile gint private_data_lock;
259   GArray *private_data;
260   GRealThread *next;
261   gpointer retval;
262   GSystemThread system_thread;
263 };
264
265 #define LOCK_PRIVATE_DATA(self)   g_bit_lock (&(self)->private_data_lock, 0)
266 #define UNLOCK_PRIVATE_DATA(self) g_bit_unlock (&(self)->private_data_lock, 0)
267
268 typedef struct _GStaticPrivateNode GStaticPrivateNode;
269 struct _GStaticPrivateNode
270 {
271   gpointer       data;
272   GDestroyNotify destroy;
273 };
274
275 static void    g_thread_cleanup (gpointer data);
276 static guint64 gettime (void);
277
278 guint64        (*g_thread_gettime) (void) = gettime;
279
280 /* Global Variables {{{1 -------------------------------------------------- */
281
282 static GSystemThread zero_thread; /* This is initialized to all zero */
283 gboolean g_thread_use_default_impl = TRUE;
284
285 /**
286  * g_thread_supported:
287  * @Returns: %TRUE, if the thread system is initialized.
288  *
289  * This function returns %TRUE if the thread system is initialized, and
290  * %FALSE if it is not.
291  *
292  * <note><para>This function is actually a macro. Apart from taking the
293  * address of it you can however use it as if it was a
294  * function.</para></note>
295  **/
296
297 /* IMPLEMENTATION NOTE:
298  *
299  * g_thread_supported() is just returns g_threads_got_initialized
300  */
301 gboolean g_threads_got_initialized = FALSE;
302
303
304 /* Thread Implementation Virtual Function Table {{{1 ---------------------- */
305 /* Virtual Function Table Documentation {{{2 ------------------------------ */
306 /**
307  * GThreadFunctions:
308  * @mutex_new: virtual function pointer for g_mutex_new()
309  * @mutex_lock: virtual function pointer for g_mutex_lock()
310  * @mutex_trylock: virtual function pointer for g_mutex_trylock()
311  * @mutex_unlock: virtual function pointer for g_mutex_unlock()
312  * @mutex_free: virtual function pointer for g_mutex_free()
313  * @cond_new: virtual function pointer for g_cond_new()
314  * @cond_signal: virtual function pointer for g_cond_signal()
315  * @cond_broadcast: virtual function pointer for g_cond_broadcast()
316  * @cond_wait: virtual function pointer for g_cond_wait()
317  * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
318  * @cond_free: virtual function pointer for g_cond_free()
319  * @private_new: virtual function pointer for g_private_new()
320  * @private_get: virtual function pointer for g_private_get()
321  * @private_set: virtual function pointer for g_private_set()
322  * @thread_create: virtual function pointer for g_thread_create()
323  * @thread_yield: virtual function pointer for g_thread_yield()
324  * @thread_join: virtual function pointer for g_thread_join()
325  * @thread_exit: virtual function pointer for g_thread_exit()
326  * @thread_set_priority: virtual function pointer for
327  *                       g_thread_set_priority()
328  * @thread_self: virtual function pointer for g_thread_self()
329  * @thread_equal: used internally by recursive mutex locks and by some
330  *                assertion checks
331  *
332  * This function table is used by g_thread_init() to initialize the
333  * thread system. The functions in the table are directly used by their
334  * g_* prepended counterparts (described in this document).  For
335  * example, if you call g_mutex_new() then mutex_new() from the table
336  * provided to g_thread_init() will be called.
337  *
338  * <note><para>Do not use this struct unless you know what you are
339  * doing.</para></note>
340  **/
341
342 /* GMutex Virtual Functions {{{2 ------------------------------------------ */
343
344 /**
345  * GMutex:
346  *
347  * The #GMutex struct is an opaque data structure to represent a mutex
348  * (mutual exclusion). It can be used to protect data against shared
349  * access. Take for example the following function:
350  *
351  * <example>
352  *  <title>A function which will not work in a threaded environment</title>
353  *  <programlisting>
354  *   int
355  *   give_me_next_number (void)
356  *   {
357  *     static int current_number = 0;
358  *
359  *     /<!-- -->* now do a very complicated calculation to calculate the new
360  *      * number, this might for example be a random number generator
361  *      *<!-- -->/
362  *     current_number = calc_next_number (current_number);
363  *
364  *     return current_number;
365  *   }
366  *  </programlisting>
367  * </example>
368  *
369  * It is easy to see that this won't work in a multi-threaded
370  * application. There current_number must be protected against shared
371  * access. A first naive implementation would be:
372  *
373  * <example>
374  *  <title>The wrong way to write a thread-safe function</title>
375  *  <programlisting>
376  *   int
377  *   give_me_next_number (void)
378  *   {
379  *     static int current_number = 0;
380  *     int ret_val;
381  *     static GMutex * mutex = NULL;
382  *
383  *     if (!mutex) mutex = g_mutex_new (<!-- -->);
384  *
385  *     g_mutex_lock (mutex);
386  *     ret_val = current_number = calc_next_number (current_number);
387  *     g_mutex_unlock (mutex);
388  *
389  *     return ret_val;
390  *   }
391  *  </programlisting>
392  * </example>
393  *
394  * This looks like it would work, but there is a race condition while
395  * constructing the mutex and this code cannot work reliable. Please do
396  * not use such constructs in your own programs! One working solution
397  * is:
398  *
399  * <example>
400  *  <title>A correct thread-safe function</title>
401  *  <programlisting>
402  *   static GMutex *give_me_next_number_mutex = NULL;
403  *
404  *   /<!-- -->* this function must be called before any call to
405  *    * give_me_next_number(<!-- -->)
406  *    *
407  *    * it must be called exactly once.
408  *    *<!-- -->/
409  *   void
410  *   init_give_me_next_number (void)
411  *   {
412  *     g_assert (give_me_next_number_mutex == NULL);
413  *     give_me_next_number_mutex = g_mutex_new (<!-- -->);
414  *   }
415  *
416  *   int
417  *   give_me_next_number (void)
418  *   {
419  *     static int current_number = 0;
420  *     int ret_val;
421  *
422  *     g_mutex_lock (give_me_next_number_mutex);
423  *     ret_val = current_number = calc_next_number (current_number);
424  *     g_mutex_unlock (give_me_next_number_mutex);
425  *
426  *     return ret_val;
427  *   }
428  *  </programlisting>
429  * </example>
430  *
431  * A statically initialized #GMutex provides an even simpler and safer
432  * way of doing this:
433  *
434  * <example>
435  *  <title>Using a statically allocated mutex</title>
436  *  <programlisting>
437  *   int
438  *   give_me_next_number (void)
439  *   {
440  *     static GMutex mutex = G_MUTEX_INIT;
441  *     static int current_number = 0;
442  *     int ret_val;
443  *
444  *     g_mutex_lock (&amp;mutex);
445  *     ret_val = current_number = calc_next_number (current_number);
446  *     g_mutex_unlock (&amp;mutex);
447  *
448  *     return ret_val;
449  *   }
450  *  </programlisting>
451  * </example>
452  *
453  * A #GMutex should only be accessed via <function>g_mutex_</function>
454  * functions.
455  **/
456
457 /* GCond Virtual Functions {{{2 ------------------------------------------ */
458
459 /**
460  * GCond:
461  *
462  * The #GCond struct is an opaque data structure that represents a
463  * condition. Threads can block on a #GCond if they find a certain
464  * condition to be false. If other threads change the state of this
465  * condition they signal the #GCond, and that causes the waiting
466  * threads to be woken up.
467  *
468  * <example>
469  *  <title>
470  *   Using GCond to block a thread until a condition is satisfied
471  *  </title>
472  *  <programlisting>
473  *   GCond* data_cond = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
474  *   GMutex* data_mutex = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
475  *   gpointer current_data = NULL;
476  *
477  *   void
478  *   push_data (gpointer data)
479  *   {
480  *     g_mutex_lock (data_mutex);
481  *     current_data = data;
482  *     g_cond_signal (data_cond);
483  *     g_mutex_unlock (data_mutex);
484  *   }
485  *
486  *   gpointer
487  *   pop_data (void)
488  *   {
489  *     gpointer data;
490  *
491  *     g_mutex_lock (data_mutex);
492  *     while (!current_data)
493  *       g_cond_wait (data_cond, data_mutex);
494  *     data = current_data;
495  *     current_data = NULL;
496  *     g_mutex_unlock (data_mutex);
497  *
498  *     return data;
499  *   }
500  *  </programlisting>
501  * </example>
502  *
503  * Whenever a thread calls pop_data() now, it will wait until
504  * current_data is non-%NULL, i.e. until some other thread
505  * has called push_data().
506  *
507  * <note><para>It is important to use the g_cond_wait() and
508  * g_cond_timed_wait() functions only inside a loop which checks for the
509  * condition to be true.  It is not guaranteed that the waiting thread
510  * will find the condition fulfilled after it wakes up, even if the
511  * signaling thread left the condition in that state: another thread may
512  * have altered the condition before the waiting thread got the chance
513  * to be woken up, even if the condition itself is protected by a
514  * #GMutex, like above.</para></note>
515  *
516  * A #GCond should only be accessed via the following functions.
517  */
518
519 /* GPrivate Virtual Functions {{{2 --------------------------------------- */
520
521 /**
522  * GPrivate:
523  *
524  * <note><para>
525  * #GStaticPrivate is a better choice for most uses.
526  * </para></note>
527  *
528  * The #GPrivate struct is an opaque data structure to represent a
529  * thread private data key. Threads can thereby obtain and set a
530  * pointer which is private to the current thread. Take our
531  * <function>give_me_next_number(<!-- -->)</function> example from
532  * above.  Suppose we don't want <literal>current_number</literal> to be
533  * shared between the threads, but instead to be private to each thread.
534  * This can be done as follows:
535  *
536  * <example>
537  *  <title>Using GPrivate for per-thread data</title>
538  *  <programlisting>
539  *   GPrivate* current_number_key = NULL; /<!-- -->* Must be initialized somewhere
540  *                                           with g_private_new (g_free); *<!-- -->/
541  *
542  *   int
543  *   give_me_next_number (void)
544  *   {
545  *     int *current_number = g_private_get (current_number_key);
546  *
547  *     if (!current_number)
548  *       {
549  *         current_number = g_new (int, 1);
550  *         *current_number = 0;
551  *         g_private_set (current_number_key, current_number);
552  *       }
553  *
554  *     *current_number = calc_next_number (*current_number);
555  *
556  *     return *current_number;
557  *   }
558  *  </programlisting>
559  * </example>
560  *
561  * Here the pointer belonging to the key
562  * <literal>current_number_key</literal> is read. If it is %NULL, it has
563  * not been set yet. Then get memory for an integer value, assign this
564  * memory to the pointer and write the pointer back. Now we have an
565  * integer value that is private to the current thread.
566  *
567  * The #GPrivate struct should only be accessed via the following
568  * functions.
569  *
570  * <note><para>All of the <function>g_private_*</function> functions are
571  * actually macros. Apart from taking their addresses, you can however
572  * use them as if they were functions.</para></note>
573  **/
574
575 /* GThread Virtual Functions {{{2 ---------------------------------------- */
576 /**
577  * GThread:
578  *
579  * The #GThread struct represents a running thread. It has three public
580  * read-only members, but the underlying struct is bigger, so you must
581  * not copy this struct.
582  *
583  * <note><para>Resources for a joinable thread are not fully released
584  * until g_thread_join() is called for that thread.</para></note>
585  **/
586
587 /**
588  * GThreadFunc:
589  * @data: data passed to the thread.
590  * @Returns: the return value of the thread, which will be returned by
591  *           g_thread_join().
592  *
593  * Specifies the type of the @func functions passed to
594  * g_thread_create() or g_thread_create_full().
595  **/
596
597 /**
598  * GThreadPriority:
599  * @G_THREAD_PRIORITY_LOW: a priority lower than normal
600  * @G_THREAD_PRIORITY_NORMAL: the default priority
601  * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
602  * @G_THREAD_PRIORITY_URGENT: the highest priority
603  *
604  * Deprecated:2.32: thread priorities no longer have any effect.
605  **/
606
607 /* Local Data {{{1 -------------------------------------------------------- */
608
609 static GMutex    g_once_mutex = G_MUTEX_INIT;
610 static GCond     g_once_cond = G_COND_INIT;
611 static GPrivate  g_thread_specific_private;
612 static GRealThread *g_thread_all_threads = NULL;
613 static GSList   *g_thread_free_indices = NULL;
614 static GSList*   g_once_init_list = NULL;
615
616 G_LOCK_DEFINE_STATIC (g_thread);
617
618 /* Initialisation {{{1 ---------------------------------------------------- */
619
620 /**
621  * g_thread_init:
622  * @vtable: a function table of type #GThreadFunctions, that provides
623  *     the entry points to the thread system to be used. Since 2.32,
624  *     this parameter is ignored and should always be %NULL
625  *
626  * If you use GLib from more than one thread, you must initialize the
627  * thread system by calling g_thread_init().
628  *
629  * Since version 2.24, calling g_thread_init() multiple times is allowed,
630  * but nothing happens except for the first call.
631  *
632  * Since version 2.32, GLib does not support custom thread implementations
633  * anymore and the @vtable parameter is ignored and you should pass %NULL.
634  *
635  * <note><para>g_thread_init() must not be called directly or indirectly
636  * in a callback from GLib. Also no mutexes may be currently locked while
637  * calling g_thread_init().</para></note>
638  *
639  * <note><para>To use g_thread_init() in your program, you have to link
640  * with the libraries that the command <command>pkg-config --libs
641  * gthread-2.0</command> outputs. This is not the case for all the
642  * other thread-related functions of GLib. Those can be used without
643  * having to link with the thread libraries.</para></note>
644  */
645
646 void
647 g_thread_init_glib (void)
648 {
649   static gboolean already_done;
650   GRealThread* main_thread;
651
652   if (already_done)
653     return;
654
655   already_done = TRUE;
656
657   /* We let the main thread (the one that calls g_thread_init) inherit
658    * the static_private data set before calling g_thread_init
659    */
660   main_thread = (GRealThread*) g_thread_self ();
661
662   /* setup the basic threading system */
663   g_threads_got_initialized = TRUE;
664   g_private_init (&g_thread_specific_private, g_thread_cleanup);
665   g_private_set (&g_thread_specific_private, main_thread);
666   g_system_thread_self (&main_thread->system_thread);
667
668   /* accomplish log system initialization to enable messaging */
669   _g_messages_thread_init_nomessage ();
670 }
671
672 /* The following sections implement: GOnce, GStaticMutex, GStaticRecMutex,
673  * GStaticPrivate,
674  **/
675
676 /* GOnce {{{1 ------------------------------------------------------------- */
677
678 /**
679  * GOnce:
680  * @status: the status of the #GOnce
681  * @retval: the value returned by the call to the function, if @status
682  *          is %G_ONCE_STATUS_READY
683  *
684  * A #GOnce struct controls a one-time initialization function. Any
685  * one-time initialization function must have its own unique #GOnce
686  * struct.
687  *
688  * Since: 2.4
689  **/
690
691 /**
692  * G_ONCE_INIT:
693  *
694  * A #GOnce must be initialized with this macro before it can be used.
695  *
696  * <informalexample>
697  *  <programlisting>
698  *   GOnce my_once = G_ONCE_INIT;
699  *  </programlisting>
700  * </informalexample>
701  *
702  * Since: 2.4
703  **/
704
705 /**
706  * GOnceStatus:
707  * @G_ONCE_STATUS_NOTCALLED: the function has not been called yet.
708  * @G_ONCE_STATUS_PROGRESS: the function call is currently in progress.
709  * @G_ONCE_STATUS_READY: the function has been called.
710  *
711  * The possible statuses of a one-time initialization function
712  * controlled by a #GOnce struct.
713  *
714  * Since: 2.4
715  **/
716
717 /**
718  * g_once:
719  * @once: a #GOnce structure
720  * @func: the #GThreadFunc function associated to @once. This function
721  *        is called only once, regardless of the number of times it and
722  *        its associated #GOnce struct are passed to g_once().
723  * @arg: data to be passed to @func
724  *
725  * The first call to this routine by a process with a given #GOnce
726  * struct calls @func with the given argument. Thereafter, subsequent
727  * calls to g_once()  with the same #GOnce struct do not call @func
728  * again, but return the stored result of the first call. On return
729  * from g_once(), the status of @once will be %G_ONCE_STATUS_READY.
730  *
731  * For example, a mutex or a thread-specific data key must be created
732  * exactly once. In a threaded environment, calling g_once() ensures
733  * that the initialization is serialized across multiple threads.
734  *
735  * <note><para>Calling g_once() recursively on the same #GOnce struct in
736  * @func will lead to a deadlock.</para></note>
737  *
738  * <informalexample>
739  *  <programlisting>
740  *   gpointer
741  *   get_debug_flags (void)
742  *   {
743  *     static GOnce my_once = G_ONCE_INIT;
744  *
745  *     g_once (&my_once, parse_debug_flags, NULL);
746  *
747  *     return my_once.retval;
748  *   }
749  *  </programlisting>
750  * </informalexample>
751  *
752  * Since: 2.4
753  **/
754 gpointer
755 g_once_impl (GOnce       *once,
756              GThreadFunc  func,
757              gpointer     arg)
758 {
759   g_mutex_lock (&g_once_mutex);
760
761   while (once->status == G_ONCE_STATUS_PROGRESS)
762     g_cond_wait (&g_once_cond, &g_once_mutex);
763
764   if (once->status != G_ONCE_STATUS_READY)
765     {
766       once->status = G_ONCE_STATUS_PROGRESS;
767       g_mutex_unlock (&g_once_mutex);
768
769       once->retval = func (arg);
770
771       g_mutex_lock (&g_once_mutex);
772       once->status = G_ONCE_STATUS_READY;
773       g_cond_broadcast (&g_once_cond);
774     }
775
776   g_mutex_unlock (&g_once_mutex);
777
778   return once->retval;
779 }
780
781 /**
782  * g_once_init_enter:
783  * @value_location: location of a static initializable variable
784  *                  containing 0.
785  * @Returns: %TRUE if the initialization section should be entered,
786  *           %FALSE and blocks otherwise
787  *
788  * Function to be called when starting a critical initialization
789  * section. The argument @value_location must point to a static
790  * 0-initialized variable that will be set to a value other than 0 at
791  * the end of the initialization section. In combination with
792  * g_once_init_leave() and the unique address @value_location, it can
793  * be ensured that an initialization section will be executed only once
794  * during a program's life time, and that concurrent threads are
795  * blocked until initialization completed. To be used in constructs
796  * like this:
797  *
798  * <informalexample>
799  *  <programlisting>
800  *   static gsize initialization_value = 0;
801  *
802  *   if (g_once_init_enter (&amp;initialization_value))
803  *     {
804  *       gsize setup_value = 42; /<!-- -->* initialization code here *<!-- -->/
805  *
806  *       g_once_init_leave (&amp;initialization_value, setup_value);
807  *     }
808  *
809  *   /<!-- -->* use initialization_value here *<!-- -->/
810  *  </programlisting>
811  * </informalexample>
812  *
813  * Since: 2.14
814  **/
815 gboolean
816 g_once_init_enter_impl (volatile gsize *value_location)
817 {
818   gboolean need_init = FALSE;
819   g_mutex_lock (&g_once_mutex);
820   if (g_atomic_pointer_get (value_location) == NULL)
821     {
822       if (!g_slist_find (g_once_init_list, (void*) value_location))
823         {
824           need_init = TRUE;
825           g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
826         }
827       else
828         do
829           g_cond_wait (&g_once_cond, &g_once_mutex);
830         while (g_slist_find (g_once_init_list, (void*) value_location));
831     }
832   g_mutex_unlock (&g_once_mutex);
833   return need_init;
834 }
835
836 /**
837  * g_once_init_leave:
838  * @value_location: location of a static initializable variable
839  *                  containing 0.
840  * @initialization_value: new non-0 value for *@value_location.
841  *
842  * Counterpart to g_once_init_enter(). Expects a location of a static
843  * 0-initialized initialization variable, and an initialization value
844  * other than 0. Sets the variable to the initialization value, and
845  * releases concurrent threads blocking in g_once_init_enter() on this
846  * initialization variable.
847  *
848  * Since: 2.14
849  **/
850 void
851 g_once_init_leave (volatile gsize *value_location,
852                    gsize           initialization_value)
853 {
854   g_return_if_fail (g_atomic_pointer_get (value_location) == NULL);
855   g_return_if_fail (initialization_value != 0);
856   g_return_if_fail (g_once_init_list != NULL);
857
858   g_atomic_pointer_set (value_location, initialization_value);
859   g_mutex_lock (&g_once_mutex);
860   g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
861   g_cond_broadcast (&g_once_cond);
862   g_mutex_unlock (&g_once_mutex);
863 }
864
865 /* GStaticMutex {{{1 ------------------------------------------------------ */
866
867 /**
868  * GStaticMutex:
869  *
870  * A #GStaticMutex works like a #GMutex.
871  *
872  * Prior to GLib 2.32, GStaticMutex had the significant advantage
873  * that it doesn't need to be created at run-time, but can be defined
874  * at compile-time. Since 2.32, #GMutex can be statically allocated
875  * as well, and GStaticMutex has been deprecated.
876  *
877  * Here is a version of our give_me_next_number() example using
878  * a GStaticMutex.
879  *
880  * <example>
881  *  <title>
882  *   Using <structname>GStaticMutex</structname>
883  *   to simplify thread-safe programming
884  *  </title>
885  *  <programlisting>
886  *   int
887  *   give_me_next_number (void)
888  *   {
889  *     static int current_number = 0;
890  *     int ret_val;
891  *     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
892  *
893  *     g_static_mutex_lock (&amp;mutex);
894  *     ret_val = current_number = calc_next_number (current_number);
895  *     g_static_mutex_unlock (&amp;mutex);
896  *
897  *     return ret_val;
898  *   }
899  *  </programlisting>
900  * </example>
901  *
902  * Sometimes you would like to dynamically create a mutex. If you don't
903  * want to require prior calling to g_thread_init(), because your code
904  * should also be usable in non-threaded programs, you are not able to
905  * use g_mutex_new() and thus #GMutex, as that requires a prior call to
906  * g_thread_init(). In theses cases you can also use a #GStaticMutex.
907  * It must be initialized with g_static_mutex_init() before using it
908  * and freed with with g_static_mutex_free() when not needed anymore to
909  * free up any allocated resources.
910  *
911  * Even though #GStaticMutex is not opaque, it should only be used with
912  * the following functions, as it is defined differently on different
913  * platforms.
914  *
915  * All of the <function>g_static_mutex_*</function> functions apart
916  * from <function>g_static_mutex_get_mutex</function> can also be used
917  * even if g_thread_init() has not yet been called. Then they do
918  * nothing, apart from <function>g_static_mutex_trylock</function>,
919  * which does nothing but returning %TRUE.
920  *
921  * <note><para>All of the <function>g_static_mutex_*</function>
922  * functions are actually macros. Apart from taking their addresses, you
923  * can however use them as if they were functions.</para></note>
924  **/
925
926 /**
927  * G_STATIC_MUTEX_INIT:
928  *
929  * A #GStaticMutex must be initialized with this macro, before it can
930  * be used. This macro can used be to initialize a variable, but it
931  * cannot be assigned to a variable. In that case you have to use
932  * g_static_mutex_init().
933  *
934  * |[
935  * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
936  * ]|
937  **/
938
939 /**
940  * g_static_mutex_init:
941  * @mutex: a #GStaticMutex to be initialized.
942  *
943  * Initializes @mutex.
944  * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
945  *
946  * Deprecated: 2.32: Use g_mutex_init()
947  */
948 void
949 g_static_mutex_init (GStaticMutex *mutex)
950 {
951   static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
952
953   g_return_if_fail (mutex);
954
955   *mutex = init_mutex;
956 }
957
958 /* IMPLEMENTATION NOTE:
959  *
960  * On some platforms a GStaticMutex is actually a normal GMutex stored
961  * inside of a structure instead of being allocated dynamically.  We can
962  * only do this for platforms on which we know, in advance, how to
963  * allocate (size) and initialise (value) that memory.
964  *
965  * On other platforms, a GStaticMutex is nothing more than a pointer to
966  * a GMutex.  In that case, the first access we make to the static mutex
967  * must first allocate the normal GMutex and store it into the pointer.
968  *
969  * configure.ac writes macros into glibconfig.h to determine if
970  * g_static_mutex_get_mutex() accesses the structure in memory directly
971  * (on platforms where we are able to do that) or if it ends up here,
972  * where we may have to allocate the GMutex before returning it.
973  */
974
975 /**
976  * g_static_mutex_get_mutex:
977  * @mutex: a #GStaticMutex.
978  * @Returns: the #GMutex corresponding to @mutex.
979  *
980  * For some operations (like g_cond_wait()) you must have a #GMutex
981  * instead of a #GStaticMutex. This function will return the
982  * corresponding #GMutex for @mutex.
983  *
984  * Deprecated: 2.32: Just use a #GMutex
985  */
986 GMutex *
987 g_static_mutex_get_mutex_impl (GMutex** mutex)
988 {
989   GMutex *result;
990
991   if (!g_thread_supported ())
992     return NULL;
993
994   result = g_atomic_pointer_get (mutex);
995
996   if (!result)
997     {
998       g_mutex_lock (&g_once_mutex);
999
1000       result = *mutex;
1001       if (!result)
1002         {
1003           result = g_mutex_new ();
1004           g_atomic_pointer_set (mutex, result);
1005         }
1006
1007       g_mutex_unlock (&g_once_mutex);
1008     }
1009
1010   return result;
1011 }
1012
1013 /* IMPLEMENTATION NOTE:
1014  *
1015  * g_static_mutex_lock(), g_static_mutex_trylock() and
1016  * g_static_mutex_unlock() are all preprocessor macros that wrap the
1017  * corresponding g_mutex_*() function around a call to
1018  * g_static_mutex_get_mutex().
1019  */
1020
1021 /**
1022  * g_static_mutex_lock:
1023  * @mutex: a #GStaticMutex.
1024  *
1025  * Works like g_mutex_lock(), but for a #GStaticMutex.
1026  *
1027  * Deprecated: 2.32: Use g_mutex_lock()
1028  */
1029
1030 /**
1031  * g_static_mutex_trylock:
1032  * @mutex: a #GStaticMutex.
1033  * @Returns: %TRUE, if the #GStaticMutex could be locked.
1034  *
1035  * Works like g_mutex_trylock(), but for a #GStaticMutex.
1036  *
1037  * Deprecated: 2.32: Use g_mutex_trylock()
1038  */
1039
1040 /**
1041  * g_static_mutex_unlock:
1042  * @mutex: a #GStaticMutex.
1043  *
1044  * Works like g_mutex_unlock(), but for a #GStaticMutex.
1045  *
1046  * Deprecated: 2.32: Use g_mutex_unlock()
1047  */
1048
1049 /**
1050  * g_static_mutex_free:
1051  * @mutex: a #GStaticMutex to be freed.
1052  *
1053  * Releases all resources allocated to @mutex.
1054  *
1055  * You don't have to call this functions for a #GStaticMutex with an
1056  * unbounded lifetime, i.e. objects declared 'static', but if you have
1057  * a #GStaticMutex as a member of a structure and the structure is
1058  * freed, you should also free the #GStaticMutex.
1059  *
1060  * <note><para>Calling g_static_mutex_free() on a locked mutex may
1061  * result in undefined behaviour.</para></note>
1062  *
1063  * Deprecated: 2.32: Use g_mutex_free()
1064  */
1065 void
1066 g_static_mutex_free (GStaticMutex* mutex)
1067 {
1068   GMutex **runtime_mutex;
1069
1070   g_return_if_fail (mutex);
1071
1072   /* The runtime_mutex is the first (or only) member of GStaticMutex,
1073    * see both versions (of glibconfig.h) in configure.ac. Note, that
1074    * this variable is NULL, if g_thread_init() hasn't been called or
1075    * if we're using the default thread implementation and it provides
1076    * static mutexes. */
1077   runtime_mutex = ((GMutex**)mutex);
1078
1079   if (*runtime_mutex)
1080     g_mutex_free (*runtime_mutex);
1081
1082   *runtime_mutex = NULL;
1083 }
1084
1085 /* ------------------------------------------------------------------------ */
1086
1087 /**
1088  * GStaticRecMutex:
1089  *
1090  * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
1091  * multiple times by one thread. If you enter it n times, you have to
1092  * unlock it n times again to let other threads lock it. An exception
1093  * is the function g_static_rec_mutex_unlock_full(): that allows you to
1094  * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
1095  * number of times this mutex was locked). The depth can later be used
1096  * to restore the state of the #GStaticRecMutex by calling
1097  * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
1098  * been deprecated in favor of #GRecMutex.
1099  *
1100  * Even though #GStaticRecMutex is not opaque, it should only be used
1101  * with the following functions.
1102  *
1103  * All of the <function>g_static_rec_mutex_*</function> functions can
1104  * be used even if g_thread_init() has not been called. Then they do
1105  * nothing, apart from <function>g_static_rec_mutex_trylock</function>,
1106  * which does nothing but returning %TRUE.
1107  **/
1108
1109 /**
1110  * G_STATIC_REC_MUTEX_INIT:
1111  *
1112  * A #GStaticRecMutex must be initialized with this macro before it can
1113  * be used. This macro can used be to initialize a variable, but it
1114  * cannot be assigned to a variable. In that case you have to use
1115  * g_static_rec_mutex_init().
1116  *
1117  * |[
1118  *   GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
1119  * ]|
1120  */
1121
1122 /**
1123  * g_static_rec_mutex_init:
1124  * @mutex: a #GStaticRecMutex to be initialized.
1125  *
1126  * A #GStaticRecMutex must be initialized with this function before it
1127  * can be used. Alternatively you can initialize it with
1128  * #G_STATIC_REC_MUTEX_INIT.
1129  *
1130  * Deprecated: 2.32: Use g_rec_mutex_init()
1131  */
1132 void
1133 g_static_rec_mutex_init (GStaticRecMutex *mutex)
1134 {
1135   static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
1136
1137   g_return_if_fail (mutex);
1138
1139   *mutex = init_mutex;
1140 }
1141
1142 /**
1143  * g_static_rec_mutex_lock:
1144  * @mutex: a #GStaticRecMutex to lock.
1145  *
1146  * Locks @mutex. If @mutex is already locked by another thread, the
1147  * current thread will block until @mutex is unlocked by the other
1148  * thread. If @mutex is already locked by the calling thread, this
1149  * functions increases the depth of @mutex and returns immediately.
1150  *
1151  * Deprecated: 2.32: Use g_rec_mutex_lock()
1152  */
1153 void
1154 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
1155 {
1156   GSystemThread self;
1157
1158   g_return_if_fail (mutex);
1159
1160   if (!g_thread_supported ())
1161     return;
1162
1163   g_system_thread_self (&self);
1164
1165   if (g_system_thread_equal (&self, &mutex->owner))
1166     {
1167       mutex->depth++;
1168       return;
1169     }
1170   g_static_mutex_lock (&mutex->mutex);
1171   g_system_thread_assign (mutex->owner, self);
1172   mutex->depth = 1;
1173 }
1174
1175 /**
1176  * g_static_rec_mutex_trylock:
1177  * @mutex: a #GStaticRecMutex to lock.
1178  * @Returns: %TRUE, if @mutex could be locked.
1179  *
1180  * Tries to lock @mutex. If @mutex is already locked by another thread,
1181  * it immediately returns %FALSE. Otherwise it locks @mutex and returns
1182  * %TRUE. If @mutex is already locked by the calling thread, this
1183  * functions increases the depth of @mutex and immediately returns
1184  * %TRUE.
1185  *
1186  * Deprecated: 2.32: Use g_rec_mutex_trylock()
1187  */
1188 gboolean
1189 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
1190 {
1191   GSystemThread self;
1192
1193   g_return_val_if_fail (mutex, FALSE);
1194
1195   if (!g_thread_supported ())
1196     return TRUE;
1197
1198   g_system_thread_self (&self);
1199
1200   if (g_system_thread_equal (&self, &mutex->owner))
1201     {
1202       mutex->depth++;
1203       return TRUE;
1204     }
1205
1206   if (!g_static_mutex_trylock (&mutex->mutex))
1207     return FALSE;
1208
1209   g_system_thread_assign (mutex->owner, self);
1210   mutex->depth = 1;
1211   return TRUE;
1212 }
1213
1214 /**
1215  * g_static_rec_mutex_unlock:
1216  * @mutex: a #GStaticRecMutex to unlock.
1217  *
1218  * Unlocks @mutex. Another thread will be allowed to lock @mutex only
1219  * when it has been unlocked as many times as it had been locked
1220  * before. If @mutex is completely unlocked and another thread is
1221  * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
1222  * woken and can lock @mutex itself.
1223  *
1224  * Deprecated: 2.32: Use g_rec_mutex_unlock()
1225  */
1226 void
1227 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
1228 {
1229   g_return_if_fail (mutex);
1230
1231   if (!g_thread_supported ())
1232     return;
1233
1234   if (mutex->depth > 1)
1235     {
1236       mutex->depth--;
1237       return;
1238     }
1239   g_system_thread_assign (mutex->owner, zero_thread);
1240   g_static_mutex_unlock (&mutex->mutex);
1241 }
1242
1243 /**
1244  * g_static_rec_mutex_lock_full:
1245  * @mutex: a #GStaticRecMutex to lock.
1246  * @depth: number of times this mutex has to be unlocked to be
1247  *         completely unlocked.
1248  *
1249  * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
1250  *
1251  * Deprecated: 2.32: Use g_rec_mutex_lock()
1252  */
1253 void
1254 g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
1255                                 guint            depth)
1256 {
1257   GSystemThread self;
1258   g_return_if_fail (mutex);
1259
1260   if (!g_thread_supported ())
1261     return;
1262
1263   if (depth == 0)
1264     return;
1265
1266   g_system_thread_self (&self);
1267
1268   if (g_system_thread_equal (&self, &mutex->owner))
1269     {
1270       mutex->depth += depth;
1271       return;
1272     }
1273   g_static_mutex_lock (&mutex->mutex);
1274   g_system_thread_assign (mutex->owner, self);
1275   mutex->depth = depth;
1276 }
1277
1278 /**
1279  * g_static_rec_mutex_unlock_full:
1280  * @mutex: a #GStaticRecMutex to completely unlock.
1281  * @Returns: number of times @mutex has been locked by the current
1282  *           thread.
1283  *
1284  * Completely unlocks @mutex. If another thread is blocked in a
1285  * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
1286  * lock @mutex itself. This function returns the number of times that
1287  * @mutex has been locked by the current thread. To restore the state
1288  * before the call to g_static_rec_mutex_unlock_full() you can call
1289  * g_static_rec_mutex_lock_full() with the depth returned by this
1290  * function.
1291  *
1292  * Deprecated: 2.32: Use g_rec_mutex_unlock()
1293  */
1294 guint
1295 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
1296 {
1297   guint depth;
1298
1299   g_return_val_if_fail (mutex, 0);
1300
1301   if (!g_thread_supported ())
1302     return 1;
1303
1304   depth = mutex->depth;
1305
1306   g_system_thread_assign (mutex->owner, zero_thread);
1307   mutex->depth = 0;
1308   g_static_mutex_unlock (&mutex->mutex);
1309
1310   return depth;
1311 }
1312
1313 /**
1314  * g_static_rec_mutex_free:
1315  * @mutex: a #GStaticRecMutex to be freed.
1316  *
1317  * Releases all resources allocated to a #GStaticRecMutex.
1318  *
1319  * You don't have to call this functions for a #GStaticRecMutex with an
1320  * unbounded lifetime, i.e. objects declared 'static', but if you have
1321  * a #GStaticRecMutex as a member of a structure and the structure is
1322  * freed, you should also free the #GStaticRecMutex.
1323  *
1324  * Deprecated: 2.32: Use g_rec_mutex_clear()
1325  */
1326 void
1327 g_static_rec_mutex_free (GStaticRecMutex *mutex)
1328 {
1329   g_return_if_fail (mutex);
1330
1331   g_static_mutex_free (&mutex->mutex);
1332 }
1333
1334 /* GStaticPrivate {{{1 ---------------------------------------------------- */
1335
1336 /**
1337  * GStaticPrivate:
1338  *
1339  * A #GStaticPrivate works almost like a #GPrivate, but it has one
1340  * significant advantage. It doesn't need to be created at run-time
1341  * like a #GPrivate, but can be defined at compile-time. This is
1342  * similar to the difference between #GMutex and #GStaticMutex. Now
1343  * look at our <function>give_me_next_number()</function> example with
1344  * #GStaticPrivate:
1345  *
1346  * <example>
1347  *  <title>Using GStaticPrivate for per-thread data</title>
1348  *  <programlisting>
1349  *   int
1350  *   give_me_next_number (<!-- -->)
1351  *   {
1352  *     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1353  *     int *current_number = g_static_private_get (&amp;current_number_key);
1354  *
1355  *     if (!current_number)
1356  *       {
1357  *         current_number = g_new (int,1);
1358  *         *current_number = 0;
1359  *         g_static_private_set (&amp;current_number_key, current_number, g_free);
1360  *       }
1361  *
1362  *     *current_number = calc_next_number (*current_number);
1363  *
1364  *     return *current_number;
1365  *   }
1366  *  </programlisting>
1367  * </example>
1368  **/
1369
1370 /**
1371  * G_STATIC_PRIVATE_INIT:
1372  *
1373  * Every #GStaticPrivate must be initialized with this macro, before it
1374  * can be used.
1375  *
1376  * |[
1377  *   GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1378  * ]|
1379  */
1380
1381 /**
1382  * g_static_private_init:
1383  * @private_key: a #GStaticPrivate to be initialized.
1384  *
1385  * Initializes @private_key. Alternatively you can initialize it with
1386  * #G_STATIC_PRIVATE_INIT.
1387  **/
1388 void
1389 g_static_private_init (GStaticPrivate *private_key)
1390 {
1391   private_key->index = 0;
1392 }
1393
1394 /**
1395  * g_static_private_get:
1396  * @private_key: a #GStaticPrivate.
1397  * @Returns: the corresponding pointer.
1398  *
1399  * Works like g_private_get() only for a #GStaticPrivate.
1400  *
1401  * This function works even if g_thread_init() has not yet been called.
1402  */
1403 gpointer
1404 g_static_private_get (GStaticPrivate *private_key)
1405 {
1406   GRealThread *self = (GRealThread*) g_thread_self ();
1407   GArray *array;
1408   gpointer ret = NULL;
1409
1410   LOCK_PRIVATE_DATA (self);
1411
1412   array = self->private_data;
1413
1414   if (array && private_key->index != 0 && private_key->index <= array->len)
1415     ret = g_array_index (array, GStaticPrivateNode,
1416                          private_key->index - 1).data;
1417
1418   UNLOCK_PRIVATE_DATA (self);
1419   return ret;
1420 }
1421
1422 /**
1423  * g_static_private_set:
1424  * @private_key: a #GStaticPrivate.
1425  * @data: the new pointer.
1426  * @notify: a function to be called with the pointer whenever the
1427  *          current thread ends or sets this pointer again.
1428  *
1429  * Sets the pointer keyed to @private_key for the current thread and
1430  * the function @notify to be called with that pointer (%NULL or
1431  * non-%NULL), whenever the pointer is set again or whenever the
1432  * current thread ends.
1433  *
1434  * This function works even if g_thread_init() has not yet been called.
1435  * If g_thread_init() is called later, the @data keyed to @private_key
1436  * will be inherited only by the main thread, i.e. the one that called
1437  * g_thread_init().
1438  *
1439  * <note><para>@notify is used quite differently from @destructor in
1440  * g_private_new().</para></note>
1441  */
1442 void
1443 g_static_private_set (GStaticPrivate *private_key,
1444                       gpointer        data,
1445                       GDestroyNotify  notify)
1446 {
1447   GRealThread *self = (GRealThread*) g_thread_self ();
1448   GArray *array;
1449   static guint next_index = 0;
1450   GStaticPrivateNode *node;
1451   gpointer ddata = NULL;
1452   GDestroyNotify ddestroy = NULL;
1453
1454   if (!private_key->index)
1455     {
1456       G_LOCK (g_thread);
1457
1458       if (!private_key->index)
1459         {
1460           if (g_thread_free_indices)
1461             {
1462               private_key->index =
1463                 GPOINTER_TO_UINT (g_thread_free_indices->data);
1464               g_thread_free_indices =
1465                 g_slist_delete_link (g_thread_free_indices,
1466                                      g_thread_free_indices);
1467             }
1468           else
1469             private_key->index = ++next_index;
1470         }
1471
1472       G_UNLOCK (g_thread);
1473     }
1474
1475   LOCK_PRIVATE_DATA (self);
1476
1477   array = self->private_data;
1478   if (!array)
1479     {
1480       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1481       self->private_data = array;
1482     }
1483
1484   if (private_key->index > array->len)
1485     g_array_set_size (array, private_key->index);
1486
1487   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1488
1489   ddata = node->data;
1490   ddestroy = node->destroy;
1491
1492   node->data = data;
1493   node->destroy = notify;
1494
1495   UNLOCK_PRIVATE_DATA (self);
1496
1497   if (ddestroy)
1498     ddestroy (ddata);
1499 }
1500
1501 /**
1502  * g_static_private_free:
1503  * @private_key: a #GStaticPrivate to be freed.
1504  *
1505  * Releases all resources allocated to @private_key.
1506  *
1507  * You don't have to call this functions for a #GStaticPrivate with an
1508  * unbounded lifetime, i.e. objects declared 'static', but if you have
1509  * a #GStaticPrivate as a member of a structure and the structure is
1510  * freed, you should also free the #GStaticPrivate.
1511  */
1512 void
1513 g_static_private_free (GStaticPrivate *private_key)
1514 {
1515   guint idx = private_key->index;
1516   GRealThread *thread, *next;
1517   GArray *garbage = NULL;
1518
1519   if (!idx)
1520     return;
1521
1522   private_key->index = 0;
1523
1524   G_LOCK (g_thread);
1525
1526   thread = g_thread_all_threads;
1527
1528   for (thread = g_thread_all_threads; thread; thread = next)
1529     {
1530       GArray *array;
1531
1532       next = thread->next;
1533
1534       LOCK_PRIVATE_DATA (thread);
1535
1536       array = thread->private_data;
1537
1538       if (array && idx <= array->len)
1539         {
1540           GStaticPrivateNode *node = &g_array_index (array,
1541                                                      GStaticPrivateNode,
1542                                                      idx - 1);
1543           gpointer ddata = node->data;
1544           GDestroyNotify ddestroy = node->destroy;
1545
1546           node->data = NULL;
1547           node->destroy = NULL;
1548
1549           if (ddestroy)
1550             {
1551               /* defer non-trivial destruction til after we've finished
1552                * iterating, since we must continue to hold the lock */
1553               if (garbage == NULL)
1554                 garbage = g_array_new (FALSE, TRUE,
1555                                        sizeof (GStaticPrivateNode));
1556
1557               g_array_set_size (garbage, garbage->len + 1);
1558
1559               node = &g_array_index (garbage, GStaticPrivateNode,
1560                                      garbage->len - 1);
1561               node->data = ddata;
1562               node->destroy = ddestroy;
1563             }
1564         }
1565
1566       UNLOCK_PRIVATE_DATA (thread);
1567     }
1568   g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1569                                            GUINT_TO_POINTER (idx));
1570   G_UNLOCK (g_thread);
1571
1572   if (garbage)
1573     {
1574       guint i;
1575
1576       for (i = 0; i < garbage->len; i++)
1577         {
1578           GStaticPrivateNode *node;
1579
1580           node = &g_array_index (garbage, GStaticPrivateNode, i);
1581           node->destroy (node->data);
1582         }
1583
1584       g_array_free (garbage, TRUE);
1585     }
1586 }
1587
1588 /* GThread Extra Functions {{{1 ------------------------------------------- */
1589 static void
1590 g_thread_cleanup (gpointer data)
1591 {
1592   if (data)
1593     {
1594       GRealThread* thread = data;
1595       GArray *array;
1596
1597       LOCK_PRIVATE_DATA (thread);
1598       array = thread->private_data;
1599       thread->private_data = NULL;
1600       UNLOCK_PRIVATE_DATA (thread);
1601
1602       if (array)
1603         {
1604           guint i;
1605
1606           for (i = 0; i < array->len; i++ )
1607             {
1608               GStaticPrivateNode *node =
1609                 &g_array_index (array, GStaticPrivateNode, i);
1610               if (node->destroy)
1611                 node->destroy (node->data);
1612             }
1613           g_array_free (array, TRUE);
1614         }
1615
1616       /* We only free the thread structure, if it isn't joinable. If
1617          it is, the structure is freed in g_thread_join */
1618       if (!thread->thread.joinable)
1619         {
1620           GRealThread *t, *p;
1621
1622           G_LOCK (g_thread);
1623           for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
1624             {
1625               if (t == thread)
1626                 {
1627                   if (p)
1628                     p->next = t->next;
1629                   else
1630                     g_thread_all_threads = t->next;
1631                   break;
1632                 }
1633             }
1634           G_UNLOCK (g_thread);
1635
1636           /* Just to make sure, this isn't used any more */
1637           g_system_thread_assign (thread->system_thread, zero_thread);
1638           g_free (thread);
1639         }
1640     }
1641 }
1642
1643 #define G_NSEC_PER_SEC 1000000000
1644
1645 static guint64
1646 gettime (void)
1647 {
1648   return g_get_monotonic_time () * 1000;
1649 }
1650
1651 static gpointer
1652 g_thread_create_proxy (gpointer data)
1653 {
1654   GRealThread* thread = data;
1655
1656   g_assert (data);
1657
1658   /* This has to happen before G_LOCK, as that might call g_thread_self */
1659   g_private_set (&g_thread_specific_private, data);
1660
1661   /* the lock makes sure, that thread->system_thread is written,
1662      before thread->thread.func is called. See g_thread_create. */
1663   G_LOCK (g_thread);
1664   G_UNLOCK (g_thread);
1665
1666   thread->retval = thread->thread.func (thread->thread.data);
1667
1668   return NULL;
1669 }
1670
1671 /**
1672  * g_thread_create:
1673  * @func: a function to execute in the new thread
1674  * @data: an argument to supply to the new thread
1675  * @joinable: should this thread be joinable?
1676  * @error: return location for error, or %NULL
1677  *
1678  * This function creates a new thread.
1679  *
1680  * If @joinable is %TRUE, you can wait for this threads termination
1681  * calling g_thread_join(). Otherwise the thread will just disappear
1682  * when it terminates.
1683  *
1684  * The new thread executes the function @func with the argument @data.
1685  * If the thread was created successfully, it is returned.
1686  *
1687  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
1688  * The error is set, if and only if the function returns %NULL.
1689  *
1690  * Returns: the new #GThread on success
1691  */
1692 GThread *
1693 g_thread_create (GThreadFunc   func,
1694                  gpointer      data,
1695                  gboolean      joinable,
1696                  GError      **error)
1697 {
1698   return g_thread_create_with_stack_size (func, data, joinable, 0, error);
1699 }
1700
1701 /**
1702  * g_thread_create_with_stack_size:
1703  * @func: a function to execute in the new thread.
1704  * @data: an argument to supply to the new thread.
1705  * @joinable: should this thread be joinable?
1706  * @stack_size: a stack size for the new thread.
1707  * @error: return location for error.
1708  * @Returns: the new #GThread on success.
1709  *
1710  * This function creates a new thread. If the underlying thread
1711  * implementation supports it, the thread gets a stack size of
1712  * @stack_size or the default value for the current platform, if
1713  * @stack_size is 0.
1714  *
1715  * If @joinable is %TRUE, you can wait for this threads termination
1716  * calling g_thread_join(). Otherwise the thread will just disappear
1717  * when it terminates.
1718  *
1719  * The new thread executes the function @func with the argument @data.
1720  * If the thread was created successfully, it is returned.
1721  *
1722  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
1723  * The error is set, if and only if the function returns %NULL.
1724  *
1725  * <note><para>
1726  *   Only use g_thread_create_with_stack_size() if you really can't use
1727  *   g_thread_create() instead. g_thread_create() does not take
1728  *   @stack_size, as it should only be used in cases in which it is
1729  *   unavoidable.
1730  * </para></note>
1731  **/
1732 GThread*
1733 g_thread_create_with_stack_size (GThreadFunc   func,
1734                                  gpointer      data,
1735                                  gboolean      joinable,
1736                                  gsize         stack_size,
1737                                  GError      **error)
1738 {
1739   GRealThread* result;
1740   GError *local_error = NULL;
1741   g_return_val_if_fail (func, NULL);
1742
1743   result = g_new0 (GRealThread, 1);
1744
1745   result->thread.joinable = joinable;
1746   result->thread.func = func;
1747   result->thread.data = data;
1748   result->private_data = NULL;
1749   G_LOCK (g_thread);
1750   g_system_thread_create (g_thread_create_proxy, result,
1751                           stack_size, joinable,
1752                           &result->system_thread, &local_error);
1753   if (!local_error)
1754     {
1755       result->next = g_thread_all_threads;
1756       g_thread_all_threads = result;
1757     }
1758   G_UNLOCK (g_thread);
1759
1760   if (local_error)
1761     {
1762       g_propagate_error (error, local_error);
1763       g_free (result);
1764       return NULL;
1765     }
1766
1767   return (GThread*) result;
1768 }
1769
1770 /**
1771  * g_thread_create_full:
1772  * @func: a function to execute in the new thread.
1773  * @data: an argument to supply to the new thread.
1774  * @stack_size: a stack size for the new thread.
1775  * @joinable: should this thread be joinable?
1776  * @bound: ignored
1777  * @priority: ignored
1778  * @error: return location for error.
1779  * @Returns: the new #GThread on success.
1780  *
1781  * This function creates a new thread.
1782  *
1783  * Deprecated:2.32: The @bound and @priority arguments are now ignored.
1784  * Use g_thread_create() or g_thread_create_with_stack_size() instead.
1785  */
1786 GThread *
1787 g_thread_create_full (GThreadFunc       func,
1788                       gpointer          data,
1789                       gulong            stack_size,
1790                       gboolean          joinable,
1791                       gboolean          bound,
1792                       GThreadPriority   priority,
1793                       GError          **error)
1794 {
1795   return g_thread_create_with_stack_size (func, data, joinable, stack_size, error);
1796 }
1797
1798 /**
1799  * g_thread_exit:
1800  * @retval: the return value of this thread.
1801  *
1802  * Exits the current thread. If another thread is waiting for that
1803  * thread using g_thread_join() and the current thread is joinable, the
1804  * waiting thread will be woken up and get @retval as the return value
1805  * of g_thread_join(). If the current thread is not joinable, @retval
1806  * is ignored. Calling
1807  *
1808  * |[
1809  *   g_thread_exit (retval);
1810  * ]|
1811  *
1812  * is equivalent to returning @retval from the function @func, as given
1813  * to g_thread_create().
1814  *
1815  * <note><para>Never call g_thread_exit() from within a thread of a
1816  * #GThreadPool, as that will mess up the bookkeeping and lead to funny
1817  * and unwanted results.</para></note>
1818  **/
1819 void
1820 g_thread_exit (gpointer retval)
1821 {
1822   GRealThread* real = (GRealThread*) g_thread_self ();
1823   real->retval = retval;
1824
1825   g_system_thread_exit ();
1826 }
1827
1828 /**
1829  * g_thread_join:
1830  * @thread: a #GThread to be waited for.
1831  * @Returns: the return value of the thread.
1832  *
1833  * Waits until @thread finishes, i.e. the function @func, as given to
1834  * g_thread_create(), returns or g_thread_exit() is called by @thread.
1835  * All resources of @thread including the #GThread struct are released.
1836  * @thread must have been created with @joinable=%TRUE in
1837  * g_thread_create(). The value returned by @func or given to
1838  * g_thread_exit() by @thread is returned by this function.
1839  **/
1840 gpointer
1841 g_thread_join (GThread* thread)
1842 {
1843   GRealThread* real = (GRealThread*) thread;
1844   GRealThread *p, *t;
1845   gpointer retval;
1846
1847   g_return_val_if_fail (thread, NULL);
1848   g_return_val_if_fail (thread->joinable, NULL);
1849   g_return_val_if_fail (!g_system_thread_equal (&real->system_thread, &zero_thread), NULL);
1850
1851   g_system_thread_join (&real->system_thread);
1852
1853   retval = real->retval;
1854
1855   G_LOCK (g_thread);
1856   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
1857     {
1858       if (t == (GRealThread*) thread)
1859         {
1860           if (p)
1861             p->next = t->next;
1862           else
1863             g_thread_all_threads = t->next;
1864           break;
1865         }
1866     }
1867   G_UNLOCK (g_thread);
1868
1869   /* Just to make sure, this isn't used any more */
1870   thread->joinable = 0;
1871   g_system_thread_assign (real->system_thread, zero_thread);
1872
1873   /* the thread structure for non-joinable threads is freed upon
1874      thread end. We free the memory here. This will leave a loose end,
1875      if a joinable thread is not joined. */
1876
1877   g_free (thread);
1878
1879   return retval;
1880 }
1881
1882 /**
1883  * g_thread_set_priority:
1884  * @thread: a #GThread.
1885  * @priority: ignored
1886  *
1887  * This function does nothing.
1888  *
1889  * Deprecated:2.32: Thread priorities no longer have any effect.
1890  */
1891 void
1892 g_thread_set_priority (GThread         *thread,
1893                        GThreadPriority  priority)
1894 {
1895 }
1896
1897 /**
1898  * g_thread_self:
1899  * @Returns: the current thread.
1900  *
1901  * This functions returns the #GThread corresponding to the calling
1902  * thread.
1903  **/
1904 GThread*
1905 g_thread_self (void)
1906 {
1907   GRealThread* thread = g_private_get (&g_thread_specific_private);
1908
1909   if (!thread)
1910     {
1911       /* If no thread data is available, provide and set one.  This
1912          can happen for the main thread and for threads, that are not
1913          created by GLib. */
1914       thread = g_new0 (GRealThread, 1);
1915       thread->thread.joinable = FALSE; /* This is a save guess */
1916       thread->thread.func = NULL;
1917       thread->thread.data = NULL;
1918       thread->private_data = NULL;
1919
1920       g_system_thread_self (&thread->system_thread);
1921
1922       g_private_set (&g_thread_specific_private, thread);
1923
1924       G_LOCK (g_thread);
1925       thread->next = g_thread_all_threads;
1926       g_thread_all_threads = thread;
1927       G_UNLOCK (g_thread);
1928     }
1929
1930   return (GThread*)thread;
1931 }
1932
1933 /* GStaticRWLock {{{1 ----------------------------------------------------- */
1934
1935 /**
1936  * GStaticRWLock:
1937  *
1938  * The #GStaticRWLock struct represents a read-write lock. A read-write
1939  * lock can be used for protecting data that some portions of code only
1940  * read from, while others also write. In such situations it is
1941  * desirable that several readers can read at once, whereas of course
1942  * only one writer may write at a time. Take a look at the following
1943  * example:
1944  *
1945  * <example>
1946  *  <title>An array with access functions</title>
1947  *  <programlisting>
1948  *   GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
1949  *   GPtrArray *array;
1950  *
1951  *   gpointer
1952  *   my_array_get (guint index)
1953  *   {
1954  *     gpointer retval = NULL;
1955  *
1956  *     if (!array)
1957  *       return NULL;
1958  *
1959  *     g_static_rw_lock_reader_lock (&amp;rwlock);
1960  *     if (index &lt; array->len)
1961  *       retval = g_ptr_array_index (array, index);
1962  *     g_static_rw_lock_reader_unlock (&amp;rwlock);
1963  *
1964  *     return retval;
1965  *   }
1966  *
1967  *   void
1968  *   my_array_set (guint index, gpointer data)
1969  *   {
1970  *     g_static_rw_lock_writer_lock (&amp;rwlock);
1971  *
1972  *     if (!array)
1973  *       array = g_ptr_array_new (<!-- -->);
1974  *
1975  *     if (index >= array->len)
1976  *       g_ptr_array_set_size (array, index+1);
1977  *     g_ptr_array_index (array, index) = data;
1978  *
1979  *     g_static_rw_lock_writer_unlock (&amp;rwlock);
1980  *   }
1981  *  </programlisting>
1982  * </example>
1983  *
1984  * This example shows an array which can be accessed by many readers
1985  * (the <function>my_array_get()</function> function) simultaneously,
1986  * whereas the writers (the <function>my_array_set()</function>
1987  * function) will only be allowed once at a time and only if no readers
1988  * currently access the array. This is because of the potentially
1989  * dangerous resizing of the array. Using these functions is fully
1990  * multi-thread safe now.
1991  *
1992  * Most of the time, writers should have precedence over readers. That
1993  * means, for this implementation, that as soon as a writer wants to
1994  * lock the data, no other reader is allowed to lock the data, whereas,
1995  * of course, the readers that already have locked the data are allowed
1996  * to finish their operation. As soon as the last reader unlocks the
1997  * data, the writer will lock it.
1998  *
1999  * Even though #GStaticRWLock is not opaque, it should only be used
2000  * with the following functions.
2001  *
2002  * All of the <function>g_static_rw_lock_*</function> functions can be
2003  * used even if g_thread_init() has not been called. Then they do
2004  * nothing, apart from <function>g_static_rw_lock_*_trylock</function>,
2005  * which does nothing but returning %TRUE.
2006  *
2007  * <note><para>A read-write lock has a higher overhead than a mutex. For
2008  * example, both g_static_rw_lock_reader_lock() and
2009  * g_static_rw_lock_reader_unlock() have to lock and unlock a
2010  * #GStaticMutex, so it takes at least twice the time to lock and unlock
2011  * a #GStaticRWLock that it does to lock and unlock a #GStaticMutex. So
2012  * only data structures that are accessed by multiple readers, and which
2013  * keep the lock for a considerable time justify a #GStaticRWLock. The
2014  * above example most probably would fare better with a
2015  * #GStaticMutex.</para></note>
2016  *
2017  * Deprecated: 2.32: Use a #GRWLock instead
2018  **/
2019
2020 /**
2021  * G_STATIC_RW_LOCK_INIT:
2022  *
2023  * A #GStaticRWLock must be initialized with this macro before it can
2024  * be used. This macro can used be to initialize a variable, but it
2025  * cannot be assigned to a variable. In that case you have to use
2026  * g_static_rw_lock_init().
2027  *
2028  * |[
2029  *   GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
2030  * ]|
2031  */
2032
2033 /**
2034  * g_static_rw_lock_init:
2035  * @lock: a #GStaticRWLock to be initialized.
2036  *
2037  * A #GStaticRWLock must be initialized with this function before it
2038  * can be used. Alternatively you can initialize it with
2039  * #G_STATIC_RW_LOCK_INIT.
2040  *
2041  * Deprecated: 2.32: Use g_rw_lock_init() instead
2042  */
2043 void
2044 g_static_rw_lock_init (GStaticRWLock* lock)
2045 {
2046   static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
2047
2048   g_return_if_fail (lock);
2049
2050   *lock = init_lock;
2051 }
2052
2053 inline static void
2054 g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
2055 {
2056   if (!*cond)
2057       *cond = g_cond_new ();
2058   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
2059 }
2060
2061 inline static void
2062 g_static_rw_lock_signal (GStaticRWLock* lock)
2063 {
2064   if (lock->want_to_write && lock->write_cond)
2065     g_cond_signal (lock->write_cond);
2066   else if (lock->want_to_read && lock->read_cond)
2067     g_cond_broadcast (lock->read_cond);
2068 }
2069
2070 /**
2071  * g_static_rw_lock_reader_lock:
2072  * @lock: a #GStaticRWLock to lock for reading.
2073  *
2074  * Locks @lock for reading. There may be unlimited concurrent locks for
2075  * reading of a #GStaticRWLock at the same time.  If @lock is already
2076  * locked for writing by another thread or if another thread is already
2077  * waiting to lock @lock for writing, this function will block until
2078  * @lock is unlocked by the other writing thread and no other writing
2079  * threads want to lock @lock. This lock has to be unlocked by
2080  * g_static_rw_lock_reader_unlock().
2081  *
2082  * #GStaticRWLock is not recursive. It might seem to be possible to
2083  * recursively lock for reading, but that can result in a deadlock, due
2084  * to writer preference.
2085  *
2086  * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
2087  */
2088 void
2089 g_static_rw_lock_reader_lock (GStaticRWLock* lock)
2090 {
2091   g_return_if_fail (lock);
2092
2093   if (!g_threads_got_initialized)
2094     return;
2095
2096   g_static_mutex_lock (&lock->mutex);
2097   lock->want_to_read++;
2098   while (lock->have_writer || lock->want_to_write)
2099     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
2100   lock->want_to_read--;
2101   lock->read_counter++;
2102   g_static_mutex_unlock (&lock->mutex);
2103 }
2104
2105 /**
2106  * g_static_rw_lock_reader_trylock:
2107  * @lock: a #GStaticRWLock to lock for reading.
2108  * @Returns: %TRUE, if @lock could be locked for reading.
2109  *
2110  * Tries to lock @lock for reading. If @lock is already locked for
2111  * writing by another thread or if another thread is already waiting to
2112  * lock @lock for writing, immediately returns %FALSE. Otherwise locks
2113  * @lock for reading and returns %TRUE. This lock has to be unlocked by
2114  * g_static_rw_lock_reader_unlock().
2115  *
2116  * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
2117  */
2118 gboolean
2119 g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
2120 {
2121   gboolean ret_val = FALSE;
2122
2123   g_return_val_if_fail (lock, FALSE);
2124
2125   if (!g_threads_got_initialized)
2126     return TRUE;
2127
2128   g_static_mutex_lock (&lock->mutex);
2129   if (!lock->have_writer && !lock->want_to_write)
2130     {
2131       lock->read_counter++;
2132       ret_val = TRUE;
2133     }
2134   g_static_mutex_unlock (&lock->mutex);
2135   return ret_val;
2136 }
2137
2138 /**
2139  * g_static_rw_lock_reader_unlock:
2140  * @lock: a #GStaticRWLock to unlock after reading.
2141  *
2142  * Unlocks @lock. If a thread waits to lock @lock for writing and all
2143  * locks for reading have been unlocked, the waiting thread is woken up
2144  * and can lock @lock for writing.
2145  *
2146  * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
2147  */
2148 void
2149 g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
2150 {
2151   g_return_if_fail (lock);
2152
2153   if (!g_threads_got_initialized)
2154     return;
2155
2156   g_static_mutex_lock (&lock->mutex);
2157   lock->read_counter--;
2158   if (lock->read_counter == 0)
2159     g_static_rw_lock_signal (lock);
2160   g_static_mutex_unlock (&lock->mutex);
2161 }
2162
2163 /**
2164  * g_static_rw_lock_writer_lock:
2165  * @lock: a #GStaticRWLock to lock for writing.
2166  *
2167  * Locks @lock for writing. If @lock is already locked for writing or
2168  * reading by other threads, this function will block until @lock is
2169  * completely unlocked and then lock @lock for writing. While this
2170  * functions waits to lock @lock, no other thread can lock @lock for
2171  * reading. When @lock is locked for writing, no other thread can lock
2172  * @lock (neither for reading nor writing). This lock has to be
2173  * unlocked by g_static_rw_lock_writer_unlock().
2174  *
2175  * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
2176  */
2177 void
2178 g_static_rw_lock_writer_lock (GStaticRWLock* lock)
2179 {
2180   g_return_if_fail (lock);
2181
2182   if (!g_threads_got_initialized)
2183     return;
2184
2185   g_static_mutex_lock (&lock->mutex);
2186   lock->want_to_write++;
2187   while (lock->have_writer || lock->read_counter)
2188     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
2189   lock->want_to_write--;
2190   lock->have_writer = TRUE;
2191   g_static_mutex_unlock (&lock->mutex);
2192 }
2193
2194 /**
2195  * g_static_rw_lock_writer_trylock:
2196  * @lock: a #GStaticRWLock to lock for writing.
2197  * @Returns: %TRUE, if @lock could be locked for writing.
2198  *
2199  * Tries to lock @lock for writing. If @lock is already locked (for
2200  * either reading or writing) by another thread, it immediately returns
2201  * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
2202  * lock has to be unlocked by g_static_rw_lock_writer_unlock().
2203  *
2204  * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
2205  */
2206 gboolean
2207 g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
2208 {
2209   gboolean ret_val = FALSE;
2210
2211   g_return_val_if_fail (lock, FALSE);
2212
2213   if (!g_threads_got_initialized)
2214     return TRUE;
2215
2216   g_static_mutex_lock (&lock->mutex);
2217   if (!lock->have_writer && !lock->read_counter)
2218     {
2219       lock->have_writer = TRUE;
2220       ret_val = TRUE;
2221     }
2222   g_static_mutex_unlock (&lock->mutex);
2223   return ret_val;
2224 }
2225
2226 /**
2227  * g_static_rw_lock_writer_unlock:
2228  * @lock: a #GStaticRWLock to unlock after writing.
2229  *
2230  * Unlocks @lock. If a thread is waiting to lock @lock for writing and
2231  * all locks for reading have been unlocked, the waiting thread is
2232  * woken up and can lock @lock for writing. If no thread is waiting to
2233  * lock @lock for writing, and some thread or threads are waiting to
2234  * lock @lock for reading, the waiting threads are woken up and can
2235  * lock @lock for reading.
2236  *
2237  * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
2238  */
2239 void
2240 g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
2241 {
2242   g_return_if_fail (lock);
2243
2244   if (!g_threads_got_initialized)
2245     return;
2246
2247   g_static_mutex_lock (&lock->mutex);
2248   lock->have_writer = FALSE;
2249   g_static_rw_lock_signal (lock);
2250   g_static_mutex_unlock (&lock->mutex);
2251 }
2252
2253 /**
2254  * g_static_rw_lock_free:
2255  * @lock: a #GStaticRWLock to be freed.
2256  *
2257  * Releases all resources allocated to @lock.
2258  *
2259  * You don't have to call this functions for a #GStaticRWLock with an
2260  * unbounded lifetime, i.e. objects declared 'static', but if you have
2261  * a #GStaticRWLock as a member of a structure, and the structure is
2262  * freed, you should also free the #GStaticRWLock.
2263  *
2264  * Deprecated: 2.32: Use a #GRWLock instead
2265  */
2266 void
2267 g_static_rw_lock_free (GStaticRWLock* lock)
2268 {
2269   g_return_if_fail (lock);
2270
2271   if (lock->read_cond)
2272     {
2273       g_cond_free (lock->read_cond);
2274       lock->read_cond = NULL;
2275     }
2276   if (lock->write_cond)
2277     {
2278       g_cond_free (lock->write_cond);
2279       lock->write_cond = NULL;
2280     }
2281   g_static_mutex_free (&lock->mutex);
2282 }
2283
2284 /* Unsorted {{{1 ---------------------------------------------------------- */
2285
2286 /**
2287  * g_thread_foreach
2288  * @thread_func: function to call for all GThread structures
2289  * @user_data:   second argument to @thread_func
2290  *
2291  * Call @thread_func on all existing #GThread structures. Note that
2292  * threads may decide to exit while @thread_func is running, so
2293  * without intimate knowledge about the lifetime of foreign threads,
2294  * @thread_func shouldn't access the GThread* pointer passed in as
2295  * first argument. However, @thread_func will not be called for threads
2296  * which are known to have exited already.
2297  *
2298  * Due to thread lifetime checks, this function has an execution complexity
2299  * which is quadratic in the number of existing threads.
2300  *
2301  * Since: 2.10
2302  */
2303 void
2304 g_thread_foreach (GFunc    thread_func,
2305                   gpointer user_data)
2306 {
2307   GSList *slist = NULL;
2308   GRealThread *thread;
2309   g_return_if_fail (thread_func != NULL);
2310   /* snapshot the list of threads for iteration */
2311   G_LOCK (g_thread);
2312   for (thread = g_thread_all_threads; thread; thread = thread->next)
2313     slist = g_slist_prepend (slist, thread);
2314   G_UNLOCK (g_thread);
2315   /* walk the list, skipping non-existent threads */
2316   while (slist)
2317     {
2318       GSList *node = slist;
2319       slist = node->next;
2320       /* check whether the current thread still exists */
2321       G_LOCK (g_thread);
2322       for (thread = g_thread_all_threads; thread; thread = thread->next)
2323         if (thread == node->data)
2324           break;
2325       G_UNLOCK (g_thread);
2326       if (thread)
2327         thread_func (thread, user_data);
2328       g_slist_free_1 (node);
2329     }
2330 }
2331
2332 /**
2333  * g_thread_get_initialized:
2334  *
2335  * Indicates if g_thread_init() has been called.
2336  *
2337  * Returns: %TRUE if threads have been initialized.
2338  *
2339  * Since: 2.20
2340  */
2341 gboolean
2342 g_thread_get_initialized ()
2343 {
2344   return g_thread_supported ();
2345 }
2346
2347 /**
2348  * g_mutex_new:
2349  *
2350  * Allocated and initializes a new #GMutex.
2351  *
2352  * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
2353  */
2354 GMutex *
2355 g_mutex_new (void)
2356 {
2357   GMutex *mutex;
2358
2359   mutex = g_slice_new (GMutex);
2360   g_mutex_init (mutex);
2361
2362   return mutex;
2363 }
2364
2365 /**
2366  * g_mutex_free:
2367  * @mutex: a #GMutex
2368  *
2369  * Destroys a @mutex that has been created with g_mutex_new().
2370  *
2371  * Calling g_mutex_free() on a locked mutex may result
2372  * in undefined behaviour.
2373  */
2374 void
2375 g_mutex_free (GMutex *mutex)
2376 {
2377   g_mutex_clear (mutex);
2378   g_slice_free (GMutex, mutex);
2379 }
2380
2381 /**
2382  * g_cond_new:
2383  *
2384  * Allocates and initializes a new #GCond.
2385  *
2386  * Returns: a newly allocated #GCond. Free with g_cond_free()
2387  */
2388 GCond *
2389 g_cond_new (void)
2390 {
2391   GCond *cond;
2392
2393   cond = g_slice_new (GCond);
2394   g_cond_init (cond);
2395
2396   return cond;
2397 }
2398
2399 /**
2400  * g_cond_free:
2401  * @cond: a #GCond
2402  *
2403  * Destroys a #GCond that has been created with g_cond_new().
2404  */
2405 void
2406 g_cond_free (GCond *cond)
2407 {
2408   g_cond_clear (cond);
2409   g_slice_free (GCond, cond);
2410 }
2411
2412 /**
2413  * g_private_new:
2414  * @destructor: a function to destroy the data keyed to
2415  *     the #GPrivate when a thread ends
2416  *
2417  * Creates a new #GPrivate. If @destructor is non-%NULL, it is a
2418  * pointer to a destructor function. Whenever a thread ends and the
2419  * corresponding pointer keyed to this instance of #GPrivate is
2420  * non-%NULL, the destructor is called with this pointer as the
2421  * argument.
2422  *
2423  * <note><para>
2424  * #GStaticPrivate is a better choice for most uses.
2425  * </para></note>
2426  *
2427  * <note><para>@destructor is used quite differently from @notify in
2428  * g_static_private_set().</para></note>
2429  *
2430  * <note><para>A #GPrivate cannot be freed. Reuse it instead, if you
2431  * can, to avoid shortage, or use #GStaticPrivate.</para></note>
2432  *
2433  * <note><para>This function will abort if g_thread_init() has not been
2434  * called yet.</para></note>
2435  *
2436  * Returns: a newly allocated #GPrivate
2437  */
2438 GPrivate *
2439 g_private_new (GDestroyNotify notify)
2440 {
2441   GPrivate *key;
2442
2443   key = g_slice_new (GPrivate);
2444   g_private_init (key, notify);
2445
2446   return key;
2447 }
2448
2449 GThreadFunctions g_thread_functions_for_glib_use =
2450 {
2451   g_mutex_new,
2452   g_mutex_lock,
2453   g_mutex_trylock,
2454   g_mutex_unlock,
2455   g_mutex_free,
2456   g_cond_new,
2457   g_cond_signal,
2458   g_cond_broadcast,
2459   g_cond_wait,
2460   g_cond_timed_wait,
2461   g_cond_free,
2462   g_private_new,
2463   g_private_get,
2464   g_private_set,
2465   NULL,
2466   g_thread_yield,
2467   NULL,
2468   NULL,
2469   NULL,
2470   NULL,
2471   NULL,
2472 };
2473
2474 /* vim: set foldmethod=marker: */