#include "pls/internal/base/tas_spin_lock.h" #include "pls/internal/base/backoff.h" namespace pls { namespace internal { namespace base { void tas_spin_lock::lock() { backoff backoff_strategy; while (true) { if (flag_.test_and_set(std::memory_order_acquire) == 0) { return; } backoff_strategy.do_backoff(); } } bool tas_spin_lock::try_lock(unsigned int num_tries) { backoff backoff_strategy; while (true) { if (flag_.test_and_set(std::memory_order_acquire) == 0) { return true; } num_tries--; if (num_tries <= 0) { return false; } backoff_strategy.do_backoff(); } } void tas_spin_lock::unlock() { flag_.clear(std::memory_order_release); } } } }