Skip to main content

platform_rs/
timer.rs

1// Copyright 2026 The Fuchsia Authors
2//
3// Use of this source code is governed by a MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT
6
7#![no_std]
8
9pub mod power;
10
11/// Monotonic timeline instant in nanoseconds.
12#[repr(transparent)]
13#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct InstantMono(pub i64);
15
16impl core::ops::Add<i64> for InstantMono {
17    type Output = Self;
18
19    #[inline]
20    fn add(self, rhs: i64) -> Self {
21        Self(self.0 + rhs)
22    }
23}
24
25/// Monotonic timeline instant in ticks.
26#[repr(transparent)]
27#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
28pub struct InstantMonoTicks(pub i64);
29
30/// Boot timeline instant in nanoseconds.
31#[repr(transparent)]
32#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct InstantBoot(pub i64);
34
35/// Boot timeline instant in ticks.
36#[repr(transparent)]
37#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
38pub struct InstantBootTicks(pub i64);
39
40/// Monotonic timeline duration in nanoseconds.
41#[repr(transparent)]
42#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
43pub struct DurationMono(pub i64);
44
45/// Monotonic timeline duration in ticks.
46#[repr(transparent)]
47#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
48pub struct DurationMonoTicks(pub i64);
49
50/// Boot timeline duration in nanoseconds.
51#[repr(transparent)]
52#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
53pub struct DurationBoot(pub i64);
54
55/// Boot timeline duration in ticks.
56#[repr(transparent)]
57#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
58pub struct DurationBootTicks(pub i64);
59
60unsafe extern "C" {
61    fn cpp_timer_current_mono_ticks() -> InstantMonoTicks;
62    fn cpp_timer_current_boot_ticks() -> InstantBootTicks;
63    fn cpp_current_mono_time() -> InstantMono;
64    fn cpp_current_boot_time() -> InstantBoot;
65}
66
67/// Returns the current monotonic time in ticks.
68#[inline]
69pub fn timer_current_mono_ticks() -> InstantMonoTicks {
70    // SAFETY: Calling this FFI function has no preconditions and safely returns the platform timer
71    // ticks.
72    unsafe { cpp_timer_current_mono_ticks() }
73}
74
75/// Returns the current boot time in ticks.
76#[inline]
77pub fn timer_current_boot_ticks() -> InstantBootTicks {
78    // SAFETY: Calling this FFI function has no preconditions and safely returns the platform timer
79    // ticks.
80    unsafe { cpp_timer_current_boot_ticks() }
81}
82
83/// Current monotonic time in nanoseconds.
84#[inline]
85pub fn current_mono_time() -> InstantMono {
86    // SAFETY: Calling this FFI function has no preconditions and safely returns the platform
87    // monotonic time.
88    unsafe { cpp_current_mono_time() }
89}
90
91/// Current boot time in nanoseconds.
92#[inline]
93pub fn current_boot_time() -> InstantBoot {
94    // SAFETY: Calling this FFI function has no preconditions and safely returns the platform boot
95    // time.
96    unsafe { cpp_current_boot_time() }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn test_time_ordering_and_equality() {
105        assert!(DurationMono(10) < DurationMono(20));
106        assert_eq!(DurationMono(15), DurationMono(15));
107        assert!(InstantMono(100) < InstantMono(200));
108        assert_eq!(InstantMono(100) + 50, InstantMono(150));
109    }
110}