diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2018-12-29 13:27:21 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2019-01-16 07:48:18 +0100 |
commit | e9bbb68829e972df2e458aa5beec0568d4737f02 (patch) | |
tree | b305103d53214dcddb76c1f26738d334972b17d3 /compat/spinlock.hpp | |
parent | a1a9a091093c9fb711a1665de9f4d46ae5c5ab67 (diff) |
compat/spinlock: implement and use it
Diffstat (limited to 'compat/spinlock.hpp')
-rw-r--r-- | compat/spinlock.hpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/compat/spinlock.hpp b/compat/spinlock.hpp new file mode 100644 index 00000000..7e4cd8cf --- /dev/null +++ b/compat/spinlock.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "macros1.h" +#include <atomic> + +struct spinlock_guard final +{ + spinlock_guard(const spinlock_guard&) = delete; + spinlock_guard& operator=(const spinlock_guard&) = delete; + constexpr spinlock_guard(spinlock_guard&&) noexcept = default; + + cc_forceinline + spinlock_guard(std::atomic_flag* lock) noexcept : spinlock_guard(*lock) {} + + cc_forceinline + spinlock_guard(std::atomic_flag& lock) noexcept : lock(lock) + { + while (lock.test_and_set(std::memory_order_acquire)) + (void)0; + } + + cc_forceinline + ~spinlock_guard() noexcept + { + lock.clear(std::memory_order_release); + } + +private: + std::atomic_flag& lock; +}; |