cds
2.3.2
|
Spin lock. More...
#include <cds/sync/spinlock.h>
Public Types | |
typedef Backoff | backoff_strategy |
back-off strategy type | |
Public Member Functions | |
spin_lock () noexcept | |
Construct free (unlocked) spin-lock. | |
spin_lock (bool bLocked) noexcept | |
Construct spin-lock in specified state. More... | |
spin_lock (const spin_lock< Backoff > &) noexcept | |
Dummy copy constructor. More... | |
~spin_lock () | |
Destructor. On debug time it checks whether spin-lock is free. | |
bool | is_locked () const noexcept |
Check if the spin is locked. | |
bool | try_lock () noexcept |
Try to lock the object. More... | |
bool | try_lock (unsigned int nTryCount) noexcept(noexcept(backoff_strategy()())) |
Try to lock the object, repeat nTryCount times if failed. More... | |
void | lock () noexcept(noexcept(backoff_strategy()())) |
Lock the spin-lock. Waits infinitely while spin-lock is locked. Debug version: deadlock may be detected. | |
void | unlock () noexcept |
Unlock the spin-lock. Debug version: deadlock may be detected. | |
Private Attributes | |
atomics::atomic< bool > | m_spin |
Spin. | |
Spin lock.
Simple and light-weight spin-lock critical section It is useful to gain access to small (short-timed) code
Algorithm:
TATAS (test-and-test-and-lock) [1984] L. Rudolph, Z. Segall. Dynamic Decentralized Cache Schemes for MIMD Parallel Processors.
No serialization performed - any of waiting threads may owns the spin-lock. This spin-lock is NOT recursive: the thread owned the lock cannot call lock()
method without deadlock. The method unlock()
can call any thread
DEBUG version: The spinlock stores owner thead id. Assertion is raised when:
If spin-lock is locked the Backoff
algorithm is called. Predefined backoff::LockDefault
class yields current thread and repeats lock attempts later
Template parameters:
Backoff
- backoff strategy. Used when spin lock is locked
|
inlineexplicitnoexcept |
Construct spin-lock in specified state.
In debug mode: if bLocked
= true then spin-lock is made owned by current thread
|
inlinenoexcept |
Dummy copy constructor.
The ctor initializes the spin to free (unlocked) state like the default ctor.
|
inlinenoexcept |
Try to lock the object.
Returns true
if locking is succeeded otherwise (if the spin is already locked) returns false
Debug version: deadlock can be detected
|
inlinenoexcept |
Try to lock the object, repeat nTryCount
times if failed.
Returns true
if locking is succeeded otherwise (if the spin is already locked) returns false