DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
dspark::SpinLock Class Reference

A minimal, real-time safe spin lock with a TTAS wait loop. More...

#include <SpinLock.h>

Classes

class  ScopedLock
 RAII wrapper that acquires the lock on construction and releases on destruction. More...
 
class  ScopedTryLock
 RAII wrapper that tries to acquire the lock without blocking. More...
 

Public Member Functions

 SpinLock () noexcept=default
 Constructs an unlocked SpinLock.
 
 SpinLock (const SpinLock &)=delete
 
SpinLockoperator= (const SpinLock &)=delete
 
void lock () noexcept
 Acquires the lock, spinning until successful.
 
bool tryLock () noexcept
 Attempts to acquire the lock without waiting.
 
bool try_lock () noexcept
 Standard-library spelling of tryLock().
 
void unlock () noexcept
 Releases the lock, restoring it to the clear state.
 

Detailed Description

A minimal, real-time safe spin lock with a TTAS wait loop.

Fulfills the standard Lockable requirements (lock / try_lock / unlock), so it composes with std::lock_guard, std::unique_lock and std::scoped_lock.

  • lock() busy-waits using a Test-and-Test-and-Set (TTAS) pattern to avoid cache thrashing.
  • tryLock() attempts a single acquire without waiting: the right call on the audio thread.
  • unlock() releases the lock safely.

Definition at line 69 of file SpinLock.h.

Constructor & Destructor Documentation

◆ SpinLock() [1/2]

dspark::SpinLock::SpinLock ( )
defaultnoexcept

Constructs an unlocked SpinLock.

◆ SpinLock() [2/2]

dspark::SpinLock::SpinLock ( const SpinLock )
delete

Member Function Documentation

◆ lock()

void dspark::SpinLock::lock ( )
inlinenoexcept

Acquires the lock, spinning until successful.

Employs a TTAS (Test-and-Test-and-Set) loop: after a failed acquire it re-reads the flag with memory_order_relaxed (C++20 atomic_flag::test), keeping the cache line in a shared state and avoiding bus traffic until the lock appears free.

Note
Call only when the lock holder is guaranteed to release quickly.

Definition at line 88 of file SpinLock.h.

◆ operator=()

SpinLock & dspark::SpinLock::operator= ( const SpinLock )
delete

◆ try_lock()

bool dspark::SpinLock::try_lock ( )
inlinenoexcept

Standard-library spelling of tryLock().

Completes the C++ Lockable requirements so the lock works with std::unique_lock(std::try_to_lock) and multi-lock std::scoped_lock.

Definition at line 119 of file SpinLock.h.

◆ tryLock()

bool dspark::SpinLock::tryLock ( )
inlinenoexcept

Attempts to acquire the lock without waiting.

Returns
true if the lock was successfully acquired, false if it was already held.

Definition at line 108 of file SpinLock.h.

◆ unlock()

void dspark::SpinLock::unlock ( )
inlinenoexcept

Releases the lock, restoring it to the clear state.

Definition at line 124 of file SpinLock.h.


The documentation for this class was generated from the following file: