Blame glib/gthread-posix.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: posix thread system implementation
Packit ae235b
 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
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
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* The GMutex, GCond and GPrivate implementations in this file are some
Packit ae235b
 * of the lowest-level code in GLib.  All other parts of GLib (messages,
Packit ae235b
 * memory, slices, etc) assume that they can freely use these facilities
Packit ae235b
 * without risking recursion.
Packit ae235b
 *
Packit ae235b
 * As such, these functions are NOT permitted to call any other part of
Packit ae235b
 * GLib.
Packit ae235b
 *
Packit ae235b
 * The thread manipulation functions (create, exit, join, etc.) have
Packit ae235b
 * more freedom -- they can do as they please.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gthread.h"
Packit ae235b
Packit ae235b
#include "gthreadprivate.h"
Packit ae235b
#include "gslice.h"
Packit ae235b
#include "gmessages.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gmain.h"
Packit ae235b
#include "gutils.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <pthread.h>
Packit ae235b
Packit ae235b
#include <sys/time.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
Packit ae235b
#ifdef HAVE_SCHED_H
Packit ae235b
#include <sched.h>
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* clang defines __ATOMIC_SEQ_CST but doesn't support the GCC extension */
Packit ae235b
#if defined(HAVE_FUTEX) && defined(__ATOMIC_SEQ_CST) && !defined(__clang__)
Packit ae235b
#define USE_NATIVE_MUTEX
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_thread_abort (gint         status,
Packit ae235b
                const gchar *function)
