Skip to content

Commit 8a50fc6

Browse files
committed
win32: swap GetTickCount64() for more precise QueryPerformanceCounters()
1 parent 8f265c9 commit 8a50fc6

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/lib/ares__timeval.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,24 @@
3030

3131
void ares__tvnow(ares_timeval_t *now)
3232
{
33-
/* GetTickCount64() is available on Windows Vista and higher */
34-
ULONGLONG milliseconds = GetTickCount64();
33+
/* QueryPerformanceCounters() has been around since Windows 2000, though
34+
* significant fixes were made in later versions. Documentation states
35+
* 1 microsecond or better resolution with a rollover not less than 100 years.
36+
* This differs from GetTickCount{64}() which has a resolution between 10 and
37+
* 16 ms. */
38+
LARGE_INTEGER freq;
39+
LARGE_INTEGER current;
3540

36-
now->sec = (ares_int64_t)milliseconds / 1000;
37-
now->usec = (unsigned int)(milliseconds % 1000) * 1000;
41+
/* Not sure how long it takes to get the frequency, I see it recommended to
42+
* cache it */
43+
QueryPerformanceFrequency(&freq);
44+
QueryPerformanceCounter(&current);
45+
46+
now->sec = current.QuadPart / freq.QuadPart;
47+
/* We want to prevent overflows so we get the remainder, then multiply to
48+
* microseconds before dividing */
49+
now->usec = (unsigned int)(((current.QuadPart % freq.QuadPart) * 1000000) /
50+
freq.QuadPart);
3851
}
3952

4053
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)

0 commit comments

Comments
 (0)