35#ifndef _ASTERISK_SPINLOCK_H
36#define _ASTERISK_SPINLOCK_H
68#ifdef HAVE_GCC_ATOMICS
69#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_GCC_ATOMICS
70#define AST_SPINLOCK_TYPE_LABEL "gcc_atomics"
81 while (__sync_lock_test_and_set(
lock, 1)) {
90 return __sync_lock_test_and_set(
lock, 1);
95 __sync_lock_release(
lock);
112#if (defined(__x86_64__) || defined(__i386__)) && !defined(AST_SPINLOCK_TYPE)
113#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_GAS_X86
114#define AST_SPINLOCK_TYPE_LABEL "gas_x86"
127 :
"+r" (v),
"=m" (*p)
136 while (x86chgl(
lock, 1)) {
145 return x86chgl(
lock, 1);
167#if defined(__arm__) && !defined(AST_SPINLOCK_TYPE)
168#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_GAS_ARM
169#define AST_SPINLOCK_TYPE_LABEL "gas_arm"
183 "1: ldrex %[tmp], %[lock];"
185#
if defined __ARM_ARCH && __ARM_ARCH >= 7
188 " strexeq %[tmp], %[c1], %[lock];"
204 " ldrex %[tmp], %[lock];"
206#
if defined __ARM_ARCH && __ARM_ARCH >= 7
209 " strexeq %[tmp], %[c1], %[lock];"
222 " str %[c0], %[lock];"
223#
if defined __ARM_ARCH && __ARM_ARCH >= 7
248#if defined(__sparc__) && !defined(AST_SPINLOCK_TYPE)
249#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_GAS_SPARC
250#define AST_SPINLOCK_TYPE_LABEL "gas_sparc"
263 __asm__ __volatile__(
264 "1: ldstub %[lock], %[tmp]\n"
265 " brnz,pn %[tmp], 2f\n"
268 "2: ldub %[lock], %[tmp]\n"
269 " brnz,pt %[tmp], 2b\n"
271 " ba,a,pt %%xcc, 1b\n"
285 __asm__ __volatile__(
286 " ldstub %[lock], %[result]\n"
297 __asm__ __volatile__(
321#if defined (HAVE_PTHREAD_SPINLOCK) && !defined(AST_SPINLOCK_TYPE)
322#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_PTHREAD_SPINLOCK
323#define AST_SPINLOCK_TYPE_LABEL "pthread_spinlock"
328 return pthread_spin_init(
lock, PTHREAD_PROCESS_PRIVATE);
333 return pthread_spin_lock(
lock);
338 return pthread_spin_trylock(
lock);
343 return pthread_spin_unlock(
lock);
348 return pthread_spin_destroy(
lock);
361#if defined(HAVE_OSX_ATOMICS) && !defined(AST_SPINLOCK_TYPE)
362#include <libkern/OSAtomic.h>
363#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_OSX_ATOMICS
364#define AST_SPINLOCK_TYPE_LABEL "osx_atomics"
369 *
lock = OS_SPINLOCK_INIT;
375 OSSpinLockLock(
lock);
381 return !OSSpinLockTry(
lock);
386 OSSpinLockUnlock(
lock);
404#if !defined(AST_SPINLOCK_TYPE)
405#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_PTHREAD_MUTEX
406#define AST_SPINLOCK_TYPE_LABEL "pthread_mutex"
436#if !defined(AST_SPINLOCK_TYPE)
437#error "No spinlock implementation could be found."
Compiler-specific macros and other items.
#define pthread_mutex_trylock
#define pthread_mutex_lock
#define pthread_mutex_unlock
#define pthread_mutex_destroy
#define pthread_mutex_init
static force_inline int ast_spinlock_lock(ast_spinlock_t *lock)
Lock a spin lock.
static force_inline int ast_spinlock_trylock(ast_spinlock_t *lock)
Try to lock a spin lock.
static force_inline int ast_spinlock_unlock(ast_spinlock_t *lock)
Unlock a spin lock.
pthread_mutex_t ast_spinlock_t
static force_inline int ast_spinlock_destroy(ast_spinlock_t *lock)
Destroy a spin lock.
ast_spinlock_type
Spinlock Implementation Types.
@ AST_SPINLOCK_TYPE_PTHREAD_MUTEX
@ AST_SPINLOCK_TYPE_OSX_ATOMICS
@ AST_SPINLOCK_TYPE_GAS_SPARC
@ AST_SPINLOCK_TYPE_GAS_X86
@ AST_SPINLOCK_TYPE_GCC_ATOMICS
@ AST_SPINLOCK_TYPE_PTHREAD_SPINLOCK
@ AST_SPINLOCK_TYPE_GAS_ARM
static force_inline int ast_spinlock_init(ast_spinlock_t *lock)
Initialize a spin lock.