Skip to content

Commit 81d5d74

Browse files
authored
Rollup merge of #152617 - joboet:fuchsia_sleep_until, r=nia-e
std: implement `sleep_until` for Fuchsia Tracking issue: #113752 Unfortunately Fuchsia's `clock_nanosleep` is [only a stub](https://cs.opensource.google/fuchsia/fuchsia/+/main:zircon/third_party/ulib/musl/src/time/clock_nanosleep.c;l=4;drc=26e68e7948a6cc4a1c4a0c8eb19b7e4a2dc27153). The kernel's `zx_nanosleep` syscall however does exactly what's needed. I've done a few drive-by changes in the two outer commits, our uses of `zx_time_t` use the `zx_instant_mono_t` alias nowadays and the syscall table in the `sleep_until` documentation didn't mark the function names as code. @rustbot ping fuchsia
2 parents 8925ea3 + 1c0a0d9 commit 81d5d74

5 files changed

Lines changed: 36 additions & 16 deletions

File tree

library/std/src/sys/pal/unix/fuchsia.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ use crate::io;
99
// Time //
1010
//////////
1111

12-
pub type zx_time_t = i64;
12+
pub type zx_instant_mono_t = i64;
1313

14-
pub const ZX_TIME_INFINITE: zx_time_t = i64::MAX;
14+
pub const ZX_TIME_INFINITE: zx_instant_mono_t = i64::MAX;
1515

1616
unsafe extern "C" {
17-
pub safe fn zx_clock_get_monotonic() -> zx_time_t;
17+
pub safe fn zx_clock_get_monotonic() -> zx_instant_mono_t;
18+
pub safe fn zx_nanosleep(deadline: zx_instant_mono_t) -> zx_status_t;
1819
}
1920

2021
/////////////
@@ -62,15 +63,15 @@ unsafe extern "C" {
6263
pub fn zx_object_wait_one(
6364
handle: zx_handle_t,
6465
signals: zx_signals_t,
65-
timeout: zx_time_t,
66+
deadline: zx_instant_mono_t,
6667
pending: *mut zx_signals_t,
6768
) -> zx_status_t;
6869

6970
pub fn zx_futex_wait(
7071
value_ptr: *const zx_futex_t,
7172
current_value: zx_futex_t,
7273
new_futex_owner: zx_handle_t,
73-
deadline: zx_time_t,
74+
deadline: zx_instant_mono_t,
7475
) -> zx_status_t;
7576
pub fn zx_futex_wake(value_ptr: *const zx_futex_t, wake_count: u32) -> zx_status_t;
7677
pub fn zx_futex_wake_single_owner(value_ptr: *const zx_futex_t) -> zx_status_t;
@@ -117,7 +118,7 @@ pub type zx_info_process_flags_t = u32;
117118
#[repr(C)]
118119
pub struct zx_info_process_t {
119120
pub return_code: i64,
120-
pub start_time: zx_time_t,
121+
pub start_time: zx_instant_mono_t,
121122
pub flags: zx_info_process_flags_t,
122123
pub reserved1: u32,
123124
}

library/std/src/sys/thread/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ cfg_select! {
7272
target_os = "vxworks",
7373
target_os = "wasi",
7474
target_vendor = "apple",
75+
target_os = "fuchsia",
7576
))]
7677
pub use unix::sleep_until;
7778
pub use unix::{
@@ -147,7 +148,8 @@ cfg_select! {
147148
target_os = "wasi",
148149
target_vendor = "apple",
149150
target_os = "motor",
150-
target_os = "vexos"
151+
target_os = "vexos",
152+
target_os = "fuchsia",
151153
)))]
152154
pub fn sleep_until(deadline: crate::time::Instant) {
153155
use crate::time::Instant;

library/std/src/sys/thread/unix.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,16 @@ pub fn sleep_until(deadline: crate::time::Instant) {
796796
}
797797
}
798798

799+
#[cfg(target_os = "fuchsia")]
800+
pub fn sleep_until(deadline: crate::time::Instant) {
801+
use crate::sys::pal::fuchsia::{zx_cvt, zx_nanosleep};
802+
803+
let deadline = deadline.into_inner().into_deadline();
804+
if let Err(error) = zx_cvt(zx_nanosleep(deadline)) {
805+
panic!("zx_nanosleep failed: {error}");
806+
}
807+
}
808+
799809
pub fn yield_now() {
800810
let ret = unsafe { libc::sched_yield() };
801811
debug_assert_eq!(ret, 0);

library/std/src/sys/time/unix.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ impl Instant {
123123
// 126 bits.
124124
Some((nanos * u128::from(timebase.denom)).div_ceil(u128::from(timebase.numer)))
125125
}
126+
127+
#[cfg(target_os = "fuchsia")]
128+
pub fn into_deadline(self) -> crate::sys::pal::fuchsia::zx_instant_mono_t {
129+
self.t.tv_sec.saturating_mul(1_000_000_000).saturating_add(self.t.tv_nsec.as_inner().into())
130+
}
126131
}
127132

128133
impl AsInner<Timespec> for Instant {

library/std/src/thread/functions.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,19 +313,21 @@ pub fn sleep(dur: Duration) {
313313
///
314314
/// | Platform | System call |
315315
/// |-----------|----------------------------------------------------------------------|
316-
/// | Linux | [clock_nanosleep] (Monotonic Clock) |
317-
/// | BSD except OpenBSD | [clock_nanosleep] (Monotonic Clock) |
318-
/// | Android | [clock_nanosleep] (Monotonic Clock) |
319-
/// | Solaris | [clock_nanosleep] (Monotonic Clock) |
320-
/// | Illumos | [clock_nanosleep] (Monotonic Clock) |
321-
/// | Dragonfly | [clock_nanosleep] (Monotonic Clock) |
322-
/// | Hurd | [clock_nanosleep] (Monotonic Clock) |
323-
/// | Vxworks | [clock_nanosleep] (Monotonic Clock) |
316+
/// | Linux | [`clock_nanosleep`] (Monotonic Clock) |
317+
/// | BSD except OpenBSD | [`clock_nanosleep`] (Monotonic Clock) |
318+
/// | Android | [`clock_nanosleep`] (Monotonic Clock) |
319+
/// | Solaris | [`clock_nanosleep`] (Monotonic Clock) |
320+
/// | Illumos | [`clock_nanosleep`] (Monotonic Clock) |
321+
/// | Dragonfly | [`clock_nanosleep`] (Monotonic Clock) |
322+
/// | Hurd | [`clock_nanosleep`] (Monotonic Clock) |
323+
/// | Vxworks | [`clock_nanosleep`] (Monotonic Clock) |
324324
/// | Apple | `mach_wait_until` |
325+
/// | Fuchsia | [`zx_nanosleep`] |
325326
/// | Other | `sleep_until` uses [`sleep`] and does not issue a syscall itself |
326327
///
327328
/// [currently]: crate::io#platform-specific-behavior
328-
/// [clock_nanosleep]: https://linux.die.net/man/3/clock_nanosleep
329+
/// [`clock_nanosleep`]: https://linux.die.net/man/3/clock_nanosleep
330+
/// [`zx_nanosleep`]: https://fuchsia.dev/reference/syscalls/nanosleep
329331
///
330332
/// **Disclaimer:** These system calls might change over time.
331333
///

0 commit comments

Comments
 (0)