|
VLink 2.0.0
A high-performance communication middleware
|
Adaptive, cache-line-aligned spin lock and RAII guard. More...
#include <algorithm>#include <atomic>#include <cstdint>#include <thread>#include "./logger.h"#include "./macros.h"#include "./utils.h"Go to the source code of this file.
Classes | |
| class | vlink::SpinLock |
| Adaptive, cache-line-aligned spin lock. More... | |
| class | vlink::SpinLockGuard |
RAII guard that acquires a SpinLock on construction and releases it on destruction. More... | |
Namespaces | |
| namespace | vlink |
Adaptive, cache-line-aligned spin lock and RAII guard.
SpinLock implements a simple user-space mutex that spins instead of sleeping. It is intended for very short critical sections (a few CPU instructions) where the overhead of a std::mutex context switch would dominate. It is used inside CpuProfiler and other hot-path VLink internals.
Locking strategy inside lock():
exchange(true, acquire); if successful, return.load(relaxed) until the flag appears free, then retry.backoff spins, call Utils::yield_cpu() (a CPU-pause instruction).kMaxSpinCount (50000) is exceeded, sleep for 10 us and log an error. This is a safety valve for pathological contention.Cache-line alignment (alignas(64)) prevents false sharing when multiple SpinLock objects reside in adjacent memory.
std::mutex instead.SpinLock is not recursive; a thread that calls lock() twice will deadlock.VLINK_EXPORT because it is header-only and entirely inlined.