1
0
Fork 0
forked from suyu/suyu
suyu/src/common/thread_worker.cpp

67 lines
1.8 KiB
C++
Raw Normal View History

// Copyright 2020 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "common/thread.h"
#include "common/thread_worker.h"
namespace Common {
ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) {
2021-04-06 04:23:02 +02:00
workers_queued.store(static_cast<u64>(num_workers), std::memory_order_release);
const auto lambda = [this, thread_name{std::string{name}}] {
Common::SetCurrentThreadName(thread_name.c_str());
while (!stop) {
UniqueFunction<void> task;
{
std::unique_lock lock{queue_mutex};
if (requests.empty()) {
wait_condition.notify_all();
}
condition.wait(lock, [this] { return stop || !requests.empty(); });
2021-04-06 06:02:44 +02:00
if (stop) {
break;
}
task = std::move(requests.front());
requests.pop();
}
task();
2021-04-06 04:23:02 +02:00
work_done++;
}
2021-04-06 04:23:02 +02:00
workers_stopped++;
wait_condition.notify_all();
};
for (size_t i = 0; i < num_workers; ++i) {
threads.emplace_back(lambda);
}
}
ThreadWorker::~ThreadWorker() {
{
std::unique_lock lock{queue_mutex};
stop = true;
}
condition.notify_all();
for (std::thread& thread : threads) {
thread.join();
}
}
void ThreadWorker::QueueWork(UniqueFunction<void> work) {
{
std::unique_lock lock{queue_mutex};
requests.emplace(std::move(work));
2021-04-06 04:23:02 +02:00
work_scheduled++;
}
condition.notify_one();
}
void ThreadWorker::WaitForRequests() {
std::unique_lock lock{queue_mutex};
2021-04-06 04:23:02 +02:00
wait_condition.wait(
lock, [this] { return workers_stopped >= workers_queued || work_done >= work_scheduled; });
}
} // namespace Common