Quick Links: |
#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 |
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:
SpinLock lock = BSLS_SPINLOCK_UNLOCKED;
A class member d_lock
of type SpinLock
may be initialized using the following idiom:
, d_lock(SpinLock::s_unlocked)
void bsls::SpinLock::lock | ( | ) |
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.
void bsls::SpinLock::lockWithBackoff | ( | ) |
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.
void bsls::SpinLock::lockWithoutBackoff | ( | ) |
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.
int bsls::SpinLock::tryLock | ( | int | numRetries = 0 |
) |
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
.
void bsls::SpinLock::unlock | ( | ) |
Release the lock. The behavior is undefined unless the current thread holds the lock.
const SpinLock bsls::SpinLock::s_unlocked [static] |
AtomicOperations::AtomicTypes::Int bsls::SpinLock::d_state |