diff --git a/backends/platform/libretro/src/libretro-threads.cpp b/backends/platform/libretro/src/libretro-threads.cpp index dc1997ccc9e..117ba7c7427 100644 --- a/backends/platform/libretro/src/libretro-threads.cpp +++ b/backends/platform/libretro/src/libretro-threads.cpp @@ -78,7 +78,6 @@ static void retro_wrap_emulator(void) { status &= ~EMU_EXITED; scummvm_res = retro_run_emulator(); status |= EMU_EXITED; - retro_exit_to_main_thread(); } @@ -106,6 +105,8 @@ static void retro_free_emu_thread() { } void retro_switch_to_emu_thread() { + if (retro_emu_thread_exited() || !retro_emu_thread_initialized()) + return; #ifdef USE_LIBCO co_switch(emu_thread); #else @@ -169,11 +170,8 @@ bool retro_init_emu_thread(void) { } void retro_deinit_emu_thread() { - if (!retro_emu_thread_initialized()) - return; - if (!retro_current_thread_is_main()) - retro_switch_to_main_thread(); - retro_free_emu_thread(); + if (retro_emu_thread_initialized()) + retro_free_emu_thread(); } int retro_get_scummvm_res() { diff --git a/backends/platform/libretro/src/libretro.cpp b/backends/platform/libretro/src/libretro.cpp index c8027cc4915..ea4a77d2ef9 100644 --- a/backends/platform/libretro/src/libretro.cpp +++ b/backends/platform/libretro/src/libretro.cpp @@ -671,15 +671,6 @@ bool retro_load_game_special(unsigned game_type, const struct retro_game_info *i } void retro_run(void) { - - if (retro_emu_thread_exited()) - retro_deinit_emu_thread(); - - if (!retro_emu_thread_initialized()) { - environ_cb(RETRO_ENVIRONMENT_SHUTDOWN, NULL); - return; - } - bool updated = false; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated){ update_variables(); @@ -763,6 +754,14 @@ void retro_run(void) { if (!skip_frame) retro_switch_to_emu_thread(); + if (retro_emu_thread_exited()) + retro_deinit_emu_thread(); + + if (!retro_emu_thread_initialized()) { + environ_cb(RETRO_ENVIRONMENT_SHUTDOWN, NULL); + return; + } + /* Retrieve audio */ samples_count = 0; if (audio_video_enable & 2) { @@ -799,13 +798,13 @@ void retro_run(void) { } void retro_unload_game(void) { - if (!retro_emu_thread_initialized()) - return; - while (!retro_emu_thread_exited()) { - retroQuit(); - retro_switch_to_emu_thread(); + if (retro_emu_thread_initialized()) { + while (!retro_emu_thread_exited()) { + retroQuit(); + retro_switch_to_emu_thread(); + } + retro_deinit_emu_thread(); } - retro_deinit_emu_thread(); if (retro_get_scummvm_res() == Common::kNoError) log_cb(RETRO_LOG_INFO, "ScummVM exited successfully.\n");