diff options
Diffstat (limited to 'src/common/thread_worker.cpp')
| -rw-r--r-- | src/common/thread_worker.cpp | 66 |
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 | |||
| 8 | namespace Common { | ||
| 9 | |||
| 10 | ThreadWorker::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 | |||
| 40 | ThreadWorker::~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 | |||
| 51 | void 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 | |||
| 60 | void 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 | ||