Rework the way GStaticPrivate data is freed
[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 "gbitlock.h"
62 #include "gslice.h"
63 #include "gslist.h"
64 #include "gtestutils.h"
65
66 /**
67  * SECTION:threads
68  * @title: Threads
69  * @short_description: portable support for threads, mutexes, locks,
70  *     conditions and thread private data
71  * @see_also: #GThreadPool, #GAsyncQueue
72  *
73  * Threads act almost like processes, but unlike processes all threads
74  * of one process share the same memory. This is good, as it provides
75  * easy communication between the involved threads via this shared
76  * memory, and it is bad, because strange things (so called
77  * "Heisenbugs") might happen if the program is not carefully designed.
78  * In particular, due to the concurrent nature of threads, no
79  * assumptions on the order of execution of code running in different
80  * threads can be made, unless order is explicitly forced by the
81  * programmer through synchronization primitives.
82  *
83  * The aim of the thread-related functions in GLib is to provide a
84  * portable means for writing multi-threaded software. There are
85  * primitives for mutexes to protect the access to portions of memory
86  * (#GMutex, #GRecMutex and #GRWLock). There is a facility to use
87  * individual bits for locks (g_bit_lock()). There are primitives
88  * for condition variables to allow synchronization of threads (#GCond).
89  * There are primitives for thread-private data - data that every thread
90  * has a private instance of (#GPrivate, #GStaticPrivate). There are
91  * facilities for one-time initialization (#GOnce, g_once_init_enter()).
92  * Finally there are primitives to create and manage threads (#GThread).
93  *
94  * The threading system is initialized with g_thread_init().
95  * You may call any other glib functions in the main thread before
96  * g_thread_init() as long as g_thread_init() is not called from
97  * a GLib callback, or with any locks held. However, many libraries
98  * above GLib does not support late initialization of threads, so
99  * doing this should be avoided if possible.
100  *
101  * Please note that since version 2.24 the GObject initialization
102  * function g_type_init() initializes threads. Since 2.32, creating
103  * a mainloop will do so too. As a consequence, most applications,
104  * including those using GTK+ will run with threads enabled.
105  *
106  * After calling g_thread_init(), GLib is completely thread safe
107  * (all global data is automatically locked), but individual data
108  * structure instances are not automatically locked for performance
109  * reasons. So, for example you must coordinate accesses to the same
110  * #GHashTable from multiple threads. The two notable exceptions from
111  * this rule are #GMainLoop and #GAsyncQueue, which <emphasis>are</emphasis>
112  * threadsafe and need no further application-level locking to be
113  * accessed from multiple threads.
114  */
115
116 /**
117  * G_THREADS_IMPL_POSIX:
118  *
119  * This macro is defined if POSIX style threads are used.
120  */
121
122 /**
123  * G_THREADS_IMPL_WIN32:
124  *
125  * This macro is defined if Windows style threads are used.
126  */
127
128 /* G_LOCK Documentation {{{1 ---------------------------------------------- */
129
130 /**
131  * G_LOCK_DEFINE:
132  * @name: the name of the lock.
133  *
134  * The %G_LOCK_* macros provide a convenient interface to #GMutex
135  * with the advantage that they will expand to nothing in programs
136  * compiled against a thread-disabled GLib, saving code and memory
137  * there. #G_LOCK_DEFINE defines a lock. It can appear anywhere
138  * variable definitions may appear in programs, i.e. in the first block
139  * of a function or outside of functions. The @name parameter will be
140  * mangled to get the name of the #GMutex. This means that you
141  * can use names of existing variables as the parameter - e.g. the name
142  * of the variable you intent to protect with the lock. Look at our
143  * <function>give_me_next_number()</function> example using the
144  * %G_LOCK_* macros:
145  *
146  * <example>
147  *  <title>Using the %G_LOCK_* convenience macros</title>
148  *  <programlisting>
149  *   G_LOCK_DEFINE (current_number);
150  *
151  *   int
152  *   give_me_next_number (void)
153  *   {
154  *     static int current_number = 0;
155  *     int ret_val;
156  *
157  *     G_LOCK (current_number);
158  *     ret_val = current_number = calc_next_number (current_number);
159  *     G_UNLOCK (current_number);
160  *
161  *     return ret_val;
162  *   }
163  *  </programlisting>
164  * </example>
165  */
166
167 /**
168  * G_LOCK_DEFINE_STATIC:
169  * @name: the name of the lock.
170  *
171  * This works like #G_LOCK_DEFINE, but it creates a static object.
172  */
173
174 /**
175  * G_LOCK_EXTERN:
176  * @name: the name of the lock.
177  *
178  * This declares a lock, that is defined with #G_LOCK_DEFINE in another
179  * module.
180  */
181
182 /**
183  * G_LOCK:
184  * @name: the name of the lock.
185  *
186  * Works like g_mutex_lock(), but for a lock defined with
187  * #G_LOCK_DEFINE.
188  */
189
190 /**
191  * G_TRYLOCK:
192  * @name: the name of the lock.
193  * @Returns: %TRUE, if the lock could be locked.
194  *
195  * Works like g_mutex_trylock(), but for a lock defined with
196  * #G_LOCK_DEFINE.
197  */
198
199 /**
200  * G_UNLOCK:
201  * @name: the name of the lock.
202  *
203  * Works like g_mutex_unlock(), but for a lock defined with
204  * #G_LOCK_DEFINE.
205  */
206
207 /* GMutex Documentation {{{1 ------------------------------------------ */
208
209 /**
210  * GMutex:
211  *
212  * The #GMutex struct is an opaque data structure to represent a mutex
213  * (mutual exclusion). It can be used to protect data against shared
214  * access. Take for example the following function:
215  *
216  * <example>
217  *  <title>A function which will not work in a threaded environment</title>
218  *  <programlisting>
219  *   int
220  *   give_me_next_number (void)
221  *   {
222  *     static int current_number = 0;
223  *
224  *     /<!-- -->* now do a very complicated calculation to calculate the new
225  *      * number, this might for example be a random number generator
226  *      *<!-- -->/
227  *     current_number = calc_next_number (current_number);
228  *
229  *     return current_number;
230  *   }
231  *  </programlisting>
232  * </example>
233  *
234  * It is easy to see that this won't work in a multi-threaded
235  * application. There current_number must be protected against shared
236  * access. A first naive implementation would be:
237  *
238  * <example>
239  *  <title>The wrong way to write a thread-safe function</title>
240  *  <programlisting>
241  *   int
242  *   give_me_next_number (void)
243  *   {
244  *     static int current_number = 0;
245  *     int ret_val;
246  *     static GMutex * mutex = NULL;
247  *
248  *     if (!mutex) mutex = g_mutex_new (<!-- -->);
249  *
250  *     g_mutex_lock (mutex);
251  *     ret_val = current_number = calc_next_number (current_number);
252  *     g_mutex_unlock (mutex);
253  *
254  *     return ret_val;
255  *   }
256  *  </programlisting>
257  * </example>
258  *
259  * This looks like it would work, but there is a race condition while
260  * constructing the mutex and this code cannot work reliable. Please do
261  * not use such constructs in your own programs! One working solution
262  * is:
263  *
264  * <example>
265  *  <title>A correct thread-safe function</title>
266  *  <programlisting>
267  *   static GMutex *give_me_next_number_mutex = NULL;
268  *
269  *   /<!-- -->* this function must be called before any call to
270  *    * give_me_next_number(<!-- -->)
271  *    *
272  *    * it must be called exactly once.
273  *    *<!-- -->/
274  *   void
275  *   init_give_me_next_number (void)
276  *   {
277  *     g_assert (give_me_next_number_mutex == NULL);
278  *     give_me_next_number_mutex = g_mutex_new (<!-- -->);
279  *   }
280  *
281  *   int
282  *   give_me_next_number (void)
283  *   {
284  *     static int current_number = 0;
285  *     int ret_val;
286  *
287  *     g_mutex_lock (give_me_next_number_mutex);
288  *     ret_val = current_number = calc_next_number (current_number);
289  *     g_mutex_unlock (give_me_next_number_mutex);
290  *
291  *     return ret_val;
292  *   }
293  *  </programlisting>
294  * </example>
295  *
296  * A statically initialized #GMutex provides an even simpler and safer
297  * way of doing this:
298  *
299  * <example>
300  *  <title>Using a statically allocated mutex</title>
301  *  <programlisting>
302  *   int
303  *   give_me_next_number (void)
304  *   {
305  *     static GMutex mutex = G_MUTEX_INIT;
306  *     static int current_number = 0;
307  *     int ret_val;
308  *
309  *     g_mutex_lock (&amp;mutex);
310  *     ret_val = current_number = calc_next_number (current_number);
311  *     g_mutex_unlock (&amp;mutex);
312  *
313  *     return ret_val;
314  *   }
315  *  </programlisting>
316  * </example>
317  *
318  * A #GMutex should only be accessed via <function>g_mutex_</function>
319  * functions.
320  */
321
322 /**
323  * G_MUTEX_INIT:
324  *
325  * Initializer for statically allocated #GMutexes.
326  * Alternatively, g_mutex_init() can be used.
327  *
328  * |[
329  *   GMutex mutex = G_MUTEX_INIT;
330  * ]|
331  *
332  * Since: 2.32
333  */
334
335 /* GRecMutex Documentation {{{1 -------------------------------------- */
336
337 /**
338  * GRecMutex:
339  *
340  * The GRecMutex struct is an opaque data structure to represent a
341  * recursive mutex. It is similar to a #GMutex with the difference
342  * that it is possible to lock a GRecMutex multiple times in the same
343  * thread without deadlock. When doing so, care has to be taken to
344  * unlock the recursive mutex as often as it has been locked.
345  *
346  * A GRecMutex should only be accessed with the
347  * <function>g_rec_mutex_</function> functions. Before a GRecMutex
348  * can be used, it has to be initialized with #G_REC_MUTEX_INIT or
349  * g_rec_mutex_init().
350  *
351  * Since: 2.32
352  */
353
354 /**
355  * G_REC_MUTEX_INIT:
356  *
357  * Initializer for statically allocated #GRecMutexes.
358  * Alternatively, g_rec_mutex_init() can be used.
359  *
360  * |[
361  *   GRecMutex mutex = G_REC_MUTEX_INIT;
362  * ]|
363  *
364  * Since: 2.32
365  */
366
367 /* GRWLock Documentation {{{1 ---------------------------------------- */
368
369 /**
370  * GRWLock:
371  *
372  * The GRWLock struct is an opaque data structure to represent a
373  * reader-writer lock. It is similar to a #GMutex in that it allows
374  * multiple threads to coordinate access to a shared resource.
375  *
376  * The difference to a mutex is that a reader-writer lock discriminates
377  * between read-only ('reader') and full ('writer') access. While only
378  * one thread at a time is allowed write access (by holding the 'writer'
379  * lock via g_rw_lock_writer_lock()), multiple threads can gain
380  * simultaneous read-only access (by holding the 'reader' lock via
381  * g_rw_lock_reader_lock()).
382  *
383  * <example>
384  *  <title>An array with access functions</title>
385  *  <programlisting>
386  *   GRWLock lock = G_RW_LOCK_INIT;
387  *   GPtrArray *array;
388  *
389  *   gpointer
390  *   my_array_get (guint index)
391  *   {
392  *     gpointer retval = NULL;
393  *
394  *     if (!array)
395  *       return NULL;
396  *
397  *     g_rw_lock_reader_lock (&amp;lock);
398  *     if (index &lt; array->len)
399  *       retval = g_ptr_array_index (array, index);
400  *     g_rw_lock_reader_unlock (&amp;lock);
401  *
402  *     return retval;
403  *   }
404  *
405  *   void
406  *   my_array_set (guint index, gpointer data)
407  *   {
408  *     g_rw_lock_writer_lock (&amp;lock);
409  *
410  *     if (!array)
411  *       array = g_ptr_array_new (<!-- -->);
412  *
413  *     if (index >= array->len)
414  *       g_ptr_array_set_size (array, index+1);
415  *     g_ptr_array_index (array, index) = data;
416  *
417  *     g_rw_lock_writer_unlock (&amp;lock);
418  *   }
419  *  </programlisting>
420  *  <para>
421  *    This example shows an array which can be accessed by many readers
422  *    (the <function>my_array_get()</function> function) simultaneously,
423  *    whereas the writers (the <function>my_array_set()</function>
424  *    function) will only be allowed once at a time and only if no readers
425  *    currently access the array. This is because of the potentially
426  *    dangerous resizing of the array. Using these functions is fully
427  *    multi-thread safe now.
428  *  </para>
429  * </example>
430  *
431  * A GRWLock should only be accessed with the
432  * <function>g_rw_lock_</function> functions. Before it can be used,
433  * it has to be initialized with #G_RW_LOCK_INIT or g_rw_lock_init().
434  *
435  * Since: 2.32
436  */
437
438 /**
439  * G_RW_LOCK_INIT:
440  *
441  * Initializer for statically allocated #GRWLocks.
442  * Alternatively, g_rw_lock_init_init() can be used.
443  *
444  * |[
445  *   GRWLock lock = G_RW_LOCK_INIT;
446  * ]|
447  *
448  * Since: 2.32
449  */
450
451 /* GCond Documentation {{{1 ------------------------------------------ */
452
453 /**
454  * GCond:
455  *
456  * The #GCond struct is an opaque data structure that represents a
457  * condition. Threads can block on a #GCond if they find a certain
458  * condition to be false. If other threads change the state of this
459  * condition they signal the #GCond, and that causes the waiting
460  * threads to be woken up.
461  *
462  * <example>
463  *  <title>
464  *   Using GCond to block a thread until a condition is satisfied
465  *  </title>
466  *  <programlisting>
467  *   GCond* data_cond = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
468  *   GMutex* data_mutex = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
469  *   gpointer current_data = NULL;
470  *
471  *   void
472  *   push_data (gpointer data)
473  *   {
474  *     g_mutex_lock (data_mutex);
475  *     current_data = data;
476  *     g_cond_signal (data_cond);
477  *     g_mutex_unlock (data_mutex);
478  *   }
479  *
480  *   gpointer
481  *   pop_data (void)
482  *   {
483  *     gpointer data;
484  *
485  *     g_mutex_lock (data_mutex);
486  *     while (!current_data)
487  *       g_cond_wait (data_cond, data_mutex);
488  *     data = current_data;
489  *     current_data = NULL;
490  *     g_mutex_unlock (data_mutex);
491  *
492  *     return data;
493  *   }
494  *  </programlisting>
495  * </example>
496  *
497  * Whenever a thread calls pop_data() now, it will wait until
498  * current_data is non-%NULL, i.e. until some other thread
499  * has called push_data().
500  *
501  * <note><para>It is important to use the g_cond_wait() and
502  * g_cond_timed_wait() functions only inside a loop which checks for the
503  * condition to be true.  It is not guaranteed that the waiting thread
504  * will find the condition fulfilled after it wakes up, even if the
505  * signaling thread left the condition in that state: another thread may
506  * have altered the condition before the waiting thread got the chance
507  * to be woken up, even if the condition itself is protected by a
508  * #GMutex, like above.</para></note>
509  *
510  * A #GCond should only be accessed via the <function>g_cond_</function>
511  * functions.
512  */
513
514 /**
515  * G_COND_INIT:
516  *
517  * Initializer for statically allocated #GConds.
518  * Alternatively, g_cond_init() can be used.
519  *
520  * |[
521  *   GCond cond = G_COND_INIT;
522  * ]|
523  *
524  * Since: 2.32
525  */
526
527 /* GPrivate Documentation {{{1 --------------------------------------- */
528
529 /**
530  * GPrivate:
531  *
532  * <note><para>
533  * #GStaticPrivate is a better choice for most uses.
534  * </para></note>
535  *
536  * The #GPrivate struct is an opaque data structure to represent a
537  * thread private data key. Threads can thereby obtain and set a
538  * pointer which is private to the current thread. Take our
539  * <function>give_me_next_number(<!-- -->)</function> example from
540  * above.  Suppose we don't want <literal>current_number</literal> to be
541  * shared between the threads, but instead to be private to each thread.
542  * This can be done as follows:
543  *
544  * <example>
545  *  <title>Using GPrivate for per-thread data</title>
546  *  <programlisting>
547  *   GPrivate* current_number_key = NULL; /<!-- -->* Must be initialized somewhere
548  *                                           with g_private_new (g_free); *<!-- -->/
549  *
550  *   int
551  *   give_me_next_number (void)
552  *   {
553  *     int *current_number = g_private_get (current_number_key);
554  *
555  *     if (!current_number)
556  *       {
557  *         current_number = g_new (int, 1);
558  *         *current_number = 0;
559  *         g_private_set (current_number_key, current_number);
560  *       }
561  *
562  *     *current_number = calc_next_number (*current_number);
563  *
564  *     return *current_number;
565  *   }
566  *  </programlisting>
567  * </example>
568  *
569  * Here the pointer belonging to the key
570  * <literal>current_number_key</literal> is read. If it is %NULL, it has
571  * not been set yet. Then get memory for an integer value, assign this
572  * memory to the pointer and write the pointer back. Now we have an
573  * integer value that is private to the current thread.
574  *
575  * The #GPrivate struct should only be accessed via the
576  * <function>g_private_</function> functions.
577  */
578
579 /* GThread Documentation {{{1 ---------------------------------------- */
580
581 /**
582  * GThread:
583  *
584  * The #GThread struct represents a running thread.
585  *
586  * Resources for a joinable thread are not fully released
587  * until g_thread_join() is called for that thread.
588  */
589
590 /**
591  * GThreadFunc:
592  * @data: data passed to the thread
593  * @Returns: the return value of the thread, which will be returned by
594  *     g_thread_join()
595  *
596  * Specifies the type of the @func functions passed to
597  * g_thread_create() or g_thread_create_full().
598  */
599
600 /**
601  * g_thread_supported:
602  *
603  * This macro returns %TRUE if the thread system is initialized,
604  * and %FALSE if it is not.
605  *
606  * For language bindings, g_thread_get_initialized() provides
607  * the same functionality as a function.
608  *
609  * Returns: %TRUE, if the thread system is initialized
610  */
611
612 /* GThreadError {{{1 ------------------------------------------------------- */
613 /**
614  * GThreadError:
615  * @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
616  *                        shortage. Try again later.
617  *
618  * Possible errors of thread related functions.
619  **/
620
621 /**
622  * G_THREAD_ERROR:
623  *
624  * The error domain of the GLib thread subsystem.
625  **/
626 GQuark
627 g_thread_error_quark (void)
628 {
629   return g_quark_from_static_string ("g_thread_error");
630 }
631
632 /* Miscellaneous Structures {{{1 ------------------------------------------ */
633
634 typedef struct _GRealThread GRealThread;
635 struct  _GRealThread
636 {
637   GThread thread;
638   GArray *private_data;
639   GRealThread *next;
640   gpointer retval;
641   GSystemThread system_thread;
642 };
643
644 /* Local Data {{{1 -------------------------------------------------------- */
645
646 gboolean         g_threads_got_initialized = FALSE;
647 GSystemThread    zero_thread; /* This is initialized to all zero */
648
649 GMutex           g_once_mutex = G_MUTEX_INIT;
650 static GCond     g_once_cond = G_COND_INIT;
651 static GSList*   g_once_init_list = NULL;
652
653 static GPrivate     g_thread_specific_private;
654 static GRealThread *g_thread_all_threads = NULL;
655 static GSList      *g_thread_free_indices = NULL;
656
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       if (G_UNLIKELY (node->owner != private_key))
1007         {
1008           if (node->destroy)
1009             node->destroy (node->data);
1010           node->destroy = NULL;
1011           node->data = NULL;
1012           node->owner = NULL;
1013         }
1014
1015       ret = node->data;
1016     }
1017
1018   return ret;
1019 }
1020
1021 /**
1022  * g_static_private_set:
1023  * @private_key: a #GStaticPrivate
1024  * @data: the new pointer
1025  * @notify: a function to be called with the pointer whenever the
1026  *     current thread ends or sets this pointer again
1027  *
1028  * Sets the pointer keyed to @private_key for the current thread and
1029  * the function @notify to be called with that pointer (%NULL or
1030  * non-%NULL), whenever the pointer is set again or whenever the
1031  * current thread ends.
1032  *
1033  * This function works even if g_thread_init() has not yet been called.
1034  * If g_thread_init() is called later, the @data keyed to @private_key
1035  * will be inherited only by the main thread, i.e. the one that called
1036  * g_thread_init().
1037  *
1038  * <note><para>@notify is used quite differently from @destructor in
1039  * g_private_new().</para></note>
1040  */
1041 void
1042 g_static_private_set (GStaticPrivate *private_key,
1043                       gpointer        data,
1044                       GDestroyNotify  notify)
1045 {
1046   GRealThread *self = (GRealThread*) g_thread_self ();
1047   GArray *array;
1048   static guint next_index = 0;
1049   GStaticPrivateNode *node;
1050
1051   if (!private_key->index)
1052     {
1053       G_LOCK (g_thread);
1054
1055       if (!private_key->index)
1056         {
1057           if (g_thread_free_indices)
1058             {
1059               private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
1060               g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
1061                                                            g_thread_free_indices);
1062             }
1063           else
1064             private_key->index = ++next_index;
1065         }
1066
1067       G_UNLOCK (g_thread);
1068     }
1069
1070   array = self->private_data;
1071   if (!array)
1072     {
1073       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
1074       self->private_data = array;
1075     }
1076
1077   if (private_key->index > array->len)
1078     g_array_set_size (array, private_key->index);
1079
1080   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
1081
1082   if (node->destroy)
1083     node->destroy (node->data);
1084
1085   node->data = data;
1086   node->destroy = notify;
1087   node->owner = private_key;
1088 }
1089
1090 /**
1091  * g_static_private_free:
1092  * @private_key: a #GStaticPrivate to be freed
1093  *
1094  * Releases all resources allocated to @private_key.
1095  *
1096  * You don't have to call this functions for a #GStaticPrivate with an
1097  * unbounded lifetime, i.e. objects declared 'static', but if you have
1098  * a #GStaticPrivate as a member of a structure and the structure is
1099  * freed, you should also free the #GStaticPrivate.
1100  */
1101 void
1102 g_static_private_free (GStaticPrivate *private_key)
1103 {
1104   guint idx = private_key->index;
1105
1106   if (!idx)
1107     return;
1108
1109   private_key->index = 0;
1110
1111   G_LOCK (g_thread);
1112   g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
1113                                            GUINT_TO_POINTER (idx));
1114   G_UNLOCK (g_thread);
1115 }
1116
1117 /* GThread {{{1 -------------------------------------------------------- */
1118
1119 static void
1120 g_thread_cleanup (gpointer data)
1121 {
1122   if (data)
1123     {
1124       GRealThread* thread = data;
1125       GArray *array;
1126
1127       array = thread->private_data;
1128       thread->private_data = NULL;
1129
1130       if (array)
1131         {
1132           guint i;
1133
1134           for (i = 0; i < array->len; i++ )
1135             {
1136               GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
1137               if (node->destroy)
1138                 node->destroy (node->data);
1139             }
1140           g_array_free (array, TRUE);
1141         }
1142
1143       /* We only free the thread structure if it isn't joinable.
1144        * If it is, the structure is freed in g_thread_join()
1145        */
1146       if (!thread->thread.joinable)
1147         {
1148           GRealThread *t, *p;
1149
1150           G_LOCK (g_thread);
1151           for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
1152             {
1153               if (t == thread)
1154                 {
1155                   if (p)
1156                     p->next = t->next;
1157                   else
1158                     g_thread_all_threads = t->next;
1159                   break;
1160                 }
1161             }
1162           G_UNLOCK (g_thread);
1163
1164           /* Just to make sure, this isn't used any more */
1165           g_system_thread_assign (thread->system_thread, zero_thread);
1166           g_free (thread);
1167         }
1168     }
1169 }
1170
1171 static gpointer
1172 g_thread_create_proxy (gpointer data)
1173 {
1174   GRealThread* thread = data;
1175
1176   g_assert (data);
1177
1178   /* This has to happen before G_LOCK, as that might call g_thread_self */
1179   g_private_set (&g_thread_specific_private, data);
1180
1181   /* The lock makes sure that thread->system_thread is written,
1182    * before thread->thread.func is called. See g_thread_create().
1183    */
1184   G_LOCK (g_thread);
1185   G_UNLOCK (g_thread);
1186
1187   thread->retval = thread->thread.func (thread->thread.data);
1188
1189   return NULL;
1190 }
1191
1192 /**
1193  * g_thread_create:
1194  * @func: a function to execute in the new thread
1195  * @data: an argument to supply to the new thread
1196  * @joinable: should this thread be joinable?
1197  * @error: return location for error, or %NULL
1198  *
1199  * This function creates a new thread.
1200  *
1201  * If @joinable is %TRUE, you can wait for this threads termination
1202  * calling g_thread_join(). Otherwise the thread will just disappear
1203  * when it terminates.
1204  *
1205  * The new thread executes the function @func with the argument @data.
1206  * If the thread was created successfully, it is returned.
1207  *
1208  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
1209  * The error is set, if and only if the function returns %NULL.
1210  *
1211  * Returns: the new #GThread on success
1212  */
1213 GThread *
1214 g_thread_create (GThreadFunc   func,
1215                  gpointer      data,
1216                  gboolean      joinable,
1217                  GError      **error)
1218 {
1219   return g_thread_create_with_stack_size (func, data, joinable, 0, error);
1220 }
1221
1222 /**
1223  * g_thread_create_with_stack_size:
1224  * @func: a function to execute in the new thread
1225  * @data: an argument to supply to the new thread
1226  * @joinable: should this thread be joinable?
1227  * @stack_size: a stack size for the new thread
1228  * @error: return location for error
1229  *
1230  * This function creates a new thread. If the underlying thread
1231  * implementation supports it, the thread gets a stack size of
1232  * @stack_size or the default value for the current platform, if
1233  * @stack_size is 0.
1234  *
1235  * If @joinable is %TRUE, you can wait for this threads termination
1236  * calling g_thread_join(). Otherwise the thread will just disappear
1237  * when it terminates.
1238  *
1239  * The new thread executes the function @func with the argument @data.
1240  * If the thread was created successfully, it is returned.
1241  *
1242  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
1243  * The error is set, if and only if the function returns %NULL.
1244  *
1245  * <note><para>Only use g_thread_create_with_stack_size() if you
1246  * really can't use g_thread_create() instead. g_thread_create()
1247  * does not take @stack_size, as it should only be used in cases
1248  * in which it is unavoidable.</para></note>
1249  *
1250  * Returns: the new #GThread on success
1251  *
1252  * Since: 2.32
1253  */
1254 GThread*
1255 g_thread_create_with_stack_size (GThreadFunc   func,
1256                                  gpointer      data,
1257                                  gboolean      joinable,
1258                                  gsize         stack_size,
1259                                  GError      **error)
1260 {
1261   GRealThread* result;
1262   GError *local_error = NULL;
1263   g_return_val_if_fail (func, NULL);
1264
1265   result = g_new0 (GRealThread, 1);
1266
1267   result->thread.joinable = joinable;
1268   result->thread.func = func;
1269   result->thread.data = data;
1270   result->private_data = NULL;
1271   G_LOCK (g_thread);
1272   g_system_thread_create (g_thread_create_proxy, result,
1273                           stack_size, joinable,
1274                           &result->system_thread, &local_error);
1275   if (!local_error)
1276     {
1277       result->next = g_thread_all_threads;
1278       g_thread_all_threads = result;
1279     }
1280   G_UNLOCK (g_thread);
1281
1282   if (local_error)
1283     {
1284       g_propagate_error (error, local_error);
1285       g_free (result);
1286       return NULL;
1287     }
1288
1289   return (GThread*) result;
1290 }
1291
1292 /**
1293  * g_thread_exit:
1294  * @retval: the return value of this thread
1295  *
1296  * Exits the current thread. If another thread is waiting for that
1297  * thread using g_thread_join() and the current thread is joinable, the
1298  * waiting thread will be woken up and get @retval as the return value
1299  * of g_thread_join(). If the current thread is not joinable, @retval
1300  * is ignored. Calling
1301  *
1302  * |[
1303  *   g_thread_exit (retval);
1304  * ]|
1305  *
1306  * is equivalent to returning @retval from the function @func, as given
1307  * to g_thread_create().
1308  *
1309  * <note><para>Never call g_thread_exit() from within a thread of a
1310  * #GThreadPool, as that will mess up the bookkeeping and lead to funny
1311  * and unwanted results.</para></note>
1312  */
1313 void
1314 g_thread_exit (gpointer retval)
1315 {
1316   GRealThread* real = (GRealThread*) g_thread_self ();
1317   real->retval = retval;
1318
1319   g_system_thread_exit ();
1320 }
1321
1322 /**
1323  * g_thread_join:
1324  * @thread: a #GThread to be waited for
1325  *
1326  * Waits until @thread finishes, i.e. the function @func, as given to
1327  * g_thread_create(), returns or g_thread_exit() is called by @thread.
1328  * All resources of @thread including the #GThread struct are released.
1329  * @thread must have been created with @joinable=%TRUE in
1330  * g_thread_create(). The value returned by @func or given to
1331  * g_thread_exit() by @thread is returned by this function.
1332  *
1333  * Returns: the return value of the thread
1334  */
1335 gpointer
1336 g_thread_join (GThread* thread)
1337 {
1338   GRealThread* real = (GRealThread*) thread;
1339   GRealThread *p, *t;
1340   gpointer retval;
1341
1342   g_return_val_if_fail (thread, NULL);
1343   g_return_val_if_fail (thread->joinable, NULL);
1344   g_return_val_if_fail (!g_system_thread_equal (&real->system_thread, &zero_thread), NULL);
1345
1346   g_system_thread_join (&real->system_thread);
1347
1348   retval = real->retval;
1349
1350   G_LOCK (g_thread);
1351   for (t = g_thread_all_threads, p = NULL; t; p = t, t = t->next)
1352     {
1353       if (t == (GRealThread*) thread)
1354         {
1355           if (p)
1356             p->next = t->next;
1357           else
1358             g_thread_all_threads = t->next;
1359           break;
1360         }
1361     }
1362   G_UNLOCK (g_thread);
1363
1364   /* Just to make sure, this isn't used any more */
1365   thread->joinable = 0;
1366   g_system_thread_assign (real->system_thread, zero_thread);
1367
1368   /* the thread structure for non-joinable threads is freed upon
1369    * thread end. We free the memory here. This will leave a loose end,
1370    * if a joinable thread is not joined.
1371    */
1372   g_free (thread);
1373
1374   return retval;
1375 }
1376
1377 /**
1378  * g_thread_self:
1379  *
1380  * This functions returns the #GThread corresponding to the calling
1381  * thread.
1382  *
1383  * Returns: the current thread
1384  */
1385 GThread*
1386 g_thread_self (void)
1387 {
1388   GRealThread* thread = g_private_get (&g_thread_specific_private);
1389
1390   if (!thread)
1391     {
1392       /* If no thread data is available, provide and set one.
1393        * This can happen for the main thread and for threads
1394        * that are not created by GLib.
1395        */
1396       thread = g_new0 (GRealThread, 1);
1397       thread->thread.joinable = FALSE; /* This is a safe guess */
1398       thread->thread.func = NULL;
1399       thread->thread.data = NULL;
1400       thread->private_data = NULL;
1401
1402       g_system_thread_self (&thread->system_thread);
1403
1404       g_private_set (&g_thread_specific_private, thread);
1405
1406       G_LOCK (g_thread);
1407       thread->next = g_thread_all_threads;
1408       g_thread_all_threads = thread;
1409       G_UNLOCK (g_thread);
1410     }
1411
1412   return (GThread*)thread;
1413 }
1414
1415 /**
1416  * g_thread_foreach:
1417  * @thread_func: function to call for all #GThread structures
1418  * @user_data: second argument to @thread_func
1419  *
1420  * Call @thread_func on all existing #GThread structures.
1421  * Note that threads may decide to exit while @thread_func is
1422  * running, so without intimate knowledge about the lifetime of
1423  * foreign threads, @thread_func shouldn't access the GThread*
1424  * pointer passed in as first argument. However, @thread_func will
1425  * not be called for threads which are known to have exited already.
1426  *
1427  * Due to thread lifetime checks, this function has an execution complexity
1428  * which is quadratic in the number of existing threads.
1429  *
1430  * Since: 2.10
1431  */
1432 void
1433 g_thread_foreach (GFunc    thread_func,
1434                   gpointer user_data)
1435 {
1436   GSList *slist = NULL;
1437   GRealThread *thread;
1438   g_return_if_fail (thread_func != NULL);
1439   /* snapshot the list of threads for iteration */
1440   G_LOCK (g_thread);
1441   for (thread = g_thread_all_threads; thread; thread = thread->next)
1442     slist = g_slist_prepend (slist, thread);
1443   G_UNLOCK (g_thread);
1444   /* walk the list, skipping non-existent threads */
1445   while (slist)
1446     {
1447       GSList *node = slist;
1448       slist = node->next;
1449       /* check whether the current thread still exists */
1450       G_LOCK (g_thread);
1451       for (thread = g_thread_all_threads; thread; thread = thread->next)
1452         if (thread == node->data)
1453           break;
1454       G_UNLOCK (g_thread);
1455       if (thread)
1456         thread_func (thread, user_data);
1457       g_slist_free_1 (node);
1458     }
1459 }
1460
1461 /* GMutex {{{1 ------------------------------------------------------ */
1462
1463 /**
1464  * g_mutex_new:
1465  *
1466  * Allocated and initializes a new #GMutex.
1467  *
1468  * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1469  */
1470 GMutex *
1471 g_mutex_new (void)
1472 {
1473   GMutex *mutex;
1474
1475   mutex = g_slice_new (GMutex);
1476   g_mutex_init (mutex);
1477
1478   return mutex;
1479 }
1480
1481 /**
1482  * g_mutex_free:
1483  * @mutex: a #GMutex
1484  *
1485  * Destroys a @mutex that has been created with g_mutex_new().
1486  *
1487  * Calling g_mutex_free() on a locked mutex may result
1488  * in undefined behaviour.
1489  */
1490 void
1491 g_mutex_free (GMutex *mutex)
1492 {
1493   g_mutex_clear (mutex);
1494   g_slice_free (GMutex, mutex);
1495 }
1496
1497 /* GCond {{{1 ------------------------------------------------------ */
1498
1499 /**
1500  * g_cond_new:
1501  *
1502  * Allocates and initializes a new #GCond.
1503  *
1504  * Returns: a newly allocated #GCond. Free with g_cond_free()
1505  */
1506 GCond *
1507 g_cond_new (void)
1508 {
1509   GCond *cond;
1510
1511   cond = g_slice_new (GCond);
1512   g_cond_init (cond);
1513
1514   return cond;
1515 }
1516
1517 /**
1518  * g_cond_free:
1519  * @cond: a #GCond
1520  *
1521  * Destroys a #GCond that has been created with g_cond_new().
1522  */
1523 void
1524 g_cond_free (GCond *cond)
1525 {
1526   g_cond_clear (cond);
1527   g_slice_free (GCond, cond);
1528 }
1529
1530 /* GPrivate {{{1 ------------------------------------------------------ */
1531
1532 /**
1533  * g_private_new:
1534  * @destructor: a function to destroy the data keyed to
1535  *     the #GPrivate when a thread ends
1536  *
1537  * Creates a new #GPrivate. If @destructor is non-%NULL, it is a
1538  * pointer to a destructor function. Whenever a thread ends and the
1539  * corresponding pointer keyed to this instance of #GPrivate is
1540  * non-%NULL, the destructor is called with this pointer as the
1541  * argument.
1542  *
1543  * <note><para>
1544  * #GStaticPrivate is a better choice for most uses.
1545  * </para></note>
1546  *
1547  * <note><para>@destructor is used quite differently from @notify in
1548  * g_static_private_set().</para></note>
1549  *
1550  * <note><para>A #GPrivate cannot be freed. Reuse it instead, if you
1551  * can, to avoid shortage, or use #GStaticPrivate.</para></note>
1552  *
1553  * <note><para>This function will abort if g_thread_init() has not been
1554  * called yet.</para></note>
1555  *
1556  * Returns: a newly allocated #GPrivate
1557  */
1558 GPrivate *
1559 g_private_new (GDestroyNotify notify)
1560 {
1561   GPrivate *key;
1562
1563   key = g_slice_new (GPrivate);
1564   g_private_init (key, notify);
1565
1566   return key;
1567 }
1568
1569  /* vim: set foldmethod=marker: */