From 98db1a57576d0875babb70ad04215507b28a4bca Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 5 Jan 2025 00:32:23 +0100 Subject: [PATCH 1/2] Further improve sleep accuracy on UNIX systems hanichad https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2583 --- src/sdl/i_system.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 882535203..102f1b162 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -2121,16 +2121,24 @@ void I_Sleep(UINT32 ms) void I_SleepDuration(precise_t duration) { -#if defined(__linux__) || defined(__FreeBSD__) +#if defined(__linux__) || defined(__FreeBSD__) || defined(__HAIKU__) UINT64 precision = I_GetPrecisePrecision(); - struct timespec ts = { - .tv_sec = duration / precision, - .tv_nsec = duration * 1000000000 / precision % 1000000000, - }; - int status; - do status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts); - while (status == EINTR); -#else + precise_t dest = I_GetPreciseTime() + duration; + if (duration > 100000) + { + duration -= 100000; // 0.1 ms slack + struct timespec ts = { + .tv_sec = duration / precision, + .tv_nsec = duration * 1000000000 / precision % 1000000000, + }; + int status; + do status = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts); + while (status == EINTR); + } + + // busy-wait the rest + while (((INT64)dest - (INT64)I_GetPreciseTime()) > 0); +#elif defined (MIN_SLEEP_DURATION_MS) UINT64 precision = I_GetPrecisePrecision(); INT32 sleepvalue = cv_sleep.value; UINT64 delaygranularity; @@ -2159,7 +2167,6 @@ void I_SleepDuration(precise_t duration) } // Otherwise, this is a spinloop. - cur = I_GetPreciseTime(); } #endif From 92649a393dbb240866c176173433c4870ee81fdf Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 5 Jan 2025 00:41:41 +0100 Subject: [PATCH 2/2] dont cast "infinity" to int lel --- src/d_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d_main.c b/src/d_main.c index 94fe8792d..9bbaa6017 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -743,7 +743,7 @@ void D_SRB2Loop(void) { // Casting the return value of a function is bad practice (apparently) - double budget = round((1.0 / R_GetFramerateCap()) * I_GetPrecisePrecision()); + double budget = ((R_GetFramerateCap() == 0) ? 0.0 : round((1.0 / R_GetFramerateCap()) * I_GetPrecisePrecision())); capbudget = (precise_t) budget; }