score
Home
Classes
Namespaces
Files
Loading...
Searching...
No Matches
StateGuard.hpp
1
#pragma once
2
#include <atomic>
3
#include <thread>
4
5
namespace
YSFX
6
{
15
class
StateGuard
16
{
17
public
:
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
33
private
:
34
std::atomic_bool m_busy{
false
};
35
};
36
37
class
StateLock
38
{
39
public
:
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
49
private
:
50
StateGuard
& m_guard;
51
};
52
}
YSFX::StateGuard
Definition
StateGuard.hpp:16
YSFX::StateGuard::try_enter
bool try_enter() noexcept
On the audio thread.
Definition
StateGuard.hpp:19
YSFX::StateGuard::enter
void enter() noexcept
Anywhere else.
Definition
StateGuard.hpp:25
YSFX::StateLock
Definition
StateGuard.hpp:38