Spinlock Struct Reference

#include <spinlock.h>

Inheritance diagram for Spinlock:

Detailed Description

The Spinlock class implements a mutex that will loop on a pause/idle instruction when it is already locked. This means you should only use a Spinlock 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). 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.

THREADSAFE.

Public Member Functions

Bool AttemptLock ()
 
void Lock ()
 
void Unlock ()
 
Bool IsLocked () const
 

Protected Member Functions

Bool CoreAttemptLock ()
 
void CoreLock ()
 
void CoreUnlock ()
 
void CoreSpinAndRetry ()
 
- Protected Member Functions inherited from BaseLock
void PerformanceMonitorLockAcquired () const
 
void PerformanceMonitorLockReleased () const
 
 BaseLock ()
 
 ~BaseLock ()
 

Private Member Functions

void SpinAndRetry ()
 

Additional Inherited Members

- Protected Attributes inherited from BaseLock
AtomicInt32 _state
 

Member Function Documentation

◆ AttemptLock()

Bool AttemptLock ( )

Tries to lock. Creates a memory barrier if the lock can be taken. If the application has crashed and is in crash dump mode you may never be able to acquire a lock when it was already locked when the crash occurred. Do not spin on AttemptLock(), always use Lock() because it consumes less resources and is much faster! THREADSAFE.

Returns
True if successful.

◆ Lock()

void Lock ( )

Locks a user level spin lock. As long as the lock cannot be taken an exponential backoff pause loop is used to wait. Creates a memory barrier. If the application has crashed and is in crash dump mode the state of the lock will be ignored to avoid unwanted deadlocks in the crash log code. THREADSAFE.

◆ Unlock()

void Unlock ( void  )

Unlocks a user level spin lock. Creates a memory barrier. THREADSAFE.

◆ IsLocked()

Bool IsLocked ( ) const

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

Returns
True if locked, false if unlocked.

◆ CoreAttemptLock()

Bool CoreAttemptLock ( )
protected

Tries to lock. Creates a memory barrier if the lock can be taken. If the application has crashed and is in crash dump mode you may never be able to acquire a lock when it was already locked when the crash occurred. THREADSAFE.

Returns
True if successful.

◆ CoreLock()

void CoreLock ( )
protected

Locks a user level spin lock. As long as the lock cannot be taken an exponential backoff pause loop is used to wait. Creates a memory barrier. THREADSAFE.

◆ CoreUnlock()

void CoreUnlock ( )
protected

Unlocks a user level spin lock. Creates a memory barrier. THREADSAFE.

◆ CoreSpinAndRetry()

void CoreSpinAndRetry ( )
protected

Spins on a volatile read using an exponential backoff pause loop until _state is zero and CAS will be tried. If the application has crashed and is in crash dump mode the state of the lock will be ignored to avoid unwanted deadlocks in the crash log code.

◆ SpinAndRetry()

void SpinAndRetry ( )
private

Spins on a volatile read using an exponential backoff pause loop until _state is zero and CAS will be tried. If the application has crashed and is in crash dump mode the state of the lock will be ignored to avoid unwanted deadlocks in the crash log code. Optionally notifies the profiler that this thread begins spinning because there is contention.