#include "pls/internal/base/spin_lock.h" namespace pls { namespace internal { namespace base { // TODO: Research/Measure how the memory fences/orders influence this. // For now we simply try to be safe by forcing this lock to // also act as a strict memory fence. void spin_lock::lock() { int tries = 0; while (flag_.test_and_set(std::memory_order_acquire)) { tries++; if (tries % yield_at_tries_ == 0) { this_thread::yield(); } } std::atomic_thread_fence(std::memory_order_seq_cst); } void spin_lock::unlock() { flag_.clear(std::memory_order_release); std::atomic_thread_fence(std::memory_order_seq_cst); } } } }