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

@ -6,10 +6,9 @@
use alloc::sync::Arc;
use core::sync::atomic::{AtomicU64, Ordering};
use ostd::arch::{
read_tsc,
timer::{self, TIMER_FREQ},
tsc_freq,
use ostd::{
arch::{read_tsc, timer::TIMER_FREQ, tsc_freq},
timer,
};
use spin::Once;

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
use ostd::arch::timer::Jiffies;
use ostd::timer::Jiffies;
pub(super) fn get_network_timestamp() -> smoltcp::time::Instant {
let millis = Jiffies::elapsed().as_duration().as_millis();

View File

@ -4,7 +4,7 @@ use alloc::sync::Arc;
use core::time::Duration;
use log::trace;
use ostd::arch::timer::Jiffies;
use ostd::timer::Jiffies;
use super::{ext::IfaceEx, Iface, IFACES};
use crate::{

View File

@ -9,11 +9,9 @@ use core::time::Duration;
use id_alloc::IdAlloc;
use ostd::{
arch::{
timer::{self, TIMER_FREQ},
trap::is_kernel_interrupted,
},
arch::{timer::TIMER_FREQ, trap::is_kernel_interrupted},
sync::Mutex,
timer,
};
use super::Process;

View File

@ -5,11 +5,11 @@ use core::time::Duration;
use aster_time::read_monotonic_time;
use ostd::{
arch::timer::Jiffies,
cpu::{num_cpus, PinCurrentCpu},
cpu_local,
sync::SpinLock,
task::disable_preempt,
timer::Jiffies,
};
use paste::paste;
use spin::Once;

View File

@ -2,7 +2,7 @@
use alloc::{boxed::Box, vec::Vec};
use ostd::{arch::timer, sync::RwLock, trap::SoftIrqLine};
use ostd::{sync::RwLock, timer, trap::SoftIrqLine};
use crate::softirq_id::TIMER_SOFTIRQ_ID;

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));
}