From 1ffde9209ccca2a58c62b369e23fdababc97e9f3 Mon Sep 17 00:00:00 2001 From: Alistair Buxton Date: Wed, 29 Sep 2021 21:02:30 +0100 Subject: [PATCH 1/2] SDL: Call multiplayer->update() from the user code thread The multiplayer update scans for messages and passes them in to the user defined function on_message(). This should be done in the user code thread for two reasons: 1. Calling it from a different thread means on_message() and update() are being run in different threads, and therefore all code in both of them has to be thread safe. This is pretty hard to do with the 32blit API and not really in the spirit of what this is supposed to be. 2. on_message() is a user-supplied function and so bugs can make it hang. We can detect hangs in update() and render() thanks to running them on the user thread (via blit::tick()). Running on_message() on that thread allows to detect hangs there too. --- 32blit-sdl/Main.cpp | 1 - 32blit-sdl/System.cpp | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/32blit-sdl/Main.cpp b/32blit-sdl/Main.cpp index 0eea5863b..bf79075e0 100644 --- a/32blit-sdl/Main.cpp +++ b/32blit-sdl/Main.cpp @@ -105,7 +105,6 @@ void handle_event(SDL_Event &event) { default: if(event.type == System::loop_event) { - blit_multiplayer->update(); blit_renderer->update(blit_system); blit_system->notify_redraw(); blit_renderer->present(); diff --git a/32blit-sdl/System.cpp b/32blit-sdl/System.cpp index 2c3d4c013..8448d5ac1 100644 --- a/32blit-sdl/System.cpp +++ b/32blit-sdl/System.cpp @@ -269,6 +269,7 @@ void System::loop() blit::joystick.y = shadow_joystick[1]; SDL_UnlockMutex(m_input); blit::tick(::now()); + blit_multiplayer->update(); } Uint32 System::mode() { From e8876fc42db9917c778e80639ccce351acba592b Mon Sep 17 00:00:00 2001 From: Alistair Buxton Date: Wed, 29 Sep 2021 21:33:17 +0100 Subject: [PATCH 2/2] Also call render from the user thread Someone moved the call to render out of the thread loop and into the texture update function. Same rationale for putting it back. It should not overlap other user code and it should be possible to detect if the user code hangs. --- 32blit-sdl/System.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/32blit-sdl/System.cpp b/32blit-sdl/System.cpp index 8448d5ac1..73e8fe7ee 100644 --- a/32blit-sdl/System.cpp +++ b/32blit-sdl/System.cpp @@ -269,6 +269,7 @@ void System::loop() blit::joystick.y = shadow_joystick[1]; SDL_UnlockMutex(m_input); blit::tick(::now()); + blit::render(::now()); blit_multiplayer->update(); } @@ -277,7 +278,6 @@ Uint32 System::mode() { } void System::update_texture(SDL_Texture *texture) { - blit::render(::now()); if (_mode == blit::ScreenMode::lores) { SDL_UpdateTexture(texture, nullptr, __fb_lores.data, 160 * 3); }