From 2fe004eb90d39710bade71f5d35a30dbdb08b8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 30 Apr 2025 15:51:25 +0200 Subject: [PATCH] Savedata iothread: Don't alloc the std::thread --- Core/Dialog/PSPSaveDialog.cpp | 72 ++++++++++++++++++++++------------- Core/Dialog/PSPSaveDialog.h | 3 +- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/Core/Dialog/PSPSaveDialog.cpp b/Core/Dialog/PSPSaveDialog.cpp index 6d11ef93be..ca739b8108 100755 --- a/Core/Dialog/PSPSaveDialog.cpp +++ b/Core/Dialog/PSPSaveDialog.cpp @@ -97,7 +97,9 @@ PSPSaveDialog::PSPSaveDialog(UtilityDialogType type) : PSPDialog(type) { } PSPSaveDialog::~PSPSaveDialog() { - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } } int PSPSaveDialog::Init(int paramAddr) { @@ -107,7 +109,11 @@ int PSPSaveDialog::Init(int paramAddr) { return SCE_ERROR_UTILITY_INVALID_STATUS; } - JoinIOThread(); + if (ioThread.joinable()) { + // Normally shouldn't be the case here. + ioThread.join(); + } + ioThreadStatus = SAVEIO_NONE; requestAddr = paramAddr; @@ -741,7 +747,9 @@ int PSPSaveDialog::Update(int animSpeed) break; case DS_SAVE_SAVING: if (ioThreadStatus != SAVEIO_PENDING) { - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } } StartDraw(); @@ -756,7 +764,9 @@ int PSPSaveDialog::Update(int animSpeed) EndDraw(); break; case DS_SAVE_FAILED: - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } StartDraw(); DisplaySaveIcon(true); @@ -780,8 +790,8 @@ int PSPSaveDialog::Update(int animSpeed) EndDraw(); break; case DS_SAVE_DONE: - if (ioThread) { - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); param.SetPspParam(param.GetPspParam()); } StartDraw(); @@ -846,7 +856,9 @@ int PSPSaveDialog::Update(int animSpeed) break; case DS_LOAD_LOADING: if (ioThreadStatus != SAVEIO_PENDING) { - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } } StartDraw(); @@ -861,7 +873,9 @@ int PSPSaveDialog::Update(int animSpeed) EndDraw(); break; case DS_LOAD_FAILED: - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } StartDraw(); DisplaySaveIcon(true); @@ -884,7 +898,9 @@ int PSPSaveDialog::Update(int animSpeed) EndDraw(); break; case DS_LOAD_DONE: - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } StartDraw(); DisplaySaveIcon(true); @@ -971,7 +987,9 @@ int PSPSaveDialog::Update(int animSpeed) break; case DS_DELETE_DELETING: if (ioThreadStatus != SAVEIO_PENDING) { - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } } StartDraw(); @@ -983,7 +1001,9 @@ int PSPSaveDialog::Update(int animSpeed) EndDraw(); break; case DS_DELETE_FAILED: - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } StartDraw(); DisplayMessage(di->T("DeleteFailed", "Unable to delete data.")); @@ -1001,8 +1021,8 @@ int PSPSaveDialog::Update(int animSpeed) EndDraw(); break; case DS_DELETE_DONE: - if (ioThread) { - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); param.SetPspParam(param.GetPspParam()); } StartDraw(); @@ -1055,7 +1075,9 @@ int PSPSaveDialog::Update(int animSpeed) // ... except in Host IO timing, where we wait as long as needed. break; } - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0); break; } @@ -1205,14 +1227,6 @@ void PSPSaveDialog::ExecuteNotVisibleIOAction() { param.ClearSFOCache(); } -void PSPSaveDialog::JoinIOThread() { - if (ioThread) { - ioThread->join(); - delete ioThread; - ioThread = 0; - } -} - static void DoExecuteIOAction(PSPSaveDialog *dialog) { SetCurrentThreadName("SaveIO"); @@ -1221,20 +1235,22 @@ static void DoExecuteIOAction(PSPSaveDialog *dialog) { } void PSPSaveDialog::StartIOThread() { - if (ioThread) { + if (ioThread.joinable()) { WARN_LOG_REPORT(Log::sceUtility, "Starting a save io thread when one already pending, uh oh."); - JoinIOThread(); + ioThread.join(); } ioThreadStatus = SAVEIO_PENDING; - ioThread = new std::thread(&DoExecuteIOAction, this); + ioThread = std::thread(&DoExecuteIOAction, this); } int PSPSaveDialog::Shutdown(bool force) { if (GetStatus() != SCE_UTILITY_STATUS_FINISHED && !force) return SCE_ERROR_UTILITY_INVALID_STATUS; - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } ioThreadStatus = SAVEIO_NONE; PSPDialog::Shutdown(force); @@ -1248,7 +1264,9 @@ int PSPSaveDialog::Shutdown(bool force) { } void PSPSaveDialog::DoState(PointerWrap &p) { - JoinIOThread(); + if (ioThread.joinable()) { + ioThread.join(); + } PSPDialog::DoState(p); auto s = p.Section("PSPSaveDialog", 1, 2); diff --git a/Core/Dialog/PSPSaveDialog.h b/Core/Dialog/PSPSaveDialog.h index c9c3a84423..8fe2b2d23e 100644 --- a/Core/Dialog/PSPSaveDialog.h +++ b/Core/Dialog/PSPSaveDialog.h @@ -50,7 +50,6 @@ private: void DisplayMessage(std::string_view text, bool hasYesNo = false); std::string GetSelectedSaveDirName() const; - void JoinIOThread(); void StartIOThread(); void ExecuteNotVisibleIOAction(); @@ -102,7 +101,7 @@ private: SAVEIO_DONE, }; - std::thread *ioThread = nullptr; + std::thread ioThread; std::mutex paramLock; volatile SaveIOStatus ioThreadStatus = SAVEIO_NONE; };