dolphin/Source/Core/Common/WorkQueueThread.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

168 lines
3.9 KiB
C
Raw Normal View History

2017-08-20 21:04:49 +00:00
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2017-08-20 21:04:49 +00:00
#pragma once
#include <atomic>
#include <condition_variable>
2017-08-20 21:04:49 +00:00
#include <functional>
#include <queue>
#include <string>
2017-08-20 21:04:49 +00:00
#include <thread>
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(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(); }
// 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.
2017-08-20 21:04:49 +00:00
void Reset(std::function<void(T)> function)
{
Shutdown();
std::lock_guard lg(m_lock);
m_shutdown = false;
2017-08-20 21:04:49 +00:00
m_function = std::move(function);
m_thread = std::thread(&WorkQueueThread::ThreadLoop, this);
2017-08-20 21:04:49 +00:00
}
// Adds an item to the work queue
2017-08-20 21:04:49 +00:00
template <typename... Args>
void EmplaceItem(Args&&... args)
{
std::lock_guard lg(m_lock);
if (m_shutdown)
return;
m_items.emplace(std::forward<Args>(args)...);
m_idle = false;
m_worker_cond_var.notify_one();
2017-08-20 21:04:49 +00:00
}
// Adds an item to the work queue
2023-01-27 14:05:51 +00:00
void Push(T&& item)
{
std::lock_guard lg(m_lock);
if (m_shutdown)
return;
m_items.push(item);
m_idle = false;
m_worker_cond_var.notify_one();
2023-01-27 14:05:51 +00:00
}
// Adds an item to the work queue
2023-01-27 14:05:51 +00:00
void Push(const T& item)
{
std::lock_guard lg(m_lock);
if (m_shutdown)
return;
m_items.push(item);
m_idle = false;
m_worker_cond_var.notify_one();
}
// Empties the queue
// If the worker polls IsCanceling(), it can abort it's work when Cancelling
void Cancel()
{
std::unique_lock lg(m_lock);
if (m_shutdown)
return;
m_cancelling = true;
m_items = std::queue<T>();
m_worker_cond_var.notify_one();
}
// 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
void Shutdown(bool cancel = false)
2017-08-20 21:04:49 +00:00
{
{
std::unique_lock lg(m_lock);
if (m_shutdown || !m_thread.joinable())
return;
if (cancel)
{
m_cancelling = true;
m_items = std::queue<T>();
}
m_shutdown = true;
m_worker_cond_var.notify_one();
2017-08-20 21:04:49 +00:00
}
m_thread.join();
2017-08-20 21:04:49 +00:00
}
// Blocks until all items in the queue have been processed (or cancelled)
void WaitForCompletion()
2023-01-23 06:48:07 +00:00
{
std::unique_lock lg(m_lock);
if (m_idle) // Only check m_idle, we want this to work even another thread called Shutdown
return;
m_wait_cond_var.wait(lg, [&] { return m_idle; });
}
2023-01-23 06:48:07 +00:00
// For the worker to check if it should abort it's work early.
bool IsCancelling() const { return m_cancelling.load(); }
private:
2017-08-20 21:04:49 +00:00
void ThreadLoop()
{
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)
{
std::unique_lock lg(m_lock);
if (m_items.empty())
2017-08-20 21:04:49 +00:00
{
m_idle = true;
m_cancelling = false;
m_wait_cond_var.notify_all();
m_worker_cond_var.wait(
lg, [&] { return m_shutdown || m_cancelling.load() || !m_items.empty(); });
if (m_shutdown)
break;
continue;
2017-08-20 21:04:49 +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));
2017-08-20 21:04:49 +00:00
}
}
std::function<void(T)> m_function;
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;
std::condition_variable m_wait_cond_var;
std::condition_variable m_worker_cond_var;
std::atomic<bool> m_cancelling = false;
bool m_idle = true;
bool m_shutdown = false;
2017-08-20 21:04:49 +00:00
};
} // namespace Common