-
Notifications
You must be signed in to change notification settings - Fork 20
thread::sleep_until, wait until a deadline is reached #237
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
Comments
The section also points to |
The other issue is the eternal question whether one wants to sleep for a duration including or excluding system suspend time. The answer often differs depending on the kind of task that is sleeping. Solving this properly would need some sort of |
Ahh sorry, what I meant is that because there is no I will edit the RFC and clarify that section. I will also add code readability for completeness. |
In my opinion |
I think some exploration which APIs and behaviors are available on supported platforms will be necessary. You don't necessarily have to do that work now. The team's answer might be "yes, but only if some portability conditions can be met" |
We discussed this in the @rust-lang/libs-api meeting today and are mostly happy with this proposal. We did discuss the possibility of support multiple clock sources ( |
Thanks for accepting the proposal! I am looking into the various APIs/platforms right now, trying to find a consistent clock source. Once I have learned a bit more I will hammer out an implementation and open a tracking issue and PR. |
APC (API change proposal): rust-lang/libs-team#237
Add implementation for thread::sleep_until - Feature gate is `thread::sleep_until` - Tracking issue is: rust-lang#113752 - APC: rust-lang/libs-team#237
Add implementation for thread::sleep_until - Feature gate is `thread::sleep_until` - Tracking issue is: #113752 - APC: rust-lang/libs-team#237
APC (API change proposal): rust-lang/libs-team#237
I just want to point out that the proposed solution isn't a full solution in that it ignores the possibility of drift. This probably doesn't matter for many implementations but is critical in situations where it does. Specifically: pub fn sleep_until(deadline: Instant) {
let now = Instant::now();
// What if the thread is suspended here? If that happens, you are no longer sleeping until `deadline` but
// are instead sleeping until `deadline + epsilon` which is not universally acceptable. The relevant OS's
// have operations to do actual deadline sleeping and those should be used here.
if let Some(delay) = deadline.checked_duration_since(now) {
thread::sleep(delay);
}
}
pub fn sleep_until(deadline: Instant) {
while libc::clock_nanosleep(libc::CLOCK_MONOTONIC, libc::TIMER_ABSTIME, deadline, ptr::null()) == libc::EINTR {}
} |
Add implementation for thread::sleep_until - Feature gate is `thread::sleep_until` - Tracking issue is: #113752 - APC: rust-lang/libs-team#237
Add implementation for thread::sleep_until - Feature gate is `thread::sleep_until` - Tracking issue is: #113752 - APC: rust-lang/libs-team#237
Implemented; see tracking issue: rust-lang/rust#113752
Proposal
Problem statement
There is no method to pause a thread until a deadline is reached. Currently that means writing one yourself. I argue the use case is common enough that it is worth it to bring this into the std. It would make code that needs this slightly shorter, a bit more readable and easier to write.
Last but not least while a pause until method is not hard to write it is easy to do incorrectly without noticing. A naive approach subtracts the deadline from
Instant::now()
. Currently, this works, however it might panic in the future see Monotonicity and Instant::sub. Addingsleep_until
lessens the impact of future rust versions panicking again a bit.Motivating examples or use cases
Most use-cases take the shape of a planning problem, performing some action at a set moment. For example today I wanted to use
sleep_until
to exponentially back off on requesting a certificate up to a deadline. Like any web request it may take a while before it fails, therefore we must take the time the work takes into account.This can be generalized by replacing
get_signed_certificate
with any fallible function that takes a non-trivial amount of time.On GitHub there are about 3k hits for rust code containing
fn sleep_until
. Though a minor argument, having this in the std could help porting async code using tokiossleep_until
easier.Solution sketch
I propose we make the solution look like the
sleep_until
function in tokio::time::sleep_until.The API:
The implementation:
playground link
Alternatives
If we chose not to implement
sleep_until
an example should be added tothread::sleep
to steer users away from using subtract on twoInstant
s.Links and related work
_until
suffix instead of_deadline
for functions that do not have meaningful blocking versions such as the thread::sleep family.What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
if the libs-api team decides to adopt this proposal I would like to implement the RFC
The text was updated successfully, but these errors were encountered: