2017-08-20 21:04:49 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-08-20 21:04:49 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <queue>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include "Common/Event.h"
|
|
|
|
#include "Common/Flag.h"
|
2020-08-22 09:55:31 +00:00
|
|
|
#include "Common/Thread.h"
|
2017-08-20 21:04:49 +00:00
|
|
|
|
|
|
|
// A thread that executes the given function for every item placed into its queue.
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
class WorkQueueThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WorkQueueThread() = default;
|
|
|
|
WorkQueueThread(std::function<void(T)> function) { Reset(std::move(function)); }
|
|
|
|
~WorkQueueThread() { Shutdown(); }
|
|
|
|
void Reset(std::function<void(T)> function)
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
m_shutdown.Clear();
|
2020-08-16 00:33:30 +00:00
|
|
|
m_cancelled.Clear();
|
2017-08-20 21:04:49 +00:00
|
|
|
m_function = std::move(function);
|
2019-10-08 20:57:33 +00:00
|
|
|
m_thread = std::thread(&WorkQueueThread::ThreadLoop, this);
|
2017-08-20 21:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
void EmplaceItem(Args&&... args)
|
|
|
|
{
|
2020-08-16 00:33:30 +00:00
|
|
|
if (!m_cancelled.IsSet())
|
2017-08-20 21:04:49 +00:00
|
|
|
{
|
2019-10-08 20:57:33 +00:00
|
|
|
std::lock_guard lg(m_lock);
|
2017-08-20 21:06:33 +00:00
|
|
|
m_items.emplace(std::forward<Args>(args)...);
|
2017-08-20 21:04:49 +00:00
|
|
|
}
|
|
|
|
m_wakeup.Set();
|
|
|
|
}
|
|
|
|
|
2023-01-27 14:05:51 +00:00
|
|
|
void Push(T&& item)
|
|
|
|
{
|
|
|
|
if (!m_cancelled.IsSet())
|
|
|
|
{
|
|
|
|
std::lock_guard lg(m_lock);
|
|
|
|
m_items.push(item);
|
|
|
|
}
|
|
|
|
m_wakeup.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Push(const T& item)
|
|
|
|
{
|
|
|
|
if (!m_cancelled.IsSet())
|
|
|
|
{
|
|
|
|
std::lock_guard lg(m_lock);
|
|
|
|
m_items.push(item);
|
|
|
|
}
|
|
|
|
m_wakeup.Set();
|
|
|
|
}
|
|
|
|
|
2020-08-16 00:33:30 +00:00
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard lg(m_lock);
|
|
|
|
m_items = std::queue<T>();
|
|
|
|
}
|
|
|
|
m_wakeup.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel()
|
|
|
|
{
|
|
|
|
m_cancelled.Set();
|
|
|
|
Clear();
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCancelled() const { return m_cancelled.IsSet(); }
|
|
|
|
|
2017-08-20 21:04:49 +00:00
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
if (m_thread.joinable())
|
|
|
|
{
|
|
|
|
m_shutdown.Set();
|
|
|
|
m_wakeup.Set();
|
|
|
|
m_thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 06:48:07 +00:00
|
|
|
// Doesn't return until the most recent function invocation has finished.
|
|
|
|
void Flush()
|
|
|
|
{
|
|
|
|
if (m_thread.joinable())
|
|
|
|
{
|
|
|
|
m_flush.Set();
|
|
|
|
Clear();
|
|
|
|
m_flushed.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFlushing() const { return m_flush.IsSet() || m_shutdown.IsSet(); }
|
|
|
|
|
2022-04-01 04:39:35 +00:00
|
|
|
private:
|
2017-08-20 21:04:49 +00:00
|
|
|
void ThreadLoop()
|
|
|
|
{
|
2020-08-22 09:55:31 +00:00
|
|
|
Common::SetCurrentThreadName("WorkQueueThread");
|
|
|
|
|
2017-08-20 21:04:49 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
m_wakeup.Wait();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2019-10-08 20:57:33 +00:00
|
|
|
std::unique_lock lg(m_lock);
|
|
|
|
if (m_items.empty())
|
2023-01-23 06:48:07 +00:00
|
|
|
{
|
|
|
|
if (m_flush.IsSet())
|
|
|
|
{
|
|
|
|
m_flush.Clear();
|
|
|
|
m_flushed.Set();
|
|
|
|
}
|
2019-10-08 20:57:33 +00:00
|
|
|
break;
|
2023-01-23 06:48:07 +00:00
|
|
|
}
|
2019-10-08 20:57:33 +00:00
|
|
|
T item{std::move(m_items.front())};
|
|
|
|
m_items.pop();
|
|
|
|
lg.unlock();
|
|
|
|
|
2017-08-20 21:04:49 +00:00
|
|
|
m_function(std::move(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_shutdown.IsSet())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(T)> m_function;
|
|
|
|
std::thread m_thread;
|
|
|
|
Common::Event m_wakeup;
|
|
|
|
Common::Flag m_shutdown;
|
2020-08-16 00:33:30 +00:00
|
|
|
Common::Flag m_cancelled;
|
2023-01-23 06:48:07 +00:00
|
|
|
Common::Flag m_flush;
|
|
|
|
Common::Event m_flushed;
|
2017-08-20 21:04:49 +00:00
|
|
|
std::mutex m_lock;
|
|
|
|
std::queue<T> m_items;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Common
|