diff options
| author | 2018-10-06 02:43:09 -0400 | |
|---|---|---|
| committer | 2018-10-06 02:43:09 -0400 | |
| 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.gz yuzu-b8b90ce6e61329ebda226b9917ed961be3b80d1f.tar.xz yuzu-b8b90ce6e61329ebda226b9917ed961be3b80d1f.zip | |
Merge pull request #1332 from FearlessTobi/port-web-backend
Port web_service from Citra
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 @@ | |||
| 1 | // Copyright 2018 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <thread> | ||
| 6 | #include "common/assert.h" | ||
| 7 | #include "common/detached_tasks.h" | ||
| 8 | |||
| 9 | namespace Common { | ||
| 10 | |||
| 11 | DetachedTasks* DetachedTasks::instance = nullptr; | ||
| 12 | |||
| 13 | DetachedTasks::DetachedTasks() { | ||
| 14 | ASSERT(instance == nullptr); | ||
| 15 | instance = this; | ||
| 16 | } | ||
| 17 | |||
| 18 | void DetachedTasks::WaitForAllTasks() { | ||
| 19 | std::unique_lock<std::mutex> lock(mutex); | ||
| 20 | cv.wait(lock, [this]() { return count == 0; }); | ||
| 21 | } | ||
| 22 | |||
| 23 | DetachedTasks::~DetachedTasks() { | ||
| 24 | std::unique_lock<std::mutex> lock(mutex); | ||
| 25 | ASSERT(count == 0); | ||
| 26 | instance = nullptr; | ||
| 27 | } | ||
| 28 | |||
| 29 | void DetachedTasks::AddTask(std::function<void()> task) { | ||
| 30 | std::unique_lock<std::mutex> lock(instance->mutex); | ||
| 31 | ++instance->count; | ||
| 32 | std::thread([task{std::move(task)}]() { | ||
| 33 | task(); | ||
| 34 | std::unique_lock<std::mutex> lock(instance->mutex); | ||
| 35 | --instance->count; | ||
| 36 | std::notify_all_at_thread_exit(instance->cv, std::move(lock)); | ||
| 37 | }) | ||
| 38 | .detach(); | ||
| 39 | } | ||
| 40 | |||
| 41 | } // namespace Common | ||