2017-08-20 15:04:49 -06:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-08-20 15:04:49 -06:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-02-03 16:32:58 -07:00
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
2017-08-20 15:04:49 -06:00
|
|
|
#include <functional>
|
|
|
|
#include <queue>
|
2023-02-03 15:31:49 -07:00
|
|
|
#include <string>
|
2023-02-03 20:42:50 -07:00
|
|
|
#include <string_view>
|
2017-08-20 15:04:49 -06:00
|
|
|
#include <thread>
|
|
|
|
|
2020-08-22 03:55:31 -06:00
|
|
|
#include "Common/Thread.h"
|
2017-08-20 15:04:49 -06: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 19:56:27 -07:00
|
|
|
WorkQueueThread() = default;
|
2023-02-03 20:42:50 -07:00
|
|
|
WorkQueueThread(const std::string_view name, std::function<void(T)> function)
|
2023-02-03 15:31:49 -07:00
|
|
|
{
|
2023-02-03 20:42:50 -07:00
|
|
|
Reset(name, std::move(function));
|
2023-02-03 15:31:49 -07:00
|
|
|
}
|
2017-08-20 15:04:49 -06:00
|
|
|
~WorkQueueThread() { Shutdown(); }
|
2023-02-03 16:32:58 -07:00
|
|
|
|
|
|
|
// Shuts the current work thread down (if any) and starts a new thread with the given function
|
|
|
|
// Note: Some consumers of this API push items to the queue before starting the thread.
|
2023-02-03 20:42:50 -07:00
|
|
|
void Reset(const std::string_view name, std::function<void(T)> function)
|
2017-08-20 15:04:49 -06:00
|
|
|
{
|
|
|
|
Shutdown();
|
2023-01-29 09:07:36 -07:00
|
|
|
std::lock_guard lg(m_lock);
|
2023-02-03 20:42:50 -07:00
|
|
|
m_thread_name = name;
|
2023-02-03 16:32:58 -07:00
|
|
|
m_shutdown = false;
|
2017-08-20 15:04:49 -06:00
|
|
|
m_function = std::move(function);
|
2019-10-08 14:57:33 -06:00
|
|
|
m_thread = std::thread(&WorkQueueThread::ThreadLoop, this);
|
2017-08-20 15:04:49 -06:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:32:58 -07:00
|
|
|
// Adds an item to the work queue
|
2017-08-20 15:04:49 -06:00
|
|
|
template <typename... Args>
|
|
|
|
void EmplaceItem(Args&&... args)
|
|
|
|
{
|
2023-01-29 09:07:36 -07:00
|
|
|
std::lock_guard lg(m_lock);
|
|
|
|
if (m_shutdown)
|
|
|
|
return;
|
2023-02-03 16:32:58 -07:00
|
|
|
|
2023-01-29 09:07:36 -07:00
|
|
|
m_items.emplace(std::forward<Args>(args)...);
|
|
|
|
m_idle = false;
|
|
|
|
m_worker_cond_var.notify_one();
|
2017-08-20 15:04:49 -06:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:32:58 -07:00
|
|
|
// Adds an item to the work queue
|
2023-01-27 07:05:51 -07:00
|
|
|
void Push(T&& item)
|
|
|
|
{
|
2023-01-29 09:07:36 -07:00
|
|
|
std::lock_guard lg(m_lock);
|
2023-02-03 16:32:58 -07:00
|
|
|
if (m_shutdown)
|
2023-01-29 09:07:36 -07:00
|
|
|
return;
|
2023-02-03 16:32:58 -07:00
|
|
|
|
2023-02-03 20:42:50 -07:00
|
|
|
m_items.push(std::move(item));
|
2023-01-29 09:07:36 -07:00
|
|
|
m_idle = false;
|
|
|
|
m_worker_cond_var.notify_one();
|
2023-01-27 07:05:51 -07:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:32:58 -07:00
|
|
|
// Adds an item to the work queue
|
2023-01-27 07:05:51 -07:00
|
|
|
void Push(const T& item)
|
2020-08-15 18:33:30 -06:00
|
|
|
{
|
2023-01-29 09:07:36 -07:00
|
|
|
std::lock_guard lg(m_lock);
|
2023-02-03 16:32:58 -07:00
|
|
|
if (m_shutdown)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_items.push(item);
|
|
|
|
m_idle = false;
|
2023-01-29 09:07:36 -07:00
|
|
|
m_worker_cond_var.notify_one();
|
2020-08-15 18:33:30 -06:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:32:58 -07:00
|
|
|
// Empties the queue
|
|
|
|
// If the worker polls IsCanceling(), it can abort it's work when Cancelling
|
2020-08-15 18:33:30 -06:00
|
|
|
void Cancel()
|
|
|
|
{
|
2023-02-03 16:32:58 -07:00
|
|
|
std::unique_lock lg(m_lock);
|
|
|
|
if (m_shutdown)
|
2023-01-29 09:07:36 -07:00
|
|
|
return;
|
2020-08-15 18:33:30 -06:00
|
|
|
|
2023-02-03 16:32:58 -07:00
|
|
|
m_cancelling = true;
|
|
|
|
m_items = std::queue<T>();
|
|
|
|
m_worker_cond_var.notify_one();
|
2023-01-29 09:07:36 -07:00
|
|
|
}
|
2020-08-15 18:33:30 -06:00
|
|
|
|
2023-02-03 16:32:58 -07:00
|
|
|
// Tells the worker to shut down when it's queue is empty
|
|
|
|
// Blocks until the worker thread exits.
|
|
|
|
// If cancel is true, will Cancel before before telling the worker to exit
|
2023-02-04 21:17:16 -07:00
|
|
|
// Otherwise, all currently queued items will complete before the worker exits
|
2023-02-03 16:32:58 -07:00
|
|
|
void Shutdown(bool cancel = false)
|
2017-08-20 15:04:49 -06:00
|
|
|
{
|
|
|
|
{
|
2023-01-29 09:07:36 -07:00
|
|
|
std::unique_lock lg(m_lock);
|
2023-02-03 16:32:58 -07:00
|
|
|
if (m_shutdown || !m_thread.joinable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cancel)
|
|
|
|
{
|
|
|
|
m_cancelling = true;
|
|
|
|
m_items = std::queue<T>();
|
|
|
|
}
|
|
|
|
|
2023-01-29 09:07:36 -07:00
|
|
|
m_shutdown = true;
|
|
|
|
m_worker_cond_var.notify_one();
|
2017-08-20 15:04:49 -06:00
|
|
|
}
|
2023-02-03 16:32:58 -07:00
|
|
|
|
2023-01-29 09:07:36 -07:00
|
|
|
m_thread.join();
|
2017-08-20 15:04:49 -06:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:32:58 -07:00
|
|
|
// Blocks until all items in the queue have been processed (or cancelled)
|
|
|
|
void WaitForCompletion()
|
2023-01-22 23:48:07 -07:00
|
|
|
{
|
2023-01-29 09:07:36 -07:00
|
|
|
std::unique_lock lg(m_lock);
|
2023-02-04 21:17:16 -07:00
|
|
|
// don't check m_shutdown, because it gets set to request a shutdown, and we want to wait until
|
|
|
|
// after the shutdown completes.
|
|
|
|
// We also check m_cancelling, because we want to ensure the worker acknowledges our cancel.
|
|
|
|
if (m_idle && !m_cancelling.load())
|
2023-01-29 09:07:36 -07:00
|
|
|
return;
|
2023-01-29 08:16:29 -07:00
|
|
|
|
2023-03-20 09:31:40 -06:00
|
|
|
m_wait_cond_var.wait(lg, [&] { return m_idle && !m_cancelling; });
|
2023-01-29 09:07:36 -07:00
|
|
|
}
|
2023-01-22 23:48:07 -07:00
|
|
|
|
2023-02-03 20:42:50 -07:00
|
|
|
// If the worker polls IsCanceling(), it can abort its work when Cancelling
|
2023-02-03 16:32:58 -07:00
|
|
|
bool IsCancelling() const { return m_cancelling.load(); }
|
|
|
|
|
2022-03-31 22:39:35 -06:00
|
|
|
private:
|
2017-08-20 15:04:49 -06:00
|
|
|
void ThreadLoop()
|
|
|
|
{
|
2023-02-03 15:31:49 -07:00
|
|
|
Common::SetCurrentThreadName(m_thread_name.c_str());
|
2020-08-22 03:55:31 -06:00
|
|
|
|
2017-08-20 15:04:49 -06:00
|
|
|
while (true)
|
|
|
|
{
|
2023-01-29 09:07:36 -07:00
|
|
|
std::unique_lock lg(m_lock);
|
2023-02-04 21:17:16 -07:00
|
|
|
while (m_items.empty())
|
2017-08-20 15:04:49 -06:00
|
|
|
{
|
2023-01-29 09:07:36 -07:00
|
|
|
m_idle = true;
|
2023-02-03 16:32:58 -07:00
|
|
|
m_cancelling = false;
|
2023-01-29 09:07:36 -07:00
|
|
|
m_wait_cond_var.notify_all();
|
2023-02-04 21:17:16 -07:00
|
|
|
if (m_shutdown)
|
|
|
|
return;
|
|
|
|
|
2023-02-03 16:32:58 -07:00
|
|
|
m_worker_cond_var.wait(
|
2023-02-04 21:20:00 -07:00
|
|
|
lg, [&] { return !m_items.empty() || m_shutdown || m_cancelling.load(); });
|
2017-08-20 15:04:49 -06:00
|
|
|
}
|
2023-01-29 09:07:36 -07:00
|
|
|
T item{std::move(m_items.front())};
|
|
|
|
m_items.pop();
|
|
|
|
lg.unlock();
|
2017-08-20 15:04:49 -06:00
|
|
|
|
2023-01-29 09:07:36 -07:00
|
|
|
m_function(std::move(item));
|
2017-08-20 15:04:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(T)> m_function;
|
2023-02-03 15:31:49 -07:00
|
|
|
std::string m_thread_name;
|
2017-08-20 15:04:49 -06:00
|
|
|
std::thread m_thread;
|
|
|
|
std::mutex m_lock;
|
|
|
|
std::queue<T> m_items;
|
2023-01-29 09:07:36 -07:00
|
|
|
std::condition_variable m_wait_cond_var;
|
|
|
|
std::condition_variable m_worker_cond_var;
|
2023-02-03 16:32:58 -07:00
|
|
|
std::atomic<bool> m_cancelling = false;
|
2023-01-29 09:07:36 -07:00
|
|
|
bool m_idle = true;
|
|
|
|
bool m_shutdown = false;
|
2017-08-20 15:04:49 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Common
|