Skip to content

Specialize sleep_until implementation for unix (except mac) #141829

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

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

dvdsk
Copy link
Contributor

@dvdsk dvdsk commented May 31, 2025

related tracking issue: #113752
Supersedes #118480 for the reasons see: #113752 (comment)

Replaces the generic catch all implementation with target_os specific ones for: linux/netbsd/freebsd/android/solaris/illumos etc. Other platforms like wasi, macos/ios/tvos/watchos and windows will follow in later separate PR's (once this is merged).

@rustbot
Copy link
Collaborator

rustbot commented May 31, 2025

r? @tgross35

rustbot has assigned @tgross35.
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

@rustbot rustbot added O-hermit Operating System: Hermit O-itron Operating System: ITRON O-SGX Target: SGX O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows 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 May 31, 2025
@dvdsk
Copy link
Contributor Author

dvdsk commented May 31, 2025

r? @cuviper

@rustbot rustbot assigned cuviper and unassigned tgross35 May 31, 2025
@workingjubilee workingjubilee changed the title Specialize sleep_until implementation for unix (expect mac) Specialize sleep_until implementation for unix (except mac) May 31, 2025
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 8010ec8 to 295e8d1 Compare May 31, 2025 19:54
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 295e8d1 to 1e7d958 Compare May 31, 2025 20:04
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 1e7d958 to 406e32b Compare May 31, 2025 21:31
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk marked this pull request as draft May 31, 2025 22:37
@dvdsk
Copy link
Contributor Author

dvdsk commented May 31, 2025

This can be done without touching all pal's, ill be doing that tomorrow, now its bed time 😴

edit: I was mistaken, tidy does not allow #[cfg(...)] in strc/thread/mod.rs probably for a good reason. Therefore we need a trivial not platform specific sleep_until in every pal that does not have a specialized one.

@rustbot rustbot added the O-wasm Target: WASM (WebAssembly), http://webassembly.org/ label Jun 1, 2025
@dvdsk dvdsk force-pushed the sleep_until_linux branch from 4bcfd27 to f657ec1 Compare June 1, 2025 07:08
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from f657ec1 to 1127a50 Compare June 1, 2025 07:26
@rust-log-analyzer

This comment has been minimized.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 1127a50 to 60edd5a Compare June 1, 2025 07:35
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented Jul 5, 2025

@rustbot author
based on review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 5, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@dvdsk dvdsk force-pushed the sleep_until_linux branch from b37e03e to d3f167f Compare July 5, 2025 10:06
@dvdsk
Copy link
Contributor Author

dvdsk commented Jul 5, 2025

@rustbot ready

Apologies for the unnecessary rebase.

Changes:

  • Moved clock_nanosleep tests in libc-time.rs to their own module. That module is only compiled for platforms supporting clock_nanosleep
  • Call the tests for clock_nanosleep at the end of the main
  • Adds a helper for adding 100ms to a timespec that handles nsec growing beyond 1s
  • Undo the accidental change to test call order in main (libc-time.rs)
  • Use eval_libc_i32 for getting the value for flag TIMER_ABSTIME

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 5, 2025
@rust-log-analyzer

This comment has been minimized.

fn add_100_millis(mut ts: libc::timespec) -> libc::timespec {
const SECOND: i64 = 1_000_000_000;
ts.tv_nsec += SECOND / 10;
ts.tv_sec = ts.tv_nsec / SECOND;
Copy link
Member

@RalfJung RalfJung Jul 5, 2025

Choose a reason for hiding this comment

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

Isn't this still wrong?

Suggested change
ts.tv_sec = ts.tv_nsec / SECOND;
// If this pushes tv_nsec to SECOND or higher, we need to overflow to tv_sec.
ts.tv_sec += ts.tv_nsec / SECOND;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be this now:

fn add_100_millis(mut ts: libc::timespec) -> libc::timespec {
    const SECOND: i64 = 1_000_000_000;
    ts.tv_nsec += SECOND / 10;
    ts.tv_sec = ts.tv_nsec / SECOND;
    ts.tv_nsec %= SECOND;
    ts
}

Copy link
Member

@RalfJung RalfJung Jul 5, 2025

Choose a reason for hiding this comment

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

But that's wrong, as I already said above. Why do you reset tv_sec to 0 or 1??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the overflow of tv_nsec is taken care of by:

ts.tv_nsec %= SECOND;

right?
I tested it in the playground and there it seems to work https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=8c8dcb8b0f9c7238b747b61e073ddc14

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But that's wrong, as I already said above. Why do you reset tv_sec to 0 or 1??

oh damn I get it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry I'm struggling with this, no idea why. Thanks for your patience Ralf.

Copy link
Member

Choose a reason for hiding this comment

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

Better take a bit more time and think things through twice next time. :)

My suggestion above has a comment, I think that's still a useful comment to explain the (clearly non-obvious!) logic. Please add that comment.

