avoid potential race conditions during reset/etc by waiting till the emu thread got the message to pause

This commit is contained in:
StapleButter 2017-09-22 01:33:18 +02:00
parent 332282c809
commit e298d50e76
1 changed files with 13 additions and 1 deletions

View File

@ -41,6 +41,7 @@ uiMenuItem* MenuItem_Stop;
SDL_Thread* EmuThread; SDL_Thread* EmuThread;
int EmuRunning; int EmuRunning;
volatile int EmuStatus;
bool RunningSomething; bool RunningSomething;
char ROMPath[1024]; char ROMPath[1024];
@ -98,6 +99,8 @@ int EmuThreadFunc(void* burp)
{ {
if (EmuRunning == 1) if (EmuRunning == 1)
{ {
EmuStatus = 1;
// emulate // emulate
u32 nlines = NDS::RunFrame(); u32 nlines = NDS::RunFrame();
@ -148,6 +151,8 @@ int EmuThreadFunc(void* burp)
} }
else else
{ {
EmuStatus = 2;
// paused // paused
nframes = 0; nframes = 0;
lasttick = SDL_GetTicks(); lasttick = SDL_GetTicks();
@ -160,6 +165,8 @@ int EmuThreadFunc(void* burp)
} }
} }
EmuStatus = 0;
if (audio) SDL_CloseAudioDevice(audio); if (audio) SDL_CloseAudioDevice(audio);
NDS::DeInit(); NDS::DeInit();
@ -269,6 +276,7 @@ void Run()
void Stop() void Stop()
{ {
EmuRunning = 2; EmuRunning = 2;
while (EmuStatus != 2);
RunningSomething = false; RunningSomething = false;
uiMenuItemDisable(MenuItem_Pause); uiMenuItemDisable(MenuItem_Pause);
@ -291,13 +299,14 @@ void OnCloseByMenu(uiMenuItem* item, uiWindow* window, void* blarg)
{ {
// TODO???? // TODO????
// uiQuit() crashes // uiQuit() crashes
printf("TODO, eventually\n");
} }
void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg) void OnOpenFile(uiMenuItem* item, uiWindow* window, void* blarg)
{ {
int prevstatus = EmuRunning; int prevstatus = EmuRunning;
EmuRunning = 2; 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); char* file = uiOpenFile(window, "DS ROM (*.nds)|*.nds;*.srl|Any file|*.*", NULL);
if (!file) if (!file)
@ -350,6 +359,9 @@ void OnReset(uiMenuItem* item, uiWindow* window, void* blarg)
{ {
if (!RunningSomething) return; if (!RunningSomething) return;
EmuRunning = 2;
while (EmuStatus != 2);
if (ROMPath[0] == '\0') if (ROMPath[0] == '\0')
NDS::LoadBIOS(); NDS::LoadBIOS();
else else