kernel/
timer.rs

1//! Kernel timer module.
2//! 
3//! This module provides the kernel timer functionality, which is responsible for
4//! managing the system timer and scheduling tasks based on time intervals.
5//! 
6
7use crate::arch::timer::ArchTimer;
8use crate::environment::NUM_OF_CPUS;
9
10pub struct KernelTimer {
11    pub core_local_timer: [ArchTimer; NUM_OF_CPUS],
12    pub interval: u64,
13}
14
15static mut KERNEL_TIMER: Option<KernelTimer> = None;
16
17pub fn get_kernel_timer() -> &'static mut KernelTimer {
18    unsafe {
19        match KERNEL_TIMER {
20            Some(ref mut t) => t,
21            None => {
22                KERNEL_TIMER = Some(KernelTimer::new());
23                get_kernel_timer()
24            }
25        }
26    }
27}
28
29impl KernelTimer {
30    const fn new() -> Self {
31        KernelTimer {
32            core_local_timer: [const { ArchTimer::new() }; NUM_OF_CPUS],
33            interval: 0xffffffff_ffffffff,
34        }
35    }
36
37    pub fn init(&mut self) {
38        for i in 0..NUM_OF_CPUS {
39            self.core_local_timer[i].stop();
40        }
41    }
42
43    pub fn start(&mut self, cpu_id: usize) {
44        self.core_local_timer[cpu_id].start();
45    }
46
47    pub fn stop(&mut self, cpu_id: usize) {
48        self.core_local_timer[cpu_id].stop();
49    }
50
51    pub fn restart(&mut self, cpu_id: usize) {
52        self.stop(cpu_id);
53        self.start(cpu_id);
54    }
55
56    /* Set the interval in microseconds */
57    pub fn set_interval_us(&mut self, cpu_id: usize, interval: u64) {
58        self.core_local_timer[cpu_id].set_interval_us(interval);
59    }
60
61    pub fn get_time_us(&self, cpu_id: usize) -> u64 {
62        self.core_local_timer[cpu_id].get_time_us()
63    }
64}
65