mirror of
https://github.com/yuzu-mirror/yuzu
synced 2024-12-22 17:03:05 +00:00
1ca9a13e50
If this is not waited on, the synchronization primitives are destroyed whe main exits and the detached task ends up signalling garbage and not properly finishing.
42 lines
1,017 B
C++
42 lines
1,017 B
C++
// 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 lock{mutex};
|
|
cv.wait(lock, [this]() { return count == 0; });
|
|
}
|
|
|
|
DetachedTasks::~DetachedTasks() {
|
|
WaitForAllTasks();
|
|
|
|
std::unique_lock lock{mutex};
|
|
ASSERT(count == 0);
|
|
instance = nullptr;
|
|
}
|
|
|
|
void DetachedTasks::AddTask(std::function<void()> task) {
|
|
std::unique_lock lock{instance->mutex};
|
|
++instance->count;
|
|
std::thread([task{std::move(task)}]() {
|
|
task();
|
|
std::unique_lock lock{instance->mutex};
|
|
--instance->count;
|
|
std::notify_all_at_thread_exit(instance->cv, std::move(lock));
|
|
}).detach();
|
|
}
|
|
|
|
} // namespace Common
|