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