Blame glib/deprecated/gthread-deprecated.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * gthread.c: MT safety related functions
Packit ae235b
 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
Packit ae235b
 *                Owen Taylor
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
/* we know we are deprecated here, no need for warnings */
Packit ae235b
#define GLIB_DISABLE_DEPRECATION_WARNINGS
Packit ae235b
Packit ae235b
#include "gmessages.h"
Packit ae235b
#include "gslice.h"
Packit ae235b
#include "gmain.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
#include "gthreadprivate.h"
Packit ae235b
#include "deprecated/gthread.h"
Packit ae235b
#include "garray.h"
Packit ae235b
Packit ae235b
#include "gutils.h"
Packit ae235b
Packit ae235b
/* {{{1 Documentation */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:threads-deprecated
Packit ae235b
 * @title: Deprecated thread API
Packit ae235b
 * @short_description: old thread APIs (for reference only)
Packit ae235b
 * @see_also: #GThread
Packit ae235b
 *
Packit ae235b
 * These APIs are deprecated.  You should not use them in new code.
Packit ae235b
 * This section remains only to assist with understanding code that was
Packit ae235b
 * written to use these APIs at some point in the past.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GThreadPriority:
Packit ae235b
 * @G_THREAD_PRIORITY_LOW: a priority lower than normal
Packit ae235b
 * @G_THREAD_PRIORITY_NORMAL: the default priority
Packit ae235b
 * @G_THREAD_PRIORITY_HIGH: a priority higher than normal
Packit ae235b
 * @G_THREAD_PRIORITY_URGENT: the highest priority
Packit ae235b
 *
Packit ae235b
 * Thread priorities.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Thread priorities no longer have any effect.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GThreadFunctions:
Packit ae235b
 * @mutex_new: virtual function pointer for g_mutex_new()
Packit ae235b
 * @mutex_lock: virtual function pointer for g_mutex_lock()
Packit ae235b
 * @mutex_trylock: virtual function pointer for g_mutex_trylock()
Packit ae235b
 * @mutex_unlock: virtual function pointer for g_mutex_unlock()
Packit ae235b
 * @mutex_free: virtual function pointer for g_mutex_free()
Packit ae235b
 * @cond_new: virtual function pointer for g_cond_new()
Packit ae235b
 * @cond_signal: virtual function pointer for g_cond_signal()
Packit ae235b
 * @cond_broadcast: virtual function pointer for g_cond_broadcast()
Packit ae235b
 * @cond_wait: virtual function pointer for g_cond_wait()
Packit ae235b
 * @cond_timed_wait: virtual function pointer for g_cond_timed_wait()
Packit ae235b
 * @cond_free: virtual function pointer for g_cond_free()
Packit ae235b
 * @private_new: virtual function pointer for g_private_new()
Packit ae235b
 * @private_get: virtual function pointer for g_private_get()
Packit ae235b
 * @private_set: virtual function pointer for g_private_set()
Packit ae235b
 * @thread_create: virtual function pointer for g_thread_create()
Packit ae235b
 * @thread_yield: virtual function pointer for g_thread_yield()
Packit ae235b
 * @thread_join: virtual function pointer for g_thread_join()
Packit ae235b
 * @thread_exit: virtual function pointer for g_thread_exit()
Packit ae235b
 * @thread_set_priority: virtual function pointer for
Packit ae235b
 *                       g_thread_set_priority()
Packit ae235b
 * @thread_self: virtual function pointer for g_thread_self()
Packit ae235b
 * @thread_equal: used internally by recursive mutex locks and by some
Packit ae235b
 *                assertion checks
Packit ae235b
 *
Packit ae235b
 * This function table is no longer used by g_thread_init()
Packit ae235b
 * to initialize the thread system.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_THREADS_IMPL_POSIX:
Packit ae235b
 *
Packit ae235b
 * This macro is defined if POSIX style threads are used.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32:POSIX threads are in use on all non-Windows systems.
Packit ae235b
 *                 Use G_OS_WIN32 to detect Windows.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_THREADS_IMPL_WIN32:
Packit ae235b
 *
Packit ae235b
 * This macro is defined if Windows style threads are used.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32:Use G_OS_WIN32 to detect Windows.
Packit ae235b
 */
Packit ae235b
Packit ae235b
Packit ae235b
/* {{{1 Exported Variables */
Packit ae235b
Packit ae235b
/* Set this FALSE to have previously-compiled GStaticMutex code use the
Packit ae235b
 * slow path (ie: call into us) to avoid compatibility problems.
Packit ae235b
 */
Packit ae235b
gboolean g_thread_use_default_impl = FALSE;
Packit ae235b
Packit ae235b
GThreadFunctions g_thread_functions_for_glib_use =
Packit ae235b
{
Packit ae235b
  g_mutex_new,
Packit ae235b
  g_mutex_lock,
Packit ae235b
  g_mutex_trylock,
Packit ae235b
  g_mutex_unlock,
Packit ae235b
  g_mutex_free,
Packit ae235b
  g_cond_new,
Packit ae235b
  g_cond_signal,
Packit ae235b
  g_cond_broadcast,
Packit ae235b
  g_cond_wait,
Packit ae235b
  g_cond_timed_wait,
Packit ae235b
  g_cond_free,
Packit ae235b
  g_private_new,
Packit ae235b
  g_private_get,
Packit ae235b
  g_private_set,
Packit ae235b
  NULL,
Packit ae235b
  g_thread_yield,
Packit ae235b
  NULL,
Packit ae235b
  NULL,
Packit ae235b
  NULL,
Packit ae235b
  NULL,
Packit ae235b
  NULL,
Packit ae235b
};
Packit ae235b
Packit ae235b
static guint64
Packit ae235b
gettime (void)
Packit ae235b
{
Packit ae235b
  return g_get_monotonic_time () * 1000;
Packit ae235b
}
Packit ae235b
Packit ae235b
guint64 (*g_thread_gettime) (void) = gettime;
Packit ae235b
Packit ae235b
/* Initialisation {{{1 ---------------------------------------------------- */
Packit ae235b
gboolean         g_threads_got_initialized = TRUE;
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_init:
Packit ae235b
 * @vtable: a function table of type #GThreadFunctions, that provides
Packit ae235b
 *     the entry points to the thread system to be used. Since 2.32,
Packit ae235b
 *     this parameter is ignored and should always be %NULL
Packit ae235b
 *
Packit ae235b
 * If you use GLib from more than one thread, you must initialize the
Packit ae235b
 * thread system by calling g_thread_init().
Packit ae235b
 *
Packit ae235b
 * Since version 2.24, calling g_thread_init() multiple times is allowed,
Packit ae235b
 * but nothing happens except for the first call.
Packit ae235b
 *
Packit ae235b
 * Since version 2.32, GLib does not support custom thread implementations
Packit ae235b
 * anymore and the @vtable parameter is ignored and you should pass %NULL.
Packit ae235b
 *
Packit ae235b
 * <note><para>g_thread_init() must not be called directly or indirectly
Packit ae235b
 * in a callback from GLib. Also no mutexes may be currently locked while
Packit ae235b
 * calling g_thread_init().</para></note>
Packit ae235b
 *
Packit ae235b
 * <note><para>To use g_thread_init() in your program, you have to link
Packit ae235b
 * with the libraries that the command <command>pkg-config --libs
Packit ae235b
 * gthread-2.0</command> outputs. This is not the case for all the
Packit ae235b
 * other thread-related functions of GLib. Those can be used without
Packit ae235b
 * having to link with the thread libraries.</para></note>
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: This function is no longer necessary. The GLib
Packit ae235b
 *     threading system is automatically initialized at the start