@RalfJung
Copy link
Member

RalfJung commented Jul 5, 2025

Miri code looks good once the last comment nit is resolved. :)

@RalfJung
Copy link
Member

RalfJung commented Jul 5, 2025

Regarding trying on more Unix platforms... this should cover various unix flavors
@bors2 try jobs=dist-x86_64-*

@rust-bors
Copy link

rust-bors bot commented Jul 5, 2025

⌛ Trying commit 6e212a3 with merge 99000fa

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jul 5, 2025
Specialize sleep_until implementation for unix (except mac)

related tracking issue: #113752
Supersedes #118480 for the reasons see: #113752 (comment)

Replaces the generic catch all implementation with target_os specific ones for: linux/netbsd/freebsd/android/solaris/illumos etc. Other platforms like wasi, macos/ios/tvos/watchos and windows will follow in later separate PR's (once this is merged).
try-job: dist-x86_64-*
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-miri failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
tests/pass-dep/tokio/sleep.rs ... ok
tests/pass-dep/libc/mmap.rs ... ok

FAILED TEST: tests/pass-dep/libc/libc-time.rs
command: MIRI_ENV_VAR_TEST="0" MIRI_TEMP="/tmp/miri-uitest-FJujnZ" RUST_BACKTRACE="1" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/miri" "--error-format=json" "--sysroot=/checkout/obj/build/x86_64-unknown-linux-gnu/miri-sysroot" "-Dwarnings" "-Dunused" "-Ainternal_features" "-Zui-testing" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/tests/pass-dep/libc" "tests/pass-dep/libc/libc-time.rs" "-Zmiri-disable-isolation" "--extern" "cfg_if=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libcfg_if-be36324f0ee95265.rlib" "--extern" "cfg_if=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libcfg_if-be36324f0ee95265.rmeta" "--extern" "getrandom_01=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libgetrandom-007181d9c4cfc055.rlib" "--extern" "getrandom_01=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libgetrandom-007181d9c4cfc055.rmeta" "--extern" "getrandom_02=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libgetrandom-84f6e4e8ddd7a771.rlib" "--extern" "getrandom_02=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libgetrandom-84f6e4e8ddd7a771.rmeta" "--extern" "getrandom_03=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libgetrandom-043c4e355c5d602c.rlib" "--extern" "getrandom_03=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libgetrandom-043c4e355c5d602c.rmeta" "--extern" "libc=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/liblibc-631e67220b03e651.rlib" "--extern" "libc=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/liblibc-631e67220b03e651.rmeta" "--extern" "num_cpus=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libnum_cpus-39c8ada6a5ad2900.rlib" "--extern" "num_cpus=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libnum_cpus-39c8ada6a5ad2900.rmeta" "--extern" "page_size=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libpage_size-fc7aa79099eb3835.rlib" "--extern" "page_size=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libpage_size-fc7aa79099eb3835.rmeta" "--extern" "tempfile=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libtempfile-5b30a8ed54e62f26.rlib" "--extern" "tempfile=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libtempfile-5b30a8ed54e62f26.rmeta" "--extern" "tokio=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libtokio-82e582889a3a648b.rlib" "--extern" "tokio=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps/libtokio-82e582889a3a648b.rmeta" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/debug/deps" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/tmp/miri_ui/0/miri/i686-unknown-linux-gnu/debug/deps" "--edition" "2021" "--target" "i686-unknown-linux-gnu"

error: test got exit status: 1, but expected 0
 = note: compilation failed, but was expected to succeed

error: no output was expected
Execute `./miri test --bless` to update `tests/pass-dep/libc/libc-time.stderr` to the actual output
+++ <stderr output>
error[E0308]: mismatched types
##[error]  --> tests/pass-dep/libc/libc-time.rs:362:23
   |
LL |         ts.tv_nsec += SECOND / 10;
   |                       ^^^^^^^^^^^ expected `i32`, found `i64`

error[E0277]: cannot add-assign `i64` to `i32`
##[error]  --> tests/pass-dep/libc/libc-time.rs:362:20
   |
LL |         ts.tv_nsec += SECOND / 10;
   |                    ^^ no implementation for `i32 += i64`
   |
   = help: the trait `std::ops::AddAssign<i64>` is not implemented for `i32`
   = help: the following other types implement trait `std::ops::AddAssign<Rhs>`:
             `i32` implements `std::ops::AddAssign<&i32>`
             `i32` implements `std::ops::AddAssign`

error[E0308]: mismatched types
##[error]  --> tests/pass-dep/libc/libc-time.rs:364:35
   |
LL |         ts.tv_sec += ts.tv_nsec / SECOND;
   |                                   ^^^^^^ expected `i32`, found `i64`

error[E0277]: cannot divide `i32` by `i64`
##[error]  --> tests/pass-dep/libc/libc-time.rs:364:33
   |
