82extern "C" void _mm_pause(
void);
121 void lock() noexcept;
145 static constexpr
size_t kInterferenceSize = 64U;
147 alignas(kInterferenceSize)
std::atomic<
bool> flag_{
false};
195 constexpr static uint32_t kMaxSpinCount = 50000U;
197 uint32_t total_spin = 0;
198 uint16_t spin_count = 0;
199 uint16_t backoff = 1;
205 if (!flag_.exchange(
true, std::memory_order_acquire)) {
209 while (flag_.load(std::memory_order_relaxed)) {
212 if (++spin_count >= backoff) {
213 if VUNLIKELY (total_spin > kMaxSpinCount) {
214 VLOG_E(
"SpinLock: exceeded max spin count.");
215 std::this_thread::sleep_for(std::chrono::microseconds(10));
220 backoff = std::min<uint16_t>(backoff * 2U, 1024U);
231 if (flag_.load(std::memory_order_relaxed)) {
235 return !flag_.exchange(
true, std::memory_order_acquire);
SpinLockGuard(SpinLock &lock) noexcept
Acquires lock immediately.
Definition spin_lock.h:240
~SpinLockGuard() noexcept
Releases the lock held by this guard.
Definition spin_lock.h:242
Adaptive, cache-line-aligned spin lock.
Definition spin_lock.h:95
SpinLock() noexcept=default
Default constructor. Initialises the flag to false (unlocked).
void unlock() noexcept
Releases the lock.
Definition spin_lock.h:238
void lock() noexcept
Acquires the lock, spinning until successful.
Definition spin_lock.h:194
bool try_lock() noexcept
Attempts to acquire the lock without blocking.
Definition spin_lock.h:230
Global singleton logger with three output styles and pluggable backends.
#define VLOG_E(...)
Definition logger.h:854
Platform-independent macro definitions for the VLink library.
#define VUNLIKELY(...)
Shorthand alias for VLINK_UNLIKELY. Hints that the expression is unlikely true.
Definition macros.h:302
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184
VLINK_EXPORT void yield_cpu() noexcept
Emits a CPU pause/yield hint to reduce bus contention in busy-wait loops.
Definition utils.h:412
Platform-agnostic system utilities for process, thread, network and signal management.