mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-01-01 01:50:52 +01:00
49 lines
1.0 KiB
C
49 lines
1.0 KiB
C
|
// 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
|