Skip to content
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

Use Windows WaitableTimer with high resolution if available #16

Closed
terrorfisch opened this issue Sep 15, 2022 · 4 comments · Fixed by #22
Closed

Use Windows WaitableTimer with high resolution if available #16

terrorfisch opened this issue Sep 15, 2022 · 4 comments · Fixed by #22

Comments

@terrorfisch
Copy link

Windows introduced a flag to create a "high resolution" timer in Windows 10, version 1803. On my system I can get 500µs resolution with it without fiddling with timeBeginPeriod.

https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createwaitabletimerexw

@alexheretic
Copy link
Owner

Seems promising. We'll need to look into:

  • How to query if the OS is Windows 10 version 1803, where we expect this to work.
  • How to actually sleep using this with rust code (not immediately obvious to me at least).
  • Also maybe switching this crate to https:/microsoft/windows-rs.

@Alainx277
Copy link

This documentation explains how to use Windows timer objects.

Seems like the timer can be created with CreateWaitableTimerExW, then be used on the thread with WaitForSingleObject.

@alexheretic
Copy link
Owner

Rust 1.75.0 std now does this.

What's missing is for spin-sleep to detect at runtime if the Windows version actually supports this (if not we can fallback to current 1ms behaviour).

To do this i think we can try once to create a high res timer and use the success/fail of that call to indicate support. If supported, and on rust >= 1.75 we can use std::thread::sleep with a much tighter accuracy configured. I'll run some tests to find a decent default accuracy for high-res win timers.

As this relies on compilation with rust 1.75 this should become the min rust-version (even though the existing policy is latest stable, we want a stronger indication that 1.75 must be used).

@alexheretic
Copy link
Owner

Alternatively we could just do the sleeping using the windows api and we wouldn't have any particular requirement on rust version, which may be nicer overall.

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 a pull request may close this issue.

3 participants