Skip to content

[Threading] Always round up when converting std::chrono::durations. #61352

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

Merged
merged 4 commits into from
Sep 30, 2022

Conversation

al45tair
Copy link
Contributor

We were inadvertently rounding down, which was fine almost 100% of the time, but on Windows where the timer resolution is only 1ms, it made the test very slightly flaky because we were asking to wait for 9ms rather than 10ms, then checking we'd waited at least 10ms.

We should explicitly round up, for that reason.

rdar://100236038

We were inadvertently rounding down, which was fine almost 100% of the
time, but on Windows where the timer resolution is only 1ms, it made
the test _very slightly_ flaky because we were asking to wait for
9ms rather than 10ms, then checking we'd waited at least 10ms.

We should explicitly round up, for that reason.

rdar://100236038
@al45tair
Copy link
Contributor Author

@swift-ci Please test

@al45tair
Copy link
Contributor Author

Hmmm. Seems this wasn't the only problem on Windows making the test flaky. I wonder if the server applied a time adjustment during the SleepConditionVariableSRW() call. Maybe the thing to do is to change the test to allow things to return 10% "sooner" than it thinks they should?

@al45tair
Copy link
Contributor Author

No, I don't think the time adjustment explanation is the right one. That shouldn't really be happening because we're using steady_clock. I think it might be the odd behaviour of Sleep functions on Windows, where (apparently) if you try to wait for less than a system tick (by default 1/64s), they can return early.

Let's try increasing the system tick rate during the test. I'll run the tests a few times to make sure it works.

@al45tair
Copy link
Contributor Author

@swift-ci Please test Windows platform

Copy link
Contributor

@mikeash mikeash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, once it works.

@al45tair
Copy link
Contributor Author

@swift-ci Please test Windows platform

@al45tair
Copy link
Contributor Author

@swift-ci Please test Windows platform

Windows' behaviour wrt `Sleep` family functions can be odd.  Apparently,
if you specify a time lower than a system tick, they can return early.
This makes the test flaky when it shouldn't be.  To fix it, we use the
Multimedia functions to adjust the system tick count as low as we can
get it.

rdar://100236038
@al45tair
Copy link
Contributor Author

@swift-ci Please test Windows platform

Rather than bodging the test to make it more robust, fix the functions
in the Threading layer to behave the same as they do on other platforms,
i.e. to guarantee that they always wait *at least* the amount of time
you asked for.

rdar://100236038
@al45tair
Copy link
Contributor Author

@swift-ci Please test Windows platform

@al45tair
Copy link
Contributor Author

Woo! That seems to have worked.

@al45tair
Copy link
Contributor Author

@swift-ci please smoke test

@al45tair al45tair merged commit 7d840f1 into swiftlang:main Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants