From 4d139943f2407144d5f8e3dc5a673f24850d43d0 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Sun, 16 Sep 2018 20:05:51 +0200 Subject: Port web_service from Citra --- src/common/CMakeLists.txt | 3 +++ src/common/detached_tasks.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/common/detached_tasks.h | 39 +++++++++++++++++++++++++++++++++++++++ src/common/web_result.h | 24 ++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/common/detached_tasks.cpp create mode 100644 src/common/detached_tasks.h create mode 100644 src/common/web_result.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 6a3f1fe08..8985e4367 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -41,6 +41,8 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scm_rev.cpp.in" "${CMAKE_CURRENT_SOU add_library(common STATIC alignment.h assert.h + detached_tasks.cpp + detached_tasks.h bit_field.h bit_set.h cityhash.cpp @@ -87,6 +89,7 @@ add_library(common STATIC timer.cpp timer.h vector_math.h + web_result.h ) if(ARCHITECTURE_x86_64) 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 @@ +// Copyright 2018 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#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() { + std::unique_lock lock(mutex); + ASSERT(count == 0); + instance = nullptr; +} + +void DetachedTasks::AddTask(std::function 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 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 +#include + +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 task); + +private: + static DetachedTasks* instance; + + std::condition_variable cv; + std::mutex mutex; + int count = 0; +}; + +} // namespace Common diff --git a/src/common/web_result.h b/src/common/web_result.h new file mode 100644 index 000000000..13610a7ea --- /dev/null +++ b/src/common/web_result.h @@ -0,0 +1,24 @@ +// Copyright 2018 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +namespace Common { +struct WebResult { + enum class Code : u32 { + Success, + InvalidURL, + CredentialsMissing, + LibError, + HttpError, + WrongContent, + NoWebservice, + }; + Code result_code; + std::string result_string; + std::string returned_data; +}; +} // namespace Commo \ No newline at end of file -- cgit v1.2.3 From b4ace6ec6f86079b3bd297f95dfe133240b53e15 Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Mon, 17 Sep 2018 17:16:01 +0200 Subject: Address a bunch of review comments --- src/common/web_result.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/web_result.h b/src/common/web_result.h index 13610a7ea..969926674 100644 --- a/src/common/web_result.h +++ b/src/common/web_result.h @@ -21,4 +21,4 @@ struct WebResult { std::string result_string; std::string returned_data; }; -} // namespace Commo \ No newline at end of file +} // namespace Common -- cgit v1.2.3 From e4daf4bee522c046e5e01eeed2c5b12bd91f489e Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Tue, 2 Oct 2018 16:04:10 +0200 Subject: Review comments - part 5 --- src/common/detached_tasks.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/common') diff --git a/src/common/detached_tasks.h b/src/common/detached_tasks.h index eae27788d..5dd8fc27b 100644 --- a/src/common/detached_tasks.h +++ b/src/common/detached_tasks.h @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #pragma once + #include #include -- cgit v1.2.3