Blame glib/glib/gthread.c

Packit db3073
/* GLIB - Library of useful routines for C programming
Packit db3073
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit db3073
 *
Packit db3073
 * gthread.c: MT safety related functions
Packit db3073
 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
Packit db3073
 *                Owen Taylor
Packit db3073
 *
Packit db3073
 * This library is free software; you can redistribute it and/or
Packit db3073
 * modify it under the terms of the GNU Lesser General Public
Packit db3073
 * License as published by the Free Software Foundation; either
Packit db3073
 * version 2 of the License, or (at your option) any later version.
Packit db3073
 *
Packit db3073
 * This library is distributed in the hope that it will be useful,
Packit db3073
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit db3073
 * Lesser General Public License for more details.
Packit db3073
 *
Packit db3073
 * You should have received a copy of the GNU Lesser General Public
Packit db3073
 * License along with this library; if not, write to the
Packit db3073
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit db3073
 * Boston, MA 02111-1307, USA.
Packit db3073
 */
Packit db3073
Packit db3073
/* Prelude {{{1 ----------------------------------------------------------- */
Packit db3073
Packit db3073
/*
Packit db3073
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit db3073
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit db3073
 * files for a list of changes.  These files are distributed with
Packit db3073
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit db3073
 */
Packit db3073
Packit db3073
/*
Packit db3073
 * MT safe
Packit db3073
 */
Packit db3073
Packit db3073
/* implement gthread.h's inline functions */
Packit db3073
#define G_IMPLEMENT_INLINES 1
Packit db3073
#define __G_THREAD_C__
Packit db3073
Packit db3073
#include "config.h"
Packit db3073
Packit db3073
#include "gthread.h"
Packit db3073
#include "gthreadprivate.h"
Packit db3073
Packit db3073
#include <string.h>
Packit db3073
Packit db3073
#ifdef HAVE_UNISTD_H
Packit db3073
#include <unistd.h>
Packit db3073
#endif
Packit db3073
Packit db3073
#ifndef G_OS_WIN32
Packit db3073
#include <sys/time.h>
Packit db3073
#include <time.h>
Packit db3073
#else
Packit db3073
#include <windows.h>
Packit db3073
#endif /* G_OS_WIN32 */
Packit db3073
Packit db3073
#include "gslice.h"
Packit db3073
#include "gstrfuncs.h"
Packit db3073
#include "gtestutils.h"
Packit db3073
Packit db3073
/**
Packit db3073
 * SECTION:threads
Packit db3073
 * @title: Threads
Packit db3073
 * @short_description: portable support for threads, mutexes, locks,
Packit db3073
 *     conditions and thread private data
Packit db3073
 * @see_also: #GThreadPool, #GAsyncQueue
Packit db3073
 *
Packit db3073
 * Threads act almost like processes, but unlike processes all threads
Packit db3073
 * of one process share the same memory. This is good, as it provides
Packit db3073
 * easy communication between the involved threads via this shared
Packit db3073
 * memory, and it is bad, because strange things (so called
Packit db3073
 * "Heisenbugs") might happen if the program is not carefully designed.
Packit db3073
 * In particular, due to the concurrent nature of threads, no
Packit db3073
 * assumptions on the order of execution of code running in different
Packit db3073
 * threads can be made, unless order is explicitly forced by the
Packit db3073
 * programmer through synchronization primitives.
Packit db3073
 *
Packit db3073
 * The aim of the thread-related functions in GLib is to provide a
Packit db3073
 * portable means for writing multi-threaded software. There are
Packit db3073
 * primitives for mutexes to protect the access to portions of memory
Packit db3073
 * (#GMutex, #GRecMutex and #GRWLock). There is a facility to use
Packit db3073
 * individual bits for locks (g_bit_lock()). There are primitives
Packit db3073
 * for condition variables to allow synchronization of threads (#GCond).
Packit db3073
 * There are primitives for thread-private data - data that every
Packit db3073
 * thread has a private instance of (#GPrivate). There are facilities
Packit db3073
 * for one-time initialization (#GOnce, g_once_init_enter()). Finally,
Packit db3073
 * there are primitives to create and manage threads (#GThread).
Packit db3073
 *
Packit db3073
 * The GLib threading system used to be initialized with g_thread_init().
Packit db3073
 * This is no longer necessary. Since version 2.32, the GLib threading
Packit db3073
 * system is automatically initialized at the start of your program,
Packit db3073
 * and all thread-creation functions and synchronization primitives
Packit db3073
 * are available right away.
Packit db3073
 *
Packit db3073
 * Note that it is not safe to assume that your program has no threads
Packit db3073
 * even if you don't call g_thread_new() yourself. GLib and GIO can
Packit db3073
 * and will create threads for their own purposes in some cases, such
Packit db3073
 * as when using g_unix_signal_source_new() or when using GDBus.
Packit db3073
 *
Packit db3073
 * Originally, UNIX did not have threads, and therefore some traditional
Packit db3073
 * UNIX APIs are problematic in threaded programs. Some notable examples
Packit db3073
 * are
Packit db3073
 * <itemizedlist>
Packit db3073
 *   <listitem>
Packit db3073
 *     C library functions that return data in statically allocated
Packit db3073
 *     buffers, such as strtok() or strerror(). For many of these,
Packit db3073
 *     there are thread-safe variants with a _r suffix, or you can
Packit db3073
 *     look at corresponding GLib APIs (like g_strsplit() or g_strerror()).
Packit db3073
 *   </listitem>
Packit db3073
 *   <listitem>
Packit db3073
 *     setenv() and unsetenv() manipulate the process environment in
Packit db3073
 *     a not thread-safe way, and may interfere with getenv() calls
Packit db3073
 *     in other threads. Note that getenv() calls may be
Packit db3073
 *     <quote>hidden</quote> behind other APIs. For example, GNU gettext()
Packit db3073
 *     calls getenv() under the covers. In general, it is best to treat
Packit db3073
 *     the environment as readonly. If you absolutely have to modify the
Packit db3073
 *     environment, do it early in main(), when no other threads are around yet.
Packit db3073
 *   </listitem>
Packit db3073
 *   <listitem>
Packit db3073
 *     setlocale() changes the locale for the entire process, affecting
Packit db3073
 *     all threads. Temporary changes to the locale are often made to
Packit db3073
 *     change the behavior of string scanning or formatting functions
Packit db3073
 *     like scanf() or printf(). GLib offers a number of string APIs
Packit db3073
 *     (like g_ascii_formatd() or g_ascii_strtod()) that can often be
Packit db3073
 *     used as an alternative. Or you can use the uselocale() function
Packit db3073
 *     to change the locale only for the current thread.
Packit db3073
 *   </listitem>
Packit db3073
 *   <listitem>
Packit db3073
 *     fork() only takes the calling thread into the child's copy of the
Packit db3073
 *     process image.  If other threads were executing in critical
Packit db3073
 *     sections they could have left mutexes locked which could easily
Packit db3073
 *     cause deadlocks in the new child.  For this reason, you should
Packit db3073
 *     call exit() or exec() as soon as possible in the child and only
Packit db3073
 *     make signal-safe library calls before that.
Packit db3073
 *   </listitem>
Packit db3073
 *   <listitem>
Packit db3073
 *     daemon() uses fork() in a way contrary to what is described
Packit db3073
 *     above.  It should not be used with GLib programs.
Packit db3073
 *   </listitem>
Packit db3073
 * </itemizedlist>
Packit db3073
 *
Packit db3073
 * GLib itself is internally completely thread-safe (all global data is
Packit db3073
 * automatically locked), but individual data structure instances are
Packit db3073
 * not automatically locked for performance reasons. For example,
Packit db3073
 * you must coordinate accesses to the same #GHashTable from multiple
Packit db3073
 * threads. The two notable exceptions from this rule are #GMainLoop
Packit db3073
 * and #GAsyncQueue, which <emphasis>are</emphasis> thread-safe and
Packit db3073
 * need no further application-level locking to be accessed from
Packit db3073
 * multiple threads. Most refcounting functions such as g_object_ref()
Packit db3073
 * are also thread-safe.
Packit db3073
 */
