-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SuspendingClock on Windows does not suspend #63225
SuspendingClock on Windows does not suspend #63225
Conversation
399945e
to
27c684f
Compare
@swift-ci please test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this! I think that the commit message could be better (the issue helped me understand the change).
stdlib/public/Concurrency/Clock.cpp
Outdated
*seconds = 0; | ||
*nanoseconds = 1000; | ||
*nanoseconds = 1000000000 / freq.QuadPart; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QueryPerformanceFrequency()
never reports zero post-Windows XP. If we're concerned about that, we can check.
stdlib/public/Concurrency/Clock.cpp
Outdated
@@ -159,7 +153,7 @@ switch (clock_id) { | |||
*nanoseconds = suspending.tv_nsec; | |||
#elif defined(_WIN32) | |||
*seconds = 0; | |||
*nanoseconds = 1000; | |||
*nanoseconds = 100; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QueryUnbiasedInterruptTimePrecise()
has a unit of 100ns, so its highest possible resolution is 100ns. Neat.
@swift-ci please test |
… be dynamically looked up. Also, fix the math after calling it, and avoid floating-point math on the continuous clock.
@swift-ci please test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the change to the division; could you check that, please.
- Use 128-bit math with QueryPerformanceCounter() to avoid overflowing. - Use swift::fatalError() instead of abort() for bad clock IDs. - Use digit separators (C++14 feature, makes large integers easier to read.)
@swift-ci please test |
stdlib/public/Concurrency/Clock.cpp
Outdated
// count in nanoseconds. Use 128-bit math to avoid overflowing. | ||
DWORD64 hi = 0; | ||
DWORD64 lo = _umul128(count.QuadPart, 1'000'000'000, &hi); | ||
DWORD64 ns = _udiv128(hi, lo, freq.QuadPart, nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_udiv128()
generates a GPF if the result can't fit in a 64-bit integer, but if that happens that means the computer's been up for more than 2^64 nanoseconds… that's 584 years. If anybody runs into that problem, please let my great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-great-grandkid know.
@swift-ci please test windows |
@swift-ci please test windows |
@swift-ci please test |
@swift-ci please test |
@swift-ci please test linux |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@swift-ci please test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me, the cleanup for non-windows platforms looks correct (and definitely cleaner).
Thanks for taking this on!
Always happy to help! (Danger: I might noodle on this code a bit more in the future if I see ways to improve it further.) |
@swift-ci please test |
@swift-ci please test windows |
1 similar comment
@swift-ci please test windows |
The reported Windows failure is not related to this PR. |
@swift-ci please test windows |
This PR replaces the implementation of
SuspendingClock
on Windows with one that usesQueryUnbiasedInterruptTimePrecise()
, which does suspend.I also updated
swift_get_clock_res()
to produce accurate values on Windows instead of a hard-coded 1µs resolution.Raymond Chen gives a little bit of info about the various timer APIs here.
Resolves #63224.