Extract shared code from timer

This commit is contained in:
YanWQ-monad
2024-09-24 21:01:34 +08:00
committed by Tate, Hongliang Tian
parent c4cb0f1aef
commit 839c2a6689
12 changed files with 47 additions and 38 deletions

View File

@ -4,19 +4,16 @@
mod apic;
mod hpet;
mod jiffies;
pub(crate) mod pit;
use alloc::{boxed::Box, vec::Vec};
use core::{cell::RefCell, sync::atomic::Ordering};
use core::sync::atomic::Ordering;
pub use jiffies::Jiffies;
use spin::Once;
use self::apic::APIC_TIMER_CALLBACK;
use crate::{
arch::x86::kernel,
cpu_local,
timer::INTERRUPT_CALLBACKS,
trap::{self, IrqLine, TrapFrame},
};
@ -51,24 +48,8 @@ pub(super) fn init() {
TIMER_IRQ.call_once(|| timer_irq);
}
cpu_local! {
static INTERRUPT_CALLBACKS: RefCell<Vec<Box<dyn Fn() + Sync + Send>>> = RefCell::new(Vec::new());
}
/// Registers 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::disable_local();
INTERRUPT_CALLBACKS
.get_with(&irq_guard)
.borrow_mut()
.push(Box::new(func));
}
fn timer_callback(_: &TrapFrame) {
jiffies::ELAPSED.fetch_add(1, Ordering::SeqCst);
crate::timer::jiffies::ELAPSED.fetch_add(1, Ordering::SeqCst);
let irq_guard = trap::disable_local();
let callbacks_guard = INTERRUPT_CALLBACKS.get_with(&irq_guard);

View File

@ -45,6 +45,7 @@ pub mod prelude;
pub mod smp;
pub mod sync;
pub mod task;
pub mod timer;
pub mod trap;
pub mod user;

View File

@ -13,8 +13,8 @@
use log::{LevelFilter, Metadata, Record};
use crate::{
arch::timer::Jiffies,
boot::{kcmdline::ModuleArg, kernel_cmdline},
timer::Jiffies,
};
const LOGGER: Logger = Logger {};

View File

@ -13,7 +13,7 @@ use core::sync::atomic::{AtomicBool, Ordering};
use spin::Once;
use super::{preempt::cpu_local, processor, Task};
use crate::{arch::timer, cpu::PinCurrentCpu, prelude::*, task::disable_preempt};
use crate::{cpu::PinCurrentCpu, prelude::*, task::disable_preempt, timer};
/// Injects a scheduler implementation into framework.
///

View File

@ -5,7 +5,7 @@ use core::{
time::Duration,
};
use super::TIMER_FREQ;
use crate::arch::timer::TIMER_FREQ;
/// Jiffies is a term used to denote the units of time measurement by the kernel.
///
@ -14,7 +14,7 @@ use super::TIMER_FREQ;
#[derive(Copy, Clone, Debug)]
pub struct Jiffies(u64);
pub(super) static ELAPSED: AtomicU64 = AtomicU64::new(0);
pub(crate) static ELAPSED: AtomicU64 = AtomicU64::new(0);
impl Jiffies {
/// Creates a new instance.

30
ostd/src/timer/mod.rs Normal file
View File

@ -0,0 +1,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::disable_local();
INTERRUPT_CALLBACKS
.get_with(&irq_guard)
.borrow_mut()
.push(Box::new(func));
}