Packit db3073
Packit db3073
/* G_LOCK Documentation {{{1 ---------------------------------------------- */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_LOCK_DEFINE:
Packit db3073
 * @name: the name of the lock
Packit db3073
 *
Packit db3073
 * The <literal>G_LOCK_*</literal> macros provide a convenient interface to #GMutex.
Packit db3073
 * #G_LOCK_DEFINE defines a lock. It can appear in any place where
Packit db3073
 * variable definitions may appear in programs, i.e. in the first block
Packit db3073
 * of a function or outside of functions. The @name parameter will be
Packit db3073
 * mangled to get the name of the #GMutex. This means that you
Packit db3073
 * can use names of existing variables as the parameter - e.g. the name
Packit db3073
 * of the variable you intend to protect with the lock. Look at our
Packit db3073
 * <function>give_me_next_number()</function> example using the
Packit db3073
 * <literal>G_LOCK_*</literal> macros:
Packit db3073
 *
Packit db3073
 * <example>
Packit db3073
 *  <title>Using the <literal>G_LOCK_*</literal> convenience macros</title>
Packit db3073
 *  <programlisting>
Packit db3073
 *   G_LOCK_DEFINE (current_number);
Packit db3073
 *
Packit db3073
 *   int
Packit db3073
 *   give_me_next_number (void)
Packit db3073
 *   {
Packit db3073
 *     static int current_number = 0;
Packit db3073
 *     int ret_val;
Packit db3073
 *
Packit db3073
 *     G_LOCK (current_number);
Packit db3073
 *     ret_val = current_number = calc_next_number (current_number);
Packit db3073
 *     G_UNLOCK (current_number);
Packit db3073
 *
Packit db3073
 *     return ret_val;
Packit db3073
 *   }
Packit db3073
 *  </programlisting>
Packit db3073
 * </example>
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_LOCK_DEFINE_STATIC:
Packit db3073
 * @name: the name of the lock
Packit db3073
 *
Packit db3073
 * This works like #G_LOCK_DEFINE, but it creates a static object.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_LOCK_EXTERN:
Packit db3073
 * @name: the name of the lock
Packit db3073
 *
Packit db3073
 * This declares a lock, that is defined with #G_LOCK_DEFINE in another
Packit db3073
 * module.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_LOCK:
Packit db3073
 * @name: the name of the lock
Packit db3073
 *
Packit db3073
 * Works like g_mutex_lock(), but for a lock defined with
Packit db3073
 * #G_LOCK_DEFINE.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_TRYLOCK:
Packit db3073
 * @name: the name of the lock
Packit db3073
 * @Returns: %TRUE, if the lock could be locked.
Packit db3073
 *
Packit db3073
 * Works like g_mutex_trylock(), but for a lock defined with
Packit db3073
 * #G_LOCK_DEFINE.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_UNLOCK:
Packit db3073
 * @name: the name of the lock
Packit db3073
 *
Packit db3073
 * Works like g_mutex_unlock(), but for a lock defined with
Packit db3073
 * #G_LOCK_DEFINE.
Packit db3073
 */
