Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions library/std/src/sys/pal/unix/fuchsia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use crate::io;
// Time //
//////////

pub type zx_time_t = i64;
pub type zx_instant_mono_t = i64;

pub const ZX_TIME_INFINITE: zx_time_t = i64::MAX;
pub const ZX_TIME_INFINITE: zx_instant_mono_t = i64::MAX;

unsafe extern "C" {
pub safe fn zx_clock_get_monotonic() -> zx_time_t;
pub safe fn zx_clock_get_monotonic() -> zx_instant_mono_t;
pub safe fn zx_nanosleep(deadline: zx_instant_mono_t) -> zx_status_t;
}

/////////////
Expand Down Expand Up @@ -62,15 +63,15 @@ unsafe extern "C" {
pub fn zx_object_wait_one(
handle: zx_handle_t,
signals: zx_signals_t,
timeout: zx_time_t,
deadline: zx_instant_mono_t,
pending: *mut zx_signals_t,
) -> zx_status_t;

pub fn zx_futex_wait(
value_ptr: *const zx_futex_t,
current_value: zx_futex_t,
new_futex_owner: zx_handle_t,
deadline: zx_time_t,
deadline: zx_instant_mono_t,
) -> zx_status_t;
pub fn zx_futex_wake(value_ptr: *const zx_futex_t, wake_count: u32) -> zx_status_t;
pub fn zx_futex_wake_single_owner(value_ptr: *const zx_futex_t) -> zx_status_t;
Expand Down Expand Up @@ -117,7 +118,7 @@ pub type zx_info_process_flags_t = u32;
#[repr(C)]
pub struct zx_info_process_t {
pub return_code: i64,
pub start_time: zx_time_t,
pub start_time: zx_instant_mono_t,
pub flags: zx_info_process_flags_t,
pub reserved1: u32,
}
Expand Down
4 changes: 3 additions & 1 deletion library/std/src/sys/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ cfg_select! {
target_os = "vxworks",
target_os = "wasi",
target_vendor = "apple",
target_os = "fuchsia",
))]
pub use unix::sleep_until;
#[expect(dead_code)]
Expand Down Expand Up @@ -134,7 +135,8 @@ cfg_select! {
target_os = "wasi",
target_vendor = "apple",
target_os = "motor",
target_os = "vexos"
target_os = "vexos",
target_os = "fuchsia",
)))]
pub fn sleep_until(deadline: crate::time::Instant) {
use crate::time::Instant;
Expand Down
10 changes: 10 additions & 0 deletions library/std/src/sys/thread/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,16 @@ pub fn sleep_until(deadline: crate::time::Instant) {
}
}

#[cfg(target_os = "fuchsia")]
pub fn sleep_until(deadline: crate::time::Instant) {
use crate::sys::pal::fuchsia::{zx_cvt, zx_nanosleep};

let deadline = deadline.into_inner().into_deadline();
if let Err(error) = zx_cvt(zx_nanosleep(deadline)) {
panic!("zx_nanosleep failed: {error}");
}
}

pub fn yield_now() {
let ret = unsafe { libc::sched_yield() };
debug_assert_eq!(ret, 0);
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sys/time/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ impl Instant {
// 126 bits.
Some((nanos * u128::from(timebase.denom)).div_ceil(u128::from(timebase.numer)))
}

#[cfg(target_os = "fuchsia")]
pub fn into_deadline(self) -> crate::sys::pal::fuchsia::zx_instant_mono_t {
self.t.tv_sec.saturating_mul(1_000_000_000).saturating_add(self.t.tv_nsec.as_inner().into())
}
}

impl AsInner<Timespec> for Instant {
Expand Down
20 changes: 11 additions & 9 deletions library/std/src/thread/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,21 @@ pub fn sleep(dur: Duration) {
///
/// | Platform | System call |
/// |-----------|----------------------------------------------------------------------|
/// | Linux | [clock_nanosleep] (Monotonic clock) |
/// | BSD except OpenBSD | [clock_nanosleep] (Monotonic Clock)] |
/// | Android | [clock_nanosleep] (Monotonic Clock)] |
/// | Solaris | [clock_nanosleep] (Monotonic Clock)] |
/// | Illumos | [clock_nanosleep] (Monotonic Clock)] |
/// | Dragonfly | [clock_nanosleep] (Monotonic Clock)] |
/// | Hurd | [clock_nanosleep] (Monotonic Clock)] |
/// | Vxworks | [clock_nanosleep] (Monotonic Clock)] |
/// | Linux | [`clock_nanosleep`] (Monotonic clock) |
/// | BSD except OpenBSD | [`clock_nanosleep`] (Monotonic clock) |
/// | Android | [`clock_nanosleep`] (Monotonic clock) |
/// | Solaris | [`clock_nanosleep`] (Monotonic clock) |
/// | Illumos | [`clock_nanosleep`] (Monotonic clock) |
/// | Dragonfly | [`clock_nanosleep`] (Monotonic clock) |
/// | Hurd | [`clock_nanosleep`] (Monotonic clock) |
/// | Vxworks | [`clock_nanosleep`] (Monotonic clock) |
/// | Apple | `mach_wait_until` |
/// | Fuchsia | [`zx_nanosleep`] |
/// | Other | `sleep_until` uses [`sleep`] and does not issue a syscall itself |
///
/// [currently]: crate::io#platform-specific-behavior
/// [clock_nanosleep]: https://linux.die.net/man/3/clock_nanosleep
/// [`clock_nanosleep`]: https://linux.die.net/man/3/clock_nanosleep
/// [`zx_nanosleep`]: https://fuchsia.dev/reference/syscalls/nanosleep
///
/// **Disclaimer:** These system calls might change over time.
///
Expand Down
Loading