Debian Codegraph @

Source/Core/Common/Flag.h

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 // Abstraction for a simple flag that can be toggled in a multithreaded way.
5 //
6 // Simple API:
7 // * Set(bool = true): sets the Flag
8 // * IsSet(): tests if the flag is set
9 // * Clear(): clears the flag (equivalent to Set(false)).
10 //
11 // More advanced features:
12 // * TestAndSet(bool = true): sets the flag to the given value. If a change was
13 // needed (the flag did not already have this value)
14 // the function returns true. Else, false.
15 // * TestAndClear(): alias for TestAndSet(false).
16
17 #pragma once
18
19 #include <atomic>
20
21 namespace Common
22 {
23 class Flag final
24 {
25 public:
26 // Declared as explicit since we do not want "= true" to work on a flag
27 // object - it should be made explicit that a flag is *not* a normal
28 // variable.
29 explicit Flag(bool initial_value = false) : m_val(initial_value) {}
30 void Set(bool val = true) { m_val.store(val); }
31 void Clear() { Set(false); }
32 bool IsSet() const { return m_val.load(); }
33 bool TestAndSet(bool val = true)
34 {
35 bool expected = !val;
36 return m_val.compare_exchange_strong(expected, val);
37 }
38
39 bool TestAndClear() { return TestAndSet(false); }
40
41 private:
42 std::atomic_bool m_val;
43 };
44
45 } // namespace Common