timer: Added SDL_GetTicks64(), for a timer that doesn't wrap every ~49 days.

Note that this removes the timeGetTime() fallback on Windows; it is a
32-bit counter and SDL2 should never choose to use it, as it only is needed
if QueryPerformanceCounter() isn't available, and QPC is _always_ available
on Windows XP and later.

OS/2 has a similar situation, but since it isn't clear to me that similar
promises can be made about DosTmrQueryTime() even in modern times, I decided
to leave the fallback in, with some heroic measures added to try to provide a
true 64-bit tick counter despite the 49-day wraparound. That approach can
migrate to Windows too, if we discover some truly broken install that doesn't
have QPC and still depends on timeGetTime().

Fixes #4870.
This commit is contained in:
Ryan C. Gordon
2021-10-23 15:00:31 -04:00
parent 0d631c741f
commit 99c9727dc0
11 changed files with 128 additions and 97 deletions

View File

@@ -104,10 +104,11 @@ SDL_TicksQuit(void)
ticks_started = SDL_FALSE;
}
Uint32
Uint64
SDL_GetTicks(void)
{
Uint32 ticks;
struct timeval now;
if (!ticks_started) {
SDL_TicksInit();
}
@@ -116,21 +117,18 @@ SDL_GetTicks(void)
#if HAVE_CLOCK_GETTIME
struct timespec now;
clock_gettime(SDL_MONOTONIC_CLOCK, &now);
ticks = (Uint32)((now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec - start_ts.tv_nsec) / 1000000);
ticks = (((Uint64) (now.tv_sec - start_ts.tv_sec)) * 1000) + (((Uint64) (now.tv_nsec - start_ts.tv_nsec)) / 1000000);
#elif defined(__APPLE__)
uint64_t now = mach_absolute_time();
ticks = (Uint32)((((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000);
const uint64_t now = mach_absolute_time();
return (Uint64) ((((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000);
#else
SDL_assert(SDL_FALSE);
ticks = 0;
return 0;
#endif
} else {
struct timeval now;
gettimeofday(&now, NULL);
ticks = (Uint32)((now.tv_sec - start_tv.tv_sec) * 1000 + (now.tv_usec - start_tv.tv_usec) / 1000);
}
return (ticks);
gettimeofday(&now, NULL);
return (((Uint64) (now.tv_sec - start_tv.tv_sec)) * 1000) + (((Uint64) (now.tv_usec - start_tv.tv_usec)) / 1000);
}
Uint64
@@ -203,7 +201,7 @@ SDL_Delay(Uint32 ms)
struct timespec elapsed, tv;
#else
struct timeval tv;
Uint32 then, now, elapsed;
Uint64 then, now, elapsed;
#endif
/* Set the timeout interval */
@@ -211,7 +209,7 @@ SDL_Delay(Uint32 ms)
elapsed.tv_sec = ms / 1000;
elapsed.tv_nsec = (ms % 1000) * 1000000;
#else
then = SDL_GetTicks();
then = SDL_GetTicks64();
#endif
do {
errno = 0;
@@ -222,13 +220,13 @@ SDL_Delay(Uint32 ms)
was_error = nanosleep(&tv, &elapsed);
#else
/* Calculate the time interval left (in case of interrupt) */
now = SDL_GetTicks();
now = SDL_GetTicks64();
elapsed = (now - then);
then = now;
if (elapsed >= ms) {
if (elapsed >= ((Uint64) ms)) {
break;
}
ms -= elapsed;
ms -= (Uint32) elapsed;
tv.tv_sec = ms / 1000;
tv.tv_usec = (ms % 1000) * 1000;