Merge pull request #3095 from void-ghost/savestate_race_condition_fix

SaveState: Fix for race condition in SaveAs(...)
This commit is contained in:
shuffle2
2015-10-03 18:08:33 -07:00
4 changed files with 66 additions and 3 deletions

View File

@ -9,6 +9,7 @@
#include "Common/CommonTypes.h"
#include "Common/Event.h"
#include "Common/ScopeGuard.h"
#include "Common/StringUtil.h"
#include "Common/Thread.h"
#include "Common/Timer.h"
@ -280,8 +281,20 @@ struct CompressAndDumpState_args
static void CompressAndDumpState(CompressAndDumpState_args save_args)
{
std::lock_guard<std::mutex> lk(*save_args.buffer_mutex);
if (!save_args.wait)
// ScopeGuard is used here to ensure that g_compressAndDumpStateSyncEvent.Set()
// will be called and that it will happen after the IOFile is closed.
// Both ScopeGuard's and IOFile's finalization occur at respective object destruction time.
// As Local (stack) objects are destructed in the reverse order of construction and "ScopeGuard on_exit"
// is created before the "IOFile f", it is guaranteed that the file will be finalized before
// the ScopeGuard's finalization (i.e. "g_compressAndDumpStateSyncEvent.Set()" call).
Common::ScopeGuard on_exit([]()
{
g_compressAndDumpStateSyncEvent.Set();
});
// If it is not required to wait, we call finalizer early (and it won't be called again at destruction).
if (!save_args.wait)
on_exit.Exit();
const u8* const buffer_data = &(*(save_args.buffer_vector))[0];
const size_t buffer_size = (save_args.buffer_vector)->size();
@ -313,7 +326,6 @@ static void CompressAndDumpState(CompressAndDumpState_args save_args)
if (!f)
{
Core::DisplayMessage("Could not save state", 2000);
g_compressAndDumpStateSyncEvent.Set();
return;
}
@ -361,7 +373,6 @@ static void CompressAndDumpState(CompressAndDumpState_args save_args)
}
Core::DisplayMessage(StringFromFormat("Saved State to %s", filename.c_str()), 2000);
g_compressAndDumpStateSyncEvent.Set();
}
void SaveAs(const std::string& filename, bool wait)