Open
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCF-sleep_until`#![feature(sleep_until)]``#![feature(sleep_until)]`T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
Feature gate: #![feature(sleep_until)]
This is a tracking issue for the thread::sleep_until method. It pauses the current thread until a given deadline.
Public API
// std::thread
pub fn sleep_until(deadline: Instant);Steps / History
- APC: thread::sleep_until, wait until a deadline is reached libs-team#237
- Implementation: Add implementation for thread::sleep_until #113753
- Use a specialized implementation on some targets
- UNIXes with
clock_nanosleep: Specialize sleep_until implementation for unix (except mac) #141829 - Apple platforms: std: implement
sleep_untilon Apple platforms #151004 - A few niche platforms: std:
sleep_untilon Motor and VEX #151538 - Fuchsia: std: implement
sleep_untilfor Fuchsia #152617 - Other platforms (notably Windows)
- UNIXes with
- Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- Clock used: should the clock be the same as
thread::sleep, something different or passed
in by the user? - Deadline type: should the argument be an
Instant, aSystemTimeor do we support both?
Footnotes
Reacted by Michael Diamond and zopsicle
Reacted by Gabriel Bjørnager Jensen
Reacted by José Luis Cruz
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCF-sleep_until`#![feature(sleep_until)]``#![feature(sleep_until)]`T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
yara-blue commentedon Jul 16, 2023
The clock
Each of the three options has its downsides:
Sleep is not consistent between platforms. For example on Linux time
suspended is not counted while on other Unixes it is. Not counting time
suspended is a bad default, kernel devs tried to fix, which caused enough issues it was reverted soon after. That just underlines the need to get this right before stabilizing.
Using a different clock could get confusing, for example when the time slept
by sleep_until was different from what a sleep call would yield. Finally, if
the argument is of type
Instantthen the time slept should match whatInstant::elapsedreports. It currently uses the Monotonic clock on allplatforms.
Making the clock a choice, means designing a new clock API (proposed in the
APC by @the8472). It would make 'sleep_until' more complex to use and breaks
the similarity between it and
sleep.I think the clock question is not constrained to
sleep_until. There are moreAPIs that could support configurable clocks. I propose we separate that question
from
sleep_untiland address it in its own APC.There we can figure out:
that still support the std such as the esp32.
I would love to write a Pre-Rfc for such an API if we decide to go that route.
Type of the deadline argument
I am in favor of using
Instant, that implies monotonic behavior which isneeded for most use cases (cache invalidation etc.). We could offer a version
that can take a
SystemTimeas argument. I am not aware of any use cases forthat though I would love to hear them.
If we decide to support both we need to figure out if we want two functions or
a generic argument. The latter would involve introducing a trait, maybe
Deadline?Auto merge of rust-lang#113753 - dvdsk:master, r=dtolnay
SUPERCILEX commentedon Sep 24, 2023
Are there OS APIs that support sleeping to a deadline? The current implementation will have large tail latencies:
the8472 commentedon Sep 24, 2023
There's
clock_nanosleepthat supports deadlines, it has been part of POSIX for a while so I assume a lot of unices have it.the8472 commentedon Sep 24, 2023
Wall-time based synchronization/execution of events. E.g. if something is supposed to run in a cronjob-like fashion. Or a distributed protocol that says "nodes try to rendezvous every 5 minutes, at the start of a minute divisible by 5, UTC".
yara-blue commentedon Sep 24, 2023
Thanks for bringing this up! We can freely use that on Linux, the minimum kernel version current Rust supports includes it. I would have to check it for POSIX. Even if that does not pan out we should 'specialize' this method on Linux.
Before we go down that road I would like to nail down the API. To answer that we must know whether we want a Clock API in Rust. That however is better addressed in a separate proposal. I am more then willing to write that up however I would like to estimate interest first.
I am not sure how to proceed there, an option would be to create a crate that adds a clock API to std-lib functions/types where appropriate (using traits). Then look at how popular that gets over time. (taking care to prevent an itertools situation). Another easier path would be a users forum post or github code search.
the8472 commentedon Sep 24, 2023
You could also look at prior art in other languages and frequently used libraries. E.g. C++ has it std::chrono.
joboet commentedon Sep 24, 2023
macOS and the other Apple platforms don't have it, even on the newest versions.
the8472 commentedon Sep 24, 2023
There seems to be a function called
mach_wait_untilbut I can't find any docs for it.yara-blue commentedon Nov 23, 2023
Replying to @neachdainn who commented on the RFC
It's temporary, the idea was to see if we could take the clock choice further and introduce a clock API. That would allow use of clocks that are not supported on each platform (such as clock
BOOTTIME). Unfortunately I haven't done that yet.I am gonna paraphrase your use case here, I think it is an important one:
As it is critical to the above use case I will open a PR to switch over the placeholder implementation for Linux to use
clock_nanosleepwithTIMER_ABSTIME. We should also document the behavior when using real time schedulers.Then I will see if we can get the same behavior on the other operation systems.
yara-blue commentedon Nov 26, 2023
This is a tracking comment tracking the effort to specialize/optimizing the
sleep_untilimplementation for major target_os's.Possible/In progress:
SubscriptionClockand flagABSTIMESeems impossible for now
Hard
Ignored
biabbas commentedon Aug 9, 2024
@dvdsk Clock Nano sleep is available in libc for VxWorks.
15 remaining items
Rollup merge of rust-lang#151004 - apple-sleep-until, r=ChrisDenton
Unrolled build for #151004
Rollup merge of #151004 - apple-sleep-until, r=ChrisDenton
Rollup merge of #151004 - apple-sleep-until, r=ChrisDenton
Auto merge of #141829 - dvdsk:sleep_until_linux, r=cuviper,RalfJung
yara-blue commentedon Feb 27, 2026
It's been a while :)
Pretty sure we can resolve all questions:
T-libs recently had a meeting and the agreement is that we should not define the clock. See rust-lang/libs-team#733 (comment)
SystemTime can be changed by the system admin while sleeping, while
clock_nanosleepwithCLOCK_REALTIMEtakes this into account I strongly doubt all current and especially future platforms will be able to. We could simulate the behavior with really short sleeps checking the current SystemTime in between but... such slop should not live in the std :). The API should therefore besleep_until(Instant).Rollup merge of #151004 - apple-sleep-until, r=ChrisDenton
Rollup merge of rust-lang#152617 - joboet:fuchsia_sleep_until, r=nia-e
Rollup merge of rust-lang#152617 - joboet:fuchsia_sleep_until, r=nia-e
Rollup merge of rust-lang#152617 - joboet:fuchsia_sleep_until, r=nia-e
Rollup merge of rust-lang#152617 - joboet:fuchsia_sleep_until, r=nia-e
Rollup merge of rust-lang#152617 - joboet:fuchsia_sleep_until, r=nia-e
Unrolled build for #152617 in rollup 161434