From a4479f9c0786356682155b597e6fe9803748c1ec Mon Sep 17 00:00:00 2001 From: John Lorentzson Date: Sat, 26 Apr 2025 13:54:12 +0200 Subject: [PATCH] Reimplement Backend_Delay to fix threads freezing So our workaround for making usleep thread-safe mostly worked in that it fixed one of the things making it thread-unsafe, but it seems that sigsuspend, which usleep uses, is also not thread-safe, since sometimes one of our two threads would freeze, stuck inside sigsuspend. So instead of usleep, we're using a trick that Stacken member map suggested: using select. Calling select with no file descriptors to work on will have it block on the kernel level until its timeout is hit. This acts as a sleep. As it turns out, this seems to completely solve our sleep-related problems, and massively reduces the amount of audio buffer underruns as a nice bonus! --- src/Backends/Platform/X11.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Backends/Platform/X11.cpp b/src/Backends/Platform/X11.cpp index 435e99e3..3160084a 100644 --- a/src/Backends/Platform/X11.cpp +++ b/src/Backends/Platform/X11.cpp @@ -158,5 +158,9 @@ unsigned long Backend_GetTicks(void) { } void Backend_Delay(unsigned int ticks) { - usleep(ticks * 1000); + struct timeval timeout; + timeout.tv_sec = ticks / 1000; + timeout.tv_usec = (ticks * 1000) % 1000000; + + select(0, NULL, NULL, NULL, &timeout); }