From 522470274dc233dfd876894b86b27ab673e3d0a6 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Mon, 20 Feb 2023 19:49:45 -0800 Subject: [PATCH] Core: Only unlock CPU thread if CPUThreadGuard locked it Before, I was getting an abort() call in debug builds when selecting Tools -> Load Wii System Menu (although I'm not sure if this will work on other machines consistently). It seems that CodeViewWidget::Update was constructing a CPUThreadGuard instance, and thus was calling Core::PauseAndLock(true, true), but the CPU thread hadn't started yet so Core::IsRunningAndStarted() returned false and thus Core::PauseAndLock() did nothing. However, after the code widget finished updating, CPUThreadGuard's destructor called PauseAndLock(false, true), and by this time the CPU thread *had* started so Core::PauseAndLock eventually called CPU::PauseAndLock(false, true, true). This resulted in s_stepping_lock.unlock() being called without a corresponding s_stepping_lock.lock() call, which calls abort() in debug builds. --- Source/Core/Core/Core.cpp | 6 +++--- Source/Core/Core/Core.h | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index 5f90f93bae..a3069604a5 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -1050,15 +1050,15 @@ void UpdateInputGate(bool require_focus, bool require_full_focus) } CPUThreadGuard::CPUThreadGuard(Core::System& system) - : m_system(system), m_was_cpu_thread(IsCPUThread()) + : m_system(system), m_was_cpu_thread(IsCPUThread()), m_has_cpu_thread(IsRunningAndStarted()) { - if (!m_was_cpu_thread) + if (m_has_cpu_thread && !m_was_cpu_thread) m_was_unpaused = PauseAndLock(system, true, true); } CPUThreadGuard::~CPUThreadGuard() { - if (!m_was_cpu_thread) + if (m_has_cpu_thread && !m_was_cpu_thread) PauseAndLock(m_system, false, m_was_unpaused); } diff --git a/Source/Core/Core/Core.h b/Source/Core/Core/Core.h index eca18cf3be..99424ffb6e 100644 --- a/Source/Core/Core/Core.h +++ b/Source/Core/Core/Core.h @@ -114,6 +114,7 @@ public: private: Core::System& m_system; const bool m_was_cpu_thread; + const bool m_has_cpu_thread; bool m_was_unpaused = false; };