summaryrefslogtreecommitdiffstats
path: root/src/common/detached_tasks.h
diff options
context:
space:
mode:
authorfearlessTobi <thm.frey@gmail.com>2018-09-16 20:05:51 +0200
committerfearlessTobi <thm.frey@gmail.com>2018-10-02 15:30:48 +0200
commit4d139943f2407144d5f8e3dc5a673f24850d43d0 (patch)
treebe24285a32c2b72b9756b69fd614f3d45c70ff41 /src/common/detached_tasks.h
parentAdd submodules (diff)
downloadyuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.tar
yuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.tar.gz
yuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.tar.bz2
yuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.tar.lz
yuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.tar.xz
yuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.tar.zst
yuzu-4d139943f2407144d5f8e3dc5a673f24850d43d0.zip
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 @@
+// Copyright 2018 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+#include <condition_variable>
+#include <functional>
+
+namespace Common {
+
+/**
+ * A background manager which ensures that all detached task is finished before program exits.
+ *
+ * Some tasks, telemetry submission for example, prefer executing asynchronously and don't care
+ * about the result. These tasks are suitable for std::thread::detach(). However, this is unsafe if
+ * the task is launched just before the program exits (which is a common case for telemetry), so we
+ * need to block on these tasks on program exit.
+ *
+ * To make detached task safe, a single DetachedTasks object should be placed in the main(), and
+ * call WaitForAllTasks() after all program execution but before global/static variable destruction.
+ * Any potentially unsafe detached task should be executed via DetachedTasks::AddTask.
+ */
+class DetachedTasks {
+public:
+ DetachedTasks();
+ ~DetachedTasks();
+ void WaitForAllTasks();
+
+ static void AddTask(std::function<void()> task);
+
+private:
+ static DetachedTasks* instance;
+
+ std::condition_variable cv;
+ std::mutex mutex;
+ int count = 0;
+};
+
+} // namespace Common