Asterisk - The Open Source Telephony Project
18.5.0
|
Spin Locks. More...
Go to the source code of this file.
Macros | |
#define | AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_PTHREAD_MUTEX |
Implementation using GCC Atomics. More... | |
#define | AST_SPINLOCK_TYPE_LABEL "pthread_mutex" |
Typedefs | |
typedef pthread_mutex_t | ast_spinlock_t |
Enumerations | |
enum | ast_spinlock_type { AST_SPINLOCK_TYPE_GCC_ATOMICS, AST_SPINLOCK_TYPE_GAS_X86, AST_SPINLOCK_TYPE_GAS_ARM, AST_SPINLOCK_TYPE_GAS_SPARC, AST_SPINLOCK_TYPE_OSX_ATOMICS, AST_SPINLOCK_TYPE_PTHREAD_SPINLOCK, AST_SPINLOCK_TYPE_PTHREAD_MUTEX } |
Spinlock Implementation Types. More... | |
Functions | |
static force_inline int | ast_spinlock_destroy (ast_spinlock_t *lock) |
Destroy a spin lock. More... | |
static force_inline int | ast_spinlock_init (ast_spinlock_t *lock) |
Initialize a spin lock. More... | |
static force_inline int | ast_spinlock_lock (ast_spinlock_t *lock) |
Lock a spin lock. More... | |
static force_inline int | ast_spinlock_trylock (ast_spinlock_t *lock) |
Try to lock a spin lock. More... | |
static force_inline int | ast_spinlock_unlock (ast_spinlock_t *lock) |
Unlock a spin lock. More... | |
Spin Locks.
In some atomic operation circumstances the __atomic calls are not quite flexible enough but a full fledged mutex or rwlock is too expensive.
Spin locks should be used only for protecting short blocks of critical code such as simple compares and assignments. Operations that may block, hold a lock, or cause the thread to give up it's timeslice should NEVER be attempted in a spin lock.
Because spinlocks must be as lightweight as possible, there are no recursion or deadlock checks.
Definition in file spinlock.h.
#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_PTHREAD_MUTEX |
Implementation using GCC Atomics.
Specifically, __sync_lock_test_and_set is used to atomically set/unset the lock variable.
Most recent gcc implementations support this method and it's performance is equal to or better than the assembly implementations hence it is the most preferred implementation on all platforms.
Implementation using x86 Assembly
For x86 implementations that don't support gcc atomics, this is the next best method.
Implementation using ARM Assembly
For ARM implementations that don't support gcc atomics, this is the next best method.
Implementation using Sparc Assembly
For Sparc implementations that don't support gcc atomics, this is the next best method.
Implementation using pthread_spinlock
pthread_spinlocks are not supported on all platforms but if for some reason none of the previous implementations are available, it can be used with reasonable performance.
Implementation using OSX Atomics
The Darwin/Mac OSX platform has its own atomics implementation but it uses more kernel time than GCC atomics and x86 assembly. It is included as an unlikely fallback.
Implementation using pthread_mutex
pthread_mutex is supported on all platforms but it is also the worst performing. It is included as an unlikely fallback.
Definition at line 405 of file spinlock.h.
#define AST_SPINLOCK_TYPE_LABEL "pthread_mutex" |
Definition at line 406 of file spinlock.h.
typedef pthread_mutex_t ast_spinlock_t |
Definition at line 407 of file spinlock.h.
enum ast_spinlock_type |
Spinlock Implementation Types.
Not all implementations will be available on all platforms.
Definition at line 47 of file spinlock.h.
|
static |
Destroy a spin lock.
lock | Address of the lock |
0 | Success |
other | Failure |
Definition at line 430 of file spinlock.h.
References ast_spinlock_init(), ast_spinlock_lock(), ast_spinlock_trylock(), ast_spinlock_unlock(), force_inline, lock, and pthread_mutex_destroy.
|
static |
Initialize a spin lock.
lock | Address of the lock |
0 | Success |
other | Failure |
Definition at line 409 of file spinlock.h.
References NULL, and pthread_mutex_init.
Referenced by ast_spinlock_destroy().
|
static |
Lock a spin lock.
lock | Address of the lock |
0 | Success |
other | Failure |
Definition at line 415 of file spinlock.h.
References pthread_mutex_lock.
Referenced by ast_spinlock_destroy().
|
static |
Try to lock a spin lock.
Attempt to gain a lock. Return immediately regardless of result.
lock | Address of the lock |
0 | Success |
other | Lock was not obtained |
Definition at line 420 of file spinlock.h.
References pthread_mutex_trylock.
Referenced by ast_spinlock_destroy().
|
static |
Unlock a spin lock.
lock | Address of the lock |
0 | Success |
other | Failure |
Definition at line 425 of file spinlock.h.
References pthread_mutex_unlock.
Referenced by ast_spinlock_destroy().