diff --git a/content.h b/content.h index c8170d17b8..e642e4bdeb 100644 --- a/content.h +++ b/content.h @@ -51,6 +51,9 @@ bool content_load_state(const char* path, bool load_to_backup_buffer, bool autol /* Save a state from memory to disk. */ bool content_save_state(const char *path, bool save_to_disk, bool autosave); +/* Returns true if a save state task is in progress */ +bool content_save_state_in_progress(void); + /* Copy a save state. */ bool content_rename_state(const char *origin, const char *dest); diff --git a/retroarch.c b/retroarch.c index ac0d42158d..b0c81d8563 100644 --- a/retroarch.c +++ b/retroarch.c @@ -7771,6 +7771,7 @@ bool command_event(enum event_command cmd, void *data) command_event_runtime_log_deinit(); command_event_save_auto_state(); + #ifdef HAVE_CONFIGFILE if (p_rarch->runloop_overrides_active) command_event_disable_overrides(); @@ -28956,6 +28957,13 @@ bool retroarch_main_quit(void) if (!p_rarch->runloop_shutdown_initiated) { command_event_save_auto_state(); + + /* If any save states are in progress, wait + * until all tasks are complete (otherwise + * save state file may be truncated) */ + if (content_save_state_in_progress()) + task_queue_wait(NULL, NULL); + #ifdef HAVE_CONFIGFILE if (p_rarch->runloop_overrides_active) command_event_disable_overrides(); diff --git a/tasks/task_save.c b/tasks/task_save.c index 9b10ffd4ba..169c9a902d 100644 --- a/tasks/task_save.c +++ b/tasks/task_save.c @@ -1352,6 +1352,31 @@ bool content_save_state(const char *path, bool save_to_disk, bool autosave) return true; } +static bool task_save_state_finder(retro_task_t *task, void *user_data) +{ + if (!task) + return false; + + if (task->handler == task_save_handler) + return true; + + return false; +} + +/* Returns true if a save state task is in progress */ +bool content_save_state_in_progress(void) +{ + task_finder_data_t find_data; + + find_data.func = task_save_state_finder; + find_data.userdata = NULL; + + if (task_queue_find(&find_data)) + return true; + + return false; +} + /** * content_load_state: * @path : path that state will be loaded from.