Asterisk - The Open Source Telephony Project  21.4.1
Macros | Typedefs | Enumerations | Functions
spinlock.h File Reference

Spin Locks. More...

#include <pthread.h>
#include "asterisk/compiler.h"

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...
 

Detailed Description

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.

Macro Definition Documentation

#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.

Enumeration Type Documentation

Spinlock Implementation Types.

Not all implementations will be available on all platforms.

Definition at line 47 of file spinlock.h.

47  {
48  AST_SPINLOCK_TYPE_GCC_ATOMICS,
49  AST_SPINLOCK_TYPE_GAS_X86,
50  AST_SPINLOCK_TYPE_GAS_ARM,
51  AST_SPINLOCK_TYPE_GAS_SPARC,
52  AST_SPINLOCK_TYPE_OSX_ATOMICS,
53  AST_SPINLOCK_TYPE_PTHREAD_SPINLOCK,
54  AST_SPINLOCK_TYPE_PTHREAD_MUTEX,
55 };

Function Documentation

static force_inline int ast_spinlock_destroy ( ast_spinlock_t *  lock)
static

Destroy a spin lock.

Parameters
lockAddress of the lock
Return values
0Success
otherFailure

Definition at line 430 of file spinlock.h.

431 {
432  return pthread_mutex_destroy(lock);
433 }
ast_mutex_t lock
static force_inline int ast_spinlock_init ( ast_spinlock_t *  lock)
static

Initialize a spin lock.

Parameters
lockAddress of the lock
Return values
0Success
otherFailure

Definition at line 409 of file spinlock.h.

410 {
411  pthread_mutex_init(lock, NULL);
412  return 0;
413 }
ast_mutex_t lock
static force_inline int ast_spinlock_lock ( ast_spinlock_t *  lock)
static

Lock a spin lock.

Parameters
lockAddress of the lock
Return values
0Success
otherFailure

Definition at line 415 of file spinlock.h.

416 {
417  return pthread_mutex_lock(lock);
418 }
ast_mutex_t lock
static force_inline int ast_spinlock_trylock ( ast_spinlock_t *  lock)
static

Try to lock a spin lock.

Attempt to gain a lock. Return immediately regardless of result.

Parameters
lockAddress of the lock
Return values
0Success
otherLock was not obtained

Definition at line 420 of file spinlock.h.

421 {
422  return pthread_mutex_trylock(lock);
423 }
ast_mutex_t lock
static force_inline int ast_spinlock_unlock ( ast_spinlock_t *  lock)
static

Unlock a spin lock.

Parameters
lockAddress of the lock
Return values
0Success
otherFailure

Definition at line 425 of file spinlock.h.

426 {
427  return pthread_mutex_unlock(lock);
428 }
ast_mutex_t lock