diff options
author | bunnei <bunneidev@gmail.com> | 2018-10-06 08:43:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-06 08:43:09 +0200 |
commit | b8b90ce6e61329ebda226b9917ed961be3b80d1f (patch) | |
tree | 8e6b82419db7fb24eb5d8f06346a0a1228f9513c /src/common/detached_tasks.cpp | |
parent | Merge pull request #1447 from lioncash/mutex (diff) | |
parent | Review comments - part 5 (diff) | |
download | yuzu-b8b90ce6e61329ebda226b9917ed961be3b80d1f.tar yuzu-b8b90ce6e61329ebda226b9917ed961be3b80d1f.tar.gz yuzu-b8b90ce6e61329ebda226b9917ed961be3b80d1f.tar.bz2 yuzu-b8b90ce6e61329ebda226b9917ed961be3b80d1f.tar.lz yuzu-b8b90ce6e61329ebda226b9917ed961be3b80d1f.tar.xz yuzu-b8b90ce6e61329ebda226b9917ed961be3b80d1f.tar.zst yuzu-b8b90ce6e61329ebda226b9917ed961be3b80d1f.zip |
Diffstat (limited to 'src/common/detached_tasks.cpp')
-rw-r--r-- | src/common/detached_tasks.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/common/detached_tasks.cpp b/src/common/detached_tasks.cpp new file mode 100644 index 000000000..a347d9e02 --- /dev/null +++ b/src/common/detached_tasks.cpp @@ -0,0 +1,41 @@ +// Copyright 2018 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <thread> +#include "common/assert.h" +#include "common/detached_tasks.h" + +namespace Common { + +DetachedTasks* DetachedTasks::instance = nullptr; + +DetachedTasks::DetachedTasks() { + ASSERT(instance == nullptr); + instance = this; +} + +void DetachedTasks::WaitForAllTasks() { + std::unique_lock<std::mutex> lock(mutex); + cv.wait(lock, [this]() { return count == 0; }); +} + +DetachedTasks::~DetachedTasks() { + std::unique_lock<std::mutex> lock(mutex); + ASSERT(count == 0); + instance = nullptr; +} + +void DetachedTasks::AddTask(std::function<void()> task) { + std::unique_lock<std::mutex> lock(instance->mutex); + ++instance->count; + std::thread([task{std::move(task)}]() { + task(); + std::unique_lock<std::mutex> lock(instance->mutex); + --instance->count; + std::notify_all_at_thread_exit(instance->cv, std::move(lock)); + }) + .detach(); +} + +} // namespace Common |