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!
This commit is contained in:
John Lorentzson 2025-04-26 13:54:12 +02:00
parent 108b0282bb
commit a4479f9c07

View file

@ -158,5 +158,9 @@ unsigned long Backend_GetTicks(void) {
} }
void Backend_Delay(unsigned int ticks) { 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);
} }