Browse Source

[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
lizzie 2 days ago
committed by crueter
parent
commit
c2e74a9380
No known key found for this signature in database GPG Key ID: 425ACD2D4830EBC6
  1. 2
      src/android/app/src/main/jni/native.cpp
  2. 2
      src/android/app/src/main/jni/native.h
  3. 2
      src/common/CMakeLists.txt
  4. 41
      src/common/detached_tasks.cpp
  5. 39
      src/common/detached_tasks.h
  6. 6
      src/dedicated_room/yuzu_room.cpp
  7. 14
      src/web_service/announce_room_json.cpp
  8. 6
      src/web_service/announce_room_json.h
  9. 13
      src/yuzu/main.cpp
  10. 3
      src/yuzu_cmd/yuzu.cpp

2
src/android/app/src/main/jni/native.cpp

@ -39,7 +39,6 @@
#include "common/android/multiplayer/multiplayer.h" #include "common/android/multiplayer/multiplayer.h"
#include "common/android/android_common.h" #include "common/android/android_common.h"
#include "common/android/id_cache.h" #include "common/android/id_cache.h"
#include "common/detached_tasks.h"
#include "common/dynamic_library.h" #include "common/dynamic_library.h"
#include "common/fs/path_util.h" #include "common/fs/path_util.h"
#include "common/logging.h" #include "common/logging.h"
@ -352,7 +351,6 @@ void EmulationSession::ShutdownEmulation() {
if (m_load_result == Core::SystemResultStatus::Success) { if (m_load_result == Core::SystemResultStatus::Success) {
m_system.DetachDebugger(); m_system.DetachDebugger();
m_system.ShutdownMainProcess(); m_system.ShutdownMainProcess();
m_detached_tasks.WaitForAllTasks();
m_load_result = Core::SystemResultStatus::ErrorNotInitialized; m_load_result = Core::SystemResultStatus::ErrorNotInitialized;
m_window.reset(); m_window.reset();
OnEmulationStopped(Core::SystemResultStatus::Success); OnEmulationStopped(Core::SystemResultStatus::Success);

2
src/android/app/src/main/jni/native.h

@ -6,7 +6,6 @@
#include <android/native_window_jni.h> #include <android/native_window_jni.h>
#include "common/android/applets/software_keyboard.h" #include "common/android/applets/software_keyboard.h"
#include "common/detached_tasks.h"
#include "core/core.h" #include "core/core.h"
#include "core/file_sys/registered_cache.h" #include "core/file_sys/registered_cache.h"
#include "core/hle/service/acc/profile_manager.h" #include "core/hle/service/acc/profile_manager.h"
@ -75,7 +74,6 @@ private:
// Core emulation // Core emulation
Core::System m_system; Core::System m_system;
InputCommon::InputSubsystem m_input_subsystem; InputCommon::InputSubsystem m_input_subsystem;
Common::DetachedTasks m_detached_tasks;
Core::PerfStatsResults m_perf_stats{}; Core::PerfStatsResults m_perf_stats{};
int m_shaders_building{0}; int m_shaders_building{0};
std::shared_ptr<FileSys::VfsFilesystem> m_vfs; std::shared_ptr<FileSys::VfsFilesystem> m_vfs;

2
src/common/CMakeLists.txt

@ -40,8 +40,6 @@ add_library(
container_hash.h container_hash.h
demangle.cpp demangle.cpp
demangle.h demangle.h
detached_tasks.cpp
detached_tasks.h
device_power_state.cpp device_power_state.cpp
device_power_state.h device_power_state.h
div_ceil.h div_ceil.h

41
src/common/detached_tasks.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

39
src/common/detached_tasks.h

@ -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

6
src/dedicated_room/yuzu_room.cpp

@ -24,7 +24,6 @@
#include <openssl/evp.h> #include <openssl/evp.h>
#include "common/common_types.h" #include "common/common_types.h"
#include "common/detached_tasks.h"
#include "common/fs/file.h" #include "common/fs/file.h"
#include "common/fs/fs.h" #include "common/fs/fs.h"
#include "common/fs/path_util.h" #include "common/fs/path_util.h"
@ -172,9 +171,7 @@ static void SaveBanList(const Network::Room::BanList& ban_list, const std::strin
} }
/// Application entry point /// Application entry point
void LaunchRoom(int argc, char** argv, bool called_by_option)
{
Common::DetachedTasks detached_tasks;
void LaunchRoom(int argc, char** argv, bool called_by_option) {
int option_index = 0; int option_index = 0;
char* endarg; char* endarg;
@ -393,6 +390,5 @@ void LaunchRoom(int argc, char** argv, bool called_by_option)
room->Destroy(); room->Destroy();
} }
Network::Shutdown(); Network::Shutdown();
detached_tasks.WaitForAllTasks();
std::exit(0); std::exit(0);
} }

14
src/web_service/announce_room_json.cpp

@ -6,7 +6,6 @@
#include <future> #include <future>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "common/detached_tasks.h"
#include "common/logging.h" #include "common/logging.h"
#include "web_service/announce_room_json.h" #include "web_service/announce_room_json.h"
#include "web_service/web_backend.h" #include "web_service/web_backend.h"
@ -136,13 +135,14 @@ AnnounceMultiplayerRoom::RoomList RoomJson::GetRoomList() {
void RoomJson::Delete() { void RoomJson::Delete() {
if (room_id.empty()) { if (room_id.empty()) {
LOG_ERROR(WebService, "Room must be registered to be deleted"); LOG_ERROR(WebService, "Room must be registered to be deleted");
return;
} else {
// This jthread won't be destroyed until after the dtor has been ran
// Once the thread finishes it will stay resident on the vector -- destroyed and freed by dtor()
// this is still valid while in dtor, so... yeah
detached_tasks.emplace_back([this](std::stop_token stop_token) {
client.DeleteJson(fmt::format("/lobby/{}", room_id), "", false);
});
} }
Common::DetachedTasks::AddTask([host_{this->host}, username_{this->username},
token_{this->token}, room_id_{this->room_id}]() {
// create a new client here because the this->client might be destroyed.
Client{host_, username_, token_}.DeleteJson(fmt::format("/lobby/{}", room_id_), "", false);
});
} }
} // namespace WebService } // namespace WebService

6
src/web_service/announce_room_json.h

@ -1,8 +1,13 @@
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project // SPDX-FileCopyrightText: Copyright 2017 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include <thread>
#include <vector>
#include <functional> #include <functional>
#include <string> #include <string>
#include "common/announce_multiplayer_room.h" #include "common/announce_multiplayer_room.h"
@ -30,6 +35,7 @@ public:
void Delete() override; void Delete() override;
private: private:
std::vector<std::jthread> detached_tasks;
AnnounceMultiplayerRoom::Room room; AnnounceMultiplayerRoom::Room room;
Client client; Client client;
std::string host; std::string host;

13
src/yuzu/main.cpp

@ -8,9 +8,6 @@
#include <cstring> #include <cstring>
#include "dedicated_room/yuzu_room.h" #include "dedicated_room/yuzu_room.h"
#endif #endif
#include <common/detached_tasks.h>
#ifdef __unix__ #ifdef __unix__
#include "qt_common/gui_settings.h" #include "qt_common/gui_settings.h"
#endif #endif
@ -104,8 +101,6 @@ int main(int argc, char* argv[]) {
Breakpad::InstallCrashHandler(); Breakpad::InstallCrashHandler();
#endif #endif
Common::DetachedTasks detached_tasks;
// Init settings params // Init settings params
QCoreApplication::setOrganizationName(QStringLiteral("eden")); QCoreApplication::setOrganizationName(QStringLiteral("eden"));
QCoreApplication::setApplicationName(QStringLiteral("eden")); QCoreApplication::setApplicationName(QStringLiteral("eden"));
@ -193,10 +188,6 @@ int main(int argc, char* argv[]) {
// After settings have been loaded by GMainWindow, apply the filter // After settings have been loaded by GMainWindow, apply the filter
main_window.show(); main_window.show();
app.connect(&app, &QGuiApplication::applicationStateChanged, &main_window,
&MainWindow::OnAppFocusStateChanged);
int result = app.exec();
detached_tasks.WaitForAllTasks();
return result;
app.connect(&app, &QGuiApplication::applicationStateChanged, &main_window, &MainWindow::OnAppFocusStateChanged);
return app.exec();
} }

3
src/yuzu_cmd/yuzu.cpp

@ -11,7 +11,6 @@
#include <fmt/ostream.h> #include <fmt/ostream.h>
#include "common/detached_tasks.h"
#include "common/logging.h" #include "common/logging.h"
#include "common/scm_rev.h" #include "common/scm_rev.h"
#include "common/settings.h" #include "common/settings.h"
@ -187,7 +186,6 @@ int main(int argc, char** argv) {
Common::Log::Initialize(); Common::Log::Initialize();
Common::Log::SetColorConsoleBackendEnabled(true); Common::Log::SetColorConsoleBackendEnabled(true);
Common::Log::Start(); Common::Log::Start();
Common::DetachedTasks detached_tasks;
int option_index = 0; int option_index = 0;
#ifdef _WIN32 #ifdef _WIN32
@ -452,7 +450,6 @@ int main(int argc, char** argv) {
system.DetachDebugger(); system.DetachDebugger();
void(system.Pause()); void(system.Pause());
system.ShutdownMainProcess(); system.ShutdownMainProcess();
detached_tasks.WaitForAllTasks();
return 0; return 0;
} }

Loading…
Cancel
Save