88 extern "C" void _mm_pause(
void);
124 void lock() noexcept;
142 static constexpr
size_t kInterferenceSize = 64U;
144 alignas(kInterferenceSize) std::atomic<
bool> flag_{
false};
145 std::atomic<bool> warned_{
false};
192 constexpr
static uint32_t kMaxSpinCount = 100000U;
194 constexpr
static uint16_t kInitialBackoff = 8U;
195 constexpr
static uint16_t kMaxBackoff = 1024U;
197 uint32_t total_spin = 0;
198 uint16_t spin_count = 0;
199 uint16_t backoff = kInitialBackoff;
202 if VLIKELY (!flag_.exchange(
true, std::memory_order_acquire)) {
210 if VUNLIKELY (total_spin > kMaxSpinCount) {
211 if VUNLIKELY (!warned_.load(std::memory_order_relaxed)) {
212 bool expected =
false;
214 if (warned_.compare_exchange_strong(expected,
true, std::memory_order_relaxed)) {
215 std::fputs(
"SpinLock: exceeded max spin count.\n", stderr);
220 std::this_thread::sleep_for(std::chrono::microseconds(10));
225 if (backoff < kMaxBackoff) {
226 backoff =
static_cast<uint16_t
>(backoff * 2U);
231 }
while (flag_.load(std::memory_order_relaxed));
236 if VUNLIKELY (flag_.load(std::memory_order_relaxed)) {
240 return !flag_.exchange(
true, std::memory_order_acquire);
RAII wrapper that locks a SpinLock on construction and unlocks on destruction.
Definition: spin_lock.h:167
SpinLockGuard(SpinLock &lock) noexcept
Acquires lock immediately.
Definition: spin_lock.h:245
~SpinLockGuard() noexcept
Releases the held spin lock.
Definition: spin_lock.h:247
Adaptive spin lock satisfying the C++ Lockable named requirement.
Definition: spin_lock.h:101
SpinLock() noexcept=default
Constructs an unlocked spin lock.
void unlock() noexcept
Releases the lock with memory_order_release.
Definition: spin_lock.h:243
void lock() noexcept
Acquires the lock, spinning with adaptive back-off until it becomes free.
Definition: spin_lock.h:191
bool try_lock() noexcept
Attempts a single non-blocking acquire of the lock.
Definition: spin_lock.h:235
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:292
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
VLINK_EXPORT void yield_cpu() noexcept
Emits the most efficient CPU pause/yield hint for the host ISA.
Definition: utils.h:482
Portable host-system utility surface used across the VLink runtime.