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 16 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
dvdsk added 4 commits July 3, 2025 21:20
- In contradiction to nanosleep clock_nanosleep returns the error
  directly and does not require a call to `os::errno()`.
- The last argument to clock_nanosleep can be NULL removing the need
  for mutating the timespec.
- Missed an `allow(unused)` that could be made conditional.
This is intended to support the std's new sleep_until. Only
the clocks REALTIME and MONOTONIC are supported. The first because it
was trivial to add the second as its needed for sleep_until. Only
passing no flags or passing only TIMER_ABSTIME is supported.

If an unsupported flags or clocks are passed this implementation panics.
@dvdsk dvdsk force-pushed the sleep_until_linux branch from d91e6a9 to dcb3593 Compare July 3, 2025 19:20
@rust-log-analyzer

This comment has been minimized.

@dvdsk
Copy link
Contributor Author

dvdsk commented Jul 3, 2025

rebased to:

  • fix commit formatting unrelated MIRI code.
  • fix spell errors for months ago without adding new useless commit

edit: I just realized rebasing all the time makes this rather hard to review. I'll rebase/squash at the end, for now I'll focus on short descriptive commits.

@@ -396,14 +393,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// No flags set, the timespec should be interperted as a duration
// to sleep for
TimeoutAnchor::Relative
} else if flag == this.eval_libc("TIMER_ABSTIME").to_int(int_size) {
} else if flags == this.eval_libc("TIMER_ABSTIME").to_i32()? {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else if flags == this.eval_libc("TIMER_ABSTIME").to_i32()? {
} else if flags == this.eval_libc_i32("TIMER_ABSTIME") {

@dvdsk dvdsk force-pushed the sleep_until_linux branch from 7c039f4 to 2645080 Compare July 4, 2025 18:41
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment on lines 9 to 14
test_nanosleep();
test_clock_nanosleep_absolute();
test_clock_nanosleep_relative();
test_localtime_r_epoch();
test_localtime_r_gmt();
test_localtime_r_pst();
Copy link
Member

Choose a reason for hiding this comment

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

Not sure why you reordered an existing test here?^^

Since the other tests are all about getting the current time, seems best to have the sleep tests all together in a separate "section", e.g. at the end of main, separated from the existing tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure why you reordered an existing test here?^^

I really need to start paying a little more attention to the diffs I make

Since the other tests are all about getting the current time, seems best to have the sleep tests all together in a separate "section", e.g. at the end of main, separated from the existing tests.

Good idea

let mut ts = timespec_now(libc::CLOCK_MONOTONIC);
ts.tv_nsec += 1_000_000_000 / 10;
ts
};
Copy link
Member

Choose a reason for hiding this comment

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

Needs logic to deal with nsec growing to more than 1s

unsafe { timespec.assume_init() }
}

fn test_clock_nanosleep_absolute() {
Copy link
Member

Choose a reason for hiding this comment

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

These functions also need the cfg to not be built on macOS.

You can test this locally even if you are not on macOS:

./x test miri --target aarch64-apple-darwin  -- time

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can test this locally even if you are not on macOS:

that's super useful thanks!

@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
Comment on lines +424 to +425
ts.tv_nsec = ts.tv_nsec % SECOND;
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.

This doesn't do the right thing. You need to swap these two lines. With your version of the code, tv_sec will always end up being 0.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, also this should be tv_sec += ...!

}

/// Helper function to get the current time for testing relative sleeps
fn timespec_now(clock: libc::clockid_t) -> libc::timespec {
Copy link
Member

Choose a reason for hiding this comment

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

Please put the 2 helper functions above the functions that use them.

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-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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.