1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: MT safety related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
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.
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.
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.
24 /* Prelude {{{1 ----------------------------------------------------------- */
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/.
37 /* implement gthread.h's inline functions */
38 #define G_IMPLEMENT_INLINES 1
39 #define __G_THREAD_C__
44 #include "gthreadprivate.h"
57 #endif /* G_OS_WIN32 */
60 #include "gtestutils.h"
65 * @short_description: portable support for threads, mutexes, locks,
66 * conditions and thread private data
67 * @see_also: #GThreadPool, #GAsyncQueue
69 * Threads act almost like processes, but unlike processes all threads
70 * of one process share the same memory. This is good, as it provides
71 * easy communication between the involved threads via this shared
72 * memory, and it is bad, because strange things (so called
73 * "Heisenbugs") might happen if the program is not carefully designed.
74 * In particular, due to the concurrent nature of threads, no
75 * assumptions on the order of execution of code running in different
76 * threads can be made, unless order is explicitly forced by the
77 * programmer through synchronization primitives.
79 * The aim of the thread-related functions in GLib is to provide a
80 * portable means for writing multi-threaded software. There are
81 * primitives for mutexes to protect the access to portions of memory
82 * (#GMutex, #GRecMutex and #GRWLock). There is a facility to use
83 * individual bits for locks (g_bit_lock()). There are primitives
84 * for condition variables to allow synchronization of threads (#GCond).
85 * There are primitives for thread-private data - data that every thread
86 * has a private instance of (#GPrivate, #GStaticPrivate). There are
87 * facilities for one-time initialization (#GOnce, g_once_init_enter()).
88 * Finally there are primitives to create and manage threads (#GThread).
90 * The threading system is initialized with g_thread_init().
91 * You may call any other glib functions in the main thread before
92 * g_thread_init() as long as g_thread_init() is not called from
93 * a GLib callback, or with any locks held. However, many libraries
94 * above GLib does not support late initialization of threads, so
95 * doing this should be avoided if possible.
97 * Please note that since version 2.24 the GObject initialization
98 * function g_type_init() initializes threads. Since 2.32, creating
99 * a mainloop will do so too. As a consequence, most applications,
100 * including those using GTK+ will run with threads enabled.
102 * After calling g_thread_init(), GLib is completely thread safe
103 * (all global data is automatically locked), but individual data
104 * structure instances are not automatically locked for performance
105 * reasons. So, for example you must coordinate accesses to the same
106 * #GHashTable from multiple threads. The two notable exceptions from
107 * this rule are #GMainLoop and #GAsyncQueue, which <emphasis>are</emphasis>
108 * threadsafe and need no further application-level locking to be
109 * accessed from multiple threads.
113 * G_THREADS_IMPL_POSIX:
115 * This macro is defined if POSIX style threads are used.
119 * G_THREADS_IMPL_WIN32:
121 * This macro is defined if Windows style threads are used.
124 /* G_LOCK Documentation {{{1 ---------------------------------------------- */
128 * @name: the name of the lock.
130 * The %G_LOCK_* macros provide a convenient interface to #GMutex
131 * with the advantage that they will expand to nothing in programs
132 * compiled against a thread-disabled GLib, saving code and memory
133 * there. #G_LOCK_DEFINE defines a lock. It can appear anywhere
134 * variable definitions may appear in programs, i.e. in the first block
135 * of a function or outside of functions. The @name parameter will be
136 * mangled to get the name of the #GMutex. This means that you
137 * can use names of existing variables as the parameter - e.g. the name
138 * of the variable you intent to protect with the lock. Look at our
139 * <function>give_me_next_number()</function> example using the
143 * <title>Using the %G_LOCK_* convenience macros</title>
145 * G_LOCK_DEFINE (current_number);
148 * give_me_next_number (void)
150 * static int current_number = 0;
153 * G_LOCK (current_number);
154 * ret_val = current_number = calc_next_number (current_number);
155 * G_UNLOCK (current_number);
164 * G_LOCK_DEFINE_STATIC:
165 * @name: the name of the lock.
167 * This works like #G_LOCK_DEFINE, but it creates a static object.
172 * @name: the name of the lock.
174 * This declares a lock, that is defined with #G_LOCK_DEFINE in another
180 * @name: the name of the lock.
182 * Works like g_mutex_lock(), but for a lock defined with
188 * @name: the name of the lock.
189 * @Returns: %TRUE, if the lock could be locked.
191 * Works like g_mutex_trylock(), but for a lock defined with
197 * @name: the name of the lock.
199 * Works like g_mutex_unlock(), but for a lock defined with
203 /* GMutex Documentation {{{1 ------------------------------------------ */
208 * The #GMutex struct is an opaque data structure to represent a mutex
209 * (mutual exclusion). It can be used to protect data against shared
210 * access. Take for example the following function:
213 * <title>A function which will not work in a threaded environment</title>
216 * give_me_next_number (void)
218 * static int current_number = 0;
220 * /<!-- -->* now do a very complicated calculation to calculate the new
221 * * number, this might for example be a random number generator
223 * current_number = calc_next_number (current_number);
225 * return current_number;
230 * It is easy to see that this won't work in a multi-threaded
231 * application. There current_number must be protected against shared
232 * access. A first naive implementation would be:
235 * <title>The wrong way to write a thread-safe function</title>
238 * give_me_next_number (void)
240 * static int current_number = 0;
242 * static GMutex * mutex = NULL;
244 * if (!mutex) mutex = g_mutex_new (<!-- -->);
246 * g_mutex_lock (mutex);
247 * ret_val = current_number = calc_next_number (current_number);
248 * g_mutex_unlock (mutex);
255 * This looks like it would work, but there is a race condition while
256 * constructing the mutex and this code cannot work reliable. Please do
257 * not use such constructs in your own programs! One working solution
261 * <title>A correct thread-safe function</title>
263 * static GMutex *give_me_next_number_mutex = NULL;
265 * /<!-- -->* this function must be called before any call to
266 * * give_me_next_number(<!-- -->)
268 * * it must be called exactly once.
271 * init_give_me_next_number (void)
273 * g_assert (give_me_next_number_mutex == NULL);
274 * give_me_next_number_mutex = g_mutex_new (<!-- -->);
278 * give_me_next_number (void)
280 * static int current_number = 0;
283 * g_mutex_lock (give_me_next_number_mutex);
284 * ret_val = current_number = calc_next_number (current_number);
285 * g_mutex_unlock (give_me_next_number_mutex);
292 * A statically initialized #GMutex provides an even simpler and safer
296 * <title>Using a statically allocated mutex</title>
299 * give_me_next_number (void)
301 * static GMutex mutex;
302 * static int current_number = 0;
305 * g_mutex_lock (&mutex);
306 * ret_val = current_number = calc_next_number (current_number);
307 * g_mutex_unlock (&mutex);
314 * A #GMutex should only be accessed via <function>g_mutex_</function>
318 /* GRecMutex Documentation {{{1 -------------------------------------- */
323 * The GRecMutex struct is an opaque data structure to represent a
324 * recursive mutex. It is similar to a #GMutex with the difference
325 * that it is possible to lock a GRecMutex multiple times in the same
326 * thread without deadlock. When doing so, care has to be taken to
327 * unlock the recursive mutex as often as it has been locked.
329 * A GRecMutex should only be accessed with the
330 * <function>g_rec_mutex_</function> functions.
335 /* GRWLock Documentation {{{1 ---------------------------------------- */
340 * The GRWLock struct is an opaque data structure to represent a
341 * reader-writer lock. It is similar to a #GMutex in that it allows
342 * multiple threads to coordinate access to a shared resource.
344 * The difference to a mutex is that a reader-writer lock discriminates
345 * between read-only ('reader') and full ('writer') access. While only
346 * one thread at a time is allowed write access (by holding the 'writer'
347 * lock via g_rw_lock_writer_lock()), multiple threads can gain
348 * simultaneous read-only access (by holding the 'reader' lock via
349 * g_rw_lock_reader_lock()).
352 * <title>An array with access functions</title>
358 * my_array_get (guint index)
360 * gpointer retval = NULL;
365 * g_rw_lock_reader_lock (&lock);
366 * if (index < array->len)
367 * retval = g_ptr_array_index (array, index);
368 * g_rw_lock_reader_unlock (&lock);
374 * my_array_set (guint index, gpointer data)
376 * g_rw_lock_writer_lock (&lock);
379 * array = g_ptr_array_new (<!-- -->);
381 * if (index >= array->len)
382 * g_ptr_array_set_size (array, index+1);
383 * g_ptr_array_index (array, index) = data;
385 * g_rw_lock_writer_unlock (&lock);
389 * This example shows an array which can be accessed by many readers
390 * (the <function>my_array_get()</function> function) simultaneously,
391 * whereas the writers (the <function>my_array_set()</function>
392 * function) will only be allowed once at a time and only if no readers
393 * currently access the array. This is because of the potentially
394 * dangerous resizing of the array. Using these functions is fully
395 * multi-thread safe now.
399 * A GRWLock should only be accessed with the
400 * <function>g_rw_lock_</function> functions.
405 /* GCond Documentation {{{1 ------------------------------------------ */
410 * The #GCond struct is an opaque data structure that represents a
411 * condition. Threads can block on a #GCond if they find a certain
412 * condition to be false. If other threads change the state of this
413 * condition they signal the #GCond, and that causes the waiting
414 * threads to be woken up.
418 * Using GCond to block a thread until a condition is satisfied
421 * GCond* data_cond = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
422 * GMutex* data_mutex = NULL; /<!-- -->* Must be initialized somewhere *<!-- -->/
423 * gpointer current_data = NULL;
426 * push_data (gpointer data)
428 * g_mutex_lock (data_mutex);
429 * current_data = data;
430 * g_cond_signal (data_cond);
431 * g_mutex_unlock (data_mutex);
439 * g_mutex_lock (data_mutex);
440 * while (!current_data)
441 * g_cond_wait (data_cond, data_mutex);
442 * data = current_data;
443 * current_data = NULL;
444 * g_mutex_unlock (data_mutex);
451 * Whenever a thread calls pop_data() now, it will wait until
452 * current_data is non-%NULL, i.e. until some other thread
453 * has called push_data().
455 * <note><para>It is important to use the g_cond_wait() and
456 * g_cond_timed_wait() functions only inside a loop which checks for the
457 * condition to be true. It is not guaranteed that the waiting thread
458 * will find the condition fulfilled after it wakes up, even if the
459 * signaling thread left the condition in that state: another thread may
460 * have altered the condition before the waiting thread got the chance
461 * to be woken up, even if the condition itself is protected by a
462 * #GMutex, like above.</para></note>
464 * A #GCond should only be accessed via the <function>g_cond_</function>
468 /* GThread Documentation {{{1 ---------------------------------------- */
473 * The #GThread struct represents a running thread.
475 * Resources for a joinable thread are not fully released
476 * until g_thread_join() is called for that thread.
481 * @data: data passed to the thread
482 * @Returns: the return value of the thread, which will be returned by
485 * Specifies the type of the @func functions passed to
486 * g_thread_create() or g_thread_create_full().
490 * g_thread_supported:
492 * This macro returns %TRUE if the thread system is initialized,
493 * and %FALSE if it is not.
495 * For language bindings, g_thread_get_initialized() provides
496 * the same functionality as a function.
498 * Returns: %TRUE, if the thread system is initialized
501 /* GThreadError {{{1 ------------------------------------------------------- */
504 * @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
505 * shortage. Try again later.
507 * Possible errors of thread related functions.
513 * The error domain of the GLib thread subsystem.
516 g_thread_error_quark (void)
518 return g_quark_from_static_string ("g_thread_error");
521 /* Local Data {{{1 -------------------------------------------------------- */
523 gboolean g_threads_got_initialized = FALSE;
524 GSystemThread zero_thread; /* This is initialized to all zero */
527 static GCond g_once_cond;
528 static GSList *g_once_init_list = NULL;
530 static void g_thread_cleanup (gpointer data);
531 static GPrivate g_thread_specific_private = G_PRIVATE_INIT (g_thread_cleanup);
533 G_LOCK_DEFINE_STATIC (g_thread_new);
535 /* Initialisation {{{1 ---------------------------------------------------- */
539 * @vtable: a function table of type #GThreadFunctions, that provides
540 * the entry points to the thread system to be used. Since 2.32,
541 * this parameter is ignored and should always be %NULL
543 * If you use GLib from more than one thread, you must initialize the
544 * thread system by calling g_thread_init().
546 * Since version 2.24, calling g_thread_init() multiple times is allowed,
547 * but nothing happens except for the first call.
549 * Since version 2.32, GLib does not support custom thread implementations
550 * anymore and the @vtable parameter is ignored and you should pass %NULL.
552 * <note><para>g_thread_init() must not be called directly or indirectly
553 * in a callback from GLib. Also no mutexes may be currently locked while
554 * calling g_thread_init().</para></note>
556 * <note><para>To use g_thread_init() in your program, you have to link
557 * with the libraries that the command <command>pkg-config --libs
558 * gthread-2.0</command> outputs. This is not the case for all the
559 * other thread-related functions of GLib. Those can be used without
560 * having to link with the thread libraries.</para></note>
564 g_thread_init_glib (void)
566 static gboolean already_done;
567 GRealThread* main_thread;
574 /* We let the main thread (the one that calls g_thread_init) inherit
575 * the static_private data set before calling g_thread_init
577 main_thread = (GRealThread*) g_thread_self ();
579 /* setup the basic threading system */
580 g_threads_got_initialized = TRUE;
581 g_private_set (&g_thread_specific_private, main_thread);
582 g_system_thread_self (&main_thread->system_thread);
584 /* accomplish log system initialization to enable messaging */
585 _g_messages_thread_init_nomessage ();
589 * g_thread_get_initialized:
591 * Indicates if g_thread_init() has been called.
593 * Returns: %TRUE if threads have been initialized.
598 g_thread_get_initialized (void)
600 return g_thread_supported ();
603 /* GOnce {{{1 ------------------------------------------------------------- */
607 * @status: the status of the #GOnce
608 * @retval: the value returned by the call to the function, if @status
609 * is %G_ONCE_STATUS_READY
611 * A #GOnce struct controls a one-time initialization function. Any
612 * one-time initialization function must have its own unique #GOnce
621 * A #GOnce must be initialized with this macro before it can be used.
624 * GOnce my_once = G_ONCE_INIT;
632 * @G_ONCE_STATUS_NOTCALLED: the function has not been called yet.
633 * @G_ONCE_STATUS_PROGRESS: the function call is currently in progress.
634 * @G_ONCE_STATUS_READY: the function has been called.
636 * The possible statuses of a one-time initialization function
637 * controlled by a #GOnce struct.
644 * @once: a #GOnce structure
645 * @func: the #GThreadFunc function associated to @once. This function
646 * is called only once, regardless of the number of times it and
647 * its associated #GOnce struct are passed to g_once().
648 * @arg: data to be passed to @func
650 * The first call to this routine by a process with a given #GOnce
651 * struct calls @func with the given argument. Thereafter, subsequent
652 * calls to g_once() with the same #GOnce struct do not call @func
653 * again, but return the stored result of the first call. On return
654 * from g_once(), the status of @once will be %G_ONCE_STATUS_READY.
656 * For example, a mutex or a thread-specific data key must be created
657 * exactly once. In a threaded environment, calling g_once() ensures
658 * that the initialization is serialized across multiple threads.
660 * Calling g_once() recursively on the same #GOnce struct in
661 * @func will lead to a deadlock.
665 * get_debug_flags (void)
667 * static GOnce my_once = G_ONCE_INIT;
669 * g_once (&my_once, parse_debug_flags, NULL);
671 * return my_once.retval;
678 g_once_impl (GOnce *once,
682 g_mutex_lock (&g_once_mutex);
684 while (once->status == G_ONCE_STATUS_PROGRESS)
685 g_cond_wait (&g_once_cond, &g_once_mutex);
687 if (once->status != G_ONCE_STATUS_READY)
689 once->status = G_ONCE_STATUS_PROGRESS;
690 g_mutex_unlock (&g_once_mutex);
692 once->retval = func (arg);
694 g_mutex_lock (&g_once_mutex);
695 once->status = G_ONCE_STATUS_READY;
696 g_cond_broadcast (&g_once_cond);
699 g_mutex_unlock (&g_once_mutex);
706 * @value_location: location of a static initializable variable
709 * Function to be called when starting a critical initialization
710 * section. The argument @value_location must point to a static
711 * 0-initialized variable that will be set to a value other than 0 at
712 * the end of the initialization section. In combination with
713 * g_once_init_leave() and the unique address @value_location, it can
714 * be ensured that an initialization section will be executed only once
715 * during a program's life time, and that concurrent threads are
716 * blocked until initialization completed. To be used in constructs
720 * static gsize initialization_value = 0;
722 * if (g_once_init_enter (&initialization_value))
724 * gsize setup_value = 42; /** initialization code here **/
726 * g_once_init_leave (&initialization_value, setup_value);
729 * /** use initialization_value here **/
732 * Returns: %TRUE if the initialization section should be entered,
733 * %FALSE and blocks otherwise
738 g_once_init_enter_impl (volatile gsize *value_location)
740 gboolean need_init = FALSE;
741 g_mutex_lock (&g_once_mutex);
742 if (g_atomic_pointer_get (value_location) == NULL)
744 if (!g_slist_find (g_once_init_list, (void*) value_location))
747 g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
751 g_cond_wait (&g_once_cond, &g_once_mutex);
752 while (g_slist_find (g_once_init_list, (void*) value_location));
754 g_mutex_unlock (&g_once_mutex);
760 * @value_location: location of a static initializable variable
762 * @initialization_value: new non-0 value for *@value_location
764 * Counterpart to g_once_init_enter(). Expects a location of a static
765 * 0-initialized initialization variable, and an initialization value
766 * other than 0. Sets the variable to the initialization value, and
767 * releases concurrent threads blocking in g_once_init_enter() on this
768 * initialization variable.
773 g_once_init_leave (volatile gsize *value_location,
774 gsize initialization_value)
776 g_return_if_fail (g_atomic_pointer_get (value_location) == NULL);
777 g_return_if_fail (initialization_value != 0);
778 g_return_if_fail (g_once_init_list != NULL);
780 g_atomic_pointer_set (value_location, initialization_value);
781 g_mutex_lock (&g_once_mutex);
782 g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
783 g_cond_broadcast (&g_once_cond);
784 g_mutex_unlock (&g_once_mutex);
787 /* GThread {{{1 -------------------------------------------------------- */
790 g_thread_cleanup (gpointer data)
794 GRealThread* thread = data;
796 g_static_private_cleanup (thread);
798 /* We only free the thread structure if it isn't joinable.
799 * If it is, the structure is freed in g_thread_join()
801 if (!thread->thread.joinable)
803 if (thread->enumerable)
804 g_enumerable_thread_remove (thread);
806 /* Just to make sure, this isn't used any more */
807 g_system_thread_assign (thread->system_thread, zero_thread);
814 g_thread_create_proxy (gpointer data)
816 GRealThread* thread = data;
821 g_system_thread_set_name (thread->name);
823 /* This has to happen before G_LOCK, as that might call g_thread_self */
824 g_private_set (&g_thread_specific_private, data);
826 /* The lock makes sure that thread->system_thread is written,
827 * before thread->thread.func is called. See g_thread_create().
829 G_LOCK (g_thread_new);
830 G_UNLOCK (g_thread_new);
832 thread->retval = thread->thread.func (thread->thread.data);
839 * @name: a name for the new thread
840 * @func: a function to execute in the new thread
841 * @data: an argument to supply to the new thread
842 * @joinable: should this thread be joinable?
843 * @error: return location for error
845 * This function creates a new thread.
847 * The @name can be useful for discriminating threads in
848 * a debugger. Some systems restrict the length of @name to
851 * If @joinable is %TRUE, you can wait for this threads termination
852 * calling g_thread_join(). Otherwise the thread will just disappear
853 * when it terminates.
855 * The new thread executes the function @func with the argument @data.
856 * If the thread was created successfully, it is returned.
858 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
859 * The error is set, if and only if the function returns %NULL.
861 * Returns: the new #GThread on success
866 g_thread_new (const gchar *name,
872 return g_thread_new_internal (name, func, data, joinable, 0, FALSE, error);
877 * @name: a name for the new thread
878 * @func: a function to execute in the new thread
879 * @data: an argument to supply to the new thread
880 * @joinable: should this thread be joinable?
881 * @stack_size: a stack size for the new thread
882 * @error: return location for error
884 * This function creates a new thread.
886 * The @name can be useful for discriminating threads in
887 * a debugger. Some systems restrict the length of @name to
890 * If the underlying thread implementation supports it, the thread
891 * gets a stack size of @stack_size or the default value for the
892 * current platform, if @stack_size is 0.
894 * If @joinable is %TRUE, you can wait for this threads termination
895 * calling g_thread_join(). Otherwise the thread will just disappear
896 * when it terminates.
898 * The new thread executes the function @func with the argument @data.
899 * If the thread was created successfully, it is returned.
901 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
902 * The error is set, if and only if the function returns %NULL.
904 * <note><para>Only use a non-zero @stack_size if you
905 * really can't use the default instead. g_thread_new()
906 * does not take @stack_size, as it should only be used in cases
907 * in which it is unavoidable.</para></note>
909 * Returns: the new #GThread on success
914 g_thread_new_full (const gchar *name,
921 return g_thread_new_internal (name, func, data, joinable, stack_size, FALSE, error);
925 g_thread_new_internal (const gchar *name,
934 GError *local_error = NULL;
935 g_return_val_if_fail (func, NULL);
937 result = g_new0 (GRealThread, 1);
939 result->thread.joinable = joinable;
940 result->thread.func = func;
941 result->thread.data = data;
942 result->private_data = NULL;
943 result->enumerable = enumerable;
945 G_LOCK (g_thread_new);
946 g_system_thread_create (g_thread_create_proxy, result,
947 stack_size, joinable,
948 &result->system_thread, &local_error);
949 if (enumerable && !local_error)
950 g_enumerable_thread_add (result);
951 G_UNLOCK (g_thread_new);
955 g_propagate_error (error, local_error);
960 return (GThread*) result;
965 * @retval: the return value of this thread
967 * Exits the current thread. If another thread is waiting for that
968 * thread using g_thread_join() and the current thread is joinable, the
969 * waiting thread will be woken up and get @retval as the return value
970 * of g_thread_join(). If the current thread is not joinable, @retval
971 * is ignored. Calling
974 * g_thread_exit (retval);
977 * is equivalent to returning @retval from the function @func, as given
978 * to g_thread_create().
980 * <note><para>Never call g_thread_exit() from within a thread of a
981 * #GThreadPool, as that will mess up the bookkeeping and lead to funny
982 * and unwanted results.</para></note>
985 g_thread_exit (gpointer retval)
987 GRealThread* real = (GRealThread*) g_thread_self ();
988 real->retval = retval;
990 g_system_thread_exit ();
995 * @thread: a #GThread to be waited for
997 * Waits until @thread finishes, i.e. the function @func, as given to
998 * g_thread_create(), returns or g_thread_exit() is called by @thread.
999 * All resources of @thread including the #GThread struct are released.
1000 * @thread must have been created with @joinable=%TRUE in
1001 * g_thread_create(). The value returned by @func or given to
1002 * g_thread_exit() by @thread is returned by this function.
1004 * Returns: the return value of the thread
1007 g_thread_join (GThread *thread)
1009 GRealThread *real = (GRealThread*) thread;
1012 g_return_val_if_fail (thread, NULL);
1013 g_return_val_if_fail (thread->joinable, NULL);
1014 g_return_val_if_fail (!g_system_thread_equal (&real->system_thread, &zero_thread), NULL);
1016 g_system_thread_join (&real->system_thread);
1018 retval = real->retval;
1020 if (real->enumerable)
1021 g_enumerable_thread_remove (real);
1023 /* Just to make sure, this isn't used any more */
1024 thread->joinable = 0;
1025 g_system_thread_assign (real->system_thread, zero_thread);
1027 /* the thread structure for non-joinable threads is freed upon
1028 * thread end. We free the memory here. This will leave a loose end,
1029 * if a joinable thread is not joined.
1039 * This functions returns the #GThread corresponding to the calling
1042 * Returns: the current thread
1045 g_thread_self (void)
1047 GRealThread* thread = g_private_get (&g_thread_specific_private);
1051 /* If no thread data is available, provide and set one.
1052 * This can happen for the main thread and for threads
1053 * that are not created by GLib.
1055 thread = g_new0 (GRealThread, 1);
1056 thread->thread.joinable = FALSE; /* This is a safe guess */
1057 thread->thread.func = NULL;
1058 thread->thread.data = NULL;
1059 thread->private_data = NULL;
1060 thread->enumerable = FALSE;
1062 g_system_thread_self (&thread->system_thread);
1064 g_private_set (&g_thread_specific_private, thread);
1067 return (GThread*)thread;
1070 /* GMutex {{{1 ------------------------------------------------------ */
1075 * Allocated and initializes a new #GMutex.
1077 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
1084 mutex = g_slice_new (GMutex);
1085 g_mutex_init (mutex);
1094 * Destroys a @mutex that has been created with g_mutex_new().
1096 * Calling g_mutex_free() on a locked mutex may result
1097 * in undefined behaviour.
1100 g_mutex_free (GMutex *mutex)
1102 g_mutex_clear (mutex);
1103 g_slice_free (GMutex, mutex);
1106 /* GCond {{{1 ------------------------------------------------------ */
1111 * Allocates and initializes a new #GCond.
1113 * Returns: a newly allocated #GCond. Free with g_cond_free()
1120 cond = g_slice_new (GCond);
1130 * Destroys a #GCond that has been created with g_cond_new().
1133 g_cond_free (GCond *cond)
1135 g_cond_clear (cond);
1136 g_slice_free (GCond, cond);
1140 /* vim: set foldmethod=marker: */