1use spin::RwLock;
4
5use crate::{device::RTC_DRIVERS, pr_info, vfs::TimeSpec};
6
7lazy_static::lazy_static! {
8 pub static ref REALTIME: RwLock<TimeSpec> = RwLock::new(TimeSpec::zero());
11}
12
13pub fn init() {
15 pr_info!("Initializing REALTIME clock...");
17 let mut realtime = REALTIME.write();
18 let sec = RTC_DRIVERS
19 .read()
20 .first()
21 .map(|rtc| rtc.read_epoch() as usize)
22 .unwrap_or(0);
23 let mtime = TimeSpec::monotonic_now();
24 let time = TimeSpec::new(sec as i64, 0) - mtime;
26 *realtime = time;
27 pr_info!(
28 "REALTIME clock initialized to {:?} seconds since epoch.",
29 time
30 );
31}
32
33pub fn update_realtime(time: &TimeSpec) {
37 let mut realtime = REALTIME.write();
38 *realtime = *time - TimeSpec::monotonic_now();
39}
40
41pub fn realtime_now() -> TimeSpec {
43 let realtime = REALTIME.read();
44 *realtime + TimeSpec::monotonic_now()
45}