Liu Hao wrote:
在 2021-05-03 14:49, Martin Storsjö 写道:
Just for the record - I wouldn't mind applying the patch or something
like it. I agree with the arguments that it would be beneficial to
get more precision here, even if one in general maybe can argue that
we don't strictly need to provide such precision.
Oh fair enough, if you would like it, I don't mind applying it either.
Attached is a new patch with '_Atomic' and 'intptr_t' as requested.
--
Best regards,
Christian Franke
From a8d782e5a51deba50f0746080c4f34bb15caf7a7 Mon Sep 17 00:00:00 2001
From: Christian Franke <[email protected]>
Date: Mon, 3 May 2021 13:28:06 +0200
Subject: [PATCH] crt: Increase precision of gettimeofday() if possible.
Use GetSystemTimePreciseAsFileTime() if available.
Signed-off-by: Christian Franke <[email protected]>
---
mingw-w64-crt/misc/gettimeofday.c | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/mingw-w64-crt/misc/gettimeofday.c
b/mingw-w64-crt/misc/gettimeofday.c
index 50ffcfc7..37dd6556 100644
--- a/mingw-w64-crt/misc/gettimeofday.c
+++ b/mingw-w64-crt/misc/gettimeofday.c
@@ -41,8 +41,24 @@ int getntptimeofday (struct timespec *tp, struct timezone *z)
}
if (tp != NULL) {
- GetSystemTimeAsFileTime (&_now.ft); /* 100-nanoseconds since
1-1-1601 */
- /* The actual accuracy on XP seems to be 125,000 nanoseconds = 125
microseconds = 0.125 milliseconds */
+ typedef void (WINAPI * GetSystemTimeAsFileTime_t)(LPFILETIME);
+ static _Atomic GetSystemTimeAsFileTime_t GetSystemTimeAsFileTime_p = NULL;
+
+ /* Set function pointer during first call */
+ GetSystemTimeAsFileTime_t get_time = GetSystemTimeAsFileTime_p;
+ if (get_time == NULL) {
+ /* Use GetSystemTimePreciseAsFileTime() if available (Windows 8 or
later) */
+ get_time = (GetSystemTimeAsFileTime_t)(intptr_t) GetProcAddress (
+ GetModuleHandle ("kernel32.dll"),
+ "GetSystemTimePreciseAsFileTime"); /* <1us precision on Windows 10 */
+ if (get_time == NULL)
+ get_time = GetSystemTimeAsFileTime; /* >15ms precision on Windows 10 */
+ /* First thread wins */
+ InterlockedCompareExchangePointer ((void * volatile *)
&GetSystemTimeAsFileTime_p,
+ get_time, NULL);
+ }
+
+ get_time (&_now.ft); /* 100 nano-seconds since 1-1-1601 */
_now.ns100 -= FILETIME_1970; /* 100 nano-seconds since 1-1-1970 */
tp->tv_sec = _now.ns100 / HECTONANOSEC_PER_SEC; /* seconds since
1-1-1970 */
tp->tv_nsec = (long) (_now.ns100 % HECTONANOSEC_PER_SEC) * 100; /*
nanoseconds */
--
2.31.1
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public