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