Skip to main content

abi_application/
time.rs

1use crate::{Status, is_in_coroutine};
2use synchronization::once_lock::OnceLock;
3
4static TIME: OnceLock<&'static time::Manager<'static>> = OnceLock::new();
5/// Install the runtime's clock once. The native executable handler calls this
6/// before launch. Earlier reads return Unavailable.
7pub fn initialize(time_manager: &'static time::Manager<'static>) {
8    TIME.get_or_init(|| time_manager);
9}
10
11/// Read monotonic nanoseconds from an arbitrary epoch; may return Overflow.
12/// # Safety
13/// Output must be aligned and writable.
14pub unsafe extern "C" fn xila_time_monotonic(nanoseconds: *mut u64) -> Status {
15    if nanoseconds.is_null() {
16        return Status::InvalidArgument;
17    }
18    let Some(time) = TIME.try_get() else {
19        return Status::Unavailable;
20    };
21    match u64::try_from(time.monotonic_now().as_nanos()) {
22        Ok(value) => {
23            unsafe { nanoseconds.write(value) };
24            Status::Ok
25        }
26        Err(_) => Status::Overflow,
27    }
28}
29/// Read signed Unix UTC nanoseconds; unsynchronized clocks return Unavailable.
30/// # Safety
31/// Output must be aligned and writable.
32pub unsafe extern "C" fn xila_time_realtime(nanoseconds: *mut i64) -> Status {
33    if nanoseconds.is_null() {
34        return Status::InvalidArgument;
35    }
36    let Some(time) = TIME.try_get() else {
37        return Status::Unavailable;
38    };
39    match time.realtime_now() {
40        Ok(value) => match i64::try_from(value.as_nanos()) {
41            Ok(value) => {
42                unsafe { nanoseconds.write(value) };
43                Status::Ok
44            }
45            Err(_) => Status::Overflow,
46        },
47        Err(time::Error::Overflow) => Status::Overflow,
48        Err(_) => Status::Unavailable,
49    }
50}
51
52/// Monotonic clock identifier for `xila_time_resolution`.
53pub const XILA_CLOCK_MONOTONIC: u32 = 0;
54/// Realtime (Unix UTC) clock identifier for `xila_time_resolution`.
55pub const XILA_CLOCK_REALTIME: u32 = 1;
56
57/// Read a clock's nominal quantization in nanoseconds. Actual precision can be
58/// coarser. Realtime resolution stays readable while UTC is unsynchronized.
59/// An unknown clock identifier returns InvalidArgument; an uninstalled clock
60/// returns Unavailable. Outputs change only on success.
61/// # Safety
62/// Output must be aligned and writable.
63pub unsafe extern "C" fn xila_time_resolution(clock: u32, nanoseconds: *mut u64) -> Status {
64    if nanoseconds.is_null() || clock > XILA_CLOCK_REALTIME {
65        return Status::InvalidArgument;
66    }
67    let Some(time) = TIME.try_get() else {
68        return Status::Unavailable;
69    };
70    let resolution = if clock == XILA_CLOCK_MONOTONIC {
71        time.monotonic_resolution()
72    } else {
73        time.realtime_resolution()
74    };
75    match u64::try_from(resolution.as_nanos()) {
76        Ok(value) => {
77            unsafe { nanoseconds.write(value) };
78            Status::Ok
79        }
80        Err(_) => Status::Overflow,
81    }
82}
83
84/// Suspend the caller for at least the given nanoseconds on the executor's clock.
85/// Inside a coroutine this suspends it on the executor's timer, so other tasks
86/// run meanwhile; zero yields once. Outside a coroutine there is no executor
87/// waker for the timer, so the call polls the clock and zero returns at once.
88/// UTC corrections do not affect the wait, and `u64::MAX` waits practically
89/// forever.
90pub extern "C" fn xila_time_sleep(nanoseconds: u64) -> Status {
91    use embassy_time::{Instant, Timer};
92    let deadline = task::deadline_after_nanoseconds(nanoseconds);
93    if is_in_coroutine() {
94        crate::suspend_dynamic(core::pin::pin!(Timer::at(deadline)));
95    } else {
96        while Instant::now() < deadline {
97            core::hint::spin_loop();
98        }
99    }
100    Status::Ok
101}