VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
vlink::SpinLock Class Referencefinal

Adaptive, cache-line-aligned spin lock. More...

#include <spin_lock.h>

Collaboration diagram for vlink::SpinLock:

Public Member Functions

 SpinLock () noexcept=default
 Default constructor. Initialises the flag to false (unlocked).
 ~SpinLock () noexcept=default
 Destructor.
void lock () noexcept
 Acquires the lock, spinning until successful.
bool try_lock () noexcept
 Attempts to acquire the lock without blocking.
void unlock () noexcept
 Releases the lock.

Detailed Description

Adaptive, cache-line-aligned spin lock.

Implements the Lockable named requirement: lock(), try_lock(), and unlock(). Can be used directly with std::lock_guard.

Constructor & Destructor Documentation

◆ SpinLock()

vlink::SpinLock::SpinLock ( )
defaultnoexcept

Default constructor. Initialises the flag to false (unlocked).

Here is the caller graph for this function:

◆ ~SpinLock()

vlink::SpinLock::~SpinLock ( )
defaultnoexcept

Destructor.

Here is the call graph for this function:

Member Function Documentation

◆ lock()

void vlink::SpinLock::lock ( )
inlinenoexcept

Acquires the lock, spinning until successful.

Details.

Uses an exponential back-off strategy to reduce bus contention:

  • Spins with load(relaxed) for up to backoff iterations.
  • Calls Utils::yield_cpu() (PAUSE/WFE/yield) when backoff is reached.
  • Back-off doubles each round up to 1024.
  • After 50000 total spins, logs an error and sleeps 10 us.
Warning
This function must not be called recursively from the same thread; doing so will cause an infinite deadlock.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_lock()

bool vlink::SpinLock::try_lock ( )
inlinenoexcept

Attempts to acquire the lock without blocking.

Performs a single exchange(true, acquire). Returns immediately regardless of whether the lock was acquired.

Returns
true if the lock was successfully acquired, false if it was already held by another thread.
Here is the caller graph for this function:

◆ unlock()

void vlink::SpinLock::unlock ( )
inlinenoexcept

Releases the lock.

Stores false with release memory order. Must only be called by the thread that successfully called lock() or try_lock().

Here is the caller graph for this function:

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