Packit db3073
Packit db3073
/* GMutex Documentation {{{1 ------------------------------------------ */
Packit db3073
Packit db3073
/**
Packit db3073
 * GMutex:
Packit db3073
 *
Packit db3073
 * The #GMutex struct is an opaque data structure to represent a mutex
Packit db3073
 * (mutual exclusion). It can be used to protect data against shared
Packit db3073
 * access. Take for example the following function:
Packit db3073
 *
Packit db3073
 * <example>
Packit db3073
 *  <title>A function which will not work in a threaded environment</title>
Packit db3073
 *  <programlisting>
Packit db3073
 *   int
Packit db3073
 *   give_me_next_number (void)
Packit db3073
 *   {
Packit db3073
 *     static int current_number = 0;
Packit db3073
 *
Packit db3073
 *     /* now do a very complicated calculation to calculate the new
Packit db3073
 *      * number, this might for example be a random number generator
Packit db3073
 *      */
Packit db3073
 *     current_number = calc_next_number (current_number);
Packit db3073
 *
Packit db3073
 *     return current_number;
Packit db3073
 *   }
Packit db3073
 *  </programlisting>
Packit db3073
 * </example>
Packit db3073
 *
Packit db3073
 * It is easy to see that this won't work in a multi-threaded
Packit db3073
 * application. There current_number must be protected against shared
Packit db3073
 * access. A #GMutex can be used as a solution to this problem:
Packit db3073
 *
Packit db3073
 * <example>
Packit db3073
 *  <title>Using GMutex to protected a shared variable</title>
Packit db3073
 *  <programlisting>
Packit db3073
 *   int
Packit db3073
 *   give_me_next_number (void)
Packit db3073
 *   {
Packit db3073
 *     static GMutex mutex;
Packit db3073
 *     static int current_number = 0;
Packit db3073
 *     int ret_val;
Packit db3073
 *
Packit db3073
 *     g_mutex_lock (&mutex);
Packit db3073
 *     ret_val = current_number = calc_next_number (current_number);
Packit db3073
 *     g_mutex_unlock (&mutex);
Packit db3073
 *
Packit db3073
 *     return ret_val;
Packit db3073
 *   }
Packit db3073
 *  </programlisting>
Packit db3073
 * </example>
Packit db3073
 *
Packit db3073
 * Notice that the #GMutex is not initialised to any particular value.
Packit db3073
 * Its placement in static storage ensures that it will be initialised
Packit db3073
 * to all-zeros, which is appropriate.
Packit db3073
 *
Packit db3073
 * If a #GMutex is placed in other contexts (eg: embedded in a struct)
Packit db3073
 * then it must be explicitly initialised using g_mutex_init().
Packit db3073
 *
Packit db3073
 * A #GMutex should only be accessed via <function>g_mutex_</function>
Packit db3073
 * functions.
Packit db3073
 */
Packit db3073
Packit db3073
/* GRecMutex Documentation {{{1 -------------------------------------- */
Packit db3073
Packit db3073
/**
Packit db3073
 * GRecMutex:
Packit db3073
 *
Packit db3073
 * The GRecMutex struct is an opaque data structure to represent a
Packit db3073
 * recursive mutex. It is similar to a #GMutex with the difference
Packit db3073
 * that it is possible to lock a GRecMutex multiple times in the same
Packit db3073
 * thread without deadlock. When doing so, care has to be taken to
Packit db3073
 * unlock the recursive mutex as often as it has been locked.
Packit db3073
 *
Packit db3073
 * If a #GRecMutex is allocated in static storage then it can be used
Packit db3073
 * without initialisation.  Otherwise, you should call
Packit db3073
 * g_rec_mutex_init() on it and g_rec_mutex_clear() when done.
Packit db3073
 *
Packit db3073
 * A GRecMutex should only be accessed with the
Packit db3073
 * <function>g_rec_mutex_</function> functions.
Packit db3073
 *
Packit db3073
 * Since: 2.32
Packit db3073
 */
Packit db3073
Packit db3073
/* GRWLock Documentation {{{1 ---------------------------------------- */
Packit db3073
Packit db3073
/**
Packit db3073
 * GRWLock:
Packit db3073
 *
Packit db3073
 * The GRWLock struct is an opaque data structure to represent a
Packit db3073
 * reader-writer lock. It is similar to a #GMutex in that it allows
Packit db3073
 * multiple threads to coordinate access to a shared resource.
Packit db3073
 *
Packit db3073
 * The difference to a mutex is that a reader-writer lock discriminates
Packit db3073
 * between read-only ('reader') and full ('writer') access. While only
Packit db3073
 * one thread at a time is allowed write access (by holding the 'writer'
Packit db3073
 * lock via g_rw_lock_writer_lock()), multiple threads can gain
Packit db3073
 * simultaneous read-only access (by holding the 'reader' lock via
Packit db3073
 * g_rw_lock_reader_lock()).
Packit db3073
 *
Packit db3073
 * <example>
Packit db3073
 *  <title>An array with access functions</title>
Packit db3073
 *  <programlisting>
Packit db3073
 *   GRWLock lock;
Packit db3073
 *   GPtrArray *array;
Packit db3073
 *
Packit db3073
 *   gpointer
Packit db3073
 *   my_array_get (guint index)
Packit db3073
 *   {
Packit db3073
 *     gpointer retval = NULL;
Packit db3073
 *
Packit db3073
 *     if (!array)
Packit db3073
 *       return NULL;
Packit db3073
 *
Packit db3073
 *     g_rw_lock_reader_lock (&lock);
Packit db3073
 *     if (index < array->len)
Packit db3073
 *       retval = g_ptr_array_index (array, index);
Packit db3073
 *     g_rw_lock_reader_unlock (&lock);
Packit db3073
 *
Packit db3073
 *     return retval;
Packit db3073
 *   }
Packit db3073
 *
Packit db3073
 *   void
Packit db3073
 *   my_array_set (guint index, gpointer data)
Packit db3073
 *   {
Packit db3073
 *     g_rw_lock_writer_lock (&lock);
Packit db3073
 *
Packit db3073
 *     if (!array)
Packit db3073
 *       array = g_ptr_array_new ();
Packit db3073
 *
Packit db3073
 *     if (index >= array->len)
Packit db3073
 *       g_ptr_array_set_size (array, index+1);
Packit db3073
 *     g_ptr_array_index (array, index) = data;
Packit db3073
 *
Packit db3073
 *     g_rw_lock_writer_unlock (&lock);
Packit db3073
 *   }
Packit db3073
 *  </programlisting>
Packit db3073
 *  <para>
Packit db3073
 *    This example shows an array which can be accessed by many readers
Packit db3073
 *    (the <function>my_array_get()</function> function) simultaneously,
Packit db3073
 *    whereas the writers (the <function>my_array_set()</function>
Packit db3073
 *    function) will only be allowed once at a time and only if no readers
Packit db3073
 *    currently access the array. This is because of the potentially
Packit db3073
 *    dangerous resizing of the array. Using these functions is fully
Packit db3073
 *    multi-thread safe now.
Packit db3073
 *  </para>
Packit db3073
 * </example>
Packit db3073
 *
Packit db3073
 * If a #GRWLock is allocated in static storage then it can be used
Packit db3073
 * without initialisation.  Otherwise, you should call
Packit db3073
 * g_rw_lock_init() on it and g_rw_lock_clear() when done.
Packit db3073
 *
Packit db3073
 * A GRWLock should only be accessed with the
Packit db3073
 * <function>g_rw_lock_</function> functions.
Packit db3073
 *
Packit db3073
 * Since: 2.32
Packit db3073
 */
