Browse Source
[common] remove DetachedTasks object usage (#4096)
[common] remove DetachedTasks object usage (#4096)
this was only ever used for announce room json on one single instance this object is pretty much a danger to society and shouldnt exist, especially since its: a) a singleton b) spawns objects out of thin air c) doesn't respect RAII and can outlive its parent objects (big no no) generally we shouldnt endorse objects like these in the future, but alas, it existed, now it has to begone additionally there is a similar object in KWorkerTaskManager but it's not as egregious as this one. check that this didnt break the usual announce room json logic Signed-off-by: lizzie <lizzie@eden-emu.dev> Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/4096 Reviewed-by: CamilleLaVey <camillelavey99@gmail.com> Reviewed-by: MaranBr <maranbr@eden-emu.dev>detectplatform
committed by
crueter
No known key found for this signature in database
GPG Key ID: 425ACD2D4830EBC6
10 changed files with 16 additions and 112 deletions
-
2src/android/app/src/main/jni/native.cpp
-
2src/android/app/src/main/jni/native.h
-
2src/common/CMakeLists.txt
-
41src/common/detached_tasks.cpp
-
39src/common/detached_tasks.h
-
6src/dedicated_room/yuzu_room.cpp
-
14src/web_service/announce_room_json.cpp
-
6src/web_service/announce_room_json.h
-
13src/yuzu/main.cpp
-
3src/yuzu_cmd/yuzu.cpp
@ -1,41 +0,0 @@ |
|||
// SPDX-FileCopyrightText: 2018 Citra Emulator Project
|
|||
// SPDX-License-Identifier: GPL-2.0-or-later
|
|||
|
|||
#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 thread_lock{instance->mutex}; |
|||
--instance->count; |
|||
std::notify_all_at_thread_exit(instance->cv, std::move(thread_lock)); |
|||
}).detach(); |
|||
} |
|||
|
|||
} // namespace Common
|
|||
@ -1,39 +0,0 @@ |
|||
// SPDX-FileCopyrightText: 2018 Citra Emulator Project |
|||
// SPDX-License-Identifier: GPL-2.0-or-later |
|||
|
|||
#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 |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue