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:
parent
2309eacf9f
commit
3f05df3fbb
|
@ -48,9 +48,10 @@
|
||||||
|
|
||||||
uint64_t host_gettime(bool rtc)
|
uint64_t host_gettime(bool rtc)
|
||||||
{
|
{
|
||||||
static LARGE_INTEGER start;
|
static long long int ticks_per_sec;
|
||||||
LARGE_INTEGER counter;
|
static uint64_t start;
|
||||||
LARGE_INTEGER freq;
|
uint64_t current;
|
||||||
|
LARGE_INTEGER now;
|
||||||
FILETIME ftime;
|
FILETIME ftime;
|
||||||
|
|
||||||
if (rtc)
|
if (rtc)
|
||||||
|
@ -61,17 +62,23 @@ uint64_t host_gettime(bool rtc)
|
||||||
ftime.dwLowDateTime) - DELTA_EPOCH_IN_100NS) * 100;
|
ftime.dwLowDateTime) - DELTA_EPOCH_IN_100NS) * 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
QueryPerformanceFrequency(&freq);
|
if (ticks_per_sec == 0)
|
||||||
QueryPerformanceCounter(&counter);
|
|
||||||
|
|
||||||
counter.QuadPart = counter.QuadPart * POW10_9 / freq.QuadPart;
|
|
||||||
|
|
||||||
if (start.QuadPart == 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|
Loading…
Reference in New Issue