Packit db3073
Packit db3073
/* GCond Documentation {{{1 ------------------------------------------ */
Packit db3073
Packit db3073
/**
Packit db3073
 * GCond:
Packit db3073
 *
Packit db3073
 * The #GCond struct is an opaque data structure that represents a
Packit db3073
 * condition. Threads can block on a #GCond if they find a certain
Packit db3073
 * condition to be false. If other threads change the state of this
Packit db3073
 * condition they signal the #GCond, and that causes the waiting
Packit db3073
 * threads to be woken up.
Packit db3073
 *
Packit db3073
 * Consider the following example of a shared variable.  One or more
Packit db3073
 * threads can wait for data to be published to the variable and when
Packit db3073
 * another thread publishes the data, it can signal one of the waiting
Packit db3073
 * threads to wake up to collect the data.
Packit db3073
 *
Packit db3073
 * <example>
Packit db3073
 *  <title>
Packit db3073
 *   Using GCond to block a thread until a condition is satisfied
Packit db3073
 *  </title>
Packit db3073
 *  <programlisting>
Packit db3073
 *   gpointer current_data = NULL;
Packit db3073
 *   GMutex data_mutex;
Packit db3073
 *   GCond data_cond;
Packit db3073
 *
Packit db3073
 *   void
Packit db3073
 *   push_data (gpointer data)
Packit db3073
 *   {
Packit db3073
 *     g_mutex_lock (&data_mutex);
Packit db3073
 *     current_data = data;
Packit db3073
 *     g_cond_signal (&data_cond);
Packit db3073
 *     g_mutex_unlock (&data_mutex);
Packit db3073
 *   }
Packit db3073
 *
Packit db3073
 *   gpointer
Packit db3073
 *   pop_data (void)
Packit db3073
 *   {
Packit db3073
 *     gpointer data;
Packit db3073
 *
Packit db3073
 *     g_mutex_lock (&data_mutex);
Packit db3073
 *     while (!current_data)
Packit db3073
 *       g_cond_wait (&data_cond, &data_mutex);
Packit db3073
 *     data = current_data;
Packit db3073
 *     current_data = NULL;
Packit db3073
 *     g_mutex_unlock (&data_mutex);
Packit db3073
 *
Packit db3073
 *     return data;
Packit db3073
 *   }
Packit db3073
 *  </programlisting>
Packit db3073
 * </example>
Packit db3073
 *
Packit db3073
 * Whenever a thread calls pop_data() now, it will wait until
Packit db3073
 * current_data is non-%NULL, i.e. until some other thread
Packit db3073
 * has called push_data().
Packit db3073
 *
Packit db3073
 * The example shows that use of a condition variable must always be
Packit db3073
 * paired with a mutex.  Without the use of a mutex, there would be a
Packit db3073
 * race between the check of <varname>current_data</varname> by the
Packit db3073
 * while loop in <function>pop_data</function> and waiting.
Packit db3073
 * Specifically, another thread could set <varname>pop_data</varname>
Packit db3073
 * after the check, and signal the cond (with nobody waiting on it)
Packit db3073
 * before the first thread goes to sleep.  #GCond is specifically useful
Packit db3073
 * for its ability to release the mutex and go to sleep atomically.
Packit db3073
 *
Packit db3073
 * It is also important to use the g_cond_wait() and g_cond_wait_until()
Packit db3073
 * functions only inside a loop which checks for the condition to be
Packit db3073
 * true.  See g_cond_wait() for an explanation of why the condition may
Packit db3073
 * not be true even after it returns.
Packit db3073
 *
Packit db3073
 * If a #GCond is allocated in static storage then it can be used
Packit db3073
 * without initialisation.  Otherwise, you should call g_cond_init() on
Packit db3073
 * it and g_cond_clear() when done.
Packit db3073
 *
Packit db3073
 * A #GCond should only be accessed via the <function>g_cond_</function>
Packit db3073
 * functions.
Packit db3073
 */
