Liu Hao wrote:
在 5/4/21 2:48 AM, Martin Storsjö 写道:
...
In practice it shouldn't really matter, because as Jacek points out,
we should be able to do without the atomics just fine in practice here.
Yes, see below.
I think I have to disagree on this. Just because something works 'in
practice' doesn't mean it is correct in principle. The atomic
load-store pair is essential to prevent races in case of multiple
threads calling this function concurrently. One may argue that such
races are harmless, but they are still undefined behavior, and could
cause faults if thread sanitizer is involved.
Next try attached.
If the '__atomic*()' are not used, gcc 10.2.0 -O2 generates the same
code. '__ATOMIC_SEQ_CST' would make a difference: the store is done via
xchgq instead of movq then.
From 155426b0b237ce1a7ee870199734f1d5e525fba8 Mon Sep 17 00:00:00 2001
From: Christian Franke <[email protected]>
Date: Tue, 4 May 2021 11:52:02 +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 | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/mingw-w64-crt/misc/gettimeofday.c
b/mingw-w64-crt/misc/gettimeofday.c
index 50ffcfc7..968f3ed3 100644
--- a/mingw-w64-crt/misc/gettimeofday.c
+++ b/mingw-w64-crt/misc/gettimeofday.c
@@ -41,8 +41,23 @@ 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 GetSystemTimeAsFileTime_t GetSystemTimeAsFileTime_p /* = 0 */;
+
+ /* Set function pointer during first call */
+ GetSystemTimeAsFileTime_t get_time =
+ __atomic_load_n (&GetSystemTimeAsFileTime_p, __ATOMIC_RELAXED);
+ 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 */
+ __atomic_store_n (&GetSystemTimeAsFileTime_p, get_time,
__ATOMIC_RELAXED);
+ }
+
+ 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