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"); 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)) 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); 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; std::string verbose_message;
usz bytes_written = 0; 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) if (!init)
{ {
pdlg->reject(); // Try to wait for the abort process to complete
return; 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()) if (auto str_ptr = code_location->load())
@ -727,11 +740,12 @@ void gui_application::InitializeCallbacks()
verbose_message = "\n" + *str_ptr; verbose_message = "\n" + *str_ptr;
} }
*half_seconds += 1; bytes_written = std::max<usz>(ar_ptr->get_size(), old_written);
break;
bytes_written = ar_ptr->get_size();
} }
*half_seconds += 1;
if (old_written == bytes_written) if (old_written == bytes_written)
{ {
if (repeat_count == 60) if (repeat_count == 60)