Packit db3073
Packit db3073
/* GThread Documentation {{{1 ---------------------------------------- */
Packit db3073
Packit db3073
/**
Packit db3073
 * GThread:
Packit db3073
 *
Packit db3073
 * The #GThread struct represents a running thread. This struct
Packit db3073
 * is returned by g_thread_new() or g_thread_try_new(). You can
Packit db3073
 * obtain the #GThread struct representing the current thead by
Packit db3073
 * calling g_thread_self().
Packit db3073
 *
Packit db3073
 * GThread is refcounted, see g_thread_ref() and g_thread_unref().
Packit db3073
 * The thread represented by it holds a reference while it is running,
Packit db3073
 * and g_thread_join() consumes the reference that it is given, so
Packit db3073
 * it is normally not necessary to manage GThread references
Packit db3073
 * explicitly.
Packit db3073
 *
Packit db3073
 * The structure is opaque -- none of its fields may be directly
Packit db3073
 * accessed.
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * GThreadFunc:
Packit db3073
 * @data: data passed to the thread
Packit db3073
 *
Packit db3073
 * Specifies the type of the @func functions passed to g_thread_new()
Packit db3073
 * or g_thread_try_new().
Packit db3073
 *
Packit db3073
 * Returns: the return value of the thread
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * g_thread_supported:
Packit db3073
 *
Packit db3073
 * This macro returns %TRUE if the thread system is initialized,
Packit db3073
 * and %FALSE if it is not.
Packit db3073
 *
Packit db3073
 * For language bindings, g_thread_get_initialized() provides
Packit db3073
 * the same functionality as a function.
Packit db3073
 *
Packit db3073
 * Returns: %TRUE, if the thread system is initialized
Packit db3073
 */
Packit db3073
Packit db3073
/* GThreadError {{{1 ------------------------------------------------------- */
Packit db3073
/**
Packit db3073
 * GThreadError:
Packit db3073
 * @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
Packit db3073
 *                        shortage. Try again later.
Packit db3073
 *
Packit db3073
 * Possible errors of thread related functions.
Packit db3073
 **/
Packit db3073
Packit db3073
/**
Packit db3073
 * G_THREAD_ERROR:
Packit db3073
 *
Packit db3073
 * The error domain of the GLib thread subsystem.
Packit db3073
 **/
Packit db3073
GQuark
Packit db3073
g_thread_error_quark (void)
Packit db3073
{
Packit db3073
  return g_quark_from_static_string ("g_thread_error");
Packit db3073
}
Packit db3073
Packit db3073
/* Local Data {{{1 -------------------------------------------------------- */
Packit db3073
Packit db3073
static GMutex    g_once_mutex;
Packit db3073
static GCond     g_once_cond;
Packit db3073
static GSList   *g_once_init_list = NULL;
Packit db3073
Packit db3073
static void g_thread_cleanup (gpointer data);
Packit db3073
static GPrivate     g_thread_specific_private = G_PRIVATE_INIT (g_thread_cleanup);
Packit db3073
Packit db3073
G_LOCK_DEFINE_STATIC (g_thread_new);
Packit db3073
Packit db3073
/* GOnce {{{1 ------------------------------------------------------------- */
Packit db3073
Packit db3073
/**
Packit db3073
 * GOnce:
Packit db3073
 * @status: the status of the #GOnce
Packit db3073
 * @retval: the value returned by the call to the function, if @status
Packit db3073
 *          is %G_ONCE_STATUS_READY
Packit db3073
 *
Packit db3073
 * A #GOnce struct controls a one-time initialization function. Any
Packit db3073
 * one-time initialization function must have its own unique #GOnce
Packit db3073
 * struct.
Packit db3073
 *
Packit db3073
 * Since: 2.4
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * G_ONCE_INIT:
Packit db3073
 *
Packit db3073
 * A #GOnce must be initialized with this macro before it can be used.
Packit db3073
 *
Packit db3073
 * |[
Packit db3073
 *   GOnce my_once = G_ONCE_INIT;
Packit db3073
 * ]|
Packit db3073
 *
Packit db3073
 * Since: 2.4
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * GOnceStatus:
Packit db3073
 * @G_ONCE_STATUS_NOTCALLED: the function has not been called yet.
Packit db3073
 * @G_ONCE_STATUS_PROGRESS: the function call is currently in progress.
Packit db3073
 * @G_ONCE_STATUS_READY: the function has been called.
Packit db3073
 *
Packit db3073
 * The possible statuses of a one-time initialization function
Packit db3073
 * controlled by a #GOnce struct.
Packit db3073
 *
Packit db3073
 * Since: 2.4
Packit db3073
 */
