diff options
| author | 2021-03-22 21:00:48 -0300 | |
|---|---|---|
| committer | 2021-07-08 19:00:39 -0300 | |
| commit | f28dd32275c1feba4854abad30ff5e21a7b39440 (patch) | |
| tree | 8cb51136e8bcd03418ce71878cc0e7478606ff25 /src | |
| parent | Merge pull request #6539 from lat9nq/default-setting (diff) | |
| download | yuzu-f28dd32275c1feba4854abad30ff5e21a7b39440.tar.gz yuzu-f28dd32275c1feba4854abad30ff5e21a7b39440.tar.xz yuzu-f28dd32275c1feba4854abad30ff5e21a7b39440.zip | |
common/thread_worker: Add wait for requests method
Diffstat (limited to '')
| -rw-r--r-- | src/common/thread_worker.cpp | 9 | ||||
| -rw-r--r-- | src/common/thread_worker.h | 2 |
2 files changed, 11 insertions, 0 deletions
diff --git a/src/common/thread_worker.cpp b/src/common/thread_worker.cpp index 8f9bf447a..745918c7e 100644 --- a/src/common/thread_worker.cpp +++ b/src/common/thread_worker.cpp | |||
| @@ -29,6 +29,10 @@ ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { | |||
| 29 | } | 29 | } |
| 30 | task = std::move(requests.front()); | 30 | task = std::move(requests.front()); |
| 31 | requests.pop(); | 31 | requests.pop(); |
| 32 | |||
| 33 | if (requests.empty()) { | ||
| 34 | wait_condition.notify_one(); | ||
| 35 | } | ||
| 32 | } | 36 | } |
| 33 | 37 | ||
| 34 | task(); | 38 | task(); |
| @@ -55,4 +59,9 @@ void ThreadWorker::QueueWork(std::function<void()>&& work) { | |||
| 55 | condition.notify_one(); | 59 | condition.notify_one(); |
| 56 | } | 60 | } |
| 57 | 61 | ||
| 62 | void ThreadWorker::WaitForRequests() { | ||
| 63 | std::unique_lock lock{queue_mutex}; | ||
| 64 | wait_condition.wait(lock, [this] { return stop || requests.empty(); }); | ||
| 65 | } | ||
| 66 | |||
| 58 | } // namespace Common | 67 | } // namespace Common |
diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h index f1859971f..7a6756eb5 100644 --- a/src/common/thread_worker.h +++ b/src/common/thread_worker.h | |||
| @@ -18,12 +18,14 @@ public: | |||
| 18 | explicit ThreadWorker(std::size_t num_workers, const std::string& name); | 18 | explicit ThreadWorker(std::size_t num_workers, const std::string& name); |
| 19 | ~ThreadWorker(); | 19 | ~ThreadWorker(); |
| 20 | void QueueWork(std::function<void()>&& work); | 20 | void QueueWork(std::function<void()>&& work); |
| 21 | void WaitForRequests(); | ||
| 21 | 22 | ||
| 22 | private: | 23 | private: |
| 23 | std::vector<std::thread> threads; | 24 | std::vector<std::thread> threads; |
| 24 | std::queue<std::function<void()>> requests; | 25 | std::queue<std::function<void()>> requests; |
| 25 | std::mutex queue_mutex; | 26 | std::mutex queue_mutex; |
| 26 | std::condition_variable condition; | 27 | std::condition_variable condition; |
| 28 | std::condition_variable wait_condition; | ||
| 27 | std::atomic_bool stop{}; | 29 | std::atomic_bool stop{}; |
| 28 | }; | 30 | }; |
| 29 | 31 | ||