Packit ae235b
 *     of your program.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_get_initialized:
Packit ae235b
 *
Packit ae235b
 * Indicates if g_thread_init() has been called.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if threads have been initialized.
Packit ae235b
 *
Packit ae235b
 * Since: 2.20
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_thread_get_initialized (void)
Packit ae235b
{
Packit ae235b
  return g_thread_supported ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/* We need this for ABI compatibility */
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void g_thread_init_glib (void);
Packit ae235b
void g_thread_init_glib (void) { }
Packit ae235b
Packit ae235b
/* Internal variables {{{1 */
Packit ae235b
Packit ae235b
static GSList      *g_thread_all_threads = NULL;
Packit ae235b
static GSList      *g_thread_free_indices = NULL;
Packit ae235b
Packit ae235b
/* Protects g_thread_all_threads and g_thread_free_indices */
Packit ae235b
G_LOCK_DEFINE_STATIC (g_static_mutex);
Packit ae235b
G_LOCK_DEFINE_STATIC (g_thread);
Packit ae235b
Packit ae235b
/* Misc. GThread functions {{{1 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_set_priority:
Packit ae235b
 * @thread: a #GThread.
Packit ae235b
 * @priority: ignored
Packit ae235b
 *
Packit ae235b
 * This function does nothing.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Thread priorities no longer have any effect.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_set_priority (GThread         *thread,
Packit ae235b
                       GThreadPriority  priority)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_foreach:
Packit ae235b
 * @thread_func: function to call for all #GThread structures
Packit ae235b
 * @user_data: second argument to @thread_func
Packit ae235b
 *
Packit ae235b
 * Call @thread_func on all #GThreads that have been
Packit ae235b
 * created with g_thread_create().
Packit ae235b
 *
Packit ae235b
 * Note that threads may decide to exit while @thread_func is
Packit ae235b
 * running, so without intimate knowledge about the lifetime of
Packit ae235b
 * foreign threads, @thread_func shouldn't access the GThread*
Packit ae235b
 * pointer passed in as first argument. However, @thread_func will
Packit ae235b
 * not be called for threads which are known to have exited already.
Packit ae235b
 *
Packit ae235b
 * Due to thread lifetime checks, this function has an execution complexity
Packit ae235b
 * which is quadratic in the number of existing threads.
Packit ae235b
 *
Packit ae235b
 * Since: 2.10
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: There aren't many things you can do with a #GThread,
Packit ae235b
 *     except comparing it with one that was returned from g_thread_create().
Packit ae235b
 *     There are better ways to find out if your thread is still alive.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_foreach (GFunc    thread_func,
Packit ae235b
                  gpointer user_data)
Packit ae235b
{
Packit ae235b
  GSList *slist = NULL;
Packit ae235b
  GRealThread *thread;
Packit ae235b
  g_return_if_fail (thread_func != NULL);
Packit ae235b
  /* snapshot the list of threads for iteration */
Packit ae235b
  G_LOCK (g_thread);
Packit ae235b
  slist = g_slist_copy (g_thread_all_threads);
Packit ae235b
  G_UNLOCK (g_thread);
Packit ae235b
  /* walk the list, skipping non-existent threads */
Packit ae235b
  while (slist)
Packit ae235b
    {
Packit ae235b
      GSList *node = slist;
Packit ae235b
      slist = node->next;
Packit ae235b
      /* check whether the current thread still exists */
Packit ae235b
      G_LOCK (g_thread);
Packit ae235b
      if (g_slist_find (g_thread_all_threads, node->data))
Packit ae235b
        thread = node->data;
Packit ae235b
      else
Packit ae235b
        thread = NULL;
Packit ae235b
      G_UNLOCK (g_thread);
Packit ae235b
      if (thread)
Packit ae235b
        thread_func (thread, user_data);
Packit ae235b
      g_slist_free_1 (node);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_enumerable_thread_remove (gpointer data)
Packit ae235b
{
Packit ae235b
  GRealThread *thread = data;
Packit ae235b
Packit ae235b
  G_LOCK (g_thread);
Packit ae235b
  g_thread_all_threads = g_slist_remove (g_thread_all_threads, thread);
Packit ae235b
  G_UNLOCK (g_thread);
Packit ae235b
}
Packit ae235b
Packit ae235b
GPrivate enumerable_thread_private = G_PRIVATE_INIT (g_enumerable_thread_remove);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_enumerable_thread_add (GRealThread *thread)
Packit ae235b
{
Packit ae235b
  G_LOCK (g_thread);
Packit ae235b
  g_thread_all_threads = g_slist_prepend (g_thread_all_threads, thread);
Packit ae235b
  G_UNLOCK (g_thread);
Packit ae235b
Packit ae235b
  g_private_set (&enumerable_thread_private, thread);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
g_deprecated_thread_proxy (gpointer data)
Packit ae235b
{
Packit ae235b
  GRealThread *real = data;
Packit ae235b
Packit ae235b
  g_enumerable_thread_add (real);
Packit ae235b
Packit ae235b
  return g_thread_proxy (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_create:
Packit ae235b
 * @func: a function to execute in the new thread
Packit ae235b
 * @data: an argument to supply to the new thread
Packit ae235b
 * @joinable: should this thread be joinable?
Packit ae235b
 * @error: return location for error, or %NULL
Packit ae235b
 *
Packit ae235b
 * This function creates a new thread.
Packit ae235b
 *
Packit ae235b
 * The new thread executes the function @func with the argument @data.
Packit ae235b
 * If the thread was created successfully, it is returned.
Packit ae235b
 *
Packit ae235b
 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
Packit ae235b
 * The error is set, if and only if the function returns %NULL.
Packit ae235b
 *
Packit ae235b
 * This function returns a reference to the created thread only if
Packit ae235b
 * @joinable is %TRUE.  In that case, you must free this reference by
Packit ae235b
 * calling g_thread_unref() or g_thread_join().  If @joinable is %FALSE
Packit ae235b
 * then you should probably not touch the return value.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GThread on success
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Use g_thread_new() instead
Packit ae235b
 */
Packit ae235b
GThread *
Packit ae235b
g_thread_create (GThreadFunc   func,
Packit ae235b
                 gpointer      data,
Packit ae235b
                 gboolean      joinable,
Packit ae235b
                 GError      **error)
Packit ae235b
{
Packit ae235b
  return g_thread_create_full (func, data, 0, joinable, 0, 0, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_create_full:
Packit ae235b
 * @func: a function to execute in the new thread.
Packit ae235b
 * @data: an argument to supply to the new thread.
Packit ae235b
 * @stack_size: a stack size for the new thread.
Packit ae235b
 * @joinable: should this thread be joinable?
Packit ae235b
 * @bound: ignored
Packit ae235b
 * @priority: ignored
Packit ae235b
 * @error: return location for error.
Packit ae235b
 *
Packit ae235b
 * This function creates a new thread.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GThread on success.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: The @bound and @priority arguments are now ignored.
Packit ae235b
 * Use g_thread_new().
Packit ae235b
 */
Packit ae235b
GThread *
Packit ae235b
g_thread_create_full (GThreadFunc       func,
Packit ae235b
                      gpointer          data,
Packit ae235b
                      gulong            stack_size,
Packit ae235b
                      gboolean          joinable,
Packit ae235b
                      gboolean          bound,
Packit ae235b
                      GThreadPriority   priority,
Packit ae235b
                      GError          **error)
Packit ae235b
{
Packit ae235b
  GThread *thread;
Packit ae235b
Packit ae235b
  thread = g_thread_new_internal (NULL, g_deprecated_thread_proxy,
Packit ae235b
                                  func, data, stack_size, error);
Packit ae235b
Packit ae235b
  if (thread && !joinable)
Packit ae235b
    {
Packit ae235b
      thread->joinable = FALSE;
Packit ae235b
      g_thread_unref (thread);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return thread;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GOnce {{{1 ------------------------------------------------------------- */
Packit ae235b
gboolean
Packit ae235b
g_once_init_enter_impl (volatile gsize *location)
Packit ae235b
{
Packit ae235b
  return (g_once_init_enter) (location);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GStaticMutex {{{1 ------------------------------------------------------ */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GStaticMutex:
Packit ae235b
 *
Packit ae235b
 * A #GStaticMutex works like a #GMutex.
Packit ae235b
 *
Packit ae235b
 * Prior to GLib 2.32, GStaticMutex had the significant advantage
Packit ae235b
 * that it doesn't need to be created at run-time, but can be defined
Packit ae235b
 * at compile-time. Since 2.32, #GMutex can be statically allocated
Packit ae235b
 * as well, and GStaticMutex has been deprecated.
Packit ae235b
 *
Packit ae235b
 * Here is a version of our give_me_next_number() example using
Packit ae235b
 * a GStaticMutex:
Packit ae235b
 * |[
Packit ae235b
 *   int
Packit ae235b
 *   give_me_next_number (void)
Packit ae235b
 *   {
Packit ae235b
 *     static int current_number = 0;
Packit ae235b
 *     int ret_val;
Packit ae235b
 *     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
Packit ae235b
 *
Packit ae235b
 *     g_static_mutex_lock (&mutex);
Packit ae235b
 *     ret_val = current_number = calc_next_number (current_number);
Packit ae235b
 *     g_static_mutex_unlock (&mutex);
Packit ae235b
 *
Packit ae235b
 *     return ret_val;
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Sometimes you would like to dynamically create a mutex. If you don't
Packit ae235b
 * want to require prior calling to g_thread_init(), because your code
Packit ae235b
 * should also be usable in non-threaded programs, you are not able to
Packit ae235b
 * use g_mutex_new() and thus #GMutex, as that requires a prior call to
Packit ae235b
 * g_thread_init(). In theses cases you can also use a #GStaticMutex.
Packit ae235b
 * It must be initialized with g_static_mutex_init() before using it
Packit ae235b
 * and freed with with g_static_mutex_free() when not needed anymore to
Packit ae235b
 * free up any allocated resources.
Packit ae235b
 *
Packit ae235b
 * Even though #GStaticMutex is not opaque, it should only be used with
Packit ae235b
 * the following functions, as it is defined differently on different
Packit ae235b
 * platforms.
Packit ae235b
 *
Packit ae235b
 * All of the g_static_mutex_* functions apart from
Packit ae235b
 * g_static_mutex_get_mutex() can also be used even if g_thread_init()
Packit ae235b
 * has not yet been called. Then they do nothing, apart from
Packit ae235b
 * g_static_mutex_trylock() which does nothing but returning %TRUE.
Packit ae235b
 *
Packit ae235b
 * All of the g_static_mutex_* functions are actually macros. Apart from
Packit ae235b
 * taking their addresses, you can however use them as if they were
Packit ae235b
 * functions.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_STATIC_MUTEX_INIT:
Packit ae235b
 *
Packit ae235b
 * A #GStaticMutex must be initialized with this macro, before it can
Packit ae235b
 * be used. This macro can used be to initialize a variable, but it
Packit ae235b
 * cannot be assigned to a variable. In that case you have to use
Packit ae235b
 * g_static_mutex_init().
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 * GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
Packit ae235b
 * ]|
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_mutex_init:
Packit ae235b
 * @mutex: a #GStaticMutex to be initialized.
Packit ae235b
 *
Packit ae235b
 * Initializes @mutex.
Packit ae235b
 * Alternatively you can initialize it with #G_STATIC_MUTEX_INIT.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_mutex_init()
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_mutex_init (GStaticMutex *mutex)
Packit ae235b
{
Packit ae235b
  static const GStaticMutex init_mutex = G_STATIC_MUTEX_INIT;
Packit ae235b
Packit ae235b
  g_return_if_fail (mutex);
Packit ae235b
Packit ae235b
  *mutex = init_mutex;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* IMPLEMENTATION NOTE:
Packit ae235b
 *
Packit ae235b
 * On some platforms a GStaticMutex is actually a normal GMutex stored
Packit ae235b
 * inside of a structure instead of being allocated dynamically.  We can
Packit ae235b
 * only do this for platforms on which we know, in advance, how to
Packit ae235b
 * allocate (size) and initialise (value) that memory.
Packit ae235b
 *
Packit ae235b
 * On other platforms, a GStaticMutex is nothing more than a pointer to
Packit ae235b
 * a GMutex.  In that case, the first access we make to the static mutex
Packit ae235b
 * must first allocate the normal GMutex and store it into the pointer.
Packit ae235b
 *
Packit ae235b
 * configure.ac writes macros into glibconfig.h to determine if
Packit ae235b
 * g_static_mutex_get_mutex() accesses the structure in memory directly
Packit ae235b
 * (on platforms where we are able to do that) or if it ends up here,
Packit ae235b
 * where we may have to allocate the GMutex before returning it.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_mutex_get_mutex:
Packit ae235b
 * @mutex: a #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * For some operations (like g_cond_wait()) you must have a #GMutex
Packit ae235b
 * instead of a #GStaticMutex. This function will return the
Packit ae235b
 * corresponding #GMutex for @mutex.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GMutex corresponding to @mutex.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Just use a #GMutex
Packit ae235b
 */
Packit ae235b
GMutex *
Packit ae235b
g_static_mutex_get_mutex_impl (GStaticMutex* mutex)
Packit ae235b
{
Packit ae235b
  GMutex *result;
Packit ae235b
Packit ae235b
  if (!g_thread_supported ())
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  result = g_atomic_pointer_get (&mutex->mutex);
Packit ae235b
Packit ae235b
  if (!result)
Packit ae235b
    {
Packit ae235b
      G_LOCK (g_static_mutex);
Packit ae235b
Packit ae235b
      result = mutex->mutex;
Packit ae235b
      if (!result)
Packit ae235b
        {
Packit ae235b
          result = g_mutex_new ();
Packit ae235b
          g_atomic_pointer_set (&mutex->mutex, result);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      G_UNLOCK (g_static_mutex);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* IMPLEMENTATION NOTE:
Packit ae235b
 *
Packit ae235b
 * g_static_mutex_lock(), g_static_mutex_trylock() and
Packit ae235b
 * g_static_mutex_unlock() are all preprocessor macros that wrap the
Packit ae235b
 * corresponding g_mutex_*() function around a call to
Packit ae235b
 * g_static_mutex_get_mutex().
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_mutex_lock:
Packit ae235b
 * @mutex: a #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * Works like g_mutex_lock(), but for a #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_mutex_lock()
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_mutex_trylock:
Packit ae235b
 * @mutex: a #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * Works like g_mutex_trylock(), but for a #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE, if the #GStaticMutex could be locked.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_mutex_trylock()
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_mutex_unlock:
Packit ae235b
 * @mutex: a #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * Works like g_mutex_unlock(), but for a #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_mutex_unlock()
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_mutex_free:
Packit ae235b
 * @mutex: a #GStaticMutex to be freed.
Packit ae235b
 *
Packit ae235b
 * Releases all resources allocated to @mutex.
Packit ae235b
 *
Packit ae235b
 * You don't have to call this functions for a #GStaticMutex with an
Packit ae235b
 * unbounded lifetime, i.e. objects declared 'static', but if you have
Packit ae235b
 * a #GStaticMutex as a member of a structure and the structure is
Packit ae235b
 * freed, you should also free the #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * Calling g_static_mutex_free() on a locked mutex may result in
Packit ae235b
 * undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_mutex_clear()
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_mutex_free (GStaticMutex* mutex)
Packit ae235b
{
Packit ae235b
  GMutex **runtime_mutex;
Packit ae235b
Packit ae235b
  g_return_if_fail (mutex);
Packit ae235b
Packit ae235b
  /* The runtime_mutex is the first (or only) member of GStaticMutex,
Packit ae235b
   * see both versions (of glibconfig.h) in configure.ac. Note, that
Packit ae235b
   * this variable is NULL, if g_thread_init() hasn't been called or
Packit ae235b
   * if we're using the default thread implementation and it provides
Packit ae235b
   * static mutexes. */
Packit ae235b
  runtime_mutex = ((GMutex**)mutex);
Packit ae235b
Packit ae235b
  if (*runtime_mutex)
Packit ae235b
    g_mutex_free (*runtime_mutex);
Packit ae235b
Packit ae235b
  *runtime_mutex = NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GStaticRecMutex */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GStaticRecMutex:
Packit ae235b
 *
Packit ae235b
 * A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
Packit ae235b
 * multiple times by one thread. If you enter it n times, you have to
Packit ae235b
 * unlock it n times again to let other threads lock it. An exception
Packit ae235b
 * is the function g_static_rec_mutex_unlock_full(): that allows you to
Packit ae235b
 * unlock a #GStaticRecMutex completely returning the depth, (i.e. the
Packit ae235b
 * number of times this mutex was locked). The depth can later be used
Packit ae235b
 * to restore the state of the #GStaticRecMutex by calling
Packit ae235b
 * g_static_rec_mutex_lock_full(). In GLib 2.32, #GStaticRecMutex has
Packit ae235b
 * been deprecated in favor of #GRecMutex.
Packit ae235b
 *
Packit ae235b
 * Even though #GStaticRecMutex is not opaque, it should only be used
Packit ae235b
 * with the following functions.
Packit ae235b
 *
Packit ae235b
 * All of the g_static_rec_mutex_* functions can be used even if
Packit ae235b
 * g_thread_init() has not been called. Then they do nothing, apart
Packit ae235b
 * from g_static_rec_mutex_trylock(), which does nothing but returning
Packit ae235b
 * %TRUE.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_STATIC_REC_MUTEX_INIT:
Packit ae235b
 *
Packit ae235b
 * A #GStaticRecMutex must be initialized with this macro before it can
Packit ae235b
 * be used. This macro can used be to initialize a variable, but it
Packit ae235b
 * cannot be assigned to a variable. In that case you have to use
Packit ae235b
 * g_static_rec_mutex_init().
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *   GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rec_mutex_init:
Packit ae235b
 * @mutex: a #GStaticRecMutex to be initialized.
Packit ae235b
 *
Packit ae235b
 * A #GStaticRecMutex must be initialized with this function before it
Packit ae235b
 * can be used. Alternatively you can initialize it with
Packit ae235b
 * #G_STATIC_REC_MUTEX_INIT.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_rec_mutex_init()
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rec_mutex_init (GStaticRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
Packit ae235b
Packit ae235b
  g_return_if_fail (mutex);
Packit ae235b
Packit ae235b
  *mutex = init_mutex;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GRecMutex *
Packit ae235b
g_static_rec_mutex_get_rec_mutex_impl (GStaticRecMutex* mutex)
Packit ae235b
{
Packit ae235b
  GRecMutex *result;
Packit ae235b
Packit ae235b
  if (!g_thread_supported ())
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  result = g_atomic_pointer_get (&mutex->mutex.mutex);
Packit ae235b
Packit ae235b
  if (!result)
Packit ae235b
    {
Packit ae235b
      G_LOCK (g_static_mutex);
Packit ae235b
Packit ae235b
      result = (GRecMutex *) mutex->mutex.mutex;
Packit ae235b
      if (!result)
Packit ae235b
        {
Packit ae235b
          result = g_slice_new (GRecMutex);
Packit ae235b
          g_rec_mutex_init (result);
Packit ae235b
          g_atomic_pointer_set (&mutex->mutex.mutex, result);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      G_UNLOCK (g_static_mutex);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rec_mutex_lock:
Packit ae235b
 * @mutex: a #GStaticRecMutex to lock.
Packit ae235b
 *
Packit ae235b
 * Locks @mutex. If @mutex is already locked by another thread, the
Packit ae235b
 * current thread will block until @mutex is unlocked by the other
Packit ae235b
 * thread. If @mutex is already locked by the calling thread, this
Packit ae235b
 * functions increases the depth of @mutex and returns immediately.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_rec_mutex_lock()
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rec_mutex_lock (GStaticRecMutex* mutex)
Packit ae235b
{
Packit ae235b
  GRecMutex *rm;
Packit ae235b
  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
Packit ae235b
  g_rec_mutex_lock (rm);
Packit ae235b
  mutex->depth++;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rec_mutex_trylock:
Packit ae235b
 * @mutex: a #GStaticRecMutex to lock.
Packit ae235b
 *
Packit ae235b
 * Tries to lock @mutex. If @mutex is already locked by another thread,
Packit ae235b
 * it immediately returns %FALSE. Otherwise it locks @mutex and returns
Packit ae235b
 * %TRUE. If @mutex is already locked by the calling thread, this
Packit ae235b
 * functions increases the depth of @mutex and immediately returns
Packit ae235b
 * %TRUE.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE, if @mutex could be locked.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_rec_mutex_trylock()
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
Packit ae235b
{
Packit ae235b
  GRecMutex *rm;
Packit ae235b
  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
Packit ae235b
Packit ae235b
  if (g_rec_mutex_trylock (rm))
Packit ae235b
    {
Packit ae235b
      mutex->depth++;
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rec_mutex_unlock:
Packit ae235b
 * @mutex: a #GStaticRecMutex to unlock.
Packit ae235b
 *
Packit ae235b
 * Unlocks @mutex. Another thread will be allowed to lock @mutex only
Packit ae235b
 * when it has been unlocked as many times as it had been locked
Packit ae235b
 * before. If @mutex is completely unlocked and another thread is
Packit ae235b
 * blocked in a g_static_rec_mutex_lock() call for @mutex, it will be
Packit ae235b
 * woken and can lock @mutex itself.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_rec_mutex_unlock()
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
Packit ae235b
{
Packit ae235b
  GRecMutex *rm;
Packit ae235b
  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
Packit ae235b
  mutex->depth--;
Packit ae235b
  g_rec_mutex_unlock (rm);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rec_mutex_lock_full:
Packit ae235b
 * @mutex: a #GStaticRecMutex to lock.
Packit ae235b
 * @depth: number of times this mutex has to be unlocked to be
Packit ae235b
 *         completely unlocked.
Packit ae235b
 *
Packit ae235b
 * Works like calling g_static_rec_mutex_lock() for @mutex @depth times.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_rec_mutex_lock()
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rec_mutex_lock_full (GStaticRecMutex *mutex,
Packit ae235b
                              guint            depth)
Packit ae235b
{
Packit ae235b
  GRecMutex *rm;
Packit ae235b
Packit ae235b
  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
Packit ae235b
  while (depth--)
Packit ae235b
    {
Packit ae235b
      g_rec_mutex_lock (rm);
Packit ae235b
      mutex->depth++;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rec_mutex_unlock_full:
Packit ae235b
 * @mutex: a #GStaticRecMutex to completely unlock.
Packit ae235b
 *
Packit ae235b
 * Completely unlocks @mutex. If another thread is blocked in a
Packit ae235b
 * g_static_rec_mutex_lock() call for @mutex, it will be woken and can
Packit ae235b
 * lock @mutex itself. This function returns the number of times that
Packit ae235b
 * @mutex has been locked by the current thread. To restore the state
Packit ae235b
 * before the call to g_static_rec_mutex_unlock_full() you can call
Packit ae235b
 * g_static_rec_mutex_lock_full() with the depth returned by this
Packit ae235b
 * function.
Packit ae235b
 *
Packit ae235b
 * Returns: number of times @mutex has been locked by the current
Packit ae235b
 *          thread.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_rec_mutex_unlock()
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  GRecMutex *rm;
Packit ae235b
  gint depth;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  rm = g_static_rec_mutex_get_rec_mutex_impl (mutex);
Packit ae235b
Packit ae235b
  /* all access to mutex->depth done while still holding the lock */
Packit ae235b
  depth = mutex->depth;
Packit ae235b
  i = mutex->depth;
Packit ae235b
  mutex->depth = 0;
Packit ae235b
Packit ae235b
  while (i--)
Packit ae235b
    g_rec_mutex_unlock (rm);
Packit ae235b
Packit ae235b
  return depth;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rec_mutex_free:
Packit ae235b
 * @mutex: a #GStaticRecMutex to be freed.
Packit ae235b
 *
Packit ae235b
 * Releases all resources allocated to a #GStaticRecMutex.
Packit ae235b
 *
Packit ae235b
 * You don't have to call this functions for a #GStaticRecMutex with an
Packit ae235b
 * unbounded lifetime, i.e. objects declared 'static', but if you have
Packit ae235b
 * a #GStaticRecMutex as a member of a structure and the structure is
Packit ae235b
 * freed, you should also free the #GStaticRecMutex.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_rec_mutex_clear()
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rec_mutex_free (GStaticRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (mutex);
Packit ae235b
Packit ae235b
  if (mutex->mutex.mutex)
Packit ae235b
    {
Packit ae235b
      GRecMutex *rm = (GRecMutex *) mutex->mutex.mutex;
Packit ae235b
Packit ae235b
      g_rec_mutex_clear (rm);
Packit ae235b
      g_slice_free (GRecMutex, rm);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GStaticRWLock {{{1 ----------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GStaticRWLock:
Packit ae235b
 *
Packit ae235b
 * The #GStaticRWLock struct represents a read-write lock. A read-write
Packit ae235b
 * lock can be used for protecting data that some portions of code only
Packit ae235b
 * read from, while others also write. In such situations it is
Packit ae235b
 * desirable that several readers can read at once, whereas of course
Packit ae235b
 * only one writer may write at a time.
Packit ae235b
 *
Packit ae235b
 * Take a look at the following example:
Packit ae235b
 * |[
Packit ae235b
 *   GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
Packit ae235b
 *   GPtrArray *array;
Packit ae235b
 *
Packit ae235b
 *   gpointer
Packit ae235b
 *   my_array_get (guint index)
Packit ae235b
 *   {
Packit ae235b
 *     gpointer retval = NULL;
Packit ae235b
 *
Packit ae235b
 *     if (!array)
Packit ae235b
 *       return NULL;
Packit ae235b
 *
Packit ae235b
 *     g_static_rw_lock_reader_lock (&rwlock);
Packit ae235b
 *     if (index < array->len)
Packit ae235b
 *       retval = g_ptr_array_index (array, index);
Packit ae235b
 *     g_static_rw_lock_reader_unlock (&rwlock);
Packit ae235b
 *
Packit ae235b
 *     return retval;
Packit ae235b
 *   }
Packit ae235b
 *
Packit ae235b
 *   void
Packit ae235b
 *   my_array_set (guint index, gpointer data)
Packit ae235b
 *   {
Packit ae235b
 *     g_static_rw_lock_writer_lock (&rwlock);
Packit ae235b
 *
Packit ae235b
 *     if (!array)
Packit ae235b
 *       array = g_ptr_array_new ();
Packit ae235b
 *
Packit ae235b
 *     if (index >= array->len)
Packit ae235b
 *       g_ptr_array_set_size (array, index + 1);
Packit ae235b
 *     g_ptr_array_index (array, index) = data;
Packit ae235b
 *
Packit ae235b
 *     g_static_rw_lock_writer_unlock (&rwlock);
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * This example shows an array which can be accessed by many readers
Packit ae235b
 * (the my_array_get() function) simultaneously, whereas the writers
Packit ae235b
 * (the my_array_set() function) will only be allowed once at a time
Packit ae235b
 * and only if no readers currently access the array. This is because
Packit ae235b
 * of the potentially dangerous resizing of the array. Using these
Packit ae235b
 * functions is fully multi-thread safe now.
Packit ae235b
 *
Packit ae235b
 * Most of the time, writers should have precedence over readers. That
Packit ae235b
 * means, for this implementation, that as soon as a writer wants to
Packit ae235b
 * lock the data, no other reader is allowed to lock the data, whereas,
Packit ae235b
 * of course, the readers that already have locked the data are allowed
Packit ae235b
 * to finish their operation. As soon as the last reader unlocks the
Packit ae235b
 * data, the writer will lock it.
Packit ae235b
 *
Packit ae235b
 * Even though #GStaticRWLock is not opaque, it should only be used
Packit ae235b
 * with the following functions.
Packit ae235b
 *
Packit ae235b
 * All of the g_static_rw_lock_* functions can be used even if
Packit ae235b
 * g_thread_init() has not been called. Then they do nothing, apart
Packit ae235b
 * from g_static_rw_lock_*_trylock, which does nothing but returning %TRUE.
Packit ae235b
 *
Packit ae235b
 * A read-write lock has a higher overhead than a mutex. For example, both
Packit ae235b
 * g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock() have
Packit ae235b
 * to lock and unlock a #GStaticMutex, so it takes at least twice the time
Packit ae235b
 * to lock and unlock a #GStaticRWLock that it does to lock and unlock a
Packit ae235b
 * #GStaticMutex. So only data structures that are accessed by multiple
Packit ae235b
 * readers, and which keep the lock for a considerable time justify a
Packit ae235b
 * #GStaticRWLock. The above example most probably would fare better with a
Packit ae235b
 * #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use a #GRWLock instead
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_STATIC_RW_LOCK_INIT:
Packit ae235b
 *
Packit ae235b
 * A #GStaticRWLock must be initialized with this macro before it can
Packit ae235b
 * be used. This macro can used be to initialize a variable, but it
Packit ae235b
 * cannot be assigned to a variable. In that case you have to use
Packit ae235b
 * g_static_rw_lock_init().
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *   GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rw_lock_init:
Packit ae235b
 * @lock: a #GStaticRWLock to be initialized.
Packit ae235b
 *
Packit ae235b
 * A #GStaticRWLock must be initialized with this function before it
Packit ae235b
 * can be used. Alternatively you can initialize it with
Packit ae235b
 * #G_STATIC_RW_LOCK_INIT.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_rw_lock_init() instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rw_lock_init (GStaticRWLock* lock)
Packit ae235b
{
Packit ae235b
  static const GStaticRWLock init_lock = G_STATIC_RW_LOCK_INIT;
Packit ae235b
Packit ae235b
  g_return_if_fail (lock);
Packit ae235b
Packit ae235b
  *lock = init_lock;
Packit ae235b
}
Packit ae235b
Packit ae235b
inline static void
Packit ae235b
g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
Packit ae235b
{
Packit ae235b
  if (!*cond)
Packit ae235b
      *cond = g_cond_new ();
Packit ae235b
  g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
Packit ae235b
}
Packit ae235b
Packit ae235b
inline static void
Packit ae235b
g_static_rw_lock_signal (GStaticRWLock* lock)
Packit ae235b
{
Packit ae235b
  if (lock->want_to_write && lock->write_cond)
Packit ae235b
    g_cond_signal (lock->write_cond);
Packit ae235b
  else if (lock->want_to_read && lock->read_cond)
Packit ae235b
    g_cond_broadcast (lock->read_cond);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rw_lock_reader_lock:
Packit ae235b
 * @lock: a #GStaticRWLock to lock for reading.
Packit ae235b
 *
Packit ae235b
 * Locks @lock for reading. There may be unlimited concurrent locks for
Packit ae235b
 * reading of a #GStaticRWLock at the same time.  If @lock is already
Packit ae235b
 * locked for writing by another thread or if another thread is already
Packit ae235b
 * waiting to lock @lock for writing, this function will block until
Packit ae235b
 * @lock is unlocked by the other writing thread and no other writing
Packit ae235b
 * threads want to lock @lock. This lock has to be unlocked by
Packit ae235b
 * g_static_rw_lock_reader_unlock().
Packit ae235b
 *
Packit ae235b
 * #GStaticRWLock is not recursive. It might seem to be possible to
Packit ae235b
 * recursively lock for reading, but that can result in a deadlock, due
Packit ae235b
 * to writer preference.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use g_rw_lock_reader_lock() instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rw_lock_reader_lock (GStaticRWLock* lock)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (lock);
Packit ae235b
Packit ae235b
  if (!g_threads_got_initialized)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_static_mutex_lock (&lock->mutex);
Packit ae235b
  lock->want_to_read++;
Packit ae235b
  while (lock->have_writer || lock->want_to_write)
Packit ae235b
    g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
Packit ae235b
  lock->want_to_read--;
Packit ae235b
  lock->read_counter++;
Packit ae235b
  g_static_mutex_unlock (&lock->mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rw_lock_reader_trylock:
Packit ae235b
 * @lock: a #GStaticRWLock to lock for reading
Packit ae235b
 *
Packit ae235b
 * Tries to lock @lock for reading. If @lock is already locked for
Packit ae235b
 * writing by another thread or if another thread is already waiting to
Packit ae235b
 * lock @lock for writing, immediately returns %FALSE. Otherwise locks
Packit ae235b
 * @lock for reading and returns %TRUE. This lock has to be unlocked by
Packit ae235b
 * g_static_rw_lock_reader_unlock().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE, if @lock could be locked for reading
Packit ae235b
 *
Packit ae235b
 * Deprectated: 2.32: Use g_rw_lock_reader_trylock() instead
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
Packit ae235b
{
Packit ae235b
  gboolean ret_val = FALSE;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (lock, FALSE);
Packit ae235b
Packit ae235b
  if (!g_threads_got_initialized)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  g_static_mutex_lock (&lock->mutex);
Packit ae235b
  if (!lock->have_writer && !lock->want_to_write)
Packit ae235b
    {
Packit ae235b
      lock->read_counter++;
Packit ae235b
      ret_val = TRUE;
Packit ae235b
    }
Packit ae235b
  g_static_mutex_unlock (&lock->mutex);
Packit ae235b
  return ret_val;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rw_lock_reader_unlock:
Packit ae235b
 * @lock: a #GStaticRWLock to unlock after reading
Packit ae235b
 *
Packit ae235b
 * Unlocks @lock. If a thread waits to lock @lock for writing and all
Packit ae235b
 * locks for reading have been unlocked, the waiting thread is woken up
Packit ae235b
 * and can lock @lock for writing.
Packit ae235b
 *
Packit ae235b
 * Deprectated: 2.32: Use g_rw_lock_reader_unlock() instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (lock);
Packit ae235b
Packit ae235b
  if (!g_threads_got_initialized)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_static_mutex_lock (&lock->mutex);
Packit ae235b
  lock->read_counter--;
Packit ae235b
  if (lock->read_counter == 0)
Packit ae235b
    g_static_rw_lock_signal (lock);
Packit ae235b
  g_static_mutex_unlock (&lock->mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rw_lock_writer_lock:
Packit ae235b
 * @lock: a #GStaticRWLock to lock for writing
Packit ae235b
 *
Packit ae235b
 * Locks @lock for writing. If @lock is already locked for writing or
Packit ae235b
 * reading by other threads, this function will block until @lock is
Packit ae235b
 * completely unlocked and then lock @lock for writing. While this
Packit ae235b
 * functions waits to lock @lock, no other thread can lock @lock for
Packit ae235b
 * reading. When @lock is locked for writing, no other thread can lock
Packit ae235b
 * @lock (neither for reading nor writing). This lock has to be
Packit ae235b
 * unlocked by g_static_rw_lock_writer_unlock().
Packit ae235b
 *
Packit ae235b
 * Deprectated: 2.32: Use g_rw_lock_writer_lock() instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rw_lock_writer_lock (GStaticRWLock* lock)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (lock);
Packit ae235b
Packit ae235b
  if (!g_threads_got_initialized)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_static_mutex_lock (&lock->mutex);
Packit ae235b
  lock->want_to_write++;
Packit ae235b
  while (lock->have_writer || lock->read_counter)
Packit ae235b
    g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
Packit ae235b
  lock->want_to_write--;
Packit ae235b
  lock->have_writer = TRUE;
Packit ae235b
  g_static_mutex_unlock (&lock->mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rw_lock_writer_trylock:
Packit ae235b
 * @lock: a #GStaticRWLock to lock for writing
Packit ae235b
 *
Packit ae235b
 * Tries to lock @lock for writing. If @lock is already locked (for
Packit ae235b
 * either reading or writing) by another thread, it immediately returns
Packit ae235b
 * %FALSE. Otherwise it locks @lock for writing and returns %TRUE. This
Packit ae235b
 * lock has to be unlocked by g_static_rw_lock_writer_unlock().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE, if @lock could be locked for writing
Packit ae235b
 *
Packit ae235b
 * Deprectated: 2.32: Use g_rw_lock_writer_trylock() instead
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
Packit ae235b
{
Packit ae235b
  gboolean ret_val = FALSE;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (lock, FALSE);
Packit ae235b
Packit ae235b
  if (!g_threads_got_initialized)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  g_static_mutex_lock (&lock->mutex);
Packit ae235b
  if (!lock->have_writer && !lock->read_counter)
Packit ae235b
    {
Packit ae235b
      lock->have_writer = TRUE;
Packit ae235b
      ret_val = TRUE;
Packit ae235b
    }
Packit ae235b
  g_static_mutex_unlock (&lock->mutex);
Packit ae235b
  return ret_val;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rw_lock_writer_unlock:
Packit ae235b
 * @lock: a #GStaticRWLock to unlock after writing.
Packit ae235b
 *
Packit ae235b
 * Unlocks @lock. If a thread is waiting to lock @lock for writing and
Packit ae235b
 * all locks for reading have been unlocked, the waiting thread is
Packit ae235b
 * woken up and can lock @lock for writing. If no thread is waiting to
Packit ae235b
 * lock @lock for writing, and some thread or threads are waiting to
Packit ae235b
 * lock @lock for reading, the waiting threads are woken up and can
Packit ae235b
 * lock @lock for reading.
Packit ae235b
 *
Packit ae235b
 * Deprectated: 2.32: Use g_rw_lock_writer_unlock() instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (lock);
Packit ae235b
Packit ae235b
  if (!g_threads_got_initialized)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_static_mutex_lock (&lock->mutex);
Packit ae235b
  lock->have_writer = FALSE;
Packit ae235b
  g_static_rw_lock_signal (lock);
Packit ae235b
  g_static_mutex_unlock (&lock->mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_rw_lock_free:
Packit ae235b
 * @lock: a #GStaticRWLock to be freed.
Packit ae235b
 *
Packit ae235b
 * Releases all resources allocated to @lock.
Packit ae235b
 *
Packit ae235b
 * You don't have to call this functions for a #GStaticRWLock with an
Packit ae235b
 * unbounded lifetime, i.e. objects declared 'static', but if you have
Packit ae235b
 * a #GStaticRWLock as a member of a structure, and the structure is
Packit ae235b
 * freed, you should also free the #GStaticRWLock.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: Use a #GRWLock instead
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_rw_lock_free (GStaticRWLock* lock)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (lock);
Packit ae235b
Packit ae235b
  if (lock->read_cond)
Packit ae235b
    {
Packit ae235b
      g_cond_free (lock->read_cond);
Packit ae235b
      lock->read_cond = NULL;
Packit ae235b
    }
Packit ae235b
  if (lock->write_cond)
Packit ae235b
    {
Packit ae235b
      g_cond_free (lock->write_cond);
Packit ae235b
      lock->write_cond = NULL;
Packit ae235b
    }
Packit ae235b
  g_static_mutex_free (&lock->mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GPrivate {{{1 ------------------------------------------------------ */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_private_new:
Packit ae235b
 * @notify: a #GDestroyNotify
Packit ae235b
 *
Packit ae235b
 * Creates a new #GPrivate.
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: dynamic allocation of #GPrivate is a bad idea.  Use
Packit ae235b
 *                  static storage and G_PRIVATE_INIT() instead.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated #GPrivate (which can never be destroyed)
Packit ae235b
 */
Packit ae235b
GPrivate *
Packit ae235b
g_private_new (GDestroyNotify notify)
Packit ae235b
{
Packit ae235b
  GPrivate tmp = G_PRIVATE_INIT (notify);
Packit ae235b
  GPrivate *key;
Packit ae235b
Packit ae235b
  key = g_slice_new (GPrivate);
Packit ae235b
  *key = tmp;
Packit ae235b
Packit ae235b
  return key;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GStaticPrivate */
Packit ae235b
Packit ae235b
typedef struct _GStaticPrivateNode GStaticPrivateNode;
Packit ae235b
struct _GStaticPrivateNode
Packit ae235b
{
Packit ae235b
  gpointer        data;
Packit ae235b
  GDestroyNotify  destroy;
Packit ae235b
  GStaticPrivate *owner;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_static_private_cleanup (gpointer data)
Packit ae235b
{
Packit ae235b
  GArray *array = data;
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  for (i = 0; i < array->len; i++ )
Packit ae235b
    {
Packit ae235b
      GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
Packit ae235b
      if (node->destroy)
Packit ae235b
        node->destroy (node->data);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_array_free (array, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
GPrivate static_private_private = G_PRIVATE_INIT (g_static_private_cleanup);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GStaticPrivate:
Packit ae235b
 *
Packit ae235b
 * A #GStaticPrivate works almost like a #GPrivate, but it has one
Packit ae235b
 * significant advantage. It doesn't need to be created at run-time
Packit ae235b
 * like a #GPrivate, but can be defined at compile-time. This is
Packit ae235b
 * similar to the difference between #GMutex and #GStaticMutex.
Packit ae235b
 *
Packit ae235b
 * Now look at our give_me_next_number() example with #GStaticPrivate:
Packit ae235b
 * |[
Packit ae235b
 *   int
Packit ae235b
 *   give_me_next_number ()
Packit ae235b
 *   {
Packit ae235b
 *     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
Packit ae235b
 *     int *current_number = g_static_private_get (&current_number_key);
Packit ae235b
 *
Packit ae235b
 *     if (!current_number)
Packit ae235b
 *       {
Packit ae235b
 *         current_number = g_new (int, 1);
Packit ae235b
 *         *current_number = 0;
Packit ae235b
 *         g_static_private_set (&current_number_key, current_number, g_free);
Packit ae235b
 *       }
Packit ae235b
 *
Packit ae235b
 *     *current_number = calc_next_number (*current_number);
Packit ae235b
 *
Packit ae235b
 *     return *current_number;
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_STATIC_PRIVATE_INIT:
Packit ae235b
 *
Packit ae235b
 * Every #GStaticPrivate must be initialized with this macro, before it
Packit ae235b
 * can be used.
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *   GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_private_init:
Packit ae235b
 * @private_key: a #GStaticPrivate to be initialized
Packit ae235b
 *
Packit ae235b
 * Initializes @private_key. Alternatively you can initialize it with
Packit ae235b
 * #G_STATIC_PRIVATE_INIT.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_private_init (GStaticPrivate *private_key)
Packit ae235b
{
Packit ae235b
  private_key->index = 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_private_get:
Packit ae235b
 * @private_key: a #GStaticPrivate
Packit ae235b
 *
Packit ae235b
 * Works like g_private_get() only for a #GStaticPrivate.
Packit ae235b
 *
Packit ae235b
 * This function works even if g_thread_init() has not yet been called.
Packit ae235b
 *
Packit ae235b
 * Returns: the corresponding pointer
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_static_private_get (GStaticPrivate *private_key)
Packit ae235b
{
Packit ae235b
  GArray *array;
Packit ae235b
  gpointer ret = NULL;
Packit ae235b
Packit ae235b
  array = g_private_get (&static_private_private);
Packit ae235b
Packit ae235b
  if (array && private_key->index != 0 && private_key->index <= array->len)
Packit ae235b
    {
Packit ae235b
      GStaticPrivateNode *node;
Packit ae235b
Packit ae235b
      node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
Packit ae235b
Packit ae235b
      /* Deal with the possibility that the GStaticPrivate which used
Packit ae235b
       * to have this index got freed and the index got allocated to
Packit ae235b
       * a new one. In this case, the data in the node is stale, so
Packit ae235b
       * free it and return NULL.
Packit ae235b
       */
Packit ae235b
      if (G_UNLIKELY (node->owner != private_key))
Packit ae235b
        {
Packit ae235b
          if (node->destroy)
Packit ae235b
            node->destroy (node->data);
Packit ae235b
          node->destroy = NULL;
Packit ae235b
          node->data = NULL;
Packit ae235b
          node->owner = NULL;
Packit ae235b
        }
Packit ae235b
      ret = node->data;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_private_set:
Packit ae235b
 * @private_key: a #GStaticPrivate
Packit ae235b
 * @data: the new pointer
Packit ae235b
 * @notify: a function to be called with the pointer whenever the
Packit ae235b
 *     current thread ends or sets this pointer again
Packit ae235b
 *
Packit ae235b
 * Sets the pointer keyed to @private_key for the current thread and
Packit ae235b
 * the function @notify to be called with that pointer (%NULL or
Packit ae235b
 * non-%NULL), whenever the pointer is set again or whenever the
Packit ae235b
 * current thread ends.
Packit ae235b
 *
Packit ae235b
 * This function works even if g_thread_init() has not yet been called.
Packit ae235b
 * If g_thread_init() is called later, the @data keyed to @private_key
Packit ae235b
 * will be inherited only by the main thread, i.e. the one that called
Packit ae235b
 * g_thread_init().
Packit ae235b
 *
Packit ae235b
 * @notify is used quite differently from @destructor in g_private_new().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_private_set (GStaticPrivate *private_key,
Packit ae235b
                      gpointer        data,
Packit ae235b
                      GDestroyNotify  notify)
Packit ae235b
{
Packit ae235b
  GArray *array;
Packit ae235b
  static guint next_index = 0;
Packit ae235b
  GStaticPrivateNode *node;
Packit ae235b
Packit ae235b
  if (!private_key->index)
Packit ae235b
    {
Packit ae235b
      G_LOCK (g_thread);
Packit ae235b
Packit ae235b
      if (!private_key->index)
Packit ae235b
        {
Packit ae235b
          if (g_thread_free_indices)
Packit ae235b
            {
Packit ae235b
              private_key->index = GPOINTER_TO_UINT (g_thread_free_indices->data);
Packit ae235b
              g_thread_free_indices = g_slist_delete_link (g_thread_free_indices,
Packit ae235b
                                                           g_thread_free_indices);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            private_key->index = ++next_index;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      G_UNLOCK (g_thread);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  array = g_private_get (&static_private_private);
Packit ae235b
  if (!array)
Packit ae235b
    {
Packit ae235b
      array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
Packit ae235b
      g_private_set (&static_private_private, array);
Packit ae235b
    }
Packit ae235b
  if (private_key->index > array->len)
Packit ae235b
    g_array_set_size (array, private_key->index);
Packit ae235b
Packit ae235b
  node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
Packit ae235b
Packit ae235b
  if (node->destroy)
Packit ae235b
    node->destroy (node->data);
Packit ae235b
Packit ae235b
  node->data = data;
Packit ae235b
  node->destroy = notify;
Packit ae235b
  node->owner = private_key;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_static_private_free:
Packit ae235b
 * @private_key: a #GStaticPrivate to be freed
Packit ae235b
 *
Packit ae235b
 * Releases all resources allocated to @private_key.
Packit ae235b
 *
Packit ae235b
 * You don't have to call this functions for a #GStaticPrivate with an
Packit ae235b
 * unbounded lifetime, i.e. objects declared 'static', but if you have
Packit ae235b
 * a #GStaticPrivate as a member of a structure and the structure is
Packit ae235b
 * freed, you should also free the #GStaticPrivate.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_static_private_free (GStaticPrivate *private_key)
Packit ae235b
{
Packit ae235b
  guint idx = private_key->index;
Packit ae235b
Packit ae235b
  if (!idx)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  private_key->index = 0;
Packit ae235b
Packit ae235b
  /* Freeing the per-thread data is deferred to either the
Packit ae235b
   * thread end or the next g_static_private_get() call for
Packit ae235b
   * the same index.
Packit ae235b
   */
Packit ae235b
  G_LOCK (g_thread);
Packit ae235b
  g_thread_free_indices = g_slist_prepend (g_thread_free_indices,
Packit ae235b
                                           GUINT_TO_POINTER (idx));
Packit ae235b
  G_UNLOCK (g_thread);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GMutex {{{1 ------------------------------------------------------ */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mutex_new:
Packit ae235b
 *
Packit ae235b
 * Allocates and initializes a new #GMutex.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated #GMutex. Use g_mutex_free() to free
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: GMutex can now be statically allocated, or embedded
Packit ae235b
 * in structures and initialised with g_mutex_init().
Packit ae235b
 */
Packit ae235b
GMutex *
Packit ae235b
g_mutex_new (void)
Packit ae235b
{
Packit ae235b
  GMutex *mutex;
Packit ae235b
Packit ae235b
  mutex = g_slice_new (GMutex);
Packit ae235b
  g_mutex_init (mutex);
Packit ae235b
Packit ae235b
  return mutex;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mutex_free:
Packit ae235b
 * @mutex: a #GMutex
Packit ae235b
 *
Packit ae235b
 * Destroys a @mutex that has been created with g_mutex_new().
Packit ae235b
 *
Packit ae235b
 * Calling g_mutex_free() on a locked mutex may result
Packit ae235b
 * in undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: GMutex can now be statically allocated, or embedded
Packit ae235b
 * in structures and initialised with g_mutex_init().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_mutex_free (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  g_mutex_clear (mutex);
Packit ae235b
  g_slice_free (GMutex, mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GCond {{{1 ------------------------------------------------------ */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cond_new:
Packit ae235b
 *
Packit ae235b
 * Allocates and initializes a new #GCond.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated #GCond. Free with g_cond_free()
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: GCond can now be statically allocated, or embedded
Packit ae235b
 * in structures and initialised with g_cond_init().
Packit ae235b
 */
Packit ae235b
GCond *
Packit ae235b
g_cond_new (void)
Packit ae235b
{
Packit ae235b
  GCond *cond;
Packit ae235b
Packit ae235b
  cond = g_slice_new (GCond);
Packit ae235b
  g_cond_init (cond);
Packit ae235b
Packit ae235b
  return cond;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cond_free:
Packit ae235b
 * @cond: a #GCond
Packit ae235b
 *
Packit ae235b
 * Destroys a #GCond that has been created with g_cond_new().
Packit ae235b
 *
Packit ae235b
 * Calling g_cond_free() for a #GCond on which threads are
Packit ae235b
 * blocking leads to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.32: GCond can now be statically allocated, or embedded
Packit ae235b
 * in structures and initialised with g_cond_init().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cond_free (GCond *cond)
Packit ae235b
{
Packit ae235b
  g_cond_clear (cond);
Packit ae235b
  g_slice_free (GCond, cond);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cond_timed_wait:
Packit ae235b
 * @cond: a #GCond
Packit ae235b
 * @mutex: a #GMutex that is currently locked
Packit ae235b
 * @abs_time: a #GTimeVal, determining the final time
Packit ae235b
 *
Packit ae235b
 * Waits until this thread is woken up on @cond, but not longer than
Packit ae235b
 * until the time specified by @abs_time. The @mutex is unlocked before
Packit ae235b
 * falling asleep and locked again before resuming.
Packit ae235b
 *
Packit ae235b
 * If @abs_time is %NULL, g_cond_timed_wait() acts like g_cond_wait().
Packit ae235b
 *
Packit ae235b
 * This function can be used even if g_thread_init() has not yet been
Packit ae235b
 * called, and, in that case, will immediately return %TRUE.
Packit ae235b
 *
Packit ae235b
 * To easily calculate @abs_time a combination of g_get_current_time()
Packit ae235b
 * and g_time_val_add() can be used.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @cond was signalled, or %FALSE on timeout
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.32: Use g_cond_wait_until() instead.
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_cond_timed_wait (GCond    *cond,
Packit ae235b
                   GMutex   *mutex,
Packit ae235b
                   GTimeVal *abs_time)
Packit ae235b
{
Packit ae235b
  gint64 end_time;
Packit ae235b
Packit ae235b
  if (abs_time == NULL)
Packit ae235b
    {
Packit ae235b
      g_cond_wait (cond, mutex);
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  end_time = abs_time->tv_sec;
Packit ae235b
  end_time *= 1000000;
Packit ae235b
  end_time += abs_time->tv_usec;
Packit ae235b
Packit ae235b
  /* would be nice if we had clock_rtoffset, but that didn't seem to
Packit ae235b
   * make it into the kernel yet...
Packit ae235b
   */
Packit ae235b
  end_time += g_get_monotonic_time () - g_get_real_time ();
Packit ae235b
Packit ae235b
  return g_cond_wait_until (cond, mutex, end_time);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 Epilogue */
Packit ae235b
/* vim: set foldmethod=marker: */