summaryrefslogtreecommitdiff
path: root/src/common/detached_tasks.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/detached_tasks.h')
-rw-r--r--src/common/detached_tasks.h39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/common/detached_tasks.h b/src/common/detached_tasks.h
new file mode 100644
index 000000000..eae27788d
--- /dev/null
+++ b/src/common/detached_tasks.h
@@ -0,0 +1,39 @@
1// Copyright 2018 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6#include <condition_variable>
7#include <functional>
8
9namespace Common {
10
11/**
12 * A background manager which ensures that all detached task is finished before program exits.
13 *
14 * Some tasks, telemetry submission for example, prefer executing asynchronously and don't care
15 * about the result. These tasks are suitable for std::thread::detach(). However, this is unsafe if
16 * the task is launched just before the program exits (which is a common case for telemetry), so we
17 * need to block on these tasks on program exit.
18 *
19 * To make detached task safe, a single DetachedTasks object should be placed in the main(), and
20 * call WaitForAllTasks() after all program execution but before global/static variable destruction.
21 * Any potentially unsafe detached task should be executed via DetachedTasks::AddTask.
22 */
23class DetachedTasks {
24public:
25 DetachedTasks();
26 ~DetachedTasks();
27 void WaitForAllTasks();
28
29 static void AddTask(std::function<void()> task);
30
31private:
32 static DetachedTasks* instance;
33
34 std::condition_variable cv;
35 std::mutex mutex;
36 int count = 0;
37};
38
39} // namespace Common