Packit ae235b
{
Packit ae235b
  fprintf (stderr, "GLib (gthread-posix.c): Unexpected error from C library during '%s': %s.  Aborting.\n",
Packit ae235b
           function, strerror (status));
Packit ae235b
  g_abort ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GMutex */
Packit ae235b
Packit ae235b
#if !defined(USE_NATIVE_MUTEX)
Packit ae235b
Packit ae235b
static pthread_mutex_t *
Packit ae235b
g_mutex_impl_new (void)
Packit ae235b
{
Packit ae235b
  pthread_mutexattr_t *pattr = NULL;
Packit ae235b
  pthread_mutex_t *mutex;
Packit ae235b
  gint status;
Packit ae235b
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
Packit ae235b
  pthread_mutexattr_t attr;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  mutex = malloc (sizeof (pthread_mutex_t));
Packit ae235b
  if G_UNLIKELY (mutex == NULL)
Packit ae235b
    g_thread_abort (errno, "malloc");
Packit ae235b
Packit ae235b
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
Packit ae235b
  pthread_mutexattr_init (&attr);
Packit ae235b
  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
Packit ae235b
  pattr = &att;;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_mutex_init (mutex, pattr)) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_mutex_init");
Packit ae235b
Packit ae235b
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
Packit ae235b
  pthread_mutexattr_destroy (&attr);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return mutex;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_mutex_impl_free (pthread_mutex_t *mutex)
Packit ae235b
{
Packit ae235b
  pthread_mutex_destroy (mutex);
Packit ae235b
  free (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline pthread_mutex_t *
Packit ae235b
g_mutex_get_impl (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  pthread_mutex_t *impl = g_atomic_pointer_get (&mutex->p);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (impl == NULL)
Packit ae235b
    {
Packit ae235b
      impl = g_mutex_impl_new ();
Packit ae235b
      if (!g_atomic_pointer_compare_and_exchange (&mutex->p, NULL, impl))
Packit ae235b
        g_mutex_impl_free (impl);
Packit ae235b
      impl = mutex->p;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return impl;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mutex_init:
Packit ae235b
 * @mutex: an uninitialized #GMutex
Packit ae235b
 *
Packit ae235b
 * Initializes a #GMutex so that it can be used.
Packit ae235b
 *
Packit ae235b
 * This function is useful to initialize a mutex that has been
Packit ae235b
 * allocated on the stack, or as part of a larger structure.
Packit ae235b
 * It is not necessary to initialize a mutex that has been
Packit ae235b
 * statically allocated.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   typedef struct {
Packit ae235b
 *     GMutex m;
Packit ae235b
 *     ...
Packit ae235b
 *   } Blob;
Packit ae235b
 *
Packit ae235b
 * Blob *b;
Packit ae235b
 *
Packit ae235b
 * b = g_new (Blob, 1);
Packit ae235b
 * g_mutex_init (&b->m);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * To undo the effect of g_mutex_init() when a mutex is no longer
Packit ae235b
 * needed, use g_mutex_clear().
Packit ae235b
 *
Packit ae235b
 * Calling g_mutex_init() on an already initialized #GMutex leads
Packit ae235b
 * to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_mutex_init (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  mutex->p = g_mutex_impl_new ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mutex_clear:
Packit ae235b
 * @mutex: an initialized #GMutex
Packit ae235b
 *
Packit ae235b
 * Frees the resources allocated to a mutex with g_mutex_init().
Packit ae235b
 *
Packit ae235b
 * This function should not be used with a #GMutex that has been
Packit ae235b
 * statically allocated.
Packit ae235b
 *
Packit ae235b
 * Calling g_mutex_clear() on a locked mutex leads to undefined
Packit ae235b
 * behaviour.
Packit ae235b
 *
Packit ae235b
 * Sine: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_mutex_clear (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  g_mutex_impl_free (mutex->p);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mutex_lock:
Packit ae235b
 * @mutex: a #GMutex
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.
Packit ae235b
 *
Packit ae235b
 * #GMutex is neither guaranteed to be recursive nor to be
Packit ae235b
 * non-recursive.  As such, calling g_mutex_lock() on a #GMutex that has
Packit ae235b
 * already been locked by the same thread results in undefined behaviour
Packit ae235b
 * (including but not limited to deadlocks).
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_mutex_lock (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_mutex_lock (g_mutex_get_impl (mutex))) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_mutex_lock");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mutex_unlock:
Packit ae235b
 * @mutex: a #GMutex
Packit ae235b
 *
Packit ae235b
 * Unlocks @mutex. If another thread is blocked in a g_mutex_lock()
Packit ae235b
 * call for @mutex, it will become unblocked and can lock @mutex itself.
Packit ae235b
 *
Packit ae235b
 * Calling g_mutex_unlock() on a mutex that is not locked by the
Packit ae235b
 * current thread leads to undefined behaviour.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_mutex_unlock (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_mutex_unlock (g_mutex_get_impl (mutex))) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_mutex_unlock");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mutex_trylock:
Packit ae235b
 * @mutex: a #GMutex
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.
Packit ae235b
 *
Packit ae235b
 * #GMutex is neither guaranteed to be recursive nor to be
Packit ae235b
 * non-recursive.  As such, calling g_mutex_lock() on a #GMutex that has
Packit ae235b
 * already been locked by the same thread results in undefined behaviour
Packit ae235b
 * (including but not limited to deadlocks or arbitrary return values).
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @mutex could be locked
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_mutex_trylock (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  if G_LIKELY ((status = pthread_mutex_trylock (g_mutex_get_impl (mutex))) == 0)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (status != EBUSY)
Packit ae235b
    g_thread_abort (status, "pthread_mutex_trylock");
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif /* !defined(USE_NATIVE_MUTEX) */
Packit ae235b
Packit ae235b
/* {{{1 GRecMutex */
Packit ae235b
Packit ae235b
static pthread_mutex_t *
Packit ae235b
g_rec_mutex_impl_new (void)
Packit ae235b
{
Packit ae235b
  pthread_mutexattr_t attr;
Packit ae235b
  pthread_mutex_t *mutex;
Packit ae235b
Packit ae235b
  mutex = malloc (sizeof (pthread_mutex_t));
Packit ae235b
  if G_UNLIKELY (mutex == NULL)
Packit ae235b
    g_thread_abort (errno, "malloc");
Packit ae235b
Packit ae235b
  pthread_mutexattr_init (&attr);
Packit ae235b
  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
Packit ae235b
  pthread_mutex_init (mutex, &attr);
Packit ae235b
  pthread_mutexattr_destroy (&attr);
Packit ae235b
Packit ae235b
  return mutex;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_rec_mutex_impl_free (pthread_mutex_t *mutex)
Packit ae235b
{
Packit ae235b
  pthread_mutex_destroy (mutex);
Packit ae235b
  free (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline pthread_mutex_t *
Packit ae235b
g_rec_mutex_get_impl (GRecMutex *rec_mutex)
Packit ae235b
{
Packit ae235b
  pthread_mutex_t *impl = g_atomic_pointer_get (&rec_mutex->p);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (impl == NULL)
Packit ae235b
    {
Packit ae235b
      impl = g_rec_mutex_impl_new ();
Packit ae235b
      if (!g_atomic_pointer_compare_and_exchange (&rec_mutex->p, NULL, impl))
Packit ae235b
        g_rec_mutex_impl_free (impl);
Packit ae235b
      impl = rec_mutex->p;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return impl;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rec_mutex_init:
Packit ae235b
 * @rec_mutex: an uninitialized #GRecMutex
Packit ae235b
 *
Packit ae235b
 * Initializes a #GRecMutex so that it can be used.
Packit ae235b
 *
Packit ae235b
 * This function is useful to initialize a recursive mutex
Packit ae235b
 * that has been allocated on the stack, or as part of a larger
Packit ae235b
 * structure.
Packit ae235b
 *
Packit ae235b
 * It is not necessary to initialise a recursive mutex that has been
Packit ae235b
 * statically allocated.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   typedef struct {
Packit ae235b
 *     GRecMutex m;
Packit ae235b
 *     ...
Packit ae235b
 *   } Blob;
Packit ae235b
 *
Packit ae235b
 * Blob *b;
Packit ae235b
 *
Packit ae235b
 * b = g_new (Blob, 1);
Packit ae235b
 * g_rec_mutex_init (&b->m);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Calling g_rec_mutex_init() on an already initialized #GRecMutex
Packit ae235b
 * leads to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * To undo the effect of g_rec_mutex_init() when a recursive mutex
Packit ae235b
 * is no longer needed, use g_rec_mutex_clear().
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rec_mutex_init (GRecMutex *rec_mutex)
Packit ae235b
{
Packit ae235b
  rec_mutex->p = g_rec_mutex_impl_new ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rec_mutex_clear:
Packit ae235b
 * @rec_mutex: an initialized #GRecMutex
Packit ae235b
 *
Packit ae235b
 * Frees the resources allocated to a recursive mutex with
Packit ae235b
 * g_rec_mutex_init().
Packit ae235b
 *
Packit ae235b
 * This function should not be used with a #GRecMutex that has been
Packit ae235b
 * statically allocated.
Packit ae235b
 *
Packit ae235b
 * Calling g_rec_mutex_clear() on a locked recursive mutex leads
Packit ae235b
 * to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Sine: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rec_mutex_clear (GRecMutex *rec_mutex)
Packit ae235b
{
Packit ae235b
  g_rec_mutex_impl_free (rec_mutex->p);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rec_mutex_lock:
Packit ae235b
 * @rec_mutex: a #GRecMutex
Packit ae235b
 *
Packit ae235b
 * Locks @rec_mutex. If @rec_mutex is already locked by another
Packit ae235b
 * thread, the current thread will block until @rec_mutex is
Packit ae235b
 * unlocked by the other thread. If @rec_mutex is already locked
Packit ae235b
 * by the current thread, the 'lock count' of @rec_mutex is increased.
Packit ae235b
 * The mutex will only become available again when it is unlocked
Packit ae235b
 * as many times as it has been locked.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rec_mutex_lock (GRecMutex *mutex)
Packit ae235b
{
Packit ae235b
  pthread_mutex_lock (g_rec_mutex_get_impl (mutex));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rec_mutex_unlock:
Packit ae235b
 * @rec_mutex: a #GRecMutex
Packit ae235b
 *
Packit ae235b
 * Unlocks @rec_mutex. If another thread is blocked in a
Packit ae235b
 * g_rec_mutex_lock() call for @rec_mutex, it will become unblocked
Packit ae235b
 * and can lock @rec_mutex itself.
Packit ae235b
 *
Packit ae235b
 * Calling g_rec_mutex_unlock() on a recursive mutex that is not
Packit ae235b
 * locked by the current thread leads to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rec_mutex_unlock (GRecMutex *rec_mutex)
Packit ae235b
{
Packit ae235b
  pthread_mutex_unlock (rec_mutex->p);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rec_mutex_trylock:
Packit ae235b
 * @rec_mutex: a #GRecMutex
Packit ae235b
 *
Packit ae235b
 * Tries to lock @rec_mutex. If @rec_mutex is already locked
Packit ae235b
 * by another thread, it immediately returns %FALSE. Otherwise
Packit ae235b
 * it locks @rec_mutex and returns %TRUE.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @rec_mutex could be locked
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_rec_mutex_trylock (GRecMutex *rec_mutex)
Packit ae235b
{
Packit ae235b
  if (pthread_mutex_trylock (g_rec_mutex_get_impl (rec_mutex)) != 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GRWLock */
Packit ae235b
Packit ae235b
static pthread_rwlock_t *
Packit ae235b
g_rw_lock_impl_new (void)
Packit ae235b
{
Packit ae235b
  pthread_rwlock_t *rwlock;
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  rwlock = malloc (sizeof (pthread_rwlock_t));
Packit ae235b
  if G_UNLIKELY (rwlock == NULL)
Packit ae235b
    g_thread_abort (errno, "malloc");
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_rwlock_init (rwlock, NULL)) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_rwlock_init");
Packit ae235b
Packit ae235b
  return rwlock;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_rw_lock_impl_free (pthread_rwlock_t *rwlock)
Packit ae235b
{
Packit ae235b
  pthread_rwlock_destroy (rwlock);
Packit ae235b
  free (rwlock);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline pthread_rwlock_t *
Packit ae235b
g_rw_lock_get_impl (GRWLock *lock)
Packit ae235b
{
Packit ae235b
  pthread_rwlock_t *impl = g_atomic_pointer_get (&lock->p);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (impl == NULL)
Packit ae235b
    {
Packit ae235b
      impl = g_rw_lock_impl_new ();
Packit ae235b
      if (!g_atomic_pointer_compare_and_exchange (&lock->p, NULL, impl))
Packit ae235b
        g_rw_lock_impl_free (impl);
Packit ae235b
      impl = lock->p;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return impl;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rw_lock_init:
Packit ae235b
 * @rw_lock: an uninitialized #GRWLock
Packit ae235b
 *
Packit ae235b
 * Initializes a #GRWLock so that it can be used.
Packit ae235b
 *
Packit ae235b
 * This function is useful to initialize a lock that has been
Packit ae235b
 * allocated on the stack, or as part of a larger structure.  It is not
Packit ae235b
 * necessary to initialise a reader-writer lock that has been statically
Packit ae235b
 * allocated.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   typedef struct {
Packit ae235b
 *     GRWLock l;
Packit ae235b
 *     ...
Packit ae235b
 *   } Blob;
Packit ae235b
 *
Packit ae235b
 * Blob *b;
Packit ae235b
 *
Packit ae235b
 * b = g_new (Blob, 1);
Packit ae235b
 * g_rw_lock_init (&b->l);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * To undo the effect of g_rw_lock_init() when a lock is no longer
Packit ae235b
 * needed, use g_rw_lock_clear().
Packit ae235b
 *
Packit ae235b
 * Calling g_rw_lock_init() on an already initialized #GRWLock leads
Packit ae235b
 * to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rw_lock_init (GRWLock *rw_lock)
Packit ae235b
{
Packit ae235b
  rw_lock->p = g_rw_lock_impl_new ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rw_lock_clear:
Packit ae235b
 * @rw_lock: an initialized #GRWLock
Packit ae235b
 *
Packit ae235b
 * Frees the resources allocated to a lock with g_rw_lock_init().
Packit ae235b
 *
Packit ae235b
 * This function should not be used with a #GRWLock that has been
Packit ae235b
 * statically allocated.
Packit ae235b
 *
Packit ae235b
 * Calling g_rw_lock_clear() when any thread holds the lock
Packit ae235b
 * leads to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Sine: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rw_lock_clear (GRWLock *rw_lock)
Packit ae235b
{
Packit ae235b
  g_rw_lock_impl_free (rw_lock->p);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rw_lock_writer_lock:
Packit ae235b
 * @rw_lock: a #GRWLock
Packit ae235b
 *
Packit ae235b
 * Obtain a write lock on @rw_lock. If any thread already holds
Packit ae235b
 * a read or write lock on @rw_lock, the current thread will block
Packit ae235b
 * until all other threads have dropped their locks on @rw_lock.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rw_lock_writer_lock (GRWLock *rw_lock)
Packit ae235b
{
Packit ae235b
  int retval = pthread_rwlock_wrlock (g_rw_lock_get_impl (rw_lock));
Packit ae235b
Packit ae235b
  if (retval != 0)
Packit ae235b
    g_critical ("Failed to get RW lock %p: %s", rw_lock, g_strerror (retval));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rw_lock_writer_trylock:
Packit ae235b
 * @rw_lock: a #GRWLock
Packit ae235b
 *
Packit ae235b
 * Tries to obtain a write lock on @rw_lock. If any other thread holds
Packit ae235b
 * a read or write lock on @rw_lock, it immediately returns %FALSE.
Packit ae235b
 * Otherwise it locks @rw_lock and returns %TRUE.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @rw_lock could be locked
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_rw_lock_writer_trylock (GRWLock *rw_lock)
Packit ae235b
{
Packit ae235b
  if (pthread_rwlock_trywrlock (g_rw_lock_get_impl (rw_lock)) != 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rw_lock_writer_unlock:
Packit ae235b
 * @rw_lock: a #GRWLock
Packit ae235b
 *
Packit ae235b
 * Release a write lock on @rw_lock.
Packit ae235b
 *
Packit ae235b
 * Calling g_rw_lock_writer_unlock() on a lock that is not held
Packit ae235b
 * by the current thread leads to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rw_lock_writer_unlock (GRWLock *rw_lock)
Packit ae235b
{
Packit ae235b
  pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rw_lock_reader_lock:
Packit ae235b
 * @rw_lock: a #GRWLock
Packit ae235b
 *
Packit ae235b
 * Obtain a read lock on @rw_lock. If another thread currently holds
Packit ae235b
 * the write lock on @rw_lock or blocks waiting for it, the current
Packit ae235b
 * thread will block. Read locks can be taken recursively.
Packit ae235b
 *
Packit ae235b
 * It is implementation-defined how many threads are allowed to
Packit ae235b
 * hold read locks on the same lock simultaneously. If the limit is hit,
Packit ae235b
 * or if a deadlock is detected, a critical warning will be emitted.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rw_lock_reader_lock (GRWLock *rw_lock)
Packit ae235b
{
Packit ae235b
  int retval = pthread_rwlock_rdlock (g_rw_lock_get_impl (rw_lock));
Packit ae235b
Packit ae235b
  if (retval != 0)
Packit ae235b
    g_critical ("Failed to get RW lock %p: %s", rw_lock, g_strerror (retval));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rw_lock_reader_trylock:
Packit ae235b
 * @rw_lock: a #GRWLock
Packit ae235b
 *
Packit ae235b
 * Tries to obtain a read lock on @rw_lock and returns %TRUE if
Packit ae235b
 * the read lock was successfully obtained. Otherwise it
Packit ae235b
 * returns %FALSE.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @rw_lock could be locked
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_rw_lock_reader_trylock (GRWLock *rw_lock)
Packit ae235b
{
Packit ae235b
  if (pthread_rwlock_tryrdlock (g_rw_lock_get_impl (rw_lock)) != 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rw_lock_reader_unlock:
Packit ae235b
 * @rw_lock: a #GRWLock
Packit ae235b
 *
Packit ae235b
 * Release a read lock on @rw_lock.
Packit ae235b
 *
Packit ae235b
 * Calling g_rw_lock_reader_unlock() on a lock that is not held
Packit ae235b
 * by the current thread leads to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_rw_lock_reader_unlock (GRWLock *rw_lock)
Packit ae235b
{
Packit ae235b
  pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GCond */
Packit ae235b
Packit ae235b
#if !defined(USE_NATIVE_MUTEX)
Packit ae235b
Packit ae235b
static pthread_cond_t *
Packit ae235b
g_cond_impl_new (void)
Packit ae235b
{
Packit ae235b
  pthread_condattr_t attr;
Packit ae235b
  pthread_cond_t *cond;
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  pthread_condattr_init (&attr);
Packit ae235b
Packit ae235b
#ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
Packit ae235b
#elif defined (HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined (CLOCK_MONOTONIC)
Packit ae235b
  if G_UNLIKELY ((status = pthread_condattr_setclock (&attr, CLOCK_MONOTONIC)) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_condattr_setclock");
Packit ae235b
#else
Packit ae235b
#error Cannot support GCond on your platform.
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  cond = malloc (sizeof (pthread_cond_t));
Packit ae235b
  if G_UNLIKELY (cond == NULL)
Packit ae235b
    g_thread_abort (errno, "malloc");
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_cond_init (cond, &attr)) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_cond_init");
Packit ae235b
Packit ae235b
  pthread_condattr_destroy (&attr);
Packit ae235b
Packit ae235b
  return cond;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_cond_impl_free (pthread_cond_t *cond)
Packit ae235b
{
Packit ae235b
  pthread_cond_destroy (cond);
Packit ae235b
  free (cond);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline pthread_cond_t *
Packit ae235b
g_cond_get_impl (GCond *cond)
Packit ae235b
{
Packit ae235b
  pthread_cond_t *impl = g_atomic_pointer_get (&cond->p);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (impl == NULL)
Packit ae235b
    {
Packit ae235b
      impl = g_cond_impl_new ();
Packit ae235b
      if (!g_atomic_pointer_compare_and_exchange (&cond->p, NULL, impl))
Packit ae235b
        g_cond_impl_free (impl);
Packit ae235b
      impl = cond->p;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return impl;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cond_init:
Packit ae235b
 * @cond: an uninitialized #GCond
Packit ae235b
 *
Packit ae235b
 * Initialises a #GCond so that it can be used.
Packit ae235b
 *
Packit ae235b
 * This function is useful to initialise a #GCond that has been
Packit ae235b
 * allocated as part of a larger structure.  It is not necessary to
Packit ae235b
 * initialise a #GCond that has been statically allocated.
Packit ae235b
 *
Packit ae235b
 * To undo the effect of g_cond_init() when a #GCond is no longer
Packit ae235b
 * needed, use g_cond_clear().
Packit ae235b
 *
Packit ae235b
 * Calling g_cond_init() on an already-initialised #GCond leads
Packit ae235b
 * to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cond_init (GCond *cond)
Packit ae235b
{
Packit ae235b
  cond->p = g_cond_impl_new ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cond_clear:
Packit ae235b
 * @cond: an initialised #GCond
Packit ae235b
 *
Packit ae235b
 * Frees the resources allocated to a #GCond with g_cond_init().
Packit ae235b
 *
Packit ae235b
 * This function should not be used with a #GCond that has been
Packit ae235b
 * statically allocated.
Packit ae235b
 *
Packit ae235b
 * Calling g_cond_clear() for a #GCond on which threads are
Packit ae235b
 * blocking leads to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cond_clear (GCond *cond)
Packit ae235b
{
Packit ae235b
  g_cond_impl_free (cond->p);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cond_wait:
Packit ae235b
 * @cond: a #GCond
Packit ae235b
 * @mutex: a #GMutex that is currently locked
Packit ae235b
 *
Packit ae235b
 * Atomically releases @mutex and waits until @cond is signalled.
Packit ae235b
 * When this function returns, @mutex is locked again and owned by the
Packit ae235b
 * calling thread.
Packit ae235b
 *
Packit ae235b
 * When using condition variables, it is possible that a spurious wakeup
Packit ae235b
 * may occur (ie: g_cond_wait() returns even though g_cond_signal() was
Packit ae235b
 * not called).  It's also possible that a stolen wakeup may occur.
Packit ae235b
 * This is when g_cond_signal() is called, but another thread acquires
Packit ae235b
 * @mutex before this thread and modifies the state of the program in
Packit ae235b
 * such a way that when g_cond_wait() is able to return, the expected
Packit ae235b
 * condition is no longer met.
Packit ae235b
 *
Packit ae235b
 * For this reason, g_cond_wait() must always be used in a loop.  See
Packit ae235b
 * the documentation for #GCond for a complete example.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_cond_wait (GCond  *cond,
Packit ae235b
             GMutex *mutex)
Packit ae235b
{
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_cond_wait (g_cond_get_impl (cond), g_mutex_get_impl (mutex))) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_cond_wait");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cond_signal:
Packit ae235b
 * @cond: a #GCond
Packit ae235b
 *
Packit ae235b
 * If threads are waiting for @cond, at least one of them is unblocked.
Packit ae235b
 * If no threads are waiting for @cond, this function has no effect.
Packit ae235b
 * It is good practice to hold the same lock as the waiting thread
Packit ae235b
 * while calling this function, though not required.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cond_signal (GCond *cond)
Packit ae235b
{
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_cond_signal (g_cond_get_impl (cond))) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_cond_signal");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cond_broadcast:
Packit ae235b
 * @cond: a #GCond
Packit ae235b
 *
Packit ae235b
 * If threads are waiting for @cond, all of them are unblocked.
Packit ae235b
 * If no threads are waiting for @cond, this function has no effect.
Packit ae235b
 * It is good practice to lock the same mutex as the waiting threads
Packit ae235b
 * while calling this function, though not required.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_cond_broadcast (GCond *cond)
Packit ae235b
{
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_cond_broadcast (g_cond_get_impl (cond))) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_cond_broadcast");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_cond_wait_until:
Packit ae235b
 * @cond: a #GCond
Packit ae235b
 * @mutex: a #GMutex that is currently locked
Packit ae235b
 * @end_time: the monotonic time to wait until
Packit ae235b
 *
Packit ae235b
 * Waits until either @cond is signalled or @end_time has passed.
Packit ae235b
 *
Packit ae235b
 * As with g_cond_wait() it is possible that a spurious or stolen wakeup
Packit ae235b
 * could occur.  For that reason, waiting on a condition variable should
Packit ae235b
 * always be in a loop, based on an explicitly-checked predicate.
Packit ae235b
 *
Packit ae235b
 * %TRUE is returned if the condition variable was signalled (or in the
Packit ae235b
 * case of a spurious wakeup).  %FALSE is returned if @end_time has
Packit ae235b
 * passed.
Packit ae235b
 *
Packit ae235b
 * The following code shows how to correctly perform a timed wait on a
Packit ae235b
 * condition variable (extending the example presented in the
Packit ae235b
 * documentation for #GCond):
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 * gpointer
Packit ae235b
 * pop_data_timed (void)
Packit ae235b
 * {
Packit ae235b
 *   gint64 end_time;
Packit ae235b
 *   gpointer data;
Packit ae235b
 *
Packit ae235b
 *   g_mutex_lock (&data_mutex);
Packit ae235b
 *
Packit ae235b
 *   end_time = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND;
Packit ae235b
 *   while (!current_data)
Packit ae235b
 *     if (!g_cond_wait_until (&data_cond, &data_mutex, end_time))
Packit ae235b
 *       {
Packit ae235b
 *         // timeout has passed.
Packit ae235b
 *         g_mutex_unlock (&data_mutex);
Packit ae235b
 *         return NULL;
Packit ae235b
 *       }
Packit ae235b
 *
Packit ae235b
 *   // there is data for us
Packit ae235b
 *   data = current_data;
Packit ae235b
 *   current_data = NULL;
Packit ae235b
 *
Packit ae235b
 *   g_mutex_unlock (&data_mutex);
Packit ae235b
 *
Packit ae235b
 *   return data;
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Notice that the end time is calculated once, before entering the
Packit ae235b
 * loop and reused.  This is the motivation behind the use of absolute
Packit ae235b
 * time on this API -- if a relative time of 5 seconds were passed
Packit ae235b
 * directly to the call and a spurious wakeup occurred, the program would
Packit ae235b
 * have to start over waiting again (which would lead to a total wait
Packit ae235b
 * time of more than 5 seconds).
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on a signal, %FALSE on a timeout
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_cond_wait_until (GCond  *cond,
Packit ae235b
                   GMutex *mutex,
Packit ae235b
                   gint64  end_time)
Packit ae235b
{
Packit ae235b
  struct timespec ts;
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
#ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
Packit ae235b
  /* end_time is given relative to the monotonic clock as returned by
Packit ae235b
   * g_get_monotonic_time().
Packit ae235b
   *
Packit ae235b
   * Since this pthreads wants the relative time, convert it back again.
Packit ae235b
   */
Packit ae235b
  {
Packit ae235b
    gint64 now = g_get_monotonic_time ();
Packit ae235b
    gint64 relative;
Packit ae235b
Packit ae235b
    if (end_time <= now)
Packit ae235b
      return FALSE;
Packit ae235b
Packit ae235b
    relative = end_time - now;
Packit ae235b
Packit ae235b
    ts.tv_sec = relative / 1000000;
Packit ae235b
    ts.tv_nsec = (relative % 1000000) * 1000;
Packit ae235b
Packit ae235b
    if ((status = pthread_cond_timedwait_relative_np (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
Packit ae235b
      return TRUE;
Packit ae235b
  }
Packit ae235b
#elif defined (HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined (CLOCK_MONOTONIC)
Packit ae235b
  /* This is the exact check we used during init to set the clock to
Packit ae235b
   * monotonic, so if we're in this branch, timedwait() will already be
Packit ae235b
   * expecting a monotonic clock.
Packit ae235b
   */
Packit ae235b
  {
Packit ae235b
    ts.tv_sec = end_time / 1000000;
Packit ae235b
    ts.tv_nsec = (end_time % 1000000) * 1000;
Packit ae235b
Packit ae235b
    if ((status = pthread_cond_timedwait (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
Packit ae235b
      return TRUE;
Packit ae235b
  }
Packit ae235b
#else
Packit ae235b
#error Cannot support GCond on your platform.
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if G_UNLIKELY (status != ETIMEDOUT)
Packit ae235b
    g_thread_abort (status, "pthread_cond_timedwait");
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif /* defined(USE_NATIVE_MUTEX) */
Packit ae235b
Packit ae235b
/* {{{1 GPrivate */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GPrivate:
Packit ae235b
 *
Packit ae235b
 * The #GPrivate struct is an opaque data structure to represent a
Packit ae235b
 * thread-local data key. It is approximately equivalent to the
Packit ae235b
 * pthread_setspecific()/pthread_getspecific() APIs on POSIX and to
Packit ae235b
 * TlsSetValue()/TlsGetValue() on Windows.
Packit ae235b
 *
Packit ae235b
 * If you don't already know why you might want this functionality,
Packit ae235b
 * then you probably don't need it.
Packit ae235b
 *
Packit ae235b
 * #GPrivate is a very limited resource (as far as 128 per program,
Packit ae235b
 * shared between all libraries). It is also not possible to destroy a
Packit ae235b
 * #GPrivate after it has been used. As such, it is only ever acceptable
Packit ae235b
 * to use #GPrivate in static scope, and even then sparingly so.
Packit ae235b
 *
Packit ae235b
 * See G_PRIVATE_INIT() for a couple of examples.
Packit ae235b
 *
Packit ae235b
 * The #GPrivate structure should be considered opaque.  It should only
Packit ae235b
 * be accessed via the g_private_ functions.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_PRIVATE_INIT:
Packit ae235b
 * @notify: a #GDestroyNotify
Packit ae235b
 *
Packit ae235b
 * A macro to assist with the static initialisation of a #GPrivate.
Packit ae235b
 *
Packit ae235b
 * This macro is useful for the case that a #GDestroyNotify function
Packit ae235b
 * should be associated with the key.  This is needed when the key will be
Packit ae235b
 * used to point at memory that should be deallocated when the thread
Packit ae235b
 * exits.
Packit ae235b
 *
Packit ae235b
 * Additionally, the #GDestroyNotify will also be called on the previous
Packit ae235b
 * value stored in the key when g_private_replace() is used.
Packit ae235b
 *
Packit ae235b
 * If no #GDestroyNotify is needed, then use of this macro is not
Packit ae235b
 * required -- if the #GPrivate is declared in static scope then it will
Packit ae235b
 * be properly initialised by default (ie: to all zeros).  See the
Packit ae235b
 * examples below.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 * static GPrivate name_key = G_PRIVATE_INIT (g_free);
Packit ae235b
 *
Packit ae235b
 * // return value should not be freed
Packit ae235b
 * const gchar *
Packit ae235b
 * get_local_name (void)
Packit ae235b
 * {
Packit ae235b
 *   return g_private_get (&name_key);
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * void
Packit ae235b
 * set_local_name (const gchar *name)
Packit ae235b
 * {
Packit ae235b
 *   g_private_replace (&name_key, g_strdup (name));
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 *
Packit ae235b
 * static GPrivate count_key;   // no free function
Packit ae235b
 *
Packit ae235b
 * gint
Packit ae235b
 * get_local_count (void)
Packit ae235b
 * {
Packit ae235b
 *   return GPOINTER_TO_INT (g_private_get (&count_key));
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * void
Packit ae235b
 * set_local_count (gint count)
Packit ae235b
 * {
Packit ae235b
 *   g_private_set (&count_key, GINT_TO_POINTER (count));
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
Packit ae235b
static pthread_key_t *
Packit ae235b
g_private_impl_new (GDestroyNotify notify)
Packit ae235b
{
Packit ae235b
  pthread_key_t *key;
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  key = malloc (sizeof (pthread_key_t));
Packit ae235b
  if G_UNLIKELY (key == NULL)
Packit ae235b
    g_thread_abort (errno, "malloc");
Packit ae235b
  status = pthread_key_create (key, notify);
Packit ae235b
  if G_UNLIKELY (status != 0)
Packit ae235b
    g_thread_abort (status, "pthread_key_create");
Packit ae235b
Packit ae235b
  return key;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_private_impl_free (pthread_key_t *key)
Packit ae235b
{
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  status = pthread_key_delete (*key);
Packit ae235b
  if G_UNLIKELY (status != 0)
Packit ae235b
    g_thread_abort (status, "pthread_key_delete");
Packit ae235b
  free (key);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline pthread_key_t *
Packit ae235b
g_private_get_impl (GPrivate *key)
Packit ae235b
{
Packit ae235b
  pthread_key_t *impl = g_atomic_pointer_get (&key->p);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (impl == NULL)
Packit ae235b
    {
Packit ae235b
      impl = g_private_impl_new (key->notify);
Packit ae235b
      if (!g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
Packit ae235b
        {
Packit ae235b
          g_private_impl_free (impl);
Packit ae235b
          impl = key->p;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return impl;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_private_get:
Packit ae235b
 * @key: a #GPrivate
Packit ae235b
 *
Packit ae235b
 * Returns the current value of the thread local variable @key.
Packit ae235b
 *
Packit ae235b
 * If the value has not yet been set in this thread, %NULL is returned.
Packit ae235b
 * Values are never copied between threads (when a new thread is
Packit ae235b
 * created, for example).
Packit ae235b
 *
Packit ae235b
 * Returns: the thread-local value
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_private_get (GPrivate *key)
Packit ae235b
{
Packit ae235b
  /* quote POSIX: No errors are returned from pthread_getspecific(). */
Packit ae235b
  return pthread_getspecific (*g_private_get_impl (key));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_private_set:
Packit ae235b
 * @key: a #GPrivate
Packit ae235b
 * @value: the new value
Packit ae235b
 *
Packit ae235b
 * Sets the thread local variable @key to have the value @value in the
Packit ae235b
 * current thread.
Packit ae235b
 *
Packit ae235b
 * This function differs from g_private_replace() in the following way:
Packit ae235b
 * the #GDestroyNotify for @key is not called on the old value.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_private_set (GPrivate *key,
Packit ae235b
               gpointer  value)
Packit ae235b
{
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_setspecific (*g_private_get_impl (key), value)) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_setspecific");
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_private_replace:
Packit ae235b
 * @key: a #GPrivate
Packit ae235b
 * @value: the new value
Packit ae235b
 *
Packit ae235b
 * Sets the thread local variable @key to have the value @value in the
Packit ae235b
 * current thread.
Packit ae235b
 *
Packit ae235b
 * This function differs from g_private_set() in the following way: if
Packit ae235b
 * the previous value was non-%NULL then the #GDestroyNotify handler for
Packit ae235b
 * @key is run on it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_private_replace (GPrivate *key,
Packit ae235b
                   gpointer  value)
Packit ae235b
{
Packit ae235b
  pthread_key_t *impl = g_private_get_impl (key);
Packit ae235b
  gpointer old;
Packit ae235b
  gint status;
Packit ae235b
Packit ae235b
  old = pthread_getspecific (*impl);
Packit ae235b
  if (old && key->notify)
Packit ae235b
    key->notify (old);
Packit ae235b
Packit ae235b
  if G_UNLIKELY ((status = pthread_setspecific (*impl, value)) != 0)
Packit ae235b
    g_thread_abort (status, "pthread_setspecific");
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GThread */
Packit ae235b
Packit ae235b
#define posix_check_err(err, name) G_STMT_START{			\
Packit ae235b
  int error = (err); 							\
Packit ae235b
  if (error)	 		 		 			\
Packit ae235b
    g_error ("file %s: line %d (%s): error '%s' during '%s'",		\
Packit ae235b
           __FILE__, __LINE__, G_STRFUNC,				\
Packit ae235b
           g_strerror (error), name);					\
Packit ae235b
  }G_STMT_END
Packit ae235b
Packit ae235b
#define posix_check_cmd(cmd) posix_check_err (cmd, #cmd)
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GRealThread thread;
Packit ae235b
Packit ae235b
  pthread_t system_thread;
Packit ae235b
  gboolean  joined;
Packit ae235b
  GMutex    lock;
Packit ae235b
} GThreadPosix;
Packit ae235b
Packit ae235b
void
Packit ae235b
g_system_thread_free (GRealThread *thread)
Packit ae235b
{
Packit ae235b
  GThreadPosix *pt = (GThreadPosix *) thread;
Packit ae235b
Packit ae235b
  if (!pt->joined)
Packit ae235b
    pthread_detach (pt->system_thread);
Packit ae235b
Packit ae235b
  g_mutex_clear (&pt->lock);
Packit ae235b
Packit ae235b
  g_slice_free (GThreadPosix, pt);
Packit ae235b
}
Packit ae235b
Packit ae235b
GRealThread *
Packit ae235b
g_system_thread_new (GThreadFunc   thread_func,
Packit ae235b
                     gulong        stack_size,
Packit ae235b
                     GError      **error)
Packit ae235b
{
Packit ae235b
  GThreadPosix *thread;
Packit ae235b
  pthread_attr_t attr;
Packit ae235b
  gint ret;
Packit ae235b
Packit ae235b
  thread = g_slice_new0 (GThreadPosix);
Packit ae235b
Packit ae235b
  posix_check_cmd (pthread_attr_init (&attr));
Packit ae235b
Packit ae235b
#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
Packit ae235b
  if (stack_size)
Packit ae235b
    {
Packit ae235b
#ifdef _SC_THREAD_STACK_MIN
Packit ae235b
      long min_stack_size = sysconf (_SC_THREAD_STACK_MIN);
Packit ae235b
      if (min_stack_size >= 0)
Packit ae235b
        stack_size = MAX (min_stack_size, stack_size);
Packit ae235b
#endif /* _SC_THREAD_STACK_MIN */
Packit ae235b
      /* No error check here, because some systems can't do it and
Packit ae235b
       * we simply don't want threads to fail because of that. */
Packit ae235b
      pthread_attr_setstacksize (&attr, stack_size);
Packit ae235b
    }
Packit ae235b
#endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
Packit ae235b
Packit ae235b
  ret = pthread_create (&thread->system_thread, &attr, (void* (*)(void*))thread_func, thread);
Packit ae235b
Packit ae235b
  posix_check_cmd (pthread_attr_destroy (&attr));
Packit ae235b
Packit ae235b
  if (ret == EAGAIN)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN, 
Packit ae235b
                   "Error creating thread: %s", g_strerror (ret));
Packit ae235b
      g_slice_free (GThreadPosix, thread);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  posix_check_err (ret, "pthread_create");
Packit ae235b
Packit ae235b
  g_mutex_init (&thread->lock);
Packit ae235b
Packit ae235b
  return (GRealThread *) thread;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_yield:
Packit ae235b
 *
Packit ae235b
 * Causes the calling thread to voluntarily relinquish the CPU, so
Packit ae235b
 * that other threads can run.
Packit ae235b
 *
Packit ae235b
 * This function is often used as a method to make busy wait less evil.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_yield (void)
Packit ae235b
{
Packit ae235b
  sched_yield ();
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_system_thread_wait (GRealThread *thread)
Packit ae235b
{
Packit ae235b
  GThreadPosix *pt = (GThreadPosix *) thread;
Packit ae235b
Packit ae235b
  g_mutex_lock (&pt->lock);
Packit ae235b
Packit ae235b
  if (!pt->joined)
Packit ae235b
    {
Packit ae235b
      posix_check_cmd (pthread_join (pt->system_thread, NULL));
Packit ae235b
      pt->joined = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_mutex_unlock (&pt->lock);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_system_thread_exit (void)
Packit ae235b
{
Packit ae235b
  pthread_exit (NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_system_thread_set_name (const gchar *name)
Packit ae235b
{
Packit ae235b
#if defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
Packit ae235b
  pthread_setname_np (pthread_self(), name); /* on Linux and Solaris */
Packit ae235b
#elif defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
Packit ae235b
  pthread_setname_np (name); /* on OS X and iOS */
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/* {{{1 GMutex and GCond futex implementation */
Packit ae235b
Packit ae235b
#if defined(USE_NATIVE_MUTEX)
Packit ae235b
Packit ae235b
#include <linux/futex.h>
Packit ae235b
#include <sys/syscall.h>
Packit ae235b
Packit ae235b
#ifndef FUTEX_WAIT_PRIVATE
Packit ae235b
#define FUTEX_WAIT_PRIVATE FUTEX_WAIT
Packit ae235b
#define FUTEX_WAKE_PRIVATE FUTEX_WAKE
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* We should expand the set of operations available in gatomic once we
Packit ae235b
 * have better C11 support in GCC in common distributions (ie: 4.9).
Packit ae235b
 *
Packit ae235b
 * Before then, let's define a couple of useful things for our own
Packit ae235b
 * purposes...
Packit ae235b
 */
Packit ae235b
Packit ae235b
#define exchange_acquire(ptr, new) \
Packit ae235b
  __atomic_exchange_4((ptr), (new), __ATOMIC_ACQUIRE)
Packit ae235b
#define compare_exchange_acquire(ptr, old, new) \
Packit ae235b
  __atomic_compare_exchange_4((ptr), (old), (new), 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)
Packit ae235b
Packit ae235b
#define exchange_release(ptr, new) \
Packit ae235b
  __atomic_exchange_4((ptr), (new), __ATOMIC_RELEASE)
Packit ae235b
#define store_release(ptr, new) \
Packit ae235b
  __atomic_store_4((ptr), (new), __ATOMIC_RELEASE)
Packit ae235b
Packit ae235b
/* Our strategy for the mutex is pretty simple:
Packit ae235b
 *
Packit ae235b
 *  0: not in use
Packit ae235b
 *
Packit ae235b
 *  1: acquired by one thread only, no contention
Packit ae235b
 *
Packit ae235b
 *  > 1: contended
Packit ae235b
 *
Packit ae235b
 *
Packit ae235b
 * As such, attempting to acquire the lock should involve an increment.
Packit ae235b
 * If we find that the previous value was 0 then we can return
Packit ae235b
 * immediately.
Packit ae235b
 *
Packit ae235b
 * On unlock, we always store 0 to indicate that the lock is available.
Packit ae235b
 * If the value there was 1 before then we didn't have contention and
Packit ae235b
 * can return immediately.  If the value was something other than 1 then
Packit ae235b
 * we have the contended case and need to wake a waiter.
Packit ae235b
 *
Packit ae235b
 * If it was not 0 then there is another thread holding it and we must
Packit ae235b
 * wait.  We must always ensure that we mark a value >1 while we are
Packit ae235b
 * waiting in order to instruct the holder to do a wake operation on
Packit ae235b
 * unlock.
Packit ae235b
 */
Packit ae235b
Packit ae235b
void
Packit ae235b
g_mutex_init (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  mutex->i[0] = 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_mutex_clear (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  if G_UNLIKELY (mutex->i[0] != 0)
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "g_mutex_clear() called on uninitialised or locked mutex\n");
Packit ae235b
      g_abort ();
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __attribute__((noinline))
Packit ae235b
g_mutex_lock_slowpath (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  /* Set to 2 to indicate contention.  If it was zero before then we
Packit ae235b
   * just acquired the lock.
Packit ae235b
   *
Packit ae235b
   * Otherwise, sleep for as long as the 2 remains...
Packit ae235b
   */
Packit ae235b
  while (exchange_acquire (&mutex->i[0], 2) != 0)
Packit ae235b
    syscall (__NR_futex, &mutex->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) 2, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void __attribute__((noinline))
Packit ae235b
g_mutex_unlock_slowpath (GMutex *mutex,
Packit ae235b
                         guint   prev)
Packit ae235b
{
Packit ae235b
  /* We seem to get better code for the uncontended case by splitting
Packit ae235b
   * this out...
Packit ae235b
   */
Packit ae235b
  if G_UNLIKELY (prev == 0)
Packit ae235b
    {
Packit ae235b
      fprintf (stderr, "Attempt to unlock mutex that was not locked\n");
Packit ae235b
      g_abort ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  syscall (__NR_futex, &mutex->i[0], (gsize) FUTEX_WAKE_PRIVATE, (gsize) 1, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_mutex_lock (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  /* 0 -> 1 and we're done.  Anything else, and we need to wait... */
Packit ae235b
  if G_UNLIKELY (g_atomic_int_add (&mutex->i[0], 1) != 0)
Packit ae235b
    g_mutex_lock_slowpath (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_mutex_unlock (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  guint prev;
Packit ae235b
Packit ae235b
  prev = exchange_release (&mutex->i[0], 0);
Packit ae235b
Packit ae235b
  /* 1-> 0 and we're done.  Anything else and we need to signal... */
Packit ae235b
  if G_UNLIKELY (prev != 1)
Packit ae235b
    g_mutex_unlock_slowpath (mutex, prev);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_mutex_trylock (GMutex *mutex)
Packit ae235b
{
Packit ae235b
  guint zero = 0;
Packit ae235b
Packit ae235b
  /* We don't want to touch the value at all unless we can move it from
Packit ae235b
   * exactly 0 to 1.
Packit ae235b
   */
Packit ae235b
  return compare_exchange_acquire (&mutex->i[0], &zero, 1);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Condition variables are implemented in a rather simple way as well.
Packit ae235b
 * In many ways, futex() as an abstraction is even more ideally suited
Packit ae235b
 * to condition variables than it is to mutexes.
Packit ae235b
 *
Packit ae235b
 * We store a generation counter.  We sample it with the lock held and
Packit ae235b
 * unlock before sleeping on the futex.
Packit ae235b
 *
Packit ae235b
 * Signalling simply involves increasing the counter and making the
Packit ae235b
 * appropriate futex call.
Packit ae235b
 *
Packit ae235b
 * The only thing that is the slightest bit complicated is timed waits
Packit ae235b
 * because we must convert our absolute time to relative.
Packit ae235b
 */
Packit ae235b
Packit ae235b
void
Packit ae235b
g_cond_init (GCond *cond)
Packit ae235b
{
Packit ae235b
  cond->i[0] = 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_cond_clear (GCond *cond)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_cond_wait (GCond  *cond,
Packit ae235b
             GMutex *mutex)
Packit ae235b
{
Packit ae235b
  guint sampled = g_atomic_int_get (&cond->i[0]);
Packit ae235b
Packit ae235b
  g_mutex_unlock (mutex);
Packit ae235b
  syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) sampled, NULL);
Packit ae235b
  g_mutex_lock (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_cond_signal (GCond *cond)
Packit ae235b
{
Packit ae235b
  g_atomic_int_inc (&cond->i[0]);
Packit ae235b
Packit ae235b
  syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAKE_PRIVATE, (gsize) 1, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_cond_broadcast (GCond *cond)
Packit ae235b
{
Packit ae235b
  g_atomic_int_inc (&cond->i[0]);
Packit ae235b
Packit ae235b
  syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAKE_PRIVATE, (gsize) INT_MAX, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_cond_wait_until (GCond  *cond,
Packit ae235b
                   GMutex *mutex,
Packit ae235b
                   gint64  end_time)
Packit ae235b
{
Packit ae235b
  struct timespec now;
Packit ae235b
  struct timespec span;
Packit ae235b
  guint sampled;
Packit ae235b
  int res;
Packit ae235b
Packit ae235b
  if (end_time < 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  clock_gettime (CLOCK_MONOTONIC, &now;;
Packit ae235b
  span.tv_sec = (end_time / 1000000) - now.tv_sec;
Packit ae235b
  span.tv_nsec = ((end_time % 1000000) * 1000) - now.tv_nsec;
Packit ae235b
  if (span.tv_nsec < 0)
Packit ae235b
    {
Packit ae235b
      span.tv_nsec += 1000000000;
Packit ae235b
      span.tv_sec--;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (span.tv_sec < 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  sampled = cond->i[0];
Packit ae235b
  g_mutex_unlock (mutex);
Packit ae235b
  res = syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) sampled, &span;;
Packit ae235b
  g_mutex_lock (mutex);
Packit ae235b
Packit ae235b
  return (res < 0 && errno == ETIMEDOUT) ? FALSE : TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  /* {{{1 Epilogue */
Packit ae235b
/* vim:set foldmethod=marker: */