akaros/tools/compilers/gcc-glibc/glibc-2.19-akaros/sysdeps/akaros/bits/libc-lock.h
<<
>>
Prefs
   1/* libc-internal interface for mutex locks.  Stub version.
   2   Copyright (C) 1996-2014 Free Software Foundation, Inc.
   3   This file is part of the GNU C Library.
   4
   5   The GNU C Library is free software; you can redistribute it and/or
   6   modify it under the terms of the GNU Lesser General Public
   7   License as published by the Free Software Foundation; either
   8   version 2.1 of the License, or (at your option) any later version.
   9
  10   The GNU C Library is distributed in the hope that it will be useful,
  11   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13   Lesser General Public License for more details.
  14
  15   You should have received a copy of the GNU Lesser General Public
  16   License along with the GNU C Library; if not, see
  17   <http://www.gnu.org/licenses/>.  */
  18
  19#ifndef _BITS_LIBC_LOCK_H
  20#define _BITS_LIBC_LOCK_H 1
  21
  22#define __need_NULL
  23#include <stddef.h>
  24
  25#ifdef _LIBC
  26# include <lowlevellock.h>
  27# include <tls.h>
  28#endif
  29
  30#include <parlib/uthread.h>
  31#include <parlib/dtls.h>
  32
  33/* Define a lock variable NAME with storage class CLASS.  The lock must be
  34   initialized with __libc_lock_init before it can be used (or define it
  35   with __libc_lock_define_initialized, below).  Use `extern' for CLASS to
  36   declare a lock defined in another module.  In public structure
  37   definitions you must use a pointer to the lock structure (i.e., NAME
  38   begins with a `*'), because its storage size will not be known outside
  39   of libc.  */
  40#define __libc_lock_define(CLASS, NAME) \
  41        CLASS uth_mutex_t NAME;
  42#define __libc_lock_define_recursive(CLASS, NAME) \
  43        CLASS uth_recurse_mutex_t NAME;
  44#define __libc_rwlock_define(CLASS, NAME) \
  45        CLASS uth_rwlock_t NAME;
  46
  47/* These don't seem to be used much outside our sysdep (malloc-machine.h), but
  48 * the RTLD one later is used. */
  49#define _LIBC_LOCK_INITIALIZER UTH_MUTEX_INIT
  50#define _LIBC_LOCK_RECURSIVE_INITIALIZER UTH_RECURSE_MUTEX_INIT
  51#define _LIBC_RWLOCK_INITIALIZER UTH_RWLOCK_INIT
  52
  53/* Define an initialized lock variable NAME with storage class CLASS.  */
  54#define __libc_lock_define_initialized(CLASS, NAME) \
  55        CLASS uth_mutex_t NAME = UTH_MUTEX_INIT;
  56#define __libc_rwlock_define_initialized(CLASS, NAME) \
  57        CLASS uth_rwlock_t NAME = UTH_RWLOCK_INIT;
  58
  59/* Define an initialized recursive lock variable NAME with storage
  60   class CLASS.  */
  61#define __libc_lock_define_initialized_recursive(CLASS, NAME) \
  62        CLASS uth_recurse_mutex_t NAME = UTH_RECURSE_MUTEX_INIT;
  63
  64/* Initialize the named lock variable, leaving it in a consistent, unlocked
  65   state.  */
  66#define __libc_lock_init(NAME) uth_mutex_init(&(NAME))
  67#define __libc_rwlock_init(NAME) uth_rwlock_init(&(NAME))
  68
  69/* Same as last but this time we initialize a recursive mutex.  */
  70#define __libc_lock_init_recursive(NAME) uth_recurse_mutex_init(&(NAME))
  71
  72/* Finalize the named lock variable, which must be locked.  It cannot be
  73   used again until __libc_lock_init is called again on it.  This must be
  74   called on a lock variable before the containing storage is reused.  */
  75#define __libc_lock_fini(NAME) uth_mutex_destroy(&(NAME))
  76#define __libc_rwlock_fini(NAME) uth_rwlock_destroy(&(NAME))
  77
  78/* Finalize recursive named lock.  */
  79#define __libc_lock_fini_recursive(NAME) uth_recurse_mutex_destroy(&(NAME))
  80
  81/* Lock the named lock variable.  */
  82#define __libc_lock_lock(NAME) uth_mutex_lock(&(NAME))
  83#define __libc_rwlock_rdlock(NAME) uth_rwlock_rdlock(&(NAME))
  84#define __libc_rwlock_wrlock(NAME) uth_rwlock_wrlock(&(NAME))
  85
  86/* Lock the recursive named lock variable.  */
  87#define __libc_lock_lock_recursive(NAME) uth_recurse_mutex_lock(&(NAME))
  88
  89/* Try to lock the named lock variable.  */
  90#define __libc_lock_trylock(NAME) \
  91        ({ uth_mutex_trylock(&(NAME)) ? 0 : EBUSY; })
  92#define __libc_rwlock_tryrdlock(NAME) \
  93        ({ uth_rwlock_try_rdlock(&(NAME)) ? 0 : EBUSY; })
  94#define __libc_rwlock_trywrlock(NAME) \
  95        ({ uth_rwlock_try_wrlock(&(NAME)) ? 0 : EBUSY; })
  96
  97/* Try to lock the recursive named lock variable.  */
  98#define __libc_lock_trylock_recursive(NAME) \
  99        ({ uth_recurse_mutex_trylock(&(NAME)) ? 0 : EBUSY; })
 100
 101/* Unlock the named lock variable.  */
 102#define __libc_lock_unlock(NAME) uth_mutex_unlock(&(NAME))
 103#define __libc_rwlock_unlock(NAME) uth_rwlock_unlock(&(NAME))
 104
 105/* Unlock the recursive named lock variable.  */
 106#define __libc_lock_unlock_recursive(NAME) uth_recurse_mutex_unlock(&(NAME))
 107
 108/* RTLD locks */
 109/* Ideally, we'd use uthread mutexes.  That's what pthreads does.  However, this
 110 * code will be in ld.so, and will never actually link against parlib.  We might
 111 * be able to do some function pointer magic, but for the most part, we'll
 112 * probably need kernel support (#futex or something).  Instead of that, we can
 113 * build recursive locks on top of spinlocks, and deal with any problems as they
 114 * arise.  By not using PDR, we run the risk of deadlocking, but I can live with
 115 * that for now (you'd need to dlopen() from vcore context, which would probably
 116 * panic for some other reason). */
 117
 118typedef struct {
 119        unsigned int lock;
 120        unsigned int count;
 121        void *owner;
 122} __rtld_lock_recursive_t;
 123
 124#define _RTLD_LOCK_RECURSIVE_INITIALIZER { 0, 0, (void*)-1 }
 125
 126static inline void *__rtld_lock_who_am_i(void)
 127{
 128        if (atomic_read(&vcpd_of(0)->flags) & VC_SCP_NOVCCTX)
 129                return (void*)0xf00baa;
 130        /* We can't use TLS related to parlib (in_vcore_context() / vcore_id()
 131         * will crash.  current_uthread won't link.).  We *can* find our thread
 132         * descriptor, which disambiguates any callers (including between vcore
 133         * context (which probably shouldn't be in here) and uthreads, so long
 134         * as uthreads have TLS - which they must if they are making glibc
 135         * calls. */
 136        return THREAD_SELF;
 137}
 138
 139static inline void rtld_lock_lock_initialize(__rtld_lock_recursive_t *lock)
 140{
 141        lock->lock = 0;
 142        lock->count = 0;
 143        lock->owner = (void*)-1;
 144}
 145
 146static inline void rtld_lock_lock_recursive(__rtld_lock_recursive_t *lock)
 147{
 148        void *me = __rtld_lock_who_am_i();
 149
 150        if (lock->owner == me) {
 151                lock->count++;
 152                return;
 153        }
 154        while (__sync_lock_test_and_set(&lock->lock, 1))
 155                cpu_relax();
 156        lock->count++;
 157        lock->owner = me;
 158}
 159
 160static inline void rtld_lock_unlock_recursive(__rtld_lock_recursive_t *lock)
 161{
 162        lock->count--;
 163        if (lock->count)
 164                return;
 165        lock->owner = (void*)-1;
 166        wmb();
 167        lock->lock = 0;
 168}
 169
 170#define __rtld_lock_define_recursive(CLASS, NAME) \
 171        CLASS __rtld_lock_recursive_t NAME;
 172#define __rtld_lock_define_initialized_recursive(CLASS, NAME) \
 173        CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
 174
 175/* __rtld_lock_initialize probably should be __rtld_lock_initialize_recursive.
 176 * Might be a glibc bug.  These also want &(NAME), and not NAME, hence the
 177 * macros. */
 178#define __rtld_lock_initialize(NAME) rtld_lock_lock_initialize(&(NAME))
 179#define __rtld_lock_lock_recursive(NAME) rtld_lock_lock_recursive(&(NAME))
 180#define __rtld_lock_unlock_recursive(NAME) rtld_lock_unlock_recursive(&(NAME))
 181
 182
 183/* Define once control variable.  */
 184#define __libc_once_define(CLASS, NAME) \
 185        CLASS parlib_once_t NAME
 186
 187/* Call handler iff the first call.  */
 188#define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
 189        parlib_run_once(&(ONCE_CONTROL), (void (*)(void*))(INIT_FUNCTION), NULL)
 190
 191/* Get once control variable.  */
 192#define __libc_once_get(ONCE_CONTROL) \
 193  ((ONCE_CONTROL).ran_once == TRUE)
 194
 195/* Start a critical region with a cleanup function */
 196#define __libc_cleanup_region_start(DOIT, FCT, ARG)                         \
 197{                                                                           \
 198  typeof (***(FCT)) *__save_FCT = (DOIT) ? (FCT) : 0;                       \
 199  typeof (ARG) __save_ARG = ARG;                                            \
 200  /* close brace is in __libc_cleanup_region_end below. */
 201
 202/* End a critical region started with __libc_cleanup_region_start. */
 203#define __libc_cleanup_region_end(DOIT)                                     \
 204  if ((DOIT) && __save_FCT != 0)                                            \
 205    (*__save_FCT)(__save_ARG);                                              \
 206}
 207
 208/* Sometimes we have to exit the block in the middle.  */
 209#define __libc_cleanup_end(DOIT)                                            \
 210  if ((DOIT) && __save_FCT != 0)                                            \
 211    (*__save_FCT)(__save_ARG);                                              \
 212
 213#define __libc_cleanup_push(fct, arg) __libc_cleanup_region_start (1, fct, arg)
 214#define __libc_cleanup_pop(execute) __libc_cleanup_region_end (execute)
 215
 216/* We need portable names for some of the functions.  */
 217#define __libc_mutex_unlock uth_mutex_unlock
 218
 219/* Type for key of thread specific data.  */
 220typedef dtls_key_t __libc_key_t;
 221
 222/* Create key for thread specific data.  */
 223#define __libc_key_create(KEY,DEST)     (*KEY = dtls_key_create(DEST), 0)
 224
 225/* Set thread-specific data associated with KEY to VAL.  */
 226#define __libc_setspecific(KEY,VAL)     set_dtls(KEY, VAL)
 227
 228/* Get thread-specific data associated with KEY.  */
 229#define __libc_getspecific(KEY) get_dtls(KEY)
 230
 231#endif  /* bits/libc-lock.h */
 232