2022-04-23 01:59:50 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-12-29 16:36:35 -08:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
2021-04-05 19:25:22 -07:00
|
|
|
#include <condition_variable>
|
2020-12-29 16:36:35 -08:00
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
2021-06-28 21:47:13 -07:00
|
|
|
#include <stop_token>
|
2020-12-29 16:36:35 -08:00
|
|
|
#include <string>
|
2021-06-28 21:47:13 -07:00
|
|
|
#include <thread>
|
2021-05-25 16:37:06 -07:00
|
|
|
#include <type_traits>
|
2020-12-29 16:36:35 -08:00
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
|
|
|
|
2021-05-25 16:37:06 -07:00
|
|
|
#include "common/thread.h"
|
2021-03-31 21:05:45 -07:00
|
|
|
#include "common/unique_function.h"
|
|
|
|
|
2020-12-29 16:36:35 -08:00
|
|
|
namespace Common {
|
|
|
|
|
2021-05-25 16:37:06 -07:00
|
|
|
template <class StateType = void>
|
|
|
|
class StatefulThreadWorker {
|
|
|
|
static constexpr bool with_state = !std::is_same_v<StateType, void>;
|
|
|
|
|
|
|
|
struct DummyCallable {
|
|
|
|
int operator()() const noexcept {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using Task =
|
|
|
|
std::conditional_t<with_state, UniqueFunction<void, StateType*>, UniqueFunction<void>>;
|
|
|
|
using StateMaker = std::conditional_t<with_state, std::function<StateType()>, DummyCallable>;
|
|
|
|
|
2020-12-29 16:36:35 -08:00
|
|
|
public:
|
2021-05-25 16:37:06 -07:00
|
|
|
explicit StatefulThreadWorker(size_t num_workers, std::string name, StateMaker func = {})
|
|
|
|
: workers_queued{num_workers}, thread_name{std::move(name)} {
|
2021-06-28 21:47:13 -07:00
|
|
|
const auto lambda = [this, func](std::stop_token stop_token) {
|
2021-05-25 16:37:06 -07:00
|
|
|
Common::SetCurrentThreadName(thread_name.c_str());
|
|
|
|
{
|
2021-07-09 10:59:09 -07:00
|
|
|
[[maybe_unused]] std::conditional_t<with_state, StateType, int> state{func()};
|
2021-06-28 21:47:13 -07:00
|
|
|
while (!stop_token.stop_requested()) {
|
2021-05-25 16:37:06 -07:00
|
|
|
Task task;
|
|
|
|
{
|
|
|
|
std::unique_lock lock{queue_mutex};
|
|
|
|
if (requests.empty()) {
|
|
|
|
wait_condition.notify_all();
|
|
|
|
}
|
2021-06-28 21:47:13 -07:00
|
|
|
condition.wait(lock, stop_token, [this] { return !requests.empty(); });
|
|
|
|
if (stop_token.stop_requested()) {
|
2021-05-25 16:37:06 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
task = std::move(requests.front());
|
|
|
|
requests.pop();
|
|
|
|
}
|
|
|
|
if constexpr (with_state) {
|
|
|
|
task(&state);
|
|
|
|
} else {
|
|
|
|
task();
|
|
|
|
}
|
|
|
|
++work_done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++workers_stopped;
|
|
|
|
wait_condition.notify_all();
|
|
|
|
};
|
2021-06-28 21:47:13 -07:00
|
|
|
threads.reserve(num_workers);
|
2021-05-25 16:37:06 -07:00
|
|
|
for (size_t i = 0; i < num_workers; ++i) {
|
|
|
|
threads.emplace_back(lambda);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:47:13 -07:00
|
|
|
StatefulThreadWorker& operator=(const StatefulThreadWorker&) = delete;
|
|
|
|
StatefulThreadWorker(const StatefulThreadWorker&) = delete;
|
|
|
|
|
|
|
|
StatefulThreadWorker& operator=(StatefulThreadWorker&&) = delete;
|
|
|
|
StatefulThreadWorker(StatefulThreadWorker&&) = delete;
|
2021-05-25 16:37:06 -07:00
|
|
|
|
|
|
|
void QueueWork(Task work) {
|
|
|
|
{
|
|
|
|
std::unique_lock lock{queue_mutex};
|
|
|
|
requests.emplace(std::move(work));
|
|
|
|
++work_scheduled;
|
|
|
|
}
|
|
|
|
condition.notify_one();
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:47:13 -07:00
|
|
|
void WaitForRequests(std::stop_token stop_token = {}) {
|
|
|
|
std::stop_callback callback(stop_token, [this] {
|
|
|
|
for (auto& thread : threads) {
|
|
|
|
thread.request_stop();
|
|
|
|
}
|
|
|
|
});
|
2021-05-25 16:37:06 -07:00
|
|
|
std::unique_lock lock{queue_mutex};
|
|
|
|
wait_condition.wait(lock, [this] {
|
|
|
|
return workers_stopped >= workers_queued || work_done >= work_scheduled;
|
|
|
|
});
|
|
|
|
}
|
2020-12-29 16:36:35 -08:00
|
|
|
|
|
|
|
private:
|
2021-05-25 16:37:06 -07:00
|
|
|
std::queue<Task> requests;
|
2020-12-29 16:36:35 -08:00
|
|
|
std::mutex queue_mutex;
|
2021-06-28 21:47:13 -07:00
|
|
|
std::condition_variable_any condition;
|
2021-03-22 17:00:48 -07:00
|
|
|
std::condition_variable wait_condition;
|
2021-05-25 16:37:06 -07:00
|
|
|
std::atomic<size_t> work_scheduled{};
|
|
|
|
std::atomic<size_t> work_done{};
|
|
|
|
std::atomic<size_t> workers_stopped{};
|
|
|
|
std::atomic<size_t> workers_queued{};
|
|
|
|
std::string thread_name;
|
2021-06-28 21:47:13 -07:00
|
|
|
std::vector<std::jthread> threads;
|
2020-12-29 16:36:35 -08:00
|
|
|
};
|
|
|
|
|
2021-05-25 16:37:06 -07:00
|
|
|
using ThreadWorker = StatefulThreadWorker<>;
|
|
|
|
|
2020-12-29 16:36:35 -08:00
|
|
|
} // namespace Common
|