Further improve sleep accuracy on UNIX systems

hanichad https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2583
This commit is contained in:
Alug committed 2025-01-05 00:32:39 +01:00
1 parent e06b61e8d2
commit ebedca82f3
1 file changed
+17 -10
+17 -10
View File
@@ -2130,16 +2130,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;
@@ -2168,7 +2176,6 @@ void I_SleepDuration(precise_t duration)
}
// Otherwise, this is a spinloop.
cur = I_GetPreciseTime();
}
#endif