sim/win/hosttime: calculate sec/ms independently to avoid overflow

In the previous implementation, PerformanceCounter would cause overflow
after running for a long time, This commit will separate the calculation
of the sec/ms part to avoid this issue, Reference:

https://github.com/cygwin/cygwin/blob/main/winsup/cygwin/clock.cc#L194-L217

Signed-off-by: chao an <anchao@xiaomi.com>
This commit is contained in:
chao an 2023-04-10 22:24:19 +08:00 committed by Alin Jerpelea
parent 2309eacf9f
commit 3f05df3fbb
1 changed files with 18 additions and 11 deletions

View File

@ -48,9 +48,10 @@
uint64_t host_gettime(bool rtc)
{
static LARGE_INTEGER start;
LARGE_INTEGER counter;
LARGE_INTEGER freq;
static long long int ticks_per_sec;
static uint64_t start;
uint64_t current;
LARGE_INTEGER now;
FILETIME ftime;
if (rtc)
@ -61,17 +62,23 @@ uint64_t host_gettime(bool rtc)
ftime.dwLowDateTime) - DELTA_EPOCH_IN_100NS) * 100;
}
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&counter);
counter.QuadPart = counter.QuadPart * POW10_9 / freq.QuadPart;
if (start.QuadPart == 0)
if (ticks_per_sec == 0)
{
start.QuadPart = counter.QuadPart;
QueryPerformanceFrequency(&now);
InterlockedExchange64(&ticks_per_sec, now.QuadPart);
}
return counter.QuadPart - start.QuadPart;
QueryPerformanceCounter(&now);
current = now.QuadPart / ticks_per_sec * POW10_9 +
(now.QuadPart % ticks_per_sec) * POW10_9 / ticks_per_sec;
if (start == 0)
{
start = current;
}
return current - start;
}
/****************************************************************************