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 = 50000U;
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 (!flag_.exchange(
true, std::memory_order_acquire)) {
209 if (++spin_count >= backoff) {
210 if VUNLIKELY (total_spin > kMaxSpinCount) {
211 if (!warned_.exchange(
true, std::memory_order_relaxed)) {
212 VLOG_E(
"SpinLock: exceeded max spin count.");
214 std::this_thread::sleep_for(std::chrono::microseconds(10));
219 if (backoff < kMaxBackoff) {
220 backoff =
static_cast<uint16_t
>(backoff * 2U);
225 }
while (flag_.load(std::memory_order_relaxed));
230 if VUNLIKELY (flag_.load(std::memory_order_relaxed)) {
234 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:239
~SpinLockGuard() noexcept
Releases the held spin lock.
Definition: spin_lock.h:241
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:237
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:229
Singleton logger with stream / format / printf / RAII-stream entry points.
#define VLOG_E(...)
Definition: logger.h:789
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:289
#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:433
Portable host-system utility surface used across the VLink runtime.