SpinLock is copied from https://rigtorp.se/spinlock/
| 75 | |
| 76 | // SpinLock is copied from https://rigtorp.se/spinlock/ |
| 77 | class 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_); |
nothing calls this directly
no outgoing calls
no test coverage detected