VLink  2.0.0
A high-performance communication middleware
vlink::SpinLock Class Referencefinal

Adaptive spin lock satisfying the C++ Lockable named requirement. More...

#include <spin_lock.h>

Collaboration diagram for vlink::SpinLock:

Public Member Functions

 SpinLock () noexcept=default
 Constructs an unlocked spin lock. More...
 
 ~SpinLock () noexcept=default
 Destructor. Assumes the lock has already been released. More...
 
void lock () noexcept
 Acquires the lock, spinning with adaptive back-off until it becomes free. More...
 
bool try_lock () noexcept
 Attempts a single non-blocking acquire of the lock. More...
 
void unlock () noexcept
 Releases the lock with memory_order_release. More...
 

Detailed Description

Adaptive spin lock satisfying the C++ Lockable named requirement.

Cache-line aligned through alignas(64) to prevent false sharing. Composes with std::lock_guard, std::unique_lock and the supplied SpinLockGuard.

Constructor & Destructor Documentation

◆ SpinLock()

vlink::SpinLock::SpinLock ( )
defaultnoexcept

Constructs an unlocked spin lock.

◆ ~SpinLock()

vlink::SpinLock::~SpinLock ( )
defaultnoexcept

Destructor. Assumes the lock has already been released.

Member Function Documentation

◆ lock()

void vlink::SpinLock::lock ( )
inlinenoexcept

Acquires the lock, spinning with adaptive back-off until it becomes free.

Details.

The inner loop alternates exchange attempts with relaxed-load spins. Each back-off rung doubles the spin budget from 8 up to 1024 then yields the CPU. After 50000 spins the back-off action switches to a 10 us sleep and a one-time warning is emitted.

Warning
Recursive acquisition by the same thread deadlocks the lock.
Here is the call graph for this function:

◆ try_lock()

bool vlink::SpinLock::try_lock ( )
inlinenoexcept

Attempts a single non-blocking acquire of the lock.

Returns
true when the lock was successfully acquired.

◆ unlock()

void vlink::SpinLock::unlock ( )
inlinenoexcept

Releases the lock with memory_order_release.

May only be called by the thread that currently owns the lock.

Here is the caller graph for this function:

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