diff options
author | bunnei <bunneidev@gmail.com> | 2020-12-31 07:06:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-31 07:06:05 +0100 |
commit | 25d607f5f63929369fb74f386a920b69bb24f442 (patch) | |
tree | 9ee5a023f033d99561a0358c5c71aeecc92c9d64 /src/common/thread_worker.cpp | |
parent | Merge pull request #5263 from lioncash/uninit (diff) | |
parent | hle: kernel: service_thread: Make thread naming more consistent. (diff) | |
download | yuzu-25d607f5f63929369fb74f386a920b69bb24f442.tar yuzu-25d607f5f63929369fb74f386a920b69bb24f442.tar.gz yuzu-25d607f5f63929369fb74f386a920b69bb24f442.tar.bz2 yuzu-25d607f5f63929369fb74f386a920b69bb24f442.tar.lz yuzu-25d607f5f63929369fb74f386a920b69bb24f442.tar.xz yuzu-25d607f5f63929369fb74f386a920b69bb24f442.tar.zst yuzu-25d607f5f63929369fb74f386a920b69bb24f442.zip |
Diffstat (limited to 'src/common/thread_worker.cpp')
-rw-r--r-- | src/common/thread_worker.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/common/thread_worker.cpp b/src/common/thread_worker.cpp new file mode 100644 index 000000000..8f9bf447a --- /dev/null +++ b/src/common/thread_worker.cpp @@ -0,0 +1,58 @@ +// 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) { + for (std::size_t i = 0; i < num_workers; ++i) + threads.emplace_back([this, thread_name{std::string{name}}] { + Common::SetCurrentThreadName(thread_name.c_str()); + + // Wait for first request + { + std::unique_lock lock{queue_mutex}; + condition.wait(lock, [this] { return stop || !requests.empty(); }); + } + + while (true) { + std::function<void()> task; + + { + std::unique_lock lock{queue_mutex}; + condition.wait(lock, [this] { return stop || !requests.empty(); }); + if (stop || requests.empty()) { + return; + } + task = std::move(requests.front()); + requests.pop(); + } + + task(); + } + }); +} + +ThreadWorker::~ThreadWorker() { + { + std::unique_lock lock{queue_mutex}; + stop = true; + } + condition.notify_all(); + for (std::thread& thread : threads) { + thread.join(); + } +} + +void ThreadWorker::QueueWork(std::function<void()>&& work) { + { + std::unique_lock lock{queue_mutex}; + requests.emplace(work); + } + condition.notify_one(); +} + +} // namespace Common |