ostd/timer/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// SPDX-License-Identifier: MPL-2.0

//! The timer support.

pub(crate) mod jiffies;

use alloc::{boxed::Box, vec::Vec};
use core::cell::RefCell;

pub use jiffies::Jiffies;

use crate::{cpu_local, trap};

type InterruptCallback = Box<dyn Fn() + Sync + Send>;

cpu_local! {
    pub(crate) static INTERRUPT_CALLBACKS: RefCell<Vec<InterruptCallback>> = RefCell::new(Vec::new());
}

/// Register a function that will be executed during the system timer interruption.
pub fn register_callback<F>(func: F)
where
    F: Fn() + Sync + Send + 'static,
{
    let irq_guard = trap::irq::disable_local();
    INTERRUPT_CALLBACKS
        .get_with(&irq_guard)
        .borrow_mut()
        .push(Box::new(func));
}