-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlipFlop.h
More file actions
53 lines (45 loc) · 1.26 KB
/
Copy pathFlipFlop.h
File metadata and controls
53 lines (45 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include "SpinLock.h"
namespace Utility
{
/**
* FlipFlop.
* Race condition arbitration between two cooperating events,
* represented as boolean transitions from false to true.
*
* Each event is informed of the status of the other. Exactly
* one of the calls to flip() and flop() will return false
* (the race winner), and the other, true (the loser).
*/
class FlipFlop
{
public:
FlipFlop() = default;
~FlipFlop() noexcept = default;
FlipFlop(FlipFlop const&) = delete;
FlipFlop& operator=( FlipFlop const& ) = delete;
bool flip()
{
SpinLock _lock(flag_);
flip_ = true;
return flop_;
}
bool flop()
{
SpinLock _lock(flag_);
flop_ = true;
return flip_;
}
bool reset( bool force = false )
{
SpinLock _lock(flag_);
if ( !force and (!flip_ or !flop_) ) { return false; }
flip_ = flop_ = false;
return true;
}
private:
std::atomic_flag flag_{ATOMIC_FLAG_INIT};
bool flip_{false};
bool flop_{false};
};
} // namespace Utility