Skip to content

Conversation

@joboet
Copy link
Member

@joboet joboet commented Jan 21, 2026

nanosleep is specified to use CLOCK_REALTIME but the documentation (especially the example) for sleep imply that it measures time using Instant, which uses CLOCK_MONOTONIC. Thus, this PR makes sleep use a relative clock_nanosleep with CLOCK_MONOTONIC where available. This doesn't make a difference for Linux (which uses CLOCK_MONOTONIC for nanosleep anyway) but is relevant for e.g. FreeBSD.

This also restores nanosecond-sleep precision for WASI, since #150290 was caused by nanosleep internally using clock_nanosleep with CLOCK_REALTIME which is unsupported on WASIp2.

CC @alexcrichton for the WASI fix

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 21, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 21, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@joboet joboet force-pushed the sleep_clock_monotonic branch from a7a0704 to 443765e Compare January 21, 2026 11:29
@alexcrichton
Copy link
Member

r=me on the wasi side of things, thanks!

@Amanieu
Copy link
Member

Amanieu commented Jan 22, 2026

LGTM for the other platforms.

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 22, 2026

📌 Commit 443765e has been approved by Amanieu

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 22, 2026
rust-bors bot pushed a commit that referenced this pull request Jan 23, 2026
Rollup of 4 pull requests

Successful merges:

 - #151450 (std: use `clock_nanosleep` for `sleep` where available)
 - #151494 (std: ensure that the deadline has passed in `sleep_until`)
 - #151498 (global.rs: improve readability of re-entrance section)
 - #151504 (Reorganizing tests/ui/issues 11 tests [3/N])

r? @ghost
@rust-bors rust-bors bot merged commit d2acb11 into rust-lang:main Jan 23, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Jan 23, 2026
rust-timer added a commit that referenced this pull request Jan 23, 2026
Rollup merge of #151450 - joboet:sleep_clock_monotonic, r=Amanieu

std: use `clock_nanosleep` for `sleep` where available

`nanosleep` is specified to use `CLOCK_REALTIME` but the documentation (especially the example) for `sleep` imply that it measures time using `Instant`, which uses `CLOCK_MONOTONIC`. Thus, this PR makes `sleep` use a relative `clock_nanosleep` with `CLOCK_MONOTONIC` where available. This doesn't make a difference for Linux (which uses `CLOCK_MONOTONIC` for `nanosleep` anyway) but is relevant for e.g. FreeBSD.

This also restores nanosecond-sleep precision for WASI, since #150290 was caused by `nanosleep` internally using `clock_nanosleep` with `CLOCK_REALTIME` which is unsupported on WASIp2.

CC @alexcrichton for the WASI fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants