RWSpinlockT< ATOMICTYPE > Struct Template Reference

#include <spinlock.h>

Inheritance diagram for RWSpinlockT< ATOMICTYPE >:

Detailed Description

template<typename ATOMICTYPE>
struct maxon::RWSpinlockT< ATOMICTYPE >

The RWSpinlock class implements a mutex that allows access of multiple readers or one exclusive writer. It will loop on a pause/idle instruction when it is already locked. This means you should only use a RWSpinlock for cases with almost no contention - otherwise you waste a lot of CPU power with idling and steal that from other tasks (and your battery). Having many concurrent readers may also noticably degrade performance because the lock is a shared resource and each read lock from a different thread will cause a write access to the RWSpinlock and requires synchronization of the CPU cores. If your problem produces a lot of contention (check scaling with more than 8 threads or profile with VTune/Instruments) it's most likely worth to rethink your approach and algorithm. Use ARWLock for high read to write ratios.

THREADSAFE.

Public Types

using RWState = typename ATOMICTYPE::Type
 

Public Member Functions

void ReadLock ()
 
void ReadUnlock ()
 
void WriteLock ()
 
void WriteUnlock ()
 
Bool AttemptWriteLock ()
 
Bool IsReadLocked () const
 
Bool IsWriteLocked () const
 

Static Public Attributes

static constexpr RWState WRITER
 
static constexpr RWState WRITER_PENDING
 
static constexpr RWState READERS
 
static constexpr RWState ONE_READER
 
static constexpr RWState BUSY
 

Additional Inherited Members

- Protected Member Functions inherited from BaseLock< ATOMICTYPE >
void PerformanceMonitorLockAcquired () const
 
void PerformanceMonitorLockReleased () const
 
 BaseLock ()
 
 ~BaseLock ()
 
- Protected Attributes inherited from BaseLock< ATOMICTYPE >
ATOMICTYPE _state
 

Member Typedef Documentation

◆ RWState

using RWState = typename ATOMICTYPE::Type

Member Function Documentation

◆ ReadLock()

void ReadLock ( )

Read locks a user level spin lock. Will force synchronization when multiple readers are pending: Does not scale! Creates a memory barrier. If a write is pending an exponential backoff pause loop is used to wait. THREADSAFE.

◆ ReadUnlock()

void ReadUnlock ( )

Balances a preceding ReadLock() when the reading thread has finished. THREADSAFE.

◆ WriteLock()

void WriteLock ( )

Write locks a user level spin lock. As long as there are pending readers an exponential backoff pause loop is used to wait. Creates a memory barrier. THREADSAFE.

◆ WriteUnlock()

void WriteUnlock ( )

Balances a preceding WriteLock() when the writing thread has finished. Creates a memory barrier. THREADSAFE.

◆ AttemptWriteLock()

Bool AttemptWriteLock ( )

Tries to write lock. Creates a memory barrier if the lock can be taken. Immediately returns with false if there are pending reads or writes.

Returns
True if successful. THREADSAFE.

◆ IsReadLocked()

Bool IsReadLocked ( ) const

Returns true if read-locked or false if unlocked (for diagnostics only). THREADSAFE.

Returns
True if locked, false if unlocked.

◆ IsWriteLocked()

Bool IsWriteLocked ( ) const

Returns true if write-locked or false if unlocked (for diagnostics only). THREADSAFE.

Returns
True if locked, false if unlocked.

Member Data Documentation

◆ WRITER

constexpr RWState WRITER
staticconstexpr

◆ WRITER_PENDING

constexpr RWState WRITER_PENDING
staticconstexpr

◆ READERS

constexpr RWState READERS
staticconstexpr

◆ ONE_READER

constexpr RWState ONE_READER
staticconstexpr

◆ BUSY

constexpr RWState BUSY
staticconstexpr