2020-12-30 01:36:35 +01:00
|
|
|
// Copyright 2020 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2021-05-26 01:37:06 +02:00
|
|
|
#include <type_traits>
|
2020-12-30 01:36:35 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
|
|
|
|
2021-05-26 01:37:06 +02:00
|
|
|
#include "common/thread.h"
|
2021-04-01 06:05:45 +02:00
|
|
|
#include "common/unique_function.h"
|
|
|
|
|
2020-12-30 01:36:35 +01:00
|
|
|
namespace Common {
|
|
|
|
|
2021-05-26 01:37:06 +02: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-30 01:36:35 +01:00
|
|
|
public:
|
2021-05-26 01:37:06 +02:00
|
|
|
explicit StatefulThreadWorker(size_t num_workers, std::string name, StateMaker func = {})
|
|
|
|
: workers_queued{num_workers}, thread_name{std::move(name)} {
|
|
|
|
const auto lambda = [this, func] {
|
|
|
|
Common::SetCurrentThreadName(thread_name.c_str());
|
|
|
|
{
|
|
|
|
std::conditional_t<with_state, StateType, int> state{func()};
|
|
|
|
while (!stop) {
|
|
|
|
Task task;
|
|
|
|
{
|
|
|
|
std::unique_lock lock{queue_mutex};
|
|
|
|
if (requests.empty()) {
|
|
|
|
wait_condition.notify_all();
|
|
|
|
}
|
|
|
|
condition.wait(lock, [this] { return stop || !requests.empty(); });
|
|
|
|
if (stop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
task = std::move(requests.front());
|
|
|
|
requests.pop();
|
|
|
|
}
|
|
|
|
if constexpr (with_state) {
|
|
|
|
task(&state);
|
|
|
|
} else {
|
|
|
|
task();
|
|
|
|
}
|
|
|
|
++work_done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++workers_stopped;
|
|
|
|
wait_condition.notify_all();
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < num_workers; ++i) {
|
|
|
|
threads.emplace_back(lambda);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~StatefulThreadWorker() {
|
|
|
|
{
|
|
|
|
std::unique_lock lock{queue_mutex};
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
condition.notify_all();
|
|
|
|
for (std::thread& thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueueWork(Task work) {
|
|
|
|
{
|
|
|
|
std::unique_lock lock{queue_mutex};
|
|
|
|
requests.emplace(std::move(work));
|
|
|
|
++work_scheduled;
|
|
|
|
}
|
|
|
|
condition.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitForRequests() {
|
|
|
|
std::unique_lock lock{queue_mutex};
|
|
|
|
wait_condition.wait(lock, [this] {
|
|
|
|
return workers_stopped >= workers_queued || work_done >= work_scheduled;
|
|
|
|
});
|
|
|
|
}
|
2020-12-30 01:36:35 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::thread> threads;
|
2021-05-26 01:37:06 +02:00
|
|
|
std::queue<Task> requests;
|
2020-12-30 01:36:35 +01:00
|
|
|
std::mutex queue_mutex;
|
|
|
|
std::condition_variable condition;
|
2021-03-23 01:00:48 +01:00
|
|
|
std::condition_variable wait_condition;
|
2020-12-30 01:36:35 +01:00
|
|
|
std::atomic_bool stop{};
|
2021-05-26 01:37:06 +02: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;
|
2020-12-30 01:36:35 +01:00
|
|
|
};
|
|
|
|
|
2021-05-26 01:37:06 +02:00
|
|
|
using ThreadWorker = StatefulThreadWorker<>;
|
|
|
|
|
2020-12-30 01:36:35 +01:00
|
|
|
} // namespace Common
|