diff options
| author | Pierre Bourdon <delroth@gmail.com> | 2014-04-14 00:26:23 +0200 |
|---|---|---|
| committer | Pierre Bourdon <delroth@gmail.com> | 2014-04-14 10:54:07 +0200 |
| commit | f9fb39d383bb462293ea1104e29c5e6651e2d0e3 (patch) | |
| tree | ac797b01d1aa85bbf4a888fe16112b0458958b6e /Source/Core/Common | |
| parent | 1b9addd594536a76b42379306ce048b0d119973e (diff) | |
Common: Add a 'Flag' class that is used to encapsulate a boolean flag manipulated from several threads
Diffstat (limited to 'Source/Core/Common')
| -rw-r--r-- | Source/Core/Common/Flag.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Source/Core/Common/Flag.h b/Source/Core/Common/Flag.h new file mode 100644 index 0000000000..9b8fa3e28e --- /dev/null +++ b/Source/Core/Common/Flag.h @@ -0,0 +1,48 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +// Abstraction for a simple flag that can be toggled in a multithreaded way. +// It exposes a very simple API: +// * Set(bool = true): sets the Flag +// * IsSet(): tests if the flag is set +// * Clear(): clears the flag (equivalent to Set(false)). + +#pragma once + +#include <atomic> + +namespace Common { + +class Flag final +{ +public: + // Declared as explicit since we do not want "= true" to work on a flag + // object - it should be made explicit that a flag is *not* a normal + // variable. + explicit Flag(bool initial_value = false) : m_val(initial_value) {} + + void Set(bool val = true) + { + m_val.store(val); + } + + void Clear() + { + Set(false); + } + + bool IsSet() const + { + return m_val.load(); + } + +private: + // We are not using std::atomic_bool here because MSVC sucks as of VC++ + // 2013 and does not implement the std::atomic_bool(bool) constructor. + // + // Re-evaluate next time we upgrade that piece of shit. + std::atomic<bool> m_val; +}; + +} // namespace Common |
