Loading...
Searching...
No Matches
StateGuard.hpp
1#pragma once
2#include <atomic>
3#include <thread>
4
5namespace YSFX
6{
16{
17public:
19 [[nodiscard]] bool try_enter() noexcept
20 {
21 return !m_busy.exchange(true, std::memory_order_acquire);
22 }
23
25 void enter() noexcept
26 {
27 while(m_busy.exchange(true, std::memory_order_acquire))
28 std::this_thread::yield();
29 }
30
31 void leave() noexcept { m_busy.store(false, std::memory_order_release); }
32
33private:
34 std::atomic_bool m_busy{false};
35};
36
38{
39public:
40 explicit StateLock(StateGuard& g) noexcept
41 : m_guard{g}
42 {
43 m_guard.enter();
44 }
45 ~StateLock() { m_guard.leave(); }
46 StateLock(const StateLock&) = delete;
47 StateLock& operator=(const StateLock&) = delete;
48
49private:
50 StateGuard& m_guard;
51};
52}
Definition StateGuard.hpp:16
bool try_enter() noexcept
On the audio thread.
Definition StateGuard.hpp:19
void enter() noexcept
Anywhere else.
Definition StateGuard.hpp:25
Definition StateGuard.hpp:38