Core: Fix crash when state is loaded while waiting for the CPU

This commit is contained in:
Stenzek 2019-06-30 00:06:59 +10:00
parent 137009affe
commit 560074cf9f
1 changed files with 14 additions and 8 deletions

View File

@ -63,7 +63,7 @@ static AfterLoadCallbackFunc s_on_after_load_callback;
// Temporary undo state buffer // Temporary undo state buffer
static std::vector<u8> g_undo_load_buffer; static std::vector<u8> g_undo_load_buffer;
static std::vector<u8> g_current_buffer; static std::vector<u8> g_current_buffer;
static int g_loadDepth = 0; static bool s_load_or_save_in_progress;
static std::mutex g_cs_undo_load_buffer; static std::mutex g_cs_undo_load_buffer;
static std::mutex g_cs_current_buffer; static std::mutex g_cs_current_buffer;
@ -385,6 +385,11 @@ static void CompressAndDumpState(CompressAndDumpState_args save_args)
void SaveAs(const std::string& filename, bool wait) void SaveAs(const std::string& filename, bool wait)
{ {
if (s_load_or_save_in_progress)
return;
s_load_or_save_in_progress = true;
Core::RunOnCPUThread( Core::RunOnCPUThread(
[&] { [&] {
// Measure the size of the buffer. // Measure the size of the buffer.
@ -423,6 +428,8 @@ void SaveAs(const std::string& filename, bool wait)
} }
}, },
true); true);
s_load_or_save_in_progress = false;
} }
bool ReadHeader(const std::string& filename, StateHeader& header) bool ReadHeader(const std::string& filename, StateHeader& header)
@ -521,7 +528,7 @@ static void LoadFileStateData(const std::string& filename, std::vector<u8>& ret_
void LoadAs(const std::string& filename) void LoadAs(const std::string& filename)
{ {
if (!Core::IsRunning()) if (!Core::IsRunning() || s_load_or_save_in_progress)
{ {
return; return;
} }
@ -531,10 +538,10 @@ void LoadAs(const std::string& filename)
return; return;
} }
s_load_or_save_in_progress = true;
Core::RunOnCPUThread( Core::RunOnCPUThread(
[&] { [&] {
g_loadDepth++;
// Save temp buffer for undo load state // Save temp buffer for undo load state
if (!Movie::IsJustStartingRecordingInputFromSaveState()) if (!Movie::IsJustStartingRecordingInputFromSaveState())
{ {
@ -580,17 +587,16 @@ void LoadAs(const std::string& filename)
Core::DisplayMessage("The savestate could not be loaded", OSD::Duration::NORMAL); Core::DisplayMessage("The savestate could not be loaded", OSD::Duration::NORMAL);
// since we could be in an inconsistent state now (and might crash or whatever), undo. // since we could be in an inconsistent state now (and might crash or whatever), undo.
if (g_loadDepth < 2) UndoLoadState();
UndoLoadState();
} }
} }
if (s_on_after_load_callback) if (s_on_after_load_callback)
s_on_after_load_callback(); s_on_after_load_callback();
g_loadDepth--;
}, },
true); true);
s_load_or_save_in_progress = false;
} }
void SetOnAfterLoadCallback(AfterLoadCallbackFunc callback) void SetOnAfterLoadCallback(AfterLoadCallbackFunc callback)