| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| //! Virtualized `#![no_std]` guest timer and sleep API (`vx_async::timer`). | |
| //! | |
| //! Multiplexes per-fiber wakeup deadlines onto the runtime's port wait loop | |
| //! via the link-time C-ABI hook `__vx_sleep_until(deadline: ZxTime) -> ZxStatus`, | |
| //! creating **zero** kernel `TimerDispatcher` objects. | |
| #![deny(unsafe_code)] | |
| use core::mem::{align_of, offset_of, size_of}; | |
| use vx_types::{ZX_TIME_INFINITE, ZxTime}; | |
| use vx_zx::Status; | |
| mod ffi { | |
| #![allow(unsafe_code)] | |
| use vx_types::{ZxStatus, ZxTime}; | |
| unsafe extern "C" { | |
| fn __vx_sleep_until(deadline: ZxTime) -> ZxStatus; | |
| } | |
| #[inline(always)] | |
| pub(crate) fn sleep_until_raw(deadline: ZxTime) -> ZxStatus { | |
| // SAFETY: `__vx_sleep_until` is a link-time C-ABI runtime symbol | |
| // provided by the runtime scheduler (or host test harness) that takes a | |
| // plain `i64` deadline and returns a `ZxStatus`. | |
| unsafe { __vx_sleep_until(deadline) } | |
| } | |
| } | |
| /// Cooperatively sleep the calling guest fiber until monotonic timestamp | |
| /// `deadline` (nanoseconds) using the runtime's virtual timer wheel | |
| /// (`0` kernel `TimerDispatcher` objects). | |
| /// | |
| /// - Returns `Ok(())` when `deadline` expires. | |
| /// - Returns `Err(Status::CANCELED)` if the fiber is cooperatively stopped | |
| /// (`ComponentController.Stop`) while sleeping. | |
| /// - Returns `Err(Status::NOT_SUPPORTED)` when invoked outside a hosted | |
| /// runtime with virtual timer support. | |
| #[curry::verified] | |
| pub fn sleep_until(deadline: ZxTime) -> Result<(), Status> { | |
| Status::ok(ffi::sleep_until_raw(deadline)) | |
| } | |
| /// Reusable virtual timer handle storing a single monotonic wakeup deadline | |
| /// (`#[repr(C)]`, `16` bytes, `8`-byte aligned, `Copy`, zero kernel handles). | |
| /// | |
| /// Designed for single-threaded state machines such as `activity` that arm, | |
| /// reset, cancel, and wait on idle-transition deadlines without allocating | |
| /// kernel `zx::Timer` objects. | |
| #[repr(C)] | |
| #[derive(Copy, Clone, PartialEq, Eq)] | |
| pub struct Timer { | |
| /// Monotonic wakeup timestamp in nanoseconds (`ZX_TIME_INFINITE` when disarmed). | |
| deadline: ZxTime, | |
| /// `true` iff a finite or explicit wakeup deadline is armed. | |
| armed: bool, | |
| /// Explicit zero padding for deterministic `#[repr(C)]` ABI layout. | |
| _pad: [u8; 7], | |
| } | |
| const _: () = assert!(size_of::<Timer>() == 16); | |
| const _: () = assert!(align_of::<Timer>() == 8); | |
| const _: () = assert!(offset_of!(Timer, deadline) == 0); | |
| const _: () = assert!(offset_of!(Timer, armed) == 8); | |
| const _: () = assert!(offset_of!(Timer, _pad) == 9); | |
| impl Timer { | |
| /// Disarmed [`Timer`] sentinel (`is_armed() == false`). | |
| const DISARMED: Self = Self { deadline: ZX_TIME_INFINITE, armed: false, _pad: [0; 7] }; | |
| /// Construct an armed [`Timer`] set to expire at monotonic `deadline` (ns). | |
| #[inline(always)] | |
| pub const fn at(deadline: ZxTime) -> Self { | |
| Self { deadline, armed: true, _pad: [0; 7] } | |
| } | |
| /// Return `true` iff this timer is currently armed. | |
| #[inline(always)] | |
| pub const fn is_armed(&self) -> bool { | |
| self.armed | |
| } | |
| /// Return the armed monotonic wakeup deadline, or `None` if disarmed. | |
| #[inline(always)] | |
| pub const fn deadline(&self) -> Option<ZxTime> { | |
| if self.armed { Some(self.deadline) } else { None } | |
| } | |
| /// Arm (or reschedule) this timer to `deadline` (monotonic ns). | |
| #[inline(always)] | |
| pub fn reset(&mut self, deadline: ZxTime) { | |
| self.deadline = deadline; | |
| self.armed = true; | |
| } | |
| /// Disarm this timer without issuing any syscall. | |
| #[inline(always)] | |
| pub fn cancel(&mut self) { | |
| self.deadline = ZX_TIME_INFINITE; | |
| self.armed = false; | |
| } | |
| /// Sleep until this timer's armed deadline via [`sleep_until`], returning | |
| /// `Err(Status::BAD_STATE)` if disarmed. | |
| #[curry::verified] | |
| pub fn wait(&self) -> Result<(), Status> { | |
| if !self.armed { | |
| return Err(Status::BAD_STATE); | |
| } | |
| sleep_until(self.deadline) | |
| } | |
| /// One-shot sleep: atomically disarms `self` and sleeps until its deadline. | |
| #[curry::verified] | |
| pub fn wait_once(&mut self) -> Result<(), Status> { | |
| if !self.armed { | |
| return Err(Status::BAD_STATE); | |
| } | |
| let d = self.deadline; | |
| self.cancel(); | |
| sleep_until(d) | |
| } | |
| } | |
| impl Default for Timer { | |
| #[inline(always)] | |
| fn default() -> Self { | |
| Self::DISARMED | |
| } | |
| } |