Skip to main content

starnix_core/fs/fuchsia/
timer.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::power::OnWakeOps;
6use crate::task::{CurrentTask, Kernel};
7use crate::time::TargetTime;
8use crate::time::utc::estimate_boot_deadline_from_utc;
9use crate::vfs::timer::{TimelineChangeObserver, TimerOps};
10use starnix_uapi::errors::Errno;
11use starnix_uapi::{error, from_status_like_fdio};
12use std::sync::{Arc, Weak};
13use zx::HandleRef;
14
15pub struct MonotonicZxTimer {
16    timer: zx::MonotonicTimer,
17}
18
19impl MonotonicZxTimer {
20    pub fn new() -> Self {
21        // Starnix passes the task's `timerslack_ns` to `timer.set()` below
22        // (note: Linux timerfd uses a zero range in `hrtimer_start()`, whereas
23        // `timerslack_ns` in Linux only applies to `nanosleep`/`poll`/`select`/
24        // `epoll`/`futex`). Use `TimerSlackMode::Late` so coalescing never
25        // causes the timer to fire before its deadline.
26        Self { timer: zx::MonotonicTimer::create_with_slack_mode(zx::TimerSlackMode::Late) }
27    }
28}
29
30impl TimerOps for MonotonicZxTimer {
31    fn start(
32        &self,
33        current_task: &CurrentTask,
34        _source: Option<Weak<dyn OnWakeOps>>,
35        deadline: TargetTime,
36    ) -> Result<(), Errno> {
37        let timerslack = current_task.read().get_timerslack();
38        match deadline {
39            TargetTime::Monotonic(t) => {
40                self.timer.set(t, timerslack).map_err(|status| from_status_like_fdio!(status))?
41            }
42            TargetTime::BootInstant(_) | TargetTime::RealTime(_) => return error!(EINVAL),
43        };
44
45        Ok(())
46    }
47
48    fn stop(&self, _kernel: &Arc<Kernel>) -> Result<(), Errno> {
49        self.timer.cancel().map_err(|status| from_status_like_fdio!(status))
50    }
51
52    fn as_handle_ref(&self) -> HandleRef<'_> {
53        self.timer.as_handle_ref()
54    }
55
56    // Not supported.
57    fn get_timeline_change_observer(&self, _: &CurrentTask) -> Option<TimelineChangeObserver> {
58        None
59    }
60}
61
62pub struct BootZxTimer {
63    timer: zx::BootTimer,
64}
65
66impl BootZxTimer {
67    pub fn new() -> Self {
68        // See comment in `MonotonicZxTimer::new`: use `TimerSlackMode::Late`
69        // so coalescing never causes the timer to fire before its deadline.
70        Self { timer: zx::BootTimer::create_with_slack_mode(zx::TimerSlackMode::Late) }
71    }
72}
73
74impl TimerOps for BootZxTimer {
75    fn start(
76        &self,
77        current_task: &CurrentTask,
78        _source: Option<Weak<dyn OnWakeOps>>,
79        deadline: TargetTime,
80    ) -> Result<(), Errno> {
81        let timerslack = current_task.read().get_timerslack();
82        match deadline {
83            TargetTime::BootInstant(t) => {
84                self.timer.set(t, timerslack).map_err(|status| from_status_like_fdio!(status))?
85            }
86            TargetTime::RealTime(t) => {
87                let (boot_instant, _) = estimate_boot_deadline_from_utc(t);
88                self.timer
89                    .set(boot_instant, timerslack)
90                    .map_err(|status| from_status_like_fdio!(status))?
91            }
92            TargetTime::Monotonic(_) => return error!(EINVAL),
93        }
94        Ok(())
95    }
96
97    fn stop(&self, _kernel: &Arc<Kernel>) -> Result<(), Errno> {
98        self.timer.cancel().map_err(|status| from_status_like_fdio!(status))
99    }
100
101    fn as_handle_ref(&self) -> HandleRef<'_> {
102        self.timer.as_handle_ref()
103    }
104
105    // Not supported.
106    fn get_timeline_change_observer(&self, _: &CurrentTask) -> Option<TimelineChangeObserver> {
107        None
108    }
109}