RWSpinlock Struct Reference

#include <spinlock.h>

Inheritance diagram for RWSpinlock:

Detailed Description

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 Member Functions

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

Additional Inherited Members

- Protected Member Functions inherited from BaseLock
void PerformanceMonitorSpin () const
 
void PerformanceMonitorLockAcquired () const
 
void PerformanceMonitorLockReleased () const
 
 BaseLock ()
 
 ~BaseLock ()
 
- Protected Attributes inherited from BaseLock
AtomicInt32 _state
 

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.