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>
|
2023-02-03 22:31:49 +00:00
|
|
|
#include <string>
|
2017-08-20 21:04:49 +00:00
|
|
|
#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:
|
2023-02-03 22:31:49 +00:00
|
|
|
WorkQueueThread(std::string name) : m_thread_name(name){};
|
|
|
|
WorkQueueThread(std::function<void(T)> function, std::string name) : m_thread_name(name)
|
|
|
|
{
|
|
|
|
Reset(std::move(function));
|
|
|
|
}
|
2017-08-20 21:04:49 +00:00
|
|
|
~WorkQueueThread() { Shutdown(); }
|
|
|
|
void Reset(std::function<void(T)> function)
|
|
|
|
{
|
|
|
|
Shutdown();
|
2023-01-29 16:07:36 +00:00
|
|
|
std::lock_guard lg(m_lock);
|
|
|
|
m_cancelled = false;
|
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)
|
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
std::lock_guard lg(m_lock);
|
|
|
|
if (m_shutdown)
|
2017-08-20 21:04:49 +00:00
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
return;
|
2017-08-20 21:04:49 +00:00
|
|
|
}
|
2023-01-29 16:07:36 +00:00
|
|
|
m_items.emplace(std::forward<Args>(args)...);
|
|
|
|
m_idle = false;
|
|
|
|
m_worker_cond_var.notify_one();
|
2017-08-20 21:04:49 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:05:51 +00:00
|
|
|
void Push(T&& item)
|
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
std::lock_guard lg(m_lock);
|
|
|
|
if (m_cancelled)
|
2023-01-27 14:05:51 +00:00
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
return;
|
2023-01-27 14:05:51 +00:00
|
|
|
}
|
2023-01-29 16:07:36 +00:00
|
|
|
m_items.push(item);
|
|
|
|
m_idle = false;
|
|
|
|
m_worker_cond_var.notify_one();
|
2023-01-27 14:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Push(const T& item)
|
|
|
|
{
|
|
|
|
std::lock_guard lg(m_lock);
|
2023-01-29 16:07:36 +00:00
|
|
|
if (m_cancelled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-01-27 14:05:51 +00:00
|
|
|
m_items.push(item);
|
2023-01-29 16:07:36 +00:00
|
|
|
m_idle = false;
|
|
|
|
m_worker_cond_var.notify_one();
|
2023-01-27 14:05:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 00:33:30 +00:00
|
|
|
void Clear()
|
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
std::lock_guard lg(m_lock);
|
|
|
|
m_items = std::queue<T>();
|
|
|
|
m_worker_cond_var.notify_one();
|
2020-08-16 00:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel()
|
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
if (!m_thread.joinable())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-08-16 00:33:30 +00:00
|
|
|
|
2023-01-29 16:07:36 +00:00
|
|
|
{
|
|
|
|
std::unique_lock lg(m_lock);
|
|
|
|
m_items = std::queue<T>();
|
|
|
|
m_cancelled = true;
|
|
|
|
m_shutdown = true;
|
|
|
|
m_worker_cond_var.notify_one();
|
|
|
|
}
|
|
|
|
m_thread.join();
|
|
|
|
}
|
2020-08-16 00:33:30 +00:00
|
|
|
|
2017-08-20 21:04:49 +00:00
|
|
|
void Shutdown()
|
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
if (!m_thread.joinable())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-20 21:04:49 +00:00
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
std::unique_lock lg(m_lock);
|
|
|
|
m_shutdown = true;
|
|
|
|
m_worker_cond_var.notify_one();
|
2017-08-20 21:04:49 +00:00
|
|
|
}
|
2023-01-29 16:07:36 +00:00
|
|
|
m_thread.join();
|
2017-08-20 21:04:49 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 06:48:07 +00:00
|
|
|
// Doesn't return until the most recent function invocation has finished.
|
2023-01-29 16:07:36 +00:00
|
|
|
void ClearAndFlush()
|
2023-01-23 06:48:07 +00:00
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
if (!m_thread.joinable())
|
2023-01-23 06:48:07 +00:00
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
return;
|
2023-01-23 06:48:07 +00:00
|
|
|
}
|
2023-01-29 16:07:36 +00:00
|
|
|
|
|
|
|
std::unique_lock lg(m_lock);
|
|
|
|
m_items = std::queue<T>();
|
|
|
|
m_wait_cond_var.wait(lg, [&] {
|
|
|
|
return m_idle;
|
|
|
|
});
|
2023-01-23 06:48:07 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 16:07:36 +00:00
|
|
|
// Doesn't return until the most recent function invocation has finished.
|
2023-01-29 15:16:29 +00:00
|
|
|
void Flush()
|
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
if (!m_thread.joinable())
|
2023-01-29 15:16:29 +00:00
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
return;
|
2023-01-29 15:16:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 16:07:36 +00:00
|
|
|
std::unique_lock lg(m_lock);
|
|
|
|
m_wait_cond_var.wait(lg, [&] {
|
|
|
|
return m_idle;
|
|
|
|
});
|
|
|
|
}
|
2023-01-23 06:48:07 +00:00
|
|
|
|
2022-04-01 04:39:35 +00:00
|
|
|
private:
|
2017-08-20 21:04:49 +00:00
|
|
|
void ThreadLoop()
|
|
|
|
{
|
2023-02-03 22:31:49 +00:00
|
|
|
Common::SetCurrentThreadName(m_thread_name.c_str());
|
2020-08-22 09:55:31 +00:00
|
|
|
|
2017-08-20 21:04:49 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
std::unique_lock lg(m_lock);
|
|
|
|
if (m_items.empty())
|
2017-08-20 21:04:49 +00:00
|
|
|
{
|
2023-01-29 16:07:36 +00:00
|
|
|
m_idle = true;
|
|
|
|
m_wait_cond_var.notify_all();
|
|
|
|
m_worker_cond_var.wait(lg, [&] {
|
|
|
|
return m_shutdown || !m_items.empty();
|
|
|
|
});
|
|
|
|
if (m_shutdown)
|
2023-01-23 06:48:07 +00:00
|
|
|
{
|
2019-10-08 20:57:33 +00:00
|
|
|
break;
|
2023-01-23 06:48:07 +00:00
|
|
|
}
|
2023-01-29 16:07:36 +00:00
|
|
|
continue;
|
2017-08-20 21:04:49 +00:00
|
|
|
}
|
2023-01-29 16:07:36 +00:00
|
|
|
T item{std::move(m_items.front())};
|
|
|
|
m_items.pop();
|
|
|
|
lg.unlock();
|
2017-08-20 21:04:49 +00:00
|
|
|
|
2023-01-29 16:07:36 +00:00
|
|
|
m_function(std::move(item));
|
2017-08-20 21:04:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(T)> m_function;
|
2023-02-03 22:31:49 +00:00
|
|
|
std::string m_thread_name;
|
2017-08-20 21:04:49 +00:00
|
|
|
std::thread m_thread;
|
|
|
|
std::mutex m_lock;
|
|
|
|
std::queue<T> m_items;
|
2023-01-29 16:07:36 +00:00
|
|
|
std::condition_variable m_wait_cond_var;
|
|
|
|
std::condition_variable m_worker_cond_var;
|
|
|
|
bool m_idle = true;
|
|
|
|
bool m_shutdown = false;
|
|
|
|
bool m_cancelled = false;
|
2017-08-20 21:04:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Common
|