diff --git a/lib/base/atomic.hpp b/lib/base/atomic.hpp index c8f169c0b72..fe03ea5501e 100644 --- a/lib/base/atomic.hpp +++ b/lib/base/atomic.hpp @@ -41,6 +41,35 @@ class Atomic : public std::atomic { } }; +/** + * Classic spinlock mutex based on std::atomic_flag. + * + * @ingroup base + */ +class SpinMutex +{ +public: + SpinMutex() = default; + SpinMutex(const SpinMutex&) = delete; + SpinMutex(SpinMutex&&) = delete; + SpinMutex& operator=(const SpinMutex&) = delete; + SpinMutex& operator=(SpinMutex&&) = delete; + + void lock() noexcept + { + while (m_State.test_and_set(std::memory_order_acquire)) { + } + } + + void unlock() noexcept + { + m_State.clear(std::memory_order_release); + } + +private: + std::atomic_flag m_State = ATOMIC_FLAG_INIT; +}; + /** * Wraps any T into a std::atomic-like interface that locks using a mutex. *