LL |         ts.tv_sec += ts.tv_nsec / SECOND;
   |                                 ^ no implementation for `i32 / i64`
   |
   = help: the trait `std::ops::Div<i64>` is not implemented for `i32`
   = help: the following other types implement trait `std::ops::Div<Rhs>`:
             `&i32` implements `std::ops::Div<i32>`
             `&i32` implements `std::ops::Div`
             `i32` implements `std::ops::Div<&i32>`
             `i32` implements `std::ops::Div`

error[E0308]: mismatched types
##[error]  --> tests/pass-dep/libc/libc-time.rs:365:23
   |
LL |         ts.tv_nsec %= SECOND;
   |                       ^^^^^^ expected `i32`, found `i64`

error[E0277]: cannot calculate and assign the remainder of `i32` divided by `i64`
##[error]  --> tests/pass-dep/libc/libc-time.rs:365:20
   |
LL |         ts.tv_nsec %= SECOND;
   |                    ^^ no implementation for `i32 %= i64`
   |
   = help: the trait `std::ops::RemAssign<i64>` is not implemented for `i32`
   = help: the following other types implement trait `std::ops::RemAssign<Rhs>`:
             `i32` implements `std::ops::RemAssign<&i32>`
             `i32` implements `std::ops::RemAssign`

error: aborting due to 6 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.


full stderr:
error[E0308]: mismatched types
##[error]  --> tests/pass-dep/libc/libc-time.rs:362:23
   |
LL |         ts.tv_nsec += SECOND / 10;
   |                       ^^^^^^^^^^^ expected `i32`, found `i64`

error[E0277]: cannot add-assign `i64` to `i32`
##[error]  --> tests/pass-dep/libc/libc-time.rs:362:20
   |
LL |         ts.tv_nsec += SECOND / 10;
   |                    ^^ no implementation for `i32 += i64`
   |
   = help: the trait `std::ops::AddAssign<i64>` is not implemented for `i32`
   = help: the following other types implement trait `std::ops::AddAssign<Rhs>`:
             `i32` implements `std::ops::AddAssign<&i32>`
             `i32` implements `std::ops::AddAssign`

error[E0308]: mismatched types
##[error]  --> tests/pass-dep/libc/libc-time.rs:364:35
   |
LL |         ts.tv_sec += ts.tv_nsec / SECOND;
   |                                   ^^^^^^ expected `i32`, found `i64`

error[E0277]: cannot divide `i32` by `i64`
##[error]  --> tests/pass-dep/libc/libc-time.rs:364:33
   |
LL |         ts.tv_sec += ts.tv_nsec / SECOND;
   |                                 ^ no implementation for `i32 / i64`
   |
   = help: the trait `std::ops::Div<i64>` is not implemented for `i32`
   = help: the following other types implement trait `std::ops::Div<Rhs>`:
             `&i32` implements `std::ops::Div<i32>`
             `&i32` implements `std::ops::Div`
             `i32` implements `std::ops::Div<&i32>`
             `i32` implements `std::ops::Div`

error[E0308]: mismatched types
##[error]  --> tests/pass-dep/libc/libc-time.rs:365:23
   |
LL |         ts.tv_nsec %= SECOND;
   |                       ^^^^^^ expected `i32`, found `i64`

error[E0277]: cannot calculate and assign the remainder of `i32` divided by `i64`
##[error]  --> tests/pass-dep/libc/libc-time.rs:365:20
   |
LL |         ts.tv_nsec %= SECOND;
   |                    ^^ no implementation for `i32 %= i64`
   |
   = help: the trait `std::ops::RemAssign<i64>` is not implemented for `i32`
   = help: the following other types implement trait `std::ops::RemAssign<Rhs>`:
             `i32` implements `std::ops::RemAssign<&i32>`
             `i32` implements `std::ops::RemAssign`

error: aborting due to 6 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
---

Location:
   /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ui_test-0.29.2/src/lib.rs:369

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
error: test failed, to rerun pass `--test ui`

Caused by:
  process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-tools/x86_64-unknown-linux-gnu/release/deps/ui-bf27d3ce51670675 pass` (exit status: 1)
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:02:26
  local time: Sat Jul  5 13:15:37 UTC 2025
  network time: Sat, 05 Jul 2025 13:15:37 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

@dvdsk
Copy link
Contributor Author

dvdsk commented Jul 5, 2025

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 5, 2025
@rust-bors
Copy link

rust-bors bot commented Jul 5, 2025

☀️ Try build successful (CI)
Build commit: 99000fa (99000fa1c60a6e398f1a0c78ad37c6d1956a1f3a, parent: f0b67dd97d74610ee4185cf01c775a563c2017a2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-hermit Operating System: Hermit O-itron Operating System: ITRON O-SGX Target: SGX O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-wasm Target: WASM (WebAssembly), http://webassembly.org/ O-windows Operating system: Windows S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.