MCPcopy Create free account
hub / github.com/bytedance/sonic-cpp / SpinLock

Class SpinLock

include/sonic/allocator.h:77–103  ·  view source on GitHub ↗

SpinLock is copied from https://rigtorp.se/spinlock/

Source from the content-addressed store, hash-verified

75
76// SpinLock is copied from https://rigtorp.se/spinlock/
77class SpinLock {
78 private:
79 std::atomic<bool> lock_ = {false};
80
81 public:
82 void lock() {
83 for (;;) {
84 if (!lock_.exchange(true, std::memory_order_acquire)) {
85 break;
86 }
87 while (lock_.load(std::memory_order_relaxed)) {
88 // use pause or yield instruction will slow down lock acquisition
89 // on contended locks.
90#ifndef SONIC_SPINLOCK_NO_PAUSE
91
92#if defined(__x86_64__) || defined(_M_AMD64)
93 __builtin_ia32_pause();
94#elif defined(__aarch64__) || defined(_M_ARM64)
95 asm volatile("yield");
96#endif
97
98#endif
99 }
100 }
101 }
102 void unlock() { lock_.store(false, std::memory_order_release); }
103};
104
105#ifdef SONIC_LOCKED_ALLOCATOR
106#define LOCK_GUARD std::lock_guard<SpinLock> guard(lock_);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected