From fe61efcd7a6539db9820c583de5c4ad2518769a6 Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:45:25 -0800 Subject: [PATCH] DVDInterface: Modernize With CPUThreadGuard --- Source/Android/jni/MainAndroid.cpp | 3 ++- Source/Core/Core/HW/DVD/DVDInterface.cpp | 20 +++++++++++--------- Source/Core/Core/HW/DVD/DVDInterface.h | 11 ++++++----- Source/Core/Core/HW/WII_IPC.cpp | 7 +++++-- Source/Core/Core/Movie.cpp | 13 ++++++------- Source/Core/DolphinQt/GameList/GameList.cpp | 5 ++--- Source/Core/DolphinQt/MainWindow.cpp | 12 +++++++----- 7 files changed, 39 insertions(+), 32 deletions(-) diff --git a/Source/Android/jni/MainAndroid.cpp b/Source/Android/jni/MainAndroid.cpp index 6a74cabd87..27374cb83a 100644 --- a/Source/Android/jni/MainAndroid.cpp +++ b/Source/Android/jni/MainAndroid.cpp @@ -635,7 +635,8 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_ChangeDisc(J HostThreadLock guard; const std::string path = GetJString(env, jFile); __android_log_print(ANDROID_LOG_INFO, DOLPHIN_TAG, "Change Disc: %s", path.c_str()); - Core::RunAsCPUThread([&path] { Core::System::GetInstance().GetDVDInterface().ChangeDisc(path); }); + auto& system = Core::System::GetInstance(); + system.GetDVDInterface().ChangeDisc(Core::CPUThreadGuard{system}, path); } JNIEXPORT jobject JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_GetLogTypeNames(JNIEnv* env, diff --git a/Source/Core/Core/HW/DVD/DVDInterface.cpp b/Source/Core/Core/HW/DVD/DVDInterface.cpp index 298d358273..adf16777ab 100644 --- a/Source/Core/Core/HW/DVD/DVDInterface.cpp +++ b/Source/Core/Core/HW/DVD/DVDInterface.cpp @@ -23,6 +23,7 @@ #include "Core/AchievementManager.h" #include "Core/Config/MainSettings.h" #include "Core/Config/SessionSettings.h" +#include "Core/Core.h" #include "Core/CoreTiming.h" #include "Core/DolphinAnalytics.h" #include "Core/HW/AudioInterface.h" @@ -419,7 +420,7 @@ bool DVDInterface::IsDiscInside() const void DVDInterface::AutoChangeDiscCallback(Core::System& system, u64 userdata, s64 cyclesLate) { - system.GetDVDInterface().AutoChangeDisc(); + system.GetDVDInterface().AutoChangeDisc(Core::CPUThreadGuard{system}); } void DVDInterface::EjectDiscCallback(Core::System& system, u64 userdata, s64 cyclesLate) @@ -441,7 +442,7 @@ void DVDInterface::InsertDiscCallback(Core::System& system, u64 userdata, s64 cy } // Must only be called on the CPU thread -void DVDInterface::EjectDisc(EjectCause cause) +void DVDInterface::EjectDisc(const Core::CPUThreadGuard& guard, EjectCause cause) { m_system.GetCoreTiming().ScheduleEvent(0, m_eject_disc); if (cause == EjectCause::User) @@ -449,7 +450,8 @@ void DVDInterface::EjectDisc(EjectCause cause) } // Must only be called on the CPU thread -void DVDInterface::ChangeDisc(const std::vector& paths) +void DVDInterface::ChangeDisc(const Core::CPUThreadGuard& guard, + const std::vector& paths) { ASSERT_MSG(DISCIO, !paths.empty(), "Trying to insert an empty list of discs"); @@ -459,11 +461,11 @@ void DVDInterface::ChangeDisc(const std::vector& paths) m_auto_disc_change_index = 0; } - ChangeDisc(paths[0]); + ChangeDisc(guard, paths[0]); } // Must only be called on the CPU thread -void DVDInterface::ChangeDisc(const std::string& new_path) +void DVDInterface::ChangeDisc(const Core::CPUThreadGuard& guard, const std::string& new_path) { if (!m_disc_path_to_insert.empty()) { @@ -471,7 +473,7 @@ void DVDInterface::ChangeDisc(const std::string& new_path) return; } - EjectDisc(EjectCause::User); + EjectDisc(guard, EjectCause::User); m_disc_path_to_insert = new_path; m_system.GetCoreTiming().ScheduleEvent(m_system.GetSystemTimers().GetTicksPerSecond(), @@ -491,13 +493,13 @@ void DVDInterface::ChangeDisc(const std::string& new_path) } // Must only be called on the CPU thread -bool DVDInterface::AutoChangeDisc() +bool DVDInterface::AutoChangeDisc(const Core::CPUThreadGuard& guard) { if (m_auto_disc_change_paths.empty()) return false; m_auto_disc_change_index = (m_auto_disc_change_index + 1) % m_auto_disc_change_paths.size(); - ChangeDisc(m_auto_disc_change_paths[m_auto_disc_change_index]); + ChangeDisc(guard, m_auto_disc_change_paths[m_auto_disc_change_index]); return true; } @@ -1096,7 +1098,7 @@ void DVDInterface::ExecuteCommand(ReplyType reply_type) } else if (force_eject) { - EjectDisc(EjectCause::Software); + EjectDisc(Core::CPUThreadGuard{m_system}, EjectCause::Software); } break; } diff --git a/Source/Core/Core/HW/DVD/DVDInterface.h b/Source/Core/Core/HW/DVD/DVDInterface.h index f01558cc1e..600edc5050 100644 --- a/Source/Core/Core/HW/DVD/DVDInterface.h +++ b/Source/Core/Core/HW/DVD/DVDInterface.h @@ -17,8 +17,9 @@ class PointerWrap; namespace Core { +class CPUThreadGuard; class System; -} +} // namespace Core namespace CoreTiming { struct EventType; @@ -140,10 +141,10 @@ public: void SetDisc(std::unique_ptr disc, std::optional> auto_disc_change_paths); bool IsDiscInside() const; - void EjectDisc(EjectCause cause); // Must only be called on the CPU thread - void ChangeDisc(const std::vector& paths); // Must only be called on the CPU thread - void ChangeDisc(const std::string& new_path); // Must only be called on the CPU thread - bool AutoChangeDisc(); // Must only be called on the CPU thread + void EjectDisc(const Core::CPUThreadGuard& guard, EjectCause cause); + void ChangeDisc(const Core::CPUThreadGuard& guard, const std::vector& paths); + void ChangeDisc(const Core::CPUThreadGuard& guard, const std::string& new_path); + bool AutoChangeDisc(const Core::CPUThreadGuard& guard); // This function returns true and calls SConfig::SetRunningGameMetadata(Volume&, Partition&) // if both of the following conditions are true: diff --git a/Source/Core/Core/HW/WII_IPC.cpp b/Source/Core/Core/HW/WII_IPC.cpp index b3c7289a87..4243f57ae6 100644 --- a/Source/Core/Core/HW/WII_IPC.cpp +++ b/Source/Core/Core/HW/WII_IPC.cpp @@ -6,6 +6,7 @@ #include "Common/ChunkFile.h" #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" +#include "Core/Core.h" #include "Core/CoreTiming.h" #include "Core/HW/DVD/DVDInterface.h" #include "Core/HW/MMIO.h" @@ -176,7 +177,8 @@ void WiiIPC::RegisterMMIO(MMIO::Mapping* mmio, u32 base) if (wii_ipc.m_gpio_out[GPIO::DO_EJECT]) { INFO_LOG_FMT(WII_IPC, "Ejecting disc due to GPIO write"); - system.GetDVDInterface().EjectDisc(DVD::EjectCause::Software); + system.GetDVDInterface().EjectDisc(Core::CPUThreadGuard{system}, + DVD::EjectCause::Software); } // SENSOR_BAR is checked by WiimoteEmu::CameraLogic // TODO: AVE, SLOT_LED @@ -212,7 +214,8 @@ void WiiIPC::RegisterMMIO(MMIO::Mapping* mmio, u32 base) if (wii_ipc.m_gpio_out[GPIO::DO_EJECT]) { INFO_LOG_FMT(WII_IPC, "Ejecting disc due to GPIO write"); - system.GetDVDInterface().EjectDisc(DVD::EjectCause::Software); + system.GetDVDInterface().EjectDisc(Core::CPUThreadGuard{system}, + DVD::EjectCause::Software); } // SENSOR_BAR is checked by WiimoteEmu::CameraLogic // TODO: AVE, SLOT_LED diff --git a/Source/Core/Core/Movie.cpp b/Source/Core/Core/Movie.cpp index a2e0d9be1f..ea6e6c0e5a 100644 --- a/Source/Core/Core/Movie.cpp +++ b/Source/Core/Core/Movie.cpp @@ -1254,13 +1254,12 @@ void MovieManager::PlayController(GCPadStatus* PadStatus, int controllerID) if (m_pad_state.disc) { - Core::RunAsCPUThread([this] { - if (!m_system.GetDVDInterface().AutoChangeDisc()) - { - m_system.GetCPU().Break(); - PanicAlertFmtT("Change the disc to {0}", m_disc_change_filename); - } - }); + const Core::CPUThreadGuard guard(m_system); + if (!m_system.GetDVDInterface().AutoChangeDisc(guard)) + { + m_system.GetCPU().Break(); + PanicAlertFmtT("Change the disc to {0}", m_disc_change_filename); + } } if (m_pad_state.reset) diff --git a/Source/Core/DolphinQt/GameList/GameList.cpp b/Source/Core/DolphinQt/GameList/GameList.cpp index 6fc893329e..4e1da45033 100644 --- a/Source/Core/DolphinQt/GameList/GameList.cpp +++ b/Source/Core/DolphinQt/GameList/GameList.cpp @@ -879,9 +879,8 @@ void GameList::ChangeDisc() if (!game) return; - Core::RunAsCPUThread([file_path = game->GetFilePath()] { - Core::System::GetInstance().GetDVDInterface().ChangeDisc(file_path); - }); + auto& system = Core::System::GetInstance(); + system.GetDVDInterface().ChangeDisc(Core::CPUThreadGuard{system}, game->GetFilePath()); } QAbstractItemView* GameList::GetActiveView() const diff --git a/Source/Core/DolphinQt/MainWindow.cpp b/Source/Core/DolphinQt/MainWindow.cpp index ff028f5d37..ab38f0d047 100644 --- a/Source/Core/DolphinQt/MainWindow.cpp +++ b/Source/Core/DolphinQt/MainWindow.cpp @@ -794,15 +794,17 @@ void MainWindow::ChangeDisc() { std::vector paths = StringListToStdVector(PromptFileNames()); - if (!paths.empty()) - Core::RunAsCPUThread( - [&paths] { Core::System::GetInstance().GetDVDInterface().ChangeDisc(paths); }); + if (paths.empty()) + return; + + auto& system = Core::System::GetInstance(); + system.GetDVDInterface().ChangeDisc(Core::CPUThreadGuard{system}, paths); } void MainWindow::EjectDisc() { - Core::RunAsCPUThread( - [] { Core::System::GetInstance().GetDVDInterface().EjectDisc(DVD::EjectCause::User); }); + auto& system = Core::System::GetInstance(); + system.GetDVDInterface().EjectDisc(Core::CPUThreadGuard{system}, DVD::EjectCause::User); } void MainWindow::OpenUserFolder()