2025-04-06 14:18:43 +02:00
|
|
|
// Copyright 2025 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2025-03-27 00:35:24 -05:00
|
|
|
// class that allows threads to wait for a bool to take on a value.
|
2025-04-06 14:18:43 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2025-03-27 00:35:24 -05:00
|
|
|
#include <atomic>
|
2025-04-06 14:18:43 +02:00
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
class WaitableFlag final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Set(bool value = true)
|
|
|
|
{
|
2025-03-27 00:35:24 -05:00
|
|
|
m_flag.store(value, std::memory_order_release);
|
|
|
|
m_flag.notify_all();
|
2025-04-06 14:18:43 +02:00
|
|
|
}
|
|
|
|
|
2025-03-27 00:35:24 -05:00
|
|
|
void Wait(bool expected_value) { m_flag.wait(!expected_value, std::memory_order_acquire); }
|
2025-04-06 14:18:43 +02:00
|
|
|
|
2025-03-27 00:35:24 -05:00
|
|
|
// Note that this does not awake Wait'ing threads. Use Set(false) if that's needed.
|
|
|
|
void Reset() { m_flag.store(false, std::memory_order_relaxed); }
|
2025-04-06 14:18:43 +02:00
|
|
|
|
|
|
|
private:
|
2025-03-27 00:35:24 -05:00
|
|
|
std::atomic_bool m_flag{};
|
2025-04-06 14:18:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Common
|