Savestates: Fix rare race

This commit is contained in:
Eladash 2024-04-13 22:43:25 +03:00 committed by Elad Ashkenazi
parent 6b906b9a9c
commit 51e1598e42
2 changed files with 28 additions and 6 deletions

View File

@ -3318,6 +3318,14 @@ void Emulator::Kill(bool allow_autoexit, bool savestate, savestate_stage* save_s
set_progress_message("Commiting File");
fs::file to_close_file;
{
auto reset = init_mtx->reset();
to_close_file = std::move(file.file);
reset.set_init();
}
to_close_file.close();
if (!file.commit() || !fs::get_stat(path, file_stat))
{
sys_log.error("Failed to write savestate to file! (path='%s', %s)", path, fs::g_tls_error);

View File

@ -713,13 +713,26 @@ void gui_application::InitializeCallbacks()
std::string verbose_message;
usz bytes_written = 0;
while (true)
{
auto init = static_cast<stx::init_mutex*>(init_mtx.get())->access();
auto mtx = static_cast<stx::init_mutex*>(init_mtx.get());
auto init = mtx->access();
if (!init)
{
pdlg->reject();
return;
// Try to wait for the abort process to complete
auto fake_reset = mtx->reset();
if (!fake_reset)
{
// End of emulation termination
pdlg->reject();
return;
}
fake_reset.set_init();
// Now ar_ptr contains a null file descriptor
continue;
}
if (auto str_ptr = code_location->load())
@ -727,11 +740,12 @@ void gui_application::InitializeCallbacks()
verbose_message = "\n" + *str_ptr;
}
*half_seconds += 1;
bytes_written = ar_ptr->get_size();
bytes_written = std::max<usz>(ar_ptr->get_size(), old_written);
break;
}
*half_seconds += 1;
if (old_written == bytes_written)
{
if (repeat_count == 60)