summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2021-04-01 01:05:45 -0300
committerGravatar ReinUsesLisp2021-07-08 19:03:26 -0300
commitbf5b5c1bf43946039d91f78253599c9996f86057 (patch)
tree2fcfd8f48297b03e3a9eb03cfe7594c7ead4edaa
parentcommon: Add unique function (diff)
downloadyuzu-bf5b5c1bf43946039d91f78253599c9996f86057.tar.gz
yuzu-bf5b5c1bf43946039d91f78253599c9996f86057.tar.xz
yuzu-bf5b5c1bf43946039d91f78253599c9996f86057.zip
common/thread_worker: Use unique function
-rw-r--r--src/common/thread_worker.cpp46
-rw-r--r--src/common/thread_worker.h6
2 files changed, 24 insertions, 28 deletions
diff --git a/src/common/thread_worker.cpp b/src/common/thread_worker.cpp
index 745918c7e..f4d8bb0f0 100644
--- a/src/common/thread_worker.cpp
+++ b/src/common/thread_worker.cpp
@@ -8,36 +8,30 @@
8namespace Common { 8namespace Common {
9 9
10ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { 10ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) {
11 for (std::size_t i = 0; i < num_workers; ++i) 11 const auto lambda = [this, thread_name{std::string{name}}] {
12 threads.emplace_back([this, thread_name{std::string{name}}] { 12 Common::SetCurrentThreadName(thread_name.c_str());
13 Common::SetCurrentThreadName(thread_name.c_str());
14 13
15 // Wait for first request 14 while (!stop) {
15 UniqueFunction<void> task;
16 { 16 {
17 std::unique_lock lock{queue_mutex}; 17 std::unique_lock lock{queue_mutex};
18 if (requests.empty()) {
19 wait_condition.notify_all();
20 }
18 condition.wait(lock, [this] { return stop || !requests.empty(); }); 21 condition.wait(lock, [this] { return stop || !requests.empty(); });
19 } 22 if (stop || requests.empty()) {
20 23 break;
21 while (true) {
22 std::function<void()> task;
23
24 {
25 std::unique_lock lock{queue_mutex};
26 condition.wait(lock, [this] { return stop || !requests.empty(); });
27 if (stop || requests.empty()) {
28 return;
29 }
30 task = std::move(requests.front());
31 requests.pop();
32
33 if (requests.empty()) {
34 wait_condition.notify_one();
35 }
36 } 24 }
37 25 task = std::move(requests.front());
38 task(); 26 requests.pop();
39 } 27 }
40 }); 28 task();
29 }
30 wait_condition.notify_all();
31 };
32 for (size_t i = 0; i < num_workers; ++i) {
33 threads.emplace_back(lambda);
34 }
41} 35}
42 36
43ThreadWorker::~ThreadWorker() { 37ThreadWorker::~ThreadWorker() {
@@ -51,10 +45,10 @@ ThreadWorker::~ThreadWorker() {
51 } 45 }
52} 46}
53 47
54void ThreadWorker::QueueWork(std::function<void()>&& work) { 48void ThreadWorker::QueueWork(UniqueFunction<void> work) {
55 { 49 {
56 std::unique_lock lock{queue_mutex}; 50 std::unique_lock lock{queue_mutex};
57 requests.emplace(work); 51 requests.emplace(std::move(work));
58 } 52 }
59 condition.notify_one(); 53 condition.notify_one();
60} 54}
diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h
index 7a6756eb5..7e2b04a07 100644
--- a/src/common/thread_worker.h
+++ b/src/common/thread_worker.h
@@ -11,18 +11,20 @@
11#include <vector> 11#include <vector>
12#include <queue> 12#include <queue>
13 13
14#include "common/unique_function.h"
15
14namespace Common { 16namespace Common {
15 17
16class ThreadWorker final { 18class ThreadWorker final {
17public: 19public:
18 explicit ThreadWorker(std::size_t num_workers, const std::string& name); 20 explicit ThreadWorker(std::size_t num_workers, const std::string& name);
19 ~ThreadWorker(); 21 ~ThreadWorker();
20 void QueueWork(std::function<void()>&& work); 22 void QueueWork(UniqueFunction<void> work);
21 void WaitForRequests(); 23 void WaitForRequests();
22 24
23private: 25private:
24 std::vector<std::thread> threads; 26 std::vector<std::thread> threads;
25 std::queue<std::function<void()>> requests; 27 std::queue<UniqueFunction<void>> requests;
26 std::mutex queue_mutex; 28 std::mutex queue_mutex;
27 std::condition_variable condition; 29 std::condition_variable condition;
28 std::condition_variable wait_condition; 30 std::condition_variable wait_condition;