Packit db3073
Packit db3073
/**
Packit db3073
 * g_once:
Packit db3073
 * @once: a #GOnce structure
Packit db3073
 * @func: the #GThreadFunc function associated to @once. This function
Packit db3073
 *        is called only once, regardless of the number of times it and
Packit db3073
 *        its associated #GOnce struct are passed to g_once().
Packit db3073
 * @arg: data to be passed to @func
Packit db3073
 *
Packit db3073
 * The first call to this routine by a process with a given #GOnce
Packit db3073
 * struct calls @func with the given argument. Thereafter, subsequent
Packit db3073
 * calls to g_once()  with the same #GOnce struct do not call @func
Packit db3073
 * again, but return the stored result of the first call. On return
Packit db3073
 * from g_once(), the status of @once will be %G_ONCE_STATUS_READY.
Packit db3073
 *
Packit db3073
 * For example, a mutex or a thread-specific data key must be created
Packit db3073
 * exactly once. In a threaded environment, calling g_once() ensures
Packit db3073
 * that the initialization is serialized across multiple threads.
Packit db3073
 *
Packit db3073
 * Calling g_once() recursively on the same #GOnce struct in
Packit db3073
 * @func will lead to a deadlock.
Packit db3073
 *
Packit db3073
 * |[
Packit db3073
 *   gpointer
Packit db3073
 *   get_debug_flags (void)
Packit db3073
 *   {
Packit db3073
 *     static GOnce my_once = G_ONCE_INIT;
Packit db3073
 *
Packit db3073
 *     g_once (&my_once, parse_debug_flags, NULL);
Packit db3073
 *
Packit db3073
 *     return my_once.retval;
Packit db3073
 *   }
Packit db3073
 * ]|
Packit db3073
 *
Packit db3073
 * Since: 2.4
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_once_impl (GOnce       *once,
Packit db3073
	     GThreadFunc  func,
Packit db3073
	     gpointer     arg)
Packit db3073
{
Packit db3073
  g_mutex_lock (&g_once_mutex);
Packit db3073
Packit db3073
  while (once->status == G_ONCE_STATUS_PROGRESS)
Packit db3073
    g_cond_wait (&g_once_cond, &g_once_mutex);
Packit db3073
Packit db3073
  if (once->status != G_ONCE_STATUS_READY)
Packit db3073
    {
Packit db3073
      once->status = G_ONCE_STATUS_PROGRESS;
Packit db3073
      g_mutex_unlock (&g_once_mutex);
Packit db3073
Packit db3073
      once->retval = func (arg);
Packit db3073
Packit db3073
      g_mutex_lock (&g_once_mutex);
Packit db3073
      once->status = G_ONCE_STATUS_READY;
Packit db3073
      g_cond_broadcast (&g_once_cond);
Packit db3073
    }
Packit db3073
Packit db3073
  g_mutex_unlock (&g_once_mutex);
Packit db3073
Packit db3073
  return once->retval;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_once_init_enter:
Packit db3073
 * @location: location of a static initializable variable containing 0
Packit db3073
 *
Packit db3073
 * Function to be called when starting a critical initialization
Packit db3073
 * section. The argument @location must point to a static
Packit db3073
 * 0-initialized variable that will be set to a value other than 0 at
Packit db3073
 * the end of the initialization section. In combination with
Packit db3073
 * g_once_init_leave() and the unique address @value_location, it can
Packit db3073
 * be ensured that an initialization section will be executed only once
Packit db3073
 * during a program's life time, and that concurrent threads are
Packit db3073
 * blocked until initialization completed. To be used in constructs
Packit db3073
 * like this:
Packit db3073
 *
Packit db3073
 * |[
Packit db3073
 *   static gsize initialization_value = 0;
Packit db3073
 *
Packit db3073
 *   if (g_once_init_enter (&initialization_value))
Packit db3073
 *     {
Packit db3073
 *       gsize setup_value = 42; /** initialization code here **/
Packit db3073
 *
Packit db3073
 *       g_once_init_leave (&initialization_value, setup_value);
Packit db3073
 *     }
Packit db3073
 *
Packit db3073
 *   /** use initialization_value here **/
Packit db3073
 * ]|
Packit db3073
 *
Packit db3073
 * Returns: %TRUE if the initialization section should be entered,
Packit db3073
 *     %FALSE and blocks otherwise
Packit db3073
 *
Packit db3073
 * Since: 2.14
Packit db3073
 */
Packit db3073
gboolean
Packit db3073
(g_once_init_enter) (volatile void *location)
Packit db3073
{
Packit db3073
  volatile gsize *value_location = location;
Packit db3073
  gboolean need_init = FALSE;
Packit db3073
  g_mutex_lock (&g_once_mutex);
Packit db3073
  if (g_atomic_pointer_get (value_location) == NULL)
Packit db3073
    {
Packit db3073
      if (!g_slist_find (g_once_init_list, (void*) value_location))
Packit db3073
        {
Packit db3073
          need_init = TRUE;
Packit db3073
          g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
Packit db3073
        }
Packit db3073
      else
Packit db3073
        do
Packit db3073
          g_cond_wait (&g_once_cond, &g_once_mutex);
Packit db3073
        while (g_slist_find (g_once_init_list, (void*) value_location));
Packit db3073
    }
Packit db3073
  g_mutex_unlock (&g_once_mutex);
Packit db3073
  return need_init;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_once_init_leave:
Packit db3073
 * @location: location of a static initializable variable containing 0
Packit db3073
 * @result: new non-0 value for *@value_location
Packit db3073
 *
Packit db3073
 * Counterpart to g_once_init_enter(). Expects a location of a static
Packit db3073
 * 0-initialized initialization variable, and an initialization value
Packit db3073
 * other than 0. Sets the variable to the initialization value, and
Packit db3073
 * releases concurrent threads blocking in g_once_init_enter() on this
Packit db3073
 * initialization variable.
Packit db3073
 *
Packit db3073
 * Since: 2.14
Packit db3073
 */
Packit db3073
void
Packit db3073
(g_once_init_leave) (volatile void *location,
Packit db3073
                     gsize          result)
Packit db3073
{
Packit db3073
  volatile gsize *value_location = location;
Packit db3073
Packit db3073
  g_return_if_fail (g_atomic_pointer_get (value_location) == NULL);
Packit db3073
  g_return_if_fail (result != 0);
Packit db3073
  g_return_if_fail (g_once_init_list != NULL);
Packit db3073
Packit db3073
  g_atomic_pointer_set (value_location, result);
Packit db3073
  g_mutex_lock (&g_once_mutex);
Packit db3073
  g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
Packit db3073
  g_cond_broadcast (&g_once_cond);
Packit db3073
  g_mutex_unlock (&g_once_mutex);
Packit db3073
}
Packit db3073
Packit db3073
/* GThread {{{1 -------------------------------------------------------- */
Packit db3073
Packit db3073
/**
Packit db3073
 * g_thread_ref:
Packit db3073
 * @thread: a #GThread
Packit db3073
 *
Packit db3073
 * Increase the reference count on @thread.
Packit db3073
 *
Packit db3073
 * Returns: a new reference to @thread
Packit db3073
 *
Packit db3073
 * Since: 2.32
Packit db3073
 */
Packit db3073
GThread *
Packit db3073
g_thread_ref (GThread *thread)
Packit db3073
{
Packit db3073
  GRealThread *real = (GRealThread *) thread;
Packit db3073
Packit db3073
  g_atomic_int_inc (&real->ref_count);
Packit db3073
Packit db3073
  return thread;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_thread_unref:
Packit db3073
 * @thread: a #GThread
Packit db3073
 *
Packit db3073
 * Decrease the reference count on @thread, possibly freeing all
Packit db3073
 * resources associated with it.
Packit db3073
 *
Packit db3073
 * Note that each thread holds a reference to its #GThread while
Packit db3073
 * it is running, so it is safe to drop your own reference to it
Packit db3073
 * if you don't need it anymore.
Packit db3073
 *
Packit db3073
 * Since: 2.32
Packit db3073
 */
Packit db3073
void
Packit db3073
g_thread_unref (GThread *thread)
Packit db3073
{
Packit db3073
  GRealThread *real = (GRealThread *) thread;
Packit db3073
Packit db3073
  if (g_atomic_int_dec_and_test (&real->ref_count))
Packit db3073
    {
Packit db3073
      if (real->ours)
Packit db3073
        g_system_thread_free (real);
Packit db3073
      else
Packit db3073
        g_slice_free (GRealThread, real);
Packit db3073
    }
Packit db3073
}
Packit db3073
Packit db3073
static void
Packit db3073
g_thread_cleanup (gpointer data)
Packit db3073
{
Packit db3073
  g_thread_unref (data);
Packit db3073
}
Packit db3073
Packit db3073
gpointer
Packit db3073
g_thread_proxy (gpointer data)
Packit db3073
{
Packit db3073
  GRealThread* thread = data;
Packit db3073
Packit db3073
  g_assert (data);
Packit db3073
Packit db3073
  /* This has to happen before G_LOCK, as that might call g_thread_self */
Packit db3073
  g_private_set (&g_thread_specific_private, data);
Packit db3073
Packit db3073
  /* The lock makes sure that g_thread_new_internal() has a chance to
Packit db3073
   * setup 'func' and 'data' before we make the call.
Packit db3073
   */
Packit db3073
  G_LOCK (g_thread_new);
Packit db3073
  G_UNLOCK (g_thread_new);
Packit db3073
Packit db3073
  if (thread->name)
Packit db3073
    {
Packit db3073
      g_system_thread_set_name (thread->name);
Packit db3073
      g_free (thread->name);
Packit db3073
      thread->name = NULL;
Packit db3073
    }
Packit db3073
Packit db3073
  thread->retval = thread->thread.func (thread->thread.data);
Packit db3073
Packit db3073
  return NULL;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_thread_new:
Packit db3073
 * @name: a name for the new thread
Packit db3073
 * @func: a function to execute in the new thread
Packit db3073
 * @data: an argument to supply to the new thread
Packit db3073
 *
Packit db3073
 * This function creates a new thread. The new thread starts by invoking
Packit db3073
 * @func with the argument data. The thread will run until @func returns
Packit db3073
 * or until g_thread_exit() is called from the new thread. The return value
Packit db3073
 * of @func becomes the return value of the thread, which can be obtained
Packit db3073
 * with g_thread_join().
Packit db3073
 *
Packit db3073
 * The @name can be useful for discriminating threads in a debugger.
Packit db3073
 * Some systems restrict the length of @name to 16 bytes.
Packit db3073
 *
Packit db3073
 * If the thread can not be created the program aborts. See
Packit db3073
 * g_thread_try_new() if you want to attempt to deal with failures.
Packit db3073
 *
Packit db3073
 * To free the struct returned by this function, use g_thread_unref().
Packit db3073
 * Note that g_thread_join() implicitly unrefs the #GThread as well.
Packit db3073
 *
Packit db3073
 * Returns: the new #GThread
Packit db3073
 *
Packit db3073
 * Since: 2.32
Packit db3073
 */
Packit db3073
GThread *
Packit db3073
g_thread_new (const gchar *name,
Packit db3073
              GThreadFunc  func,
Packit db3073
              gpointer     data)
Packit db3073
{
Packit db3073
  GError *error = NULL;
Packit db3073
  GThread *thread;
Packit db3073
Packit db3073
  thread = g_thread_new_internal (name, g_thread_proxy, func, data, 0, &error);
Packit db3073
Packit db3073
  if G_UNLIKELY (thread == NULL)
Packit db3073
    g_error ("creating thread '%s': %s", name ? name : "", error->message);
Packit db3073
Packit db3073
  return thread;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_thread_try_new:
Packit db3073
 * @name: a name for the new thread
Packit db3073
 * @func: a function to execute in the new thread
Packit db3073
 * @data: an argument to supply to the new thread
Packit db3073
 * @error: return location for error, or %NULL
Packit db3073
 *
Packit db3073
 * This function is the same as g_thread_new() except that
Packit db3073
 * it allows for the possibility of failure.
Packit db3073
 *
Packit db3073
 * If a thread can not be created (due to resource limits),
Packit db3073
 * @error is set and %NULL is returned.
Packit db3073
 *
Packit db3073
 * Returns: the new #GThread, or %NULL if an error occurred
Packit db3073
 *
Packit db3073
 * Since: 2.32
Packit db3073
 */
Packit db3073
GThread *
Packit db3073
g_thread_try_new (const gchar  *name,
Packit db3073
                  GThreadFunc   func,
Packit db3073
                  gpointer      data,
Packit db3073
                  GError      **error)
Packit db3073
{
Packit db3073
  return g_thread_new_internal (name, g_thread_proxy, func, data, 0, error);
Packit db3073
}
Packit db3073
Packit db3073
GThread *
Packit db3073
g_thread_new_internal (const gchar   *name,
Packit db3073
                       GThreadFunc    proxy,
Packit db3073
                       GThreadFunc    func,
Packit db3073
                       gpointer       data,
Packit db3073
                       gsize          stack_size,
Packit db3073
                       GError       **error)
Packit db3073
{
Packit db3073
  GRealThread *thread;
Packit db3073
Packit db3073
  g_return_val_if_fail (func != NULL, NULL);
Packit db3073
Packit db3073
  G_LOCK (g_thread_new);
Packit db3073
  thread = g_system_thread_new (proxy, stack_size, error);
Packit db3073
  if (thread)
Packit db3073
    {
Packit db3073
      thread->ref_count = 2;
Packit db3073
      thread->ours = TRUE;
Packit db3073
      thread->thread.joinable = TRUE;
Packit db3073
      thread->thread.func = func;
Packit db3073
      thread->thread.data = data;
Packit db3073
      thread->name = g_strdup (name);
Packit db3073
    }
Packit db3073
  G_UNLOCK (g_thread_new);
Packit db3073
Packit db3073
  return (GThread*) thread;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_thread_exit:
Packit db3073
 * @retval: the return value of this thread
Packit db3073
 *
Packit db3073
 * Terminates the current thread.
Packit db3073
 *
Packit db3073
 * If another thread is waiting for us using g_thread_join() then the
Packit db3073
 * waiting thread will be woken up and get @retval as the return value
Packit db3073
 * of g_thread_join().
Packit db3073
 *
Packit db3073
 * Calling <literal>g_thread_exit (retval)</literal> is equivalent to
Packit db3073
 * returning @retval from the function @func, as given to g_thread_new().
Packit db3073
 *
Packit db3073
 * <note><para>
Packit db3073
 *   You must only call g_thread_exit() from a thread that you created
Packit db3073
 *   yourself with g_thread_new() or related APIs.  You must not call
Packit db3073
 *   this function from a thread created with another threading library
Packit db3073
 *   or or from within a #GThreadPool.
Packit db3073
 * </para></note>
Packit db3073
 */
Packit db3073
void
Packit db3073
g_thread_exit (gpointer retval)
Packit db3073
{
Packit db3073
  GRealThread* real = (GRealThread*) g_thread_self ();
Packit db3073
Packit db3073
  if G_UNLIKELY (!real->ours)
Packit db3073
    g_error ("attempt to g_thread_exit() a thread not created by GLib");
Packit db3073
Packit db3073
  real->retval = retval;
Packit db3073
Packit db3073
  g_system_thread_exit ();
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_thread_join:
Packit db3073
 * @thread: a #GThread
Packit db3073
 *
Packit db3073
 * Waits until @thread finishes, i.e. the function @func, as
Packit db3073
 * given to g_thread_new(), returns or g_thread_exit() is called.
Packit db3073
 * If @thread has already terminated, then g_thread_join()
Packit db3073
 * returns immediately.
Packit db3073
 *
Packit db3073
 * Any thread can wait for any other thread by calling g_thread_join(),
Packit db3073
 * not just its 'creator'. Calling g_thread_join() from multiple threads
Packit db3073
 * for the same @thread leads to undefined behaviour.
Packit db3073
 *
Packit db3073
 * The value returned by @func or given to g_thread_exit() is
Packit db3073
 * returned by this function.
Packit db3073
 *
Packit db3073
 * g_thread_join() consumes the reference to the passed-in @thread.
Packit db3073
 * This will usually cause the #GThread struct and associated resources
Packit db3073
 * to be freed. Use g_thread_ref() to obtain an extra reference if you
Packit db3073
 * want to keep the GThread alive beyond the g_thread_join() call.
Packit db3073
 *
Packit db3073
 * Returns: the return value of the thread
Packit db3073
 */
Packit db3073
gpointer
Packit db3073
g_thread_join (GThread *thread)
Packit db3073
{
Packit db3073
  GRealThread *real = (GRealThread*) thread;
Packit db3073
  gpointer retval;
Packit db3073
Packit db3073
  g_return_val_if_fail (thread, NULL);
Packit db3073
  g_return_val_if_fail (real->ours, NULL);
Packit db3073
Packit db3073
  g_system_thread_wait (real);
Packit db3073
Packit db3073
  retval = real->retval;
Packit db3073
Packit db3073
  /* Just to make sure, this isn't used any more */
Packit db3073
  thread->joinable = 0;
Packit db3073
Packit db3073
  g_thread_unref (thread);
Packit db3073
Packit db3073
  return retval;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_thread_self:
Packit db3073
 *
Packit db3073
 * This functions returns the #GThread corresponding to the
Packit db3073
 * current thread. Note that this function does not increase
Packit db3073
 * the reference count of the returned struct.
Packit db3073
 *
Packit db3073
 * This function will return a #GThread even for threads that
Packit db3073
 * were not created by GLib (i.e. those created by other threading
Packit db3073
 * APIs). This may be useful for thread identification purposes
Packit db3073
 * (i.e. comparisons) but you must not use GLib functions (such
Packit db3073
 * as g_thread_join()) on these threads.
Packit db3073
 *
Packit db3073
 * Returns: the #GThread representing the current thread
Packit db3073
 */
Packit db3073
GThread*
Packit db3073
g_thread_self (void)
Packit db3073
{
Packit db3073
  GRealThread* thread = g_private_get (&g_thread_specific_private);
Packit db3073
Packit db3073
  if (!thread)
Packit db3073
    {
Packit db3073
      /* If no thread data is available, provide and set one.
Packit db3073
       * This can happen for the main thread and for threads
Packit db3073
       * that are not created by GLib.
Packit db3073
       */
Packit db3073
      thread = g_slice_new0 (GRealThread);
Packit db3073
      thread->ref_count = 1;
Packit db3073
Packit db3073
      g_private_set (&g_thread_specific_private, thread);
Packit db3073
    }
Packit db3073
Packit db3073
  return (GThread*) thread;
Packit db3073
}
Packit db3073
Packit db3073
/* Epilogue {{{1 */
Packit db3073
/* vim: set foldmethod=marker: */