@@ -41,19 +41,15 @@ void swift_get_time(
41
41
#elif (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__wasi__))
42
42
clock_gettime (CLOCK_MONOTONIC, &continuous);
43
43
#elif defined(_WIN32)
44
- LARGE_INTEGER freq;
45
- QueryPerformanceFrequency (&freq);
46
- LARGE_INTEGER count;
47
- QueryPerformanceCounter (&count);
48
- // Divide count (number of ticks) by frequency (number of ticks per
49
- // second) to get the counter in seconds. We also need to multiply the
50
- // count by 1,000,000,000 to get nanosecond resolution. By multiplying
51
- // first, we maintain high precision. The resulting value is the tick
52
- // count in nanoseconds. Use 128-bit math to avoid overflowing.
53
- auto quadPart = static_cast <unsigned _BitInt (128 )>(count.QuadPart );
54
- auto ns = (quadPart * 1'000'000'000 ) / freq.QuadPart ;
55
- continuous.tv_sec = ns / 1'000'000'000 ;
56
- continuous.tv_nsec = ns % 1'000'000'000 ;
44
+ // This needs to match what swift-corelibs-libdispatch does
45
+
46
+ // QueryInterruptTimePrecise() outputs a value measured in 100ns
47
+ // units. We must divide the output by 10,000,000 to get a value in
48
+ // seconds and multiply the remainder by 100 to get nanoseconds.
49
+ ULONGLONG interruptTime;
50
+ (void )QueryInterruptTimePrecise (&interruptTime);
51
+ continuous.tv_sec = interruptTime / 10'000'000 ;
52
+ continuous.tv_nsec = (interruptTime % 10'000'000 ) * 100 ;
57
53
#else
58
54
#error Missing platform continuous time definition
59
55
#endif
@@ -72,32 +68,13 @@ void swift_get_time(
72
68
#elif (defined(__OpenBSD__) || defined(__FreeBSD__))
73
69
clock_gettime (CLOCK_UPTIME, &suspending);
74
70
#elif defined(_WIN32)
75
- // QueryUnbiasedInterruptTimePrecise() was added in Windows 10 and is, as
76
- // the name suggests, more precise than QueryUnbiasedInterruptTime().
77
- // Unfortunately, the symbol is not listed in any .lib file in the SDK and
78
- // must be looked up dynamically at runtime even if our minimum deployment
79
- // target is Windows 10.
80
- typedef decltype (QueryUnbiasedInterruptTimePrecise) *QueryUITP_FP;
81
- static QueryUITP_FP queryUITP = nullptr ;
82
- static swift::once_t onceToken;
83
- swift::once (onceToken, [] {
84
- if (HMODULE hKernelBase = GetModuleHandleW (L" KernelBase.dll" )) {
85
- queryUITP = reinterpret_cast <QueryUITP_FP>(
86
- GetProcAddress (hKernelBase, " QueryUnbiasedInterruptTimePrecise" )
87
- );
88
- }
89
- });
71
+ // This needs to match what swift-corelibs-libdispatch does
90
72
91
- // Call whichever API is available. Both output a value measured in 100ns
73
+ // QueryUnbiasedInterruptTimePrecise() outputs a value measured in 100ns
92
74
// units. We must divide the output by 10,000,000 to get a value in
93
75
// seconds and multiply the remainder by 100 to get nanoseconds.
94
76
ULONGLONG unbiasedTime;
95
- if (queryUITP) {
96
- (* queryUITP)(&unbiasedTime);
97
- } else {
98
- // Fall back to the older, less precise API.
99
- (void )QueryUnbiasedInterruptTime (&unbiasedTime);
100
- }
77
+ (void )QueryUnbiasedInterruptTimePrecise (&unbiasedTime);
101
78
suspending.tv_sec = unbiasedTime / 10'000'000 ;
102
79
suspending.tv_nsec = (unbiasedTime % 10'000'000 ) * 100 ;
103
80
#else
@@ -128,10 +105,8 @@ switch (clock_id) {
128
105
#elif (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__wasi__))
129
106
clock_getres (CLOCK_MONOTONIC, &continuous);
130
107
#elif defined(_WIN32)
131
- LARGE_INTEGER freq;
132
- QueryPerformanceFrequency (&freq);
133
108
continuous.tv_sec = 0 ;
134
- continuous.tv_nsec = 1'000'000'000 / freq. QuadPart ;
109
+ continuous.tv_nsec = 100 ;
135
110
#else
136
111
#error Missing platform continuous time definition
137
112
#endif
0 commit comments