Asterisk - The Open Source Telephony Project GIT-master-2de1a68
Macros | Typedefs | Enumerations | Functions
spinlock.h File Reference

Spin Locks. More...

#include <pthread.h>
#include "asterisk/compiler.h"
Include dependency graph for spinlock.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

◆ AST_SPINLOCK_TYPE

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

◆ AST_SPINLOCK_TYPE_LABEL

#define AST_SPINLOCK_TYPE_LABEL   "pthread_mutex"

Definition at line 406 of file spinlock.h.

Typedef Documentation

◆ ast_spinlock_t

Definition at line 407 of file spinlock.h.

Enumeration Type Documentation

◆ ast_spinlock_type

Spinlock Implementation Types.

Not all implementations will be available on all platforms.

Enumerator
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 

Definition at line 47 of file spinlock.h.

47 {
55};
@ AST_SPINLOCK_TYPE_PTHREAD_MUTEX
Definition: spinlock.h:54
@ AST_SPINLOCK_TYPE_OSX_ATOMICS
Definition: spinlock.h:52
@ AST_SPINLOCK_TYPE_GAS_SPARC
Definition: spinlock.h:51
@ AST_SPINLOCK_TYPE_GAS_X86
Definition: spinlock.h:49
@ AST_SPINLOCK_TYPE_GCC_ATOMICS
Definition: spinlock.h:48
@ AST_SPINLOCK_TYPE_PTHREAD_SPINLOCK
Definition: spinlock.h:53
@ AST_SPINLOCK_TYPE_GAS_ARM
Definition: spinlock.h:50

Function Documentation

◆ ast_spinlock_destroy()

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{
433}
ast_mutex_t lock
Definition: app_sla.c:331
#define pthread_mutex_destroy
Definition: lock.h:629

References lock, and pthread_mutex_destroy.

◆ ast_spinlock_init()

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{
412 return 0;
413}
#define pthread_mutex_init
Definition: lock.h:628
#define NULL
Definition: resample.c:96

References lock, NULL, and pthread_mutex_init.

◆ ast_spinlock_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}
#define pthread_mutex_lock
Definition: lock.h:625

References lock, and pthread_mutex_lock.

◆ ast_spinlock_trylock()

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{
423}
#define pthread_mutex_trylock
Definition: lock.h:627

References lock, and pthread_mutex_trylock.

◆ ast_spinlock_unlock()

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{
428}
#define pthread_mutex_unlock
Definition: lock.h:626

References lock, and pthread_mutex_unlock.