diff options
author | Ameer J <52414509+ameerj@users.noreply.github.com> | 2021-07-08 19:20:57 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-08 19:20:57 -0400 |
commit | 975a7b3a78af40f05b92a53a96b9dcd7e85969e4 (patch) | |
tree | 56196b303ab9dd7f138beb45c471169647e1144a /src/common/thread_worker.cpp | |
parent | 5edc96f4a47bff64cfd97f0c91e42b56ab58b2d7 (diff) | |
parent | 0ddbbb64e514ea9bba6a4f8bd6908d654e7f114c (diff) |
Merge pull request #6563 from ReinUsesLisp/thread-worker
common: Add stateful thread worker and unique function utilities
Diffstat (limited to 'src/common/thread_worker.cpp')
-rw-r--r-- | src/common/thread_worker.cpp | 58 |
1 files changed, 0 insertions, 58 deletions
diff --git a/src/common/thread_worker.cpp b/src/common/thread_worker.cpp deleted file mode 100644 index 8f9bf447a..000000000 --- a/src/common/thread_worker.cpp +++ /dev/null @@ -1,58 +0,0 @@ -// 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 |