#include "pls/internal/helpers/profiler.h" #include "pls/internal/base/tas_spin_lock.h" namespace pls { namespace internal { namespace base { void tas_spin_lock::lock() { PROFILE_LOCK("Acquire Lock") int tries = 0; while (true) { tries++; if (tries % yield_at_tries_ == 0) { this_thread::yield(); } if (flag_.test_and_set(std::memory_order_acquire) == 0) { return; } } } bool tas_spin_lock::try_lock(unsigned int num_tries) { PROFILE_LOCK("Try Acquire Lock") while (true) { num_tries--; if (num_tries <= 0) { return false; } if (flag_.test_and_set(std::memory_order_acquire) == 0) { return true; } } } void tas_spin_lock::unlock() { PROFILE_LOCK("Unlock") flag_.clear(std::memory_order_release); } } } }