From 953e153d73985a898aa8e37434ca35e9a8eee8b2 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Sat, 9 Nov 2024 15:34:29 +0000 Subject: [PATCH] sdl: fix exiting while waiting for a render to be presented This would sometimes trigger the "user code frozen" message at exit, in some cases crashing when the semaphores get destroyed whilst the update thread is waiting on them. --- 32blit-sdl/System.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/32blit-sdl/System.cpp b/32blit-sdl/System.cpp index dd13bd85d..cc698c202 100644 --- a/32blit-sdl/System.cpp +++ b/32blit-sdl/System.cpp @@ -445,16 +445,19 @@ void System::set_button(int button, bool state) { } void System::stop() { - int returnValue; - running = false; + int returnValue; + running = false; - if(SDL_SemWaitTimeout(s_loop_ended, 500)) { - std::cerr << "User code appears to have frozen. Detaching thread." << std::endl; - SDL_DetachThread(t_system_loop); - } else { - SDL_WaitThread(t_system_loop, &returnValue); - } + // make sure the update thread is not waiting for a render to complete + SDL_SemPost(s_loop_redraw); + + if(SDL_SemWaitTimeout(s_loop_ended, 500)) { + std::cerr << "User code appears to have frozen. Detaching thread." << std::endl; + SDL_DetachThread(t_system_loop); + } else { + SDL_WaitThread(t_system_loop, &returnValue); + } - SDL_SemPost(s_timer_stop); - SDL_WaitThread(t_system_timer, &returnValue); + SDL_SemPost(s_timer_stop); + SDL_WaitThread(t_system_timer, &returnValue); }