From e298d50e76d9c37fbb8919b7a73327f036ade845 Mon Sep 17 00:00:00 2001 From: StapleButter Date: Fri, 22 Sep 2017 01:33:18 +0200 Subject: [PATCH] avoid potential race conditions during reset/etc by waiting till the emu thread got the message to pause --- src/libui_sdl/main.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp index 7ccc8f78..69e1bae7 100644 --- a/src/libui_sdl/main.cpp +++ b/src/libui_sdl/main.cpp @@ -41,6 +41,7 @@ uiMenuItem* MenuItem_Stop; SDL_Thread* EmuThread; int EmuRunning; +volatile int EmuStatus; bool RunningSomething; char ROMPath[1024]; @@ -98,6 +99,8 @@ int EmuThreadFunc(void* burp) { if (EmuRunning == 1) { + EmuStatus = 1; + // emulate u32 nlines = NDS::RunFrame(); @@ -148,6 +151,8 @@ int EmuThreadFunc(void* burp) } else { + EmuStatus = 2; + // paused nframes = 0; lasttick = SDL_GetTicks(); @@ -160,6 +165,8 @@ int EmuThreadFunc(void* burp) } } + EmuStatus = 0; + if (audio) SDL_CloseAudioDevice(audio); NDS::DeInit(); @@ -269,6 +276,7 @@ void Run() void Stop() { EmuRunning = 2; + while (EmuStatus != 2); RunningSomething = false; uiMenuItemDisable(MenuItem_Pause); @@ -291,13 +299,14 @@ void OnCloseByMenu(uiMenuItem* item, uiWindow* window, void* blarg) { // TODO???? // uiQuit() crashes + printf("TODO, eventually\n"); } void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg) { int prevstatus = EmuRunning; EmuRunning = 2; - // TODO: ensure the emu thread has indeed stopped at this point + while (EmuStatus != 2); char* file = uiOpenFile(window, "DS ROM (*.nds)|*.nds;*.srl|Any file|*.*", NULL); if (!file) @@ -350,6 +359,9 @@ void OnReset(uiMenuItem* item, uiWindow* window, void* blarg) { if (!RunningSomething) return; + EmuRunning = 2; + while (EmuStatus != 2); + if (ROMPath[0] == '\0') NDS::LoadBIOS(); else