BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls::SpinLock Struct Reference

#include <bsls_spinlock.h>

Public Member Functions

void lock ()
 
void lockWithBackoff ()
 
void lockWithoutBackoff ()
 
int tryLock (int numRetries=0)
 
void unlock ()
 

Public Attributes

AtomicOperations::AtomicTypes::Int d_state
 

Static Public Attributes

static const SpinLock s_unlocked
 

Detailed Description

A statically-initializable synchronization primitive that "spins" (i.e., executes user instructions in a tight loop) rather than blocking waiting threads using system calls. The following idiom is used to initialize SpinLock variables:

#define BSLS_SPINLOCK_UNLOCKED
Definition bsls_spinlock.h:326
Definition bsls_spinlock.h:381
void lock()
Definition bsls_spinlock.h:591

A class member d_lock of type SpinLock may be initialized using the following idiom:

static const SpinLock s_unlocked
Definition bsls_spinlock.h:456

Member Function Documentation

◆ lock()

void bsls::SpinLock::lock ( )
inline

Spin (repeat a loop continuously without using the system to pause or reschedule the thread) until this object is unlocked, then atomically acquire the lock.

◆ lockWithBackoff()

void bsls::SpinLock::lockWithBackoff ( )
inline

Repeat a loop continuously, potentially using the system to pause or reschedule the thread, until this object is unlocked, then atomically acquire the lock. The spinning has backoff logic. Note that this method is recommended when system calls are permissible, significant contention is expected, and no better mutual exclusion primitive is available.

◆ lockWithoutBackoff()

void bsls::SpinLock::lockWithoutBackoff ( )
inline

Spin (repeat a loop continuously without using the system to pause or reschedule the thread) until this object is unlocked, then atomically acquire the lock. The spinning does not perform a backoff.

◆ tryLock()

int bsls::SpinLock::tryLock ( int  numRetries = 0)
inline

Attempt to acquire the lock; optionally specify the numRetries times to attempt again if this object is already locked. Return 0 on success, and a non-zero value if the lock was not successfully acquired. The behavior is undefined unless 0 <= numRetries.

◆ unlock()

void bsls::SpinLock::unlock ( )
inline

Release the lock. The behavior is undefined unless the current thread holds the lock.

Member Data Documentation

◆ d_state

AtomicOperations::AtomicTypes::Int bsls::SpinLock::d_state

0 when unlocked. 'd_state is public to allow aggregate initialization where constexpr constructors are not available. It is semantically private, and should not be used directly.

◆ s_unlocked

const SpinLock bsls::SpinLock::s_unlocked
static

The documentation for this struct was generated from the following file: