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.
This commit is contained in:
Pokechu22 2023-02-20 19:49:45 -08:00
parent 019bde6afc
commit 0f16ed5da9
2 changed files with 5 additions and 4 deletions

View File

@ -1060,15 +1060,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)
m_was_unpaused = PauseAndLock(system, true, true);
if (m_has_cpu_thread && !m_was_cpu_thread)
m_was_unpaused = PauseAndLock(m_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);
}

View File

@ -119,6 +119,7 @@ public:
private:
Core::System& m_system;
const bool m_was_cpu_thread;
const bool m_has_cpu_thread;
bool m_was_unpaused = false;
};