2015-05-27 20:53:09 +02:00
|
|
|
// Copyright 2015 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-05-27 20:53:09 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include "Common/Event.h"
|
|
|
|
#include "Common/Flag.h"
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
// This class provides a synchronized loop.
|
|
|
|
// It's a thread-safe way to trigger a new iteration without busy loops.
|
|
|
|
// It's optimized for high-usage iterations which usually are already running while it's triggered
|
|
|
|
// often.
|
2015-06-05 19:28:11 +02:00
|
|
|
// Be careful when using Wait() and Wakeup() at the same time. Wait() may block forever while
|
|
|
|
// Wakeup() is called regularly.
|
2015-05-27 20:53:09 +02:00
|
|
|
class BlockingLoop
|
|
|
|
{
|
|
|
|
public:
|
2023-06-05 05:25:20 +02:00
|
|
|
enum class StopMode
|
2017-06-22 14:42:14 +02:00
|
|
|
{
|
2023-06-05 05:25:20 +02:00
|
|
|
NonBlock,
|
|
|
|
Block,
|
|
|
|
BlockAndGiveUp,
|
2017-06-22 14:42:14 +02:00
|
|
|
};
|
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
BlockingLoop() { m_stopped.Set(); }
|
2023-06-05 05:25:20 +02:00
|
|
|
~BlockingLoop() { Stop(StopMode::BlockAndGiveUp); }
|
2015-05-27 20:53:09 +02:00
|
|
|
// Triggers to rerun the payload of the Run() function at least once again.
|
|
|
|
// This function will never block and is designed to finish as fast as possible.
|
|
|
|
void Wakeup()
|
|
|
|
{
|
2015-05-30 02:42:32 +02:00
|
|
|
// Already running, so no need for a wakeup.
|
|
|
|
// This is the common case, so try to get this as fast as possible.
|
|
|
|
if (m_running_state.load() >= STATE_NEED_EXECUTION)
|
2015-05-27 20:53:09 +02:00
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
// Mark that new data is available. If the old state will rerun the payload
|
|
|
|
// itself, we don't have to set the event to interrupt the worker.
|
|
|
|
if (m_running_state.exchange(STATE_NEED_EXECUTION) != STATE_SLEEPING)
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
// Else as the worker thread may sleep now, we have to set the event.
|
2015-05-27 20:53:09 +02:00
|
|
|
m_new_work_event.Set();
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
// Wait for a complete payload run after the last Wakeup() call.
|
|
|
|
// If stopped, this returns immediately.
|
|
|
|
void Wait()
|
|
|
|
{
|
2015-05-30 02:42:32 +02:00
|
|
|
// already done
|
2015-06-03 23:21:46 +02:00
|
|
|
if (IsDone())
|
2015-05-27 20:53:09 +02:00
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
// notifying this event will only wake up one thread, so use a mutex here to
|
|
|
|
// allow only one waiting thread. And in this way, we get an event free wakeup
|
|
|
|
// but for the first thread for free
|
|
|
|
std::lock_guard<std::mutex> lk(m_wait_lock);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
// Wait for the worker thread to finish.
|
2015-06-03 23:21:46 +02:00
|
|
|
while (!IsDone())
|
2015-05-27 20:53:09 +02:00
|
|
|
{
|
|
|
|
m_done_event.Wait();
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
// As we wanted to wait for the other thread, there is likely no work remaining.
|
|
|
|
// So there is no need for a busy loop any more.
|
|
|
|
m_may_sleep.Set();
|
2015-05-27 20:53:09 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-11-10 17:55:21 +01:00
|
|
|
// Wait for a complete payload run after the last Wakeup() call.
|
|
|
|
// This version will call a yield function every 100ms.
|
|
|
|
// If stopped, this returns immediately.
|
|
|
|
template <class Rep, class Period, typename Functor>
|
|
|
|
void WaitYield(const std::chrono::duration<Rep, Period>& rel_time, Functor yield_func)
|
|
|
|
{
|
|
|
|
// already done
|
|
|
|
if (IsDone())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// notifying this event will only wake up one thread, so use a mutex here to
|
|
|
|
// allow only one waiting thread. And in this way, we get an event free wakeup
|
|
|
|
// but for the first thread for free
|
|
|
|
std::lock_guard<std::mutex> lk(m_wait_lock);
|
|
|
|
|
|
|
|
// Wait for the worker thread to finish.
|
|
|
|
while (!IsDone())
|
|
|
|
{
|
|
|
|
if (!m_done_event.WaitFor(rel_time))
|
|
|
|
yield_func();
|
|
|
|
}
|
|
|
|
|
|
|
|
// As we wanted to wait for the other thread, there is likely no work remaining.
|
|
|
|
// So there is no need for a busy loop any more.
|
|
|
|
m_may_sleep.Set();
|
|
|
|
}
|
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
// Half start the worker.
|
2015-05-30 02:42:32 +02:00
|
|
|
// So this object is in a running state and Wait() will block until the worker calls Run().
|
2015-06-05 19:28:11 +02:00
|
|
|
// This may be called from any thread and is supposed to be called at least once before Wait() is
|
|
|
|
// used.
|
2015-05-27 20:53:09 +02:00
|
|
|
void Prepare()
|
|
|
|
{
|
|
|
|
// There is a race condition if the other threads call this function while
|
|
|
|
// the loop thread is initializing. Using this lock will ensure a valid state.
|
|
|
|
std::lock_guard<std::mutex> lk(m_prepare_lock);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
if (!m_stopped.TestAndClear())
|
|
|
|
return;
|
2015-05-30 02:42:32 +02:00
|
|
|
m_running_state.store(
|
|
|
|
STATE_LAST_EXECUTION); // so the payload will only be executed once without any Wakeup call
|
2015-05-27 20:53:09 +02:00
|
|
|
m_shutdown.Clear();
|
|
|
|
m_may_sleep.Set();
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-05 19:28:11 +02:00
|
|
|
// Main loop of this object.
|
2015-05-27 20:53:09 +02:00
|
|
|
// The payload callback is called at least as often as it's needed to match the Wakeup()
|
|
|
|
// requirements.
|
2015-06-05 19:28:11 +02:00
|
|
|
// The optional timeout parameter is a timeout for how periodically the payload should be called.
|
2015-05-30 02:42:32 +02:00
|
|
|
// Use timeout = 0 to run without a timeout at all.
|
|
|
|
template <class F>
|
|
|
|
void Run(F payload, int64_t timeout = 0)
|
2015-05-27 20:53:09 +02:00
|
|
|
{
|
2015-05-30 02:42:32 +02:00
|
|
|
// Asserts that Prepare is called at least once before we enter the loop.
|
|
|
|
// But a good implementation should call this before already.
|
2015-05-27 20:53:09 +02:00
|
|
|
Prepare();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
while (!m_shutdown.IsSet())
|
|
|
|
{
|
|
|
|
payload();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
switch (m_running_state.load())
|
2015-05-27 20:53:09 +02:00
|
|
|
{
|
2015-05-30 02:42:32 +02:00
|
|
|
case STATE_NEED_EXECUTION:
|
|
|
|
// We won't get notified while we are in the STATE_NEED_EXECUTION state, so maybe Wakeup was
|
|
|
|
// called.
|
|
|
|
// So we have to assume on finishing the STATE_NEED_EXECUTION state, that there may be some
|
|
|
|
// remaining tasks.
|
|
|
|
// To process this tasks, we call the payload again within the STATE_LAST_EXECUTION state.
|
|
|
|
m_running_state--;
|
|
|
|
break;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
case STATE_LAST_EXECUTION:
|
2015-06-05 19:28:11 +02:00
|
|
|
// If we're still in the STATE_LAST_EXECUTION state, then Wakeup wasn't called within the
|
|
|
|
// last
|
|
|
|
// execution of the payload. This means we should be ready now.
|
|
|
|
// But bad luck, Wakeup may have been called right now. So break and rerun the payload
|
|
|
|
// if the state was touched.
|
2015-05-30 02:42:32 +02:00
|
|
|
if (m_running_state-- != STATE_LAST_EXECUTION)
|
|
|
|
break;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
// Else we're likely in the STATE_DONE state now, so wakeup the waiting threads right now.
|
|
|
|
// However, if we're not in the STATE_DONE state any more, the event should also be
|
|
|
|
// triggered so that we'll skip the next waiting call quite fast.
|
|
|
|
m_done_event.Set();
|
2021-04-19 23:34:46 +02:00
|
|
|
[[fallthrough]];
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
case STATE_DONE:
|
|
|
|
// We're done now. So time to check if we want to sleep or if we want to stay in a busy
|
|
|
|
// loop.
|
|
|
|
if (m_may_sleep.TestAndClear())
|
|
|
|
{
|
|
|
|
// Try to set the sleeping state.
|
|
|
|
if (m_running_state-- != STATE_DONE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Busy loop.
|
|
|
|
break;
|
|
|
|
}
|
2021-04-19 23:34:46 +02:00
|
|
|
[[fallthrough]];
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
case STATE_SLEEPING:
|
|
|
|
// Just relax
|
|
|
|
if (timeout > 0)
|
|
|
|
{
|
|
|
|
m_new_work_event.WaitFor(std::chrono::milliseconds(timeout));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_new_work_event.Wait();
|
|
|
|
}
|
|
|
|
break;
|
2015-05-27 20:53:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
// Shutdown down, so get a safe state
|
|
|
|
m_running_state.store(STATE_DONE);
|
2015-05-27 20:53:09 +02:00
|
|
|
m_stopped.Set();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
// Wake up the last Wait calls.
|
2015-05-27 20:53:09 +02:00
|
|
|
m_done_event.Set();
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-05 19:28:11 +02:00
|
|
|
// Quits the main loop.
|
|
|
|
// By default, it will wait until the main loop quits.
|
2015-05-30 02:42:32 +02:00
|
|
|
// Be careful to not use the blocking way within the payload of the Run() method.
|
2023-06-05 05:25:20 +02:00
|
|
|
void Stop(StopMode mode = StopMode::Block)
|
2016-06-24 10:43:46 +02:00
|
|
|
{
|
2015-05-30 02:42:32 +02:00
|
|
|
if (m_stopped.IsSet())
|
2016-06-24 10:43:46 +02:00
|
|
|
return;
|
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
m_shutdown.Set();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
// We have to interrupt the sleeping call to let the worker shut down soon.
|
|
|
|
Wakeup();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-06-22 14:42:14 +02:00
|
|
|
switch (mode)
|
|
|
|
{
|
2023-06-05 05:25:20 +02:00
|
|
|
case StopMode::NonBlock:
|
2017-06-27 08:33:45 +02:00
|
|
|
break;
|
2023-06-05 05:25:20 +02:00
|
|
|
case StopMode::Block:
|
2016-06-24 10:43:46 +02:00
|
|
|
Wait();
|
2017-06-22 14:42:14 +02:00
|
|
|
break;
|
2023-06-05 05:25:20 +02:00
|
|
|
case StopMode::BlockAndGiveUp:
|
2017-06-22 14:42:14 +02:00
|
|
|
WaitYield(std::chrono::milliseconds(100), [&] {
|
|
|
|
// If timed out, assume no one will come along to call Run, so force a break
|
|
|
|
m_stopped.Set();
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
}
|
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
bool IsRunning() const { return !m_stopped.IsSet() && !m_shutdown.IsSet(); }
|
2015-06-03 23:21:46 +02:00
|
|
|
bool IsDone() const { return m_stopped.IsSet() || m_running_state.load() <= STATE_DONE; }
|
2015-06-05 19:28:11 +02:00
|
|
|
// This function should be triggered regularly over time so
|
|
|
|
// that we will fall back from the busy loop to sleeping.
|
2015-05-27 20:53:09 +02:00
|
|
|
void AllowSleep() { m_may_sleep.Set(); }
|
2018-04-12 14:18:04 +02:00
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
private:
|
|
|
|
std::mutex m_wait_lock;
|
|
|
|
std::mutex m_prepare_lock;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-05 19:28:11 +02:00
|
|
|
Flag m_stopped; // If this is set, Wait() shall not block.
|
|
|
|
Flag m_shutdown; // If this is set, the loop shall end.
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-27 20:53:09 +02:00
|
|
|
Event m_new_work_event;
|
|
|
|
Event m_done_event;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-05-30 02:42:32 +02:00
|
|
|
enum RUNNING_TYPE
|
|
|
|
{
|
|
|
|
STATE_SLEEPING = 0,
|
|
|
|
STATE_DONE = 1,
|
|
|
|
STATE_LAST_EXECUTION = 2,
|
|
|
|
STATE_NEED_EXECUTION = 3
|
|
|
|
};
|
|
|
|
std::atomic<int> m_running_state; // must be of type RUNNING_TYPE
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-05 19:28:11 +02:00
|
|
|
Flag m_may_sleep; // If this is set, we fall back from the busy loop to an event based
|
|
|
|
// synchronization.
|
2015-05-27 20:53:09 +02:00
|
|
|
};
|
2019-05-06 01:48:12 +02:00
|
|
|
} // namespace Common
|