summaryrefslogtreecommitdiff
path: root/src/common/thread_worker.cpp
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2021-05-25 20:37:06 -0300
committerGravatar ReinUsesLisp2021-07-08 19:03:26 -0300
commitda34d3704405665b68d3d992f37a7eeb541238af (patch)
tree69ac607431b72f5088c6d57a0348cedb5fdcb835 /src/common/thread_worker.cpp
parentcommon/thread_worker: Simplify logic (diff)
downloadyuzu-da34d3704405665b68d3d992f37a7eeb541238af.tar.gz
yuzu-da34d3704405665b68d3d992f37a7eeb541238af.tar.xz
yuzu-da34d3704405665b68d3d992f37a7eeb541238af.zip
common/thread_worker: Add support for stateful threads
Diffstat (limited to 'src/common/thread_worker.cpp')
-rw-r--r--src/common/thread_worker.cpp66
1 files changed, 0 insertions, 66 deletions
diff --git a/src/common/thread_worker.cpp b/src/common/thread_worker.cpp
deleted file mode 100644
index 32be49b15..000000000
--- a/src/common/thread_worker.cpp
+++ /dev/null
@@ -1,66 +0,0 @@
1// Copyright 2020 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/thread.h"
6#include "common/thread_worker.h"
7
8namespace Common {
9
10ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) {
11 workers_queued.store(static_cast<u64>(num_workers), std::memory_order_release);
12 const auto lambda = [this, thread_name{std::string{name}}] {
13 Common::SetCurrentThreadName(thread_name.c_str());
14
15 while (!stop) {
16 UniqueFunction<void> task;
17 {
18 std::unique_lock lock{queue_mutex};
19 if (requests.empty()) {
20 wait_condition.notify_all();
21 }
22 condition.wait(lock, [this] { return stop || !requests.empty(); });
23 if (stop) {
24 break;
25 }
26 task = std::move(requests.front());
27 requests.pop();
28 }
29 task();
30 work_done++;
31 }
32 workers_stopped++;
33 wait_condition.notify_all();
34 };
35 for (size_t i = 0; i < num_workers; ++i) {
36 threads.emplace_back(lambda);
37 }
38}
39
40ThreadWorker::~ThreadWorker() {
41 {
42 std::unique_lock lock{queue_mutex};
43 stop = true;
44 }
45 condition.notify_all();
46 for (std::thread& thread : threads) {
47 thread.join();
48 }
49}
50
51void ThreadWorker::QueueWork(UniqueFunction<void> work) {
52 {
53 std::unique_lock lock{queue_mutex};
54 requests.emplace(std::move(work));
55 work_scheduled++;
56 }
57 condition.notify_one();
58}
59
60void ThreadWorker::WaitForRequests() {
61 std::unique_lock lock{queue_mutex};
62 wait_condition.wait(
63 lock, [this] { return workers_stopped >= workers_queued || work_done >= work_scheduled; });
64}
65
66} // namespace Common