Add a few comments
[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 /* Prelude {{{1 ----------------------------------------------------------- */
25
26 /*
27  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
28  * file for a list of people on the GLib Team.  See the ChangeLog
29  * files for a list of changes.  These files are distributed with
30  * GLib at ftp://ftp.gtk.org/pub/gtk/.
31  */
32
33 /*
34  * MT safe
35  */
36
37 /* implement gthread.h's inline functions */
38 #define G_IMPLEMENT_INLINES 1
39 #define __G_THREAD_C__
40
41 #include "config.h"
42
43 #include "gthread.h"
44 #include "gthreadprivate.h"
45 #include "deprecated/gthread.h"
46
47 #include <string.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 "garray.h"
61 #include "gslice.h"
62 #include "gslist.h"
63 #include "gtestutils.h"
64
65 /**
66  * SECTION:threads
67  * @title: Threads
68  * @short_description: portable support for threads, mutexes, locks,
69  *     conditions and thread private data
70  * @see_also: #GThreadPool, #GAsyncQueue
71  *
72  * Threads act almost like processes, but unlike processes all threads
73  * of one process share the same memory. This is good, as it provides
74  * easy communication between the involved threads via this shared
75  * memory, and it is bad, because strange things (so called
76  * "Heisenbugs") might happen if the program is not carefully designed.
77  * In particular, due to the concurrent nature of threads, no
78  * assumptions on the order of execution of code running in different
79  * threads can be made, unless order is explicitly forced by the
80  * programmer through synchronization primitives.
81  *
82  * The aim of the thread-related functions in GLib is to provide a
83  * portable means for writing multi-threaded software. There are
84  * primitives for mutexes to protect the access to portions of memory
85  * (#GMutex, #GRecMutex and #GRWLock). There is a facility to use
86  * individual bits for locks (g_bit_lock()). There are primitives
87  * for condition variables to allow synchronization of threads (#GCond).
88  * There are primitives for thread-private data - data that every thread
89  * has a private instance of (#GPrivate, #GStaticPrivate). There are
90  * facilities for one-time initialization (#GOnce, g_once_init_enter()).
91  * Finally there are primitives to create and manage threads (#GThread).
92  *
93  * The threading system is initialized with g_thread_init().
94  * You may call any other glib functions in the main thread before
95  * g_thread_init() as long as g_thread_init() is not called from
96  * a GLib callback, or with any locks held. However, many libraries
97  * above GLib does not support late initialization of threads, so
98  * doing this should be avoided if possible.
99  *
100  * Please note that since version 2.24 the GObject initialization
101  * function g_type_init() initializes threads. Since 2.32, creating
102  * a mainloop will do so too. As a consequence, most applications,
103  * including those using GTK+ will run with threads enabled.
104  *
105  * After calling g_thread_init(), GLib is completely thread safe
106  * (all global data is automatically locked), but individual data
107  * structure instances are not automatically locked for performance
108  * reasons. So, for example you must coordinate accesses to the same
109  * #GHashTable from multiple threads. The two notable exceptions from
110  * this rule are #GMainLoop and #GAsyncQueue, which <emphasis>are</emphasis>
111  * threadsafe and need no further application-level locking to be
112  * accessed from multiple threads.
113  */
114
115 /**
116  * G_THREADS_IMPL_POSIX:
117  *
118  * This macro is defined if POSIX style threads are used.
119  */
120
121 /**
122  * G_THREADS_IMPL_WIN32:
123  *
124  * This macro is defined if Windows style threads are used.
125  */
126
127 /* G_LOCK Documentation {{{1 ---------------------------------------------- */
128
129 /**
130  * G_LOCK_DEFINE:
131  * @name: the name of the lock.
132  *
133  * The %G_LOCK_* macros provide a convenient interface to #GMutex
134  * with the advantage that they will expand to nothing in programs
135  * compiled against a thread-disabled GLib, saving code and memory
136  * there. #G_LOCK_DEFINE defines a lock. It can appear anywhere
137  * variable definitions may appear in programs, i.e. in the first block
138  * of a function or outside of functions. The @name parameter will be
139  * mangled to get the name of the #GMutex. This means that you
140  * can use names of existing variables as the parameter - e.g. the name
141  * of the variable you intent to protect with the lock. Look at our
142  * <function>give_me_next_number()</function> example using the
143  * %G_LOCK_* macros:
144  *
145  * <example>
146  *  <title>Using the %G_LOCK_* convenience macros</title>
147  *  <programlisting>
148  *   G_LOCK_DEFINE (current_number);
149  *
150  *   int
151  *   give_me_next_number (void)
152  *   {
153  *     static int current_number = 0;
154  *     int ret_val;
155  *
156  *     G_LOCK (current_number);
157  *     ret_val = current_number = calc_next_number (current_number);
158  *     G_UNLOCK (current_number);
159  *
160  *     return ret_val;
161  *   }
162  *  </programlisting>
163  * </example>
164  */
165
166 /**
167  * G_LOCK_DEFINE_STATIC:
168  * @name: the name of the lock.
169  *
170  * This works like #G_LOCK_DEFINE, but it creates a static object.
171  */
172
173 /**
174  * G_LOCK_EXTERN:
175  * @name: the name of the lock.
176  *
177  * This declares a lock, that is defined with #G_LOCK_DEFINE in another
178  * module.
179  */
180
181 /**
182  * G_LOCK:
183  * @name: the name of the lock.
184  *
185  * Works like g_mutex_lock(), but for a lock defined with
186  * #G_LOCK_DEFINE.
187  */
188
189 /**
190  * G_TRYLOCK:
191  * @name: the name of the lock.
192  * @Returns: %TRUE, if the lock could be locked.
193  *
194  * Works like g_mutex_trylock(), but for a lock defined with
195  * #G_LOCK_DEFINE.
196  */
197
198 /**
199  * G_UNLOCK:
200  * @name: the name of the lock.
201  *
202  * Works like g_mutex_unlock(), but for a lock defined with
203  * #G_LOCK_DEFINE.
204  */
205
206 /* GMutex Documentation {{{1 ------------------------------------------ */
207
208 /**
209  * GMutex:
210  *
211  * The #GMutex struct is an opaque data structure to represent a mutex
212  * (mutual exclusion). It can be used to protect data against shared
213  * access. Take for example the following function:
214  *
215  * <example>
216  *  <title>A function which will not work in a threaded environment</title>
217  *  <programlisting>
218  *   int
219  *   give_me_next_number (void)
220  *   {
221  *     static int current_number = 0;
222  *
223  *     /<!-- -->* now do a very complicated calculation to calculate the new
224  *      * number, this might for example be a random number generator
225  *      *<!-- -->/
226  *     current_number = calc_next_number (current_number);
227  *
228  *     return current_number;
229  *   }
230  *  </programlisting>
231  * </example>
232  *
233  * It is easy to see that this won't work in a multi-threaded
234  * application. There current_number must be protected against shared
235  * access. A first naive implementation would be:
236  *
237  * <example>
238  *  <title>The wrong way to write a thread-safe function</title>
239  *  <programlisting>
240  *   int
241  *   give_me_next_number (void)
242  *   {
243  *     static int current_number = 0;
244  *     int ret_val;
245  *     static GMutex * mutex = NULL;
246  *
247  *     if (!mutex) mutex = g_mutex_new (<!-- -->);
248  *
249  *     g_mutex_lock (mutex);
250  *     ret_val = current_number = calc_next_number (current_number);
251  *     g_mutex_unlock (mutex);
252  *
253  *     return ret_val;
254  *   }
255  *  </programlisting>
256  * </example>
257  *
258  * This looks like it would work, but there is a race condition while
259  * constructing the mutex and this code cannot work reliable. Please do
260  * not use such constructs in your own programs! One working solution
261  * is:
262  *
263  * <example>
264  *  <title>A correct thread-safe function</title>
265  *  <programlisting>
266  *   static GMutex *give_me_next_number_mutex = NULL;
267  *
268  *   /<!-- -->* this function must be called before any call to
269  *    * give_me_next_number(<!-- -->)
270  *    *
271  *    * it must be called exactly once.
272  *    *<!-- -->/
273  *   void
274  *   init_give_me_next_number (void)
275  *   {
276  *     g_assert (give_me_next_number_mutex == NULL);
277  *     give_me_next_number_mutex = g_mutex_new (<!-- -->);
278  *   }
279  *
280  *   int
281  *   give_me_next_number (void)
282  *   {
283  *     static int current_number = 0;
284  *     int ret_val;
285  *
286  *     g_mutex_lock (give_me_next_number_mutex);
287  *     ret_val = current_number = calc_next_number (current_number);
288  *     g_mutex_unlock (give_me_next_number_mutex);
289  *
290  *     return ret_val;
291  *   }
292  *  </programlisting>
293  * </example>
294  *
295  * A statically initialized #GMutex provides an even simpler and safer
296  * way of doing this:
297  *
298  * <example>
299  *  <title>Using a statically allocated mutex</title>
300  *  <programlisting>
301  *   int
302  *   give_me_next_number (void)
303  *   {
304  *     static GMutex mutex = G_MUTEX_INIT;
305  *     static int current_number = 0;
306  *     int ret_val;
307  *
308  *     g_mutex_lock (&amp;mutex);
309  *     ret_val = current_number = calc_next_number (current_number);
310  *     g_mutex_unlock (&amp;mutex);
311  *
312  *     return ret_val;
313  *   }
314  *  </programlisting>
315  * </example>
316  *
317  * A #GMutex should only be accessed via <function>g_mutex_</function>
318  * functions.
319  */
320
321 /**
322  * G_MUTEX_INIT:
323  *
324  * Initializer for statically allocated #GMutexes.
325  * Alternatively, g_mutex_init() can be used.
326  *
327  * |[
328  *   GMutex mutex = G_MUTEX_INIT;
329  * ]|
330  *
331  * Since: 2.32
332  */
333
334 /* GRecMutex Documentation {{{1 -------------------------------------- */
335
336 /**
337  * GRecMutex:
338  *
339  * The GRecMutex struct is an opaque data structure to represent a
340  * recursive mutex. It is similar to a #GMutex with the difference
341  * that it is possible to lock a GRecMutex multiple times in the same
342  * thread without deadlock. When doing so, care has to be taken to
343  * unlock the recursive mutex as often as it has been locked.
344  *
345  * A GRecMutex should only be accessed with the
346  * <function>g_rec_mutex_</function> functions. Before a GRecMutex
347  * can be used, it has to be initialized with #G_REC_MUTEX_INIT or
348  * g_rec_mutex_init().
349  *
350  * Since: 2.32
351  */
352
353 /**
354  * G_REC_MUTEX_INIT:
355  *
356  * Initializer for statically allocated #GRecMutexes.
357  * Alternatively, g_rec_mutex_init() can be used.
358  *
359  * |[
360  *   GRecMutex mutex = G_REC_MUTEX_INIT;
361  * ]|
362  *
363  * Since: 2.32
364  */
365
366 /* GRWLock Documentation {{{1 ---------------------------------------- */
367
368 /**
369  * GRWLock:
370  *
371  * The GRWLock struct is an opaque data structure to represent a
372  * reader-writer lock. It is similar to a #GMutex in that it allows
373  * multiple threads to coordinate access to a shared resource.
374  *
375  * The difference to a mutex is that a reader-writer lock discriminates
376  * between read-only ('reader') and full ('writer') access. While only
377  * one thread at a time is allowed write access (by holding the 'writer'
378  * lock via g_rw_lock_writer_lock()), multiple threads can gain
379  * simultaneous read-only access (by holding the 'reader' lock via
380  * g_rw_lock_reader_lock()).
381  *
382  * <example>
383  *  <title>An array with access functions</title>
384  *  <programlisting>
385  *   GRWLock lock = G_RW_LOCK_INIT;
386  *   GPtrArray *array;
387  *
388  *   gpointer
389  *   my_array_get (guint index)
390  *   {
391  *     gpointer retval = NULL;
392  *
393  *     if (!array)
394  *       return NULL;
395  *
396  *     g_rw_lock_reader_lock (&amp;lock);
397  *     if (index &lt; array->len)
398  *       retval = g_ptr_array_index (array, index);
399  *     g_rw_lock_reader_unlock (&amp;lock);
400  *
401  *     return retval;
402  *   }
403  *
404  *   void
405  *   my_array_set (guint index, gpointer data)
406  *   {
407  *     g_rw_lock_writer_lock (&amp;lock);
408  *
409  *     if (!array)
410  *       array = g_ptr_array_new (<!-- -->);
411  *
412  *     if (index >= array->len)
413  *       g_ptr_array_set_size (array, index+1);
414  *     g_ptr_array_index (array, index) = data;
415  *
416  *     g_rw_lock_writer_unlock (&amp;lock);
417  *   }
418  *  </programlisting>
419  *  <para>
420  *    This example shows an array which can be accessed by many readers
421  *    (the <function>my_array_get()</function> function) simultaneously,
422  *    whereas the writers (the <function>my_array_set()</function>
423  *    function) will only be allowed once at a time and only if no readers
424  *    currently access the array. This is because of the potentially
425  *    dangerous resizing of the array. Using these functions is fully
426  *    multi-thread safe now.
427  *  </para>
428  * </example>
429  *
430  * A GRWLock should only be accessed with the
431  * <function>g_rw_lock_</function> functions. Before it can be used,
432  * it has to be initialized with #G_RW_LOCK_INIT or g_rw_lock_init().
433  *
434  * Since: 2.32
435  */
436
437 /**
438  * G_RW_LOCK_INIT:
439  *
440  * Initializer for statically allocated #GRWLocks.
441  * Alternatively, g_rw_lock_init_init() can be used.
442  *
443  * |[
444  *   GRWLock lock = G_RW_LOCK_INIT;
445  * ]|
446  *
447  * Since: 2.32
448  */
449
450 /* GCond Documentation {{{1 ------------------------------------------ */
451
452 /**
453  * GCond:
454  *
455  * The #GCond struct is an opaque data structure that represents a
456  * condition. Threads can block on a #GCond if they find a certain
457  * condition to be false. If other threads change the state of this
458  * condition they signal the #GCond, and that causes the waiting
459  * threads to be woken up.
460  *
461  * <example>
462  *  <title>
463  *   Using GCond to block a thread until a condition is satisfied
464  *  </title>
465  *  <programlisting>
466  *   GCond* data_cond = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
467  *   GMutex* data_mutex = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
468  *   gpointer current_data = NULL;
469  *
470  *   void
471  *   push_data (gpointer data)
472  *   {
473  *     g_mutex_lock (data_mutex);
474  *     current_data = data;
475  *     g_cond_signal (data_cond);
476  *     g_mutex_unlock (data_mutex);
477  *   }
478  *
479  *   gpointer
480  *   pop_data (void)
481  *   {
482  *     gpointer data;
483  *
484  *     g_mutex_lock (data_mutex);
485  *     while (!current_data)
486  *       g_cond_wait (data_cond, data_mutex);
487  *     data = current_data;
488  *     current_data = NULL;
489  *     g_mutex_unlock (data_mutex);
490  *
491  *     return data;
492  *   }
493  *  </programlisting>
494  * </example>
495  *
496  * Whenever a thread calls pop_data() now, it will wait until
497  * current_data is non-%NULL, i.e. until some other thread
498  * has called push_data().
499  *
500  * <note><para>It is important to use the g_cond_wait() and
501  * g_cond_timed_wait() functions only inside a loop which checks for the
502  * condition to be true.  It is not guaranteed that the waiting thread
503  * will find the condition fulfilled after it wakes up, even if the
504  * signaling thread left the condition in that state: another thread may
505  * have altered the condition before the waiting thread got the chance
506  * to be woken up, even if the condition itself is protected by a
507  * #GMutex, like above.</para></note>
508  *
509  * A #GCond should only be accessed via the <function>g_cond_</function>
510  * functions.
511  */
512
513 /**
514  * G_COND_INIT:
515  *
516  * Initializer for statically allocated #GConds.
517  * Alternatively, g_cond_init() can be used.
518  *
519  * |[
520  *   GCond cond = G_COND_INIT;
521  * ]|
522  *
523  * Since: 2.32
524  */
525
526 /* GPrivate Documentation {{{1 --------------------------------------- */
527
528 /**
529  * GPrivate:
530  *
531  * <note><para>
532  * #GStaticPrivate is a better choice for most uses.
533  * </para></note>
534  *
535  * The #GPrivate struct is an opaque data structure to represent a
536  * thread private data key. Threads can thereby obtain and set a
537  * pointer which is private to the current thread. Take our
538  * <function>give_me_next_number(<!-- -->)</function> example from
539  * above.  Suppose we don't want <literal>current_number</literal> to be
540  * shared between the threads, but instead to be private to each thread.
541  * This can be done as follows:
542  *
543  * <example>
544  *  <title>Using GPrivate for per-thread data</title>
545  *  <programlisting>
546  *   GPrivate* current_number_key = NULL; /<!-- -->* Must be initialized somewhere
547  *                                           with g_private_new (g_free); *<!-- -->/
548  *
549  *   int
550  *   give_me_next_number (void)
551  *   {
552  *     int *current_number = g_private_get (current_number_key);
553  *
554  *     if (!current_number)
555  *       {
556  *         current_number = g_new (int, 1);
557  *         *current_number = 0;
558  *         g_private_set (current_number_key, current_number);
559  *       }
560  *
561  *     *current_number = calc_next_number (*current_number);
562  *
563  *     return *current_number;
564  *   }
565  *  </programlisting>
566  * </example>
567  *
568  * Here the pointer belonging to the key
569  * <literal>current_number_key</literal> is read. If it is %NULL, it has
570  * not been set yet. Then get memory for an integer value, assign this
571  * memory to the pointer and write the pointer back. Now we have an
572  * integer value that is private to the current thread.
573  *
574  * The #GPrivate struct should only be accessed via the
575  * <function>g_private_</function> functions.
576  */
577
578 /* GThread Documentation {{{1 ---------------------------------------- */
579
580 /**
581  * GThread:
582  *
583  * The #GThread struct represents a running thread.
584  *
585  * Resources for a joinable thread are not fully released
586  * until g_thread_join() is called for that thread.
587  */
588
589 /**
590  * GThreadFunc:
591  * @data: data passed to the thread
592  * @Returns: the return value of the thread, which will be returned by
593  *     g_thread_join()
594  *
595  * Specifies the type of the @func functions passed to
596  * g_thread_create() or g_thread_create_full().
597  */
598
599 /**
600  * g_thread_supported:
601  *
602  * This macro returns %TRUE if the thread system is initialized,
603  * and %FALSE if it is not.
604  *
605  * For language bindings, g_thread_get_initialized() provides
606  * the same functionality as a function.
607  *
608  * Returns: %TRUE, if the thread system is initialized
609  */
610
611 /* GThreadError {{{1 ------------------------------------------------------- */
612 /**
613  * GThreadError:
614  * @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
615  *                        shortage. Try again later.
616  *
617  * Possible errors of thread related functions.
618  **/
619
620 /**
621  * G_THREAD_ERROR:
622  *
623  * The error domain of the GLib thread subsystem.
624  **/
625 GQuark
626 g_thread_error_quark (void)
627 {
628   return g_quark_from_static_string ("g_thread_error");
629 }
630
631 /* Miscellaneous Structures {{{1 ------------------------------------------ */
632
633 typedef struct _GRealThread GRealThread;
634 struct  _GRealThread
635 {
636   GThread thread;
637   GArray *private_data;
638   GRealThread *next;
639   gpointer retval;
640   GSystemThread system_thread;
641 };
642
643 /* Local Data {{{1 -------------------------------------------------------- */
644
645 gboolean         g_threads_got_initialized = FALSE;
646 GSystemThread    zero_thread; /* This is initialized to all zero */
647
648 GMutex           g_once_mutex = G_MUTEX_INIT;
649 static GCond     g_once_cond = G_COND_INIT;
650 static GSList   *g_once_init_list = NULL;
651
652 static GPrivate     g_thread_specific_private;
653 static GRealThread *g_thread_all_threads = NULL;
654 static GSList      *g_thread_free_indices = NULL;
655
656 /* Protects g_thread_all_threads and g_thread_free_indices */
657 G_LOCK_DEFINE_STATIC (g_thread);
658
659 /* Initialisation {{{1 ---------------------------------------------------- */
660
661 /**
662  * g_thread_init:
663  * @vtable: a function table of type #GThreadFunctions, that provides
664  *     the entry points to the thread system to be used. Since 2.32,
665  *     this parameter is ignored and should always be %NULL
666  *
667  * If you use GLib from more than one thread, you must initialize the
668  * thread system by calling g_thread_init().
669  *
670  * Since version 2.24, calling g_thread_init() multiple times is allowed,
671  * but nothing happens except for the first call.
672  *
673  * Since version 2.32, GLib does not support custom thread implementations
674  * anymore and the @vtable parameter is ignored and you should pass %NULL.
675  *
676  * <note><para>g_thread_init() must not be called directly or indirectly
677  * in a callback from GLib. Also no mutexes may be currently locked while
678  * calling g_thread_init().</para></note>
679  *
680  * <note><para>To use g_thread_init() in your program, you have to link
681  * with the libraries that the command <command>pkg-config --libs
682  * gthread-2.0</command> outputs. This is not the case for all the
683  * other thread-related functions of GLib. Those can be used without
684  * having to link with the thread libraries.</para></note>
685  */
686
687 static void g_thread_cleanup (gpointer data);
688
689 void
690 g_thread_init_glib (void)
691 {
692   static gboolean already_done;
693   GRealThread* main_thread;
694
695   if (already_done)
696     return;
697
698   already_done = TRUE;
699
700   /* We let the main thread (the one that calls g_thread_init) inherit
701    * the static_private data set before calling g_thread_init
702    */
703   main_thread = (GRealThread*) g_thread_self ();
704
705   /* setup the basic threading system */
706   g_threads_got_initialized = TRUE;
707   g_private_init (&g_thread_specific_private, g_thread_cleanup);
708   g_private_set (&g_thread_specific_private, main_thread);
709   g_system_thread_self (&main_thread->system_thread);
710
711   /* accomplish log system initialization to enable messaging */
712   _g_messages_thread_init_nomessage ();
713 }
714
715 /**
716  * g_thread_get_initialized:
717  *
718  * Indicates if g_thread_init() has been called.
719  *
720  * Returns: %TRUE if threads have been initialized.
721  *
722  * Since: 2.20
723  */
724 gboolean
725 g_thread_get_initialized (void)
726 {
727   return g_thread_supported ();
728 }
729
730 /* GOnce {{{1 ------------------------------------------------------------- */
731
732 /**
733  * GOnce:
734  * @status: the status of the #GOnce
735  * @retval: the value returned by the call to the function, if @status
736  *          is %G_ONCE_STATUS_READY
737  *
738  * A #GOnce struct controls a one-time initialization function. Any
739  * one-time initialization function must have its own unique #GOnce
740  * struct.
741  *
742  * Since: 2.4
743  */
744
745 /**
746  * G_ONCE_INIT:
747  *
748  * A #GOnce must be initialized with this macro before it can be used.
749  *
750  * |[
751  *   GOnce my_once = G_ONCE_INIT;
752  * ]|
753  *
754  * Since: 2.4
755  */
756
757 /**
758  * GOnceStatus:
759  * @G_ONCE_STATUS_NOTCALLED: the function has not been called yet.
760  * @G_ONCE_STATUS_PROGRESS: the function call is currently in progress.
761  * @G_ONCE_STATUS_READY: the function has been called.
762  *
763  * The possible statuses of a one-time initialization function
764  * controlled by a #GOnce struct.
765  *
766  * Since: 2.4
767  */
768
769 /**
770  * g_once:
771  * @once: a #GOnce structure
772  * @func: the #GThreadFunc function associated to @once. This function
773  *        is called only once, regardless of the number of times it and
774  *        its associated #GOnce struct are passed to g_once().
775  * @arg: data to be passed to @func
776  *
777  * The first call to this routine by a process with a given #GOnce
778  * struct calls @func with the given argument. Thereafter, subsequent
779  * calls to g_once()  with the same #GOnce struct do not call @func
780  * again, but return the stored result of the first call. On return
781  * from g_once(), the status of @once will be %G_ONCE_STATUS_READY.
782  *
783  * For example, a mutex or a thread-specific data key must be created
784  * exactly once. In a threaded environment, calling g_once() ensures
785  * that the initialization is serialized across multiple threads.
786  *
787  * Calling g_once() recursively on the same #GOnce struct in
788  * @func will lead to a deadlock.
789  *
790  * |[
791  *   gpointer
792  *   get_debug_flags (void)
793  *   {
794  *     static GOnce my_once = G_ONCE_INIT;
795  *
796  *     g_once (&my_once, parse_debug_flags, NULL);
797  *
798  *     return my_once.retval;
799  *   }
800  * ]|
801  *
802  * Since: 2.4
803  */
804 gpointer
805 g_once_impl (GOnce       *once,
806              GThreadFunc  func,
807              gpointer     arg)
808 {
809   g_mutex_lock (&g_once_mutex);
810
811   while (once->status == G_ONCE_STATUS_PROGRESS)
812     g_cond_wait (&g_once_cond, &g_once_mutex);
813
814   if (once->status != G_ONCE_STATUS_READY)
815     {
816       once->status = G_ONCE_STATUS_PROGRESS;
817       g_mutex_unlock (&g_once_mutex);
818
819       once->retval = func (arg);
820
821       g_mutex_lock (&g_once_mutex);
822       once->status = G_ONCE_STATUS_READY;
823       g_cond_broadcast (&g_once_cond);
824     }
825
826   g_mutex_unlock (&g_once_mutex);
827
828   return once->retval;
829 }
830
831 /**
832  * g_once_init_enter:
833  * @value_location: location of a static initializable variable
834  *     containing 0
835  *
836  * Function to be called when starting a critical initialization
837  * section. The argument @value_location must point to a static
838  * 0-initialized variable that will be set to a value other than 0 at
839  * the end of the initialization section. In combination with
840  * g_once_init_leave() and the unique address @value_location, it can
841  * be ensured that an initialization section will be executed only once
842  * during a program's life time, and that concurrent threads are
843  * blocked until initialization completed. To be used in constructs
844  * like this:
845  *
846  * |[
847  *   static gsize initialization_value = 0;
848  *
849  *   if (g_once_init_enter (&amp;initialization_value))
850  *     {
851  *       gsize setup_value = 42; /&ast;* initialization code here *&ast;/
852  *
853  *       g_once_init_leave (&amp;initialization_value, setup_value);
854  *     }
855  *
856  *   /&ast;* use initialization_value here *&ast;/
857  * ]|
858  *
859  * Returns: %TRUE if the initialization section should be entered,
860  *     %FALSE and blocks otherwise
861  *
862  * Since: 2.14
863  */
864 gboolean
865 g_once_init_enter_impl (volatile gsize *value_location)
866 {
867   gboolean need_init = FALSE;
868   g_mutex_lock (&g_once_mutex);
869   if (g_atomic_pointer_get (value_location) == NULL)
870     {
871       if (!g_slist_find (g_once_init_list, (void*) value_location))
872         {
873           need_init = TRUE;
874           g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
875         }
876       else
877         do
878           g_cond_wait (&g_once_cond, &g_once_mutex);
879         while (g_slist_find (g_once_init_list, (void*) value_location));
880     }
881   g_mutex_unlock (&g_once_mutex);
882   return need_init;
883 }
884
885 /**
886  * g_once_init_leave:
887  * @value_location: location of a static initializable variable
888  *     containing 0
889  * @initialization_value: new non-0 value for *@value_location
890  *
891  * Counterpart to g_once_init_enter(). Expects a location of a static
892  * 0-initialized initialization variable, and an initialization value
893  * other than 0. Sets the variable to the initialization value, and
894  * releases concurrent threads blocking in g_once_init_enter() on this
895  * initialization variable.
896  *
897  * Since: 2.14
898  */
899 void
900 g_once_init_leave (volatile gsize *value_location,
901                    gsize           initialization_value)
902 {
903   g_return_if_fail (g_atomic_pointer_get (value_location) == NULL);
904   g_return_if_fail (initialization_value != 0);
905   g_return_if_fail (g_once_init_list != NULL);
906
907   g_atomic_pointer_set (value_location, initialization_value);
908   g_mutex_lock (&g_once_mutex);
909   g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
910   g_cond_broadcast (&g_once_cond);
911   g_mutex_unlock (&g_once_mutex);
912 }
913
914 /* GStaticPrivate {{{1 ---------------------------------------------------- */
915
916 typedef struct _GStaticPrivateNode GStaticPrivateNode;
917 struct _GStaticPrivateNode
918 {
919   gpointer        data;
920   GDestroyNotify  destroy;
921   GStaticPrivate *owner;
922 };
923
924 /**
925  * GStaticPrivate:
926  *
927  * A #GStaticPrivate works almost like a #GPrivate, but it has one
928  * significant advantage. It doesn't need to be created at run-time
929  * like a #GPrivate, but can be defined at compile-time. This is
930  * similar to the difference between #GMutex and #GStaticMutex. Now
931  * look at our <function>give_me_next_number()</function> example with
932  * #GStaticPrivate:
933  *
934  * <example>
935  *  <title>Using GStaticPrivate for per-thread data</title>
936  *  <programlisting>
937  *   int
938  *   give_me_next_number (<!-- -->)
939  *   {
940  *     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
941  *     int *current_number = g_static_private_get (&amp;current_number_key);
942  *
943  *     if (!current_number)
944  *       {
945  *         current_number = g_new (int,1);
946  *         *current_number = 0;
947  *         g_static_private_set (&amp;current_number_key, current_number, g_free);
948  *       }
949  *
950  *     *current_number = calc_next_number (*current_number);
951  *
952  *     return *current_number;
953  *   }
954  *  </programlisting>
955  * </example>
956  */
957
958 /**
959  * G_STATIC_PRIVATE_INIT:
960  *
961  * Every #GStaticPrivate must be initialized with this macro, before it
962  * can be used.
963  *
964  * |[
965  *   GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
966  * ]|
967  */
968
969 /**
970  * g_static_private_init:
971  * @private_key: a #GStaticPrivate to be initialized
972  *
973  * Initializes @private_key. Alternatively you can initialize it with
974  * #G_STATIC_PRIVATE_INIT.
975  */
976 void
977 g_static_private_init (GStaticPrivate *private_key)
978 {
979   private_key->index = 0;
980 }
981
982 /**
983  * g_static_private_get:
984  * @private_key: a #GStaticPrivate
985  *
986  * Works like g_private_get() only for a #GStaticPrivate.
987  *
988  * This function works even if g_thread_init() has not yet been called.
989  *
990  * Returns: the corresponding pointer
991  */
992 gpointer
993 g_static_private_get (GStaticPrivate *private_key)
994 {
995   GRealThread *self = (GRealThread*) g_thread_self ();
996   GArray *array;
997   gpointer ret = NULL;
998
999   array = self->private_data;
1000
1001   if (array && private_key->index != 0 && private_key->index <= array->len)
1002     {
1003       GStaticPrivateNode *node;
1004
1005       node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1006
1007       /* Deal with the possibility that the GStaticPrivate which used
1008        * to have this index got freed and the index got allocated to
1009        * a new one. In this case, the data in the node is stale, so
1010        * free it and return NULL.
1011        */
1012       if (G_UNLIKELY (node->owner != private_key))
1013         {
1014           if (node->destroy)
1015             node->destroy (node->data);
1016           node->destroy = NULL;
1017           node->data = NULL;
1018           node->owner = NULL;
1019         }
1020
1021       ret = node->data;
1022     }
1023
1024   return ret;
1025 }
1026
1027 /**
1028  * g_static_private_set:
1029  * @private_key: a #GStaticPrivate
1030  * @data: the new pointer
1031  * @notify: a function to be called with the pointer whenever the
1032  *     current thread ends or sets this pointer again
1033  *
1034  * Sets the pointer keyed to @private_key for the current thread and
1035  * the function @notify to be called with that pointer (%NULL or
1036  * non-%NULL), whenever the pointer is set again or whenever the
1037  * current thread ends.
1038  *
1039  * This function works even if g_thread_init() has not yet been called.
1040  * If g_thread_init() is called later, the @data keyed to @private_key
1041  * will be inherited only by the main thread, i.e. the one that called
1042  * g_thread_init().
1043  *
1044  * <note><para>@notify is used quite differently from @destructor in
1045  * g_private_new().</para></note>
1046  */
1047 void
1048 g_static_private_set (GStaticPrivate *private_key,
1049                       gpointer        data,
1050                       GDestroyNotify  notify)
1051 {
1052   GRealThread *self = (GRealThread*) g_thread_self ();
1053   GArray *array;
1054   static guint next_index = 0;
1055   GStaticPrivateNode *node;
1056
1057   if (!private_key->index)
1058     {
1059       G_LOCK (g_thread);
1060
1061       if (!private_key->index)
1062         {
1063           if (g_thread_free_indices)
1064             {
1065               private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1066               g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1067                                                            g_thread_free_indices);
1068             }
1069           else
1070             private_key->index = ++next_index;
1071         }
1072
1073       G_UNLOCK (g_thread);
1074     }
1075
1076   array = self->private_data;
1077   if (!array)
1078     {
1079       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1080       self->private_data = array;
1081     }
1082
1083   if (private_key->index > array->len)
1084     g_array_set_size (array, private_key->index);
1085
1086   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1087
1088   if (node->destroy)
1089     node->destroy (node->data);
1090
1091   node->data = data;
1092   node->destroy = notify;
1093   node->owner = private_key;
1094 }
1095
1096 /**
1097  * g_static_private_free:
1098  * @private_key: a #GStaticPrivate to be freed
1099  *
1100  * Releases all resources allocated to @private_key.
1101  *
1102  * You don't have to call this functions for a #GStaticPrivate with an
1103  * unbounded lifetime, i.e. objects declared 'static', but if you have
1104  * a #GStaticPrivate as a member of a structure and the structure is
1105  * freed, you should also free the #GStaticPrivate.
1106  */
1107 void
1108 g_static_private_free (GStaticPrivate *private_key)
1109 {
1110   guint idx = private_key->index;
1111
1112   if (!idx)
1113     return;
1114
1115   private_key->index = 0;
1116
1117   /* Freeing the per-thread data is deferred to either the
1118    * thread end or the next g_static_private_get() call for
1119    * the same index.
1120    */
1121   G_LOCK (g_thread);
1122   g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1123                                            GUINT_TO_POINTER (idx));
1124   G_UNLOCK (g_thread);
1125 }
1126
1127 /* GThread {{{1 -------------------------------------------------------- */
1128
1129 static void
1130 g_thread_cleanup (gpointer data)
1131 {
1132   if (data)
1133     {
1134       GRealThread* thread = data;
1135       GArray *array;
1136
1137       array = thread->private_data;
1138       thread->private_data = NULL;
1139
1140       if (array)
1141         {
1142           guint i;
1143
1144           for (i = 0; i < array->len; i++ )
1145             {
1146               GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1147               if (node->destroy)
1148                 node->destroy (node->data);
1149             }
1150           g_array_free (array, TRUE);
1151         }
1152
1153       /* We only free the thread structure if it isn't joinable.
1154        * If it is, the structure is freed in g_thread_join()
1155        */
1156       if (!thread->thread.joinable)
1157         {
1158           GRealThread *t, *p;
1159
1160           G_LOCK (g_thread);
1161           for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
1162             {
1163               if (t == thread)
1164                 {
1165                   if (p)
1166                     p->next = t->next;
1167                   else
1168                     g_thread_all_threads = t->next;
1169                   break;
1170                 }
1171             }
1172           G_UNLOCK (g_thread);
1173
1174           /* Just to make sure, this isn't used any more */
1175           g_system_thread_assign (thread->system_thread, zero_thread);
1176           g_free (thread);
1177         }
1178     }
1179 }
1180
1181 static gpointer
1182 g_thread_create_proxy (gpointer data)
1183 {
1184   GRealThread* thread = data;
1185
1186   g_assert (data);
1187
1188   /* This has to happen before G_LOCK, as that might call g_thread_self */
1189   g_private_set (&g_thread_specific_private, data);
1190
1191   /* The lock makes sure that thread->system_thread is written,
1192    * before thread->thread.func is called. See g_thread_create().
1193    */
1194   G_LOCK (g_thread);
1195   G_UNLOCK (g_thread);
1196
1197   thread->retval = thread->thread.func (thread->thread.data);
1198
1199   return NULL;
1200 }
1201
1202 /**
1203  * g_thread_create:
1204  * @func: a function to execute in the new thread
1205  * @data: an argument to supply to the new thread
1206  * @joinable: should this thread be joinable?
1207  * @error: return location for error, or %NULL
1208  *
1209  * This function creates a new thread.
1210  *
1211  * If @joinable is %TRUE, you can wait for this threads termination
1212  * calling g_thread_join(). Otherwise the thread will just disappear
1213  * when it terminates.
1214  *
1215  * The new thread executes the function @func with the argument @data.
1216  * If the thread was created successfully, it is returned.
1217  *
1218  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
1219  * The error is set, if and only if the function returns %NULL.
1220  *
1221  * Returns: the new #GThread on success
1222  */
1223 GThread *
1224 g_thread_create (GThreadFunc   func,
1225                  gpointer      data,
1226                  gboolean      joinable,
1227                  GError      **error)
1228 {
1229   return g_thread_create_with_stack_size (func, data, joinable, 0, error);
1230 }
1231
1232 /**
1233  * g_thread_create_with_stack_size:
1234  * @func: a function to execute in the new thread
1235  * @data: an argument to supply to the new thread
1236  * @joinable: should this thread be joinable?
1237  * @stack_size: a stack size for the new thread
1238  * @error: return location for error
1239  *
1240  * This function creates a new thread. If the underlying thread
1241  * implementation supports it, the thread gets a stack size of
1242  * @stack_size or the default value for the current platform, if
1243  * @stack_size is 0.
1244  *
1245  * If @joinable is %TRUE, you can wait for this threads termination
1246  * calling g_thread_join(). Otherwise the thread will just disappear
1247  * when it terminates.
1248  *
1249  * The new thread executes the function @func with the argument @data.
1250  * If the thread was created successfully, it is returned.
1251  *
1252  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
1253  * The error is set, if and only if the function returns %NULL.
1254  *
1255  * <note><para>Only use g_thread_create_with_stack_size() if you
1256  * really can't use g_thread_create() instead. g_thread_create()
1257  * does not take @stack_size, as it should only be used in cases
1258  * in which it is unavoidable.</para></note>
1259  *
1260  * Returns: the new #GThread on success
1261  *
1262  * Since: 2.32
1263  */
1264 GThread*
1265 g_thread_create_with_stack_size (GThreadFunc   func,
1266                                  gpointer      data,
1267                                  gboolean      joinable,
1268                                  gsize         stack_size,
1269                                  GError      **error)
1270 {
1271   GRealThread* result;
1272   GError *local_error = NULL;
1273   g_return_val_if_fail (func, NULL);
1274
1275   result = g_new0 (GRealThread, 1);
1276
1277   result->thread.joinable = joinable;
1278   result->thread.func = func;
1279   result->thread.data = data;
1280   result->private_data = NULL;
1281   G_LOCK (g_thread);
1282   g_system_thread_create (g_thread_create_proxy, result,
1283                           stack_size, joinable,
1284                           &result->system_thread, &local_error);
1285   if (!local_error)
1286     {
1287       result->next = g_thread_all_threads;
1288       g_thread_all_threads = result;
1289     }
1290   G_UNLOCK (g_thread);
1291
1292   if (local_error)
1293     {
1294       g_propagate_error (error, local_error);
1295       g_free (result);
1296       return NULL;
1297     }
1298
1299   return (GThread*) result;
1300 }
1301
1302 /**
1303  * g_thread_exit:
1304  * @retval: the return value of this thread
1305  *
1306  * Exits the current thread. If another thread is waiting for that
1307  * thread using g_thread_join() and the current thread is joinable, the
1308  * waiting thread will be woken up and get @retval as the return value
1309  * of g_thread_join(). If the current thread is not joinable, @retval
1310  * is ignored. Calling
1311  *
1312  * |[
1313  *   g_thread_exit (retval);
1314  * ]|
1315  *
1316  * is equivalent to returning @retval from the function @func, as given
1317  * to g_thread_create().
1318  *
1319  * <note><para>Never call g_thread_exit() from within a thread of a
1320  * #GThreadPool, as that will mess up the bookkeeping and lead to funny
1321  * and unwanted results.</para></note>
1322  */
1323 void
1324 g_thread_exit (gpointer retval)
1325 {
1326   GRealThread* real = (GRealThread*) g_thread_self ();
1327   real->retval = retval;
1328
1329   g_system_thread_exit ();
1330 }
1331
1332 /**
1333  * g_thread_join:
1334  * @thread: a #GThread to be waited for
1335  *
1336  * Waits until @thread finishes, i.e. the function @func, as given to
1337  * g_thread_create(), returns or g_thread_exit() is called by @thread.
1338  * All resources of @thread including the #GThread struct are released.
1339  * @thread must have been created with @joinable=%TRUE in
1340  * g_thread_create(). The value returned by @func or given to
1341  * g_thread_exit() by @thread is returned by this function.
1342  *
1343  * Returns: the return value of the thread
1344  */
1345 gpointer
1346 g_thread_join (GThread* thread)
1347 {
1348   GRealThread* real = (GRealThread*) thread;
1349   GRealThread *p, *t;
1350   gpointer retval;
1351
1352   g_return_val_if_fail (thread, NULL);
1353   g_return_val_if_fail (thread->joinable, NULL);
1354   g_return_val_if_fail (!g_system_thread_equal (&real->system_thread, &zero_thread), NULL);
1355
1356   g_system_thread_join (&real->system_thread);
1357
1358   retval = real->retval;
1359
1360   G_LOCK (g_thread);
1361   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
1362     {
1363       if (t == (GRealThread*) thread)
1364         {
1365           if (p)
1366             p->next = t->next;
1367           else
1368             g_thread_all_threads = t->next;
1369           break;
1370         }
1371     }
1372   G_UNLOCK (g_thread);
1373
1374   /* Just to make sure, this isn't used any more */
1375   thread->joinable = 0;
1376   g_system_thread_assign (real->system_thread, zero_thread);
1377
1378   /* the thread structure for non-joinable threads is freed upon
1379    * thread end. We free the memory here. This will leave a loose end,
1380    * if a joinable thread is not joined.
1381    */
1382   g_free (thread);
1383
1384   return retval;
1385 }
1386
1387 /**
1388  * g_thread_self:
1389  *
1390  * This functions returns the #GThread corresponding to the calling
1391  * thread.
1392  *
1393  * Returns: the current thread
1394  */
1395 GThread*
1396 g_thread_self (void)
1397 {
1398   GRealThread* thread = g_private_get (&g_thread_specific_private);
1399
1400   if (!thread)
1401     {
1402       /* If no thread data is available, provide and set one.
1403        * This can happen for the main thread and for threads
1404        * that are not created by GLib.
1405        */
1406       thread = g_new0 (GRealThread, 1);
1407       thread->thread.joinable = FALSE; /* This is a safe guess */
1408       thread->thread.func = NULL;
1409       thread->thread.data = NULL;
1410       thread->private_data = NULL;
1411
1412       g_system_thread_self (&thread->system_thread);
1413
1414       g_private_set (&g_thread_specific_private, thread);
1415
1416       G_LOCK (g_thread);
1417       thread->next = g_thread_all_threads;
1418       g_thread_all_threads = thread;
1419       G_UNLOCK (g_thread);
1420     }
1421
1422   return (GThread*)thread;
1423 }
1424
1425 /**
1426  * g_thread_foreach:
1427  * @thread_func: function to call for all #GThread structures
1428  * @user_data: second argument to @thread_func
1429  *
1430  * Call @thread_func on all existing #GThread structures.
1431  * Note that threads may decide to exit while @thread_func is
1432  * running, so without intimate knowledge about the lifetime of
1433  * foreign threads, @thread_func shouldn't access the GThread*
1434  * pointer passed in as first argument. However, @thread_func will
1435  * not be called for threads which are known to have exited already.
1436  *
1437  * Due to thread lifetime checks, this function has an execution complexity
1438  * which is quadratic in the number of existing threads.
1439  *
1440  * Since: 2.10
1441  */
1442 void
1443 g_thread_foreach (GFunc    thread_func,
1444                   gpointer user_data)
1445 {
1446   GSList *slist = NULL;
1447   GRealThread *thread;
1448   g_return_if_fail (thread_func != NULL);
1449   /* snapshot the list of threads for iteration */
1450   G_LOCK (g_thread);
1451   for (thread = g_thread_all_threads; thread; thread = thread->next)
1452     slist = g_slist_prepend (slist, thread);
1453   G_UNLOCK (g_thread);
1454   /* walk the list, skipping non-existent threads */
1455   while (slist)
1456     {
1457       GSList *node = slist;
1458       slist = node->next;
1459       /* check whether the current thread still exists */
1460       G_LOCK (g_thread);
1461       for (thread = g_thread_all_threads; thread; thread = thread->next)
1462         if (thread == node->data)
1463           break;
1464       G_UNLOCK (g_thread);
1465       if (thread)
1466         thread_func (thread, user_data);
1467       g_slist_free_1 (node);
1468     }
1469 }
1470
1471 /* GMutex {{{1 ------------------------------------------------------ */
1472
1473 /**
1474  * g_mutex_new:
1475  *
1476  * Allocated and initializes a new #GMutex.
1477  *
1478  * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1479  */
1480 GMutex *
1481 g_mutex_new (void)
1482 {
1483   GMutex *mutex;
1484
1485   mutex = g_slice_new (GMutex);
1486   g_mutex_init (mutex);
1487
1488   return mutex;
1489 }
1490
1491 /**
1492  * g_mutex_free:
1493  * @mutex: a #GMutex
1494  *
1495  * Destroys a @mutex that has been created with g_mutex_new().
1496  *
1497  * Calling g_mutex_free() on a locked mutex may result
1498  * in undefined behaviour.
1499  */
1500 void
1501 g_mutex_free (GMutex *mutex)
1502 {
1503   g_mutex_clear (mutex);
1504   g_slice_free (GMutex, mutex);
1505 }
1506
1507 /* GCond {{{1 ------------------------------------------------------ */
1508
1509 /**
1510  * g_cond_new:
1511  *
1512  * Allocates and initializes a new #GCond.
1513  *
1514  * Returns: a newly allocated #GCond. Free with g_cond_free()
1515  */
1516 GCond *
1517 g_cond_new (void)
1518 {
1519   GCond *cond;
1520
1521   cond = g_slice_new (GCond);
1522   g_cond_init (cond);
1523
1524   return cond;
1525 }
1526
1527 /**
1528  * g_cond_free:
1529  * @cond: a #GCond
1530  *
1531  * Destroys a #GCond that has been created with g_cond_new().
1532  */
1533 void
1534 g_cond_free (GCond *cond)
1535 {
1536   g_cond_clear (cond);
1537   g_slice_free (GCond, cond);
1538 }
1539
1540 /* GPrivate {{{1 ------------------------------------------------------ */
1541
1542 /**
1543  * g_private_new:
1544  * @destructor: a function to destroy the data keyed to
1545  *     the #GPrivate when a thread ends
1546  *
1547  * Creates a new #GPrivate. If @destructor is non-%NULL, it is a
1548  * pointer to a destructor function. Whenever a thread ends and the
1549  * corresponding pointer keyed to this instance of #GPrivate is
1550  * non-%NULL, the destructor is called with this pointer as the
1551  * argument.
1552  *
1553  * <note><para>
1554  * #GStaticPrivate is a better choice for most uses.
1555  * </para></note>
1556  *
1557  * <note><para>@destructor is used quite differently from @notify in
1558  * g_static_private_set().</para></note>
1559  *
1560  * <note><para>A #GPrivate cannot be freed. Reuse it instead, if you
1561  * can, to avoid shortage, or use #GStaticPrivate.</para></note>
1562  *
1563  * <note><para>This function will abort if g_thread_init() has not been
1564  * called yet.</para></note>
1565  *
1566  * Returns: a newly allocated #GPrivate
1567  */
1568 GPrivate *
1569 g_private_new (GDestroyNotify notify)
1570 {
1571   GPrivate *key;
1572
1573   key = g_slice_new (GPrivate);
1574   g_private_init (key, notify);
1575
1576   return key;
1577 }
1578
1579  /* vim: set foldmethod=marker: */