mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 09:45:19 +08:00
feat: record time ticks
This commit is contained in:
parent
ea98eb5c61
commit
a65176bc57
@ -13,7 +13,7 @@ impl TimerOps for Timer {
|
|||||||
|
|
||||||
// enable timer interrupt and set next tick
|
// enable timer interrupt and set next tick
|
||||||
unsafe { riscv::register::sie::set_stimer() }
|
unsafe { riscv::register::sie::set_stimer() }
|
||||||
Self::tick();
|
Self::set_next();
|
||||||
info!("[Timer] begin to tick");
|
info!("[Timer] begin to tick");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ impl TimerOps for Timer {
|
|||||||
riscv::register::time::read64()
|
riscv::register::time::read64()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tick() {
|
fn set_next() {
|
||||||
sbi_rt::set_timer(Self::read_cycle() + TIMER_TICKS);
|
sbi_rt::set_timer(Self::read_cycle() + TIMER_TICKS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,8 @@ fn trap_handler(tf: &mut TrapContext, from_kernel: bool) {
|
|||||||
|
|
||||||
match scause.cause() {
|
match scause.cause() {
|
||||||
T::Interrupt(I::SupervisorTimer) => {
|
T::Interrupt(I::SupervisorTimer) => {
|
||||||
|
Timer::do_tick();
|
||||||
|
|
||||||
if !from_kernel {
|
if !from_kernel {
|
||||||
SCHEDULER.schedule()
|
SCHEDULER.schedule()
|
||||||
}
|
}
|
||||||
@ -61,7 +63,7 @@ fn trap_handler(tf: &mut TrapContext, from_kernel: bool) {
|
|||||||
_ => panic_fatal!("Unhandled Trap"),
|
_ => panic_fatal!("Unhandled Trap"),
|
||||||
}
|
}
|
||||||
|
|
||||||
Timer::tick();
|
Timer::set_next();
|
||||||
trace!("[Trap] exiting...");
|
trace!("[Trap] exiting...");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,16 @@
|
|||||||
pub struct Timer;
|
use core::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
|
||||||
|
#[thread_local]
|
||||||
|
pub static CURRENT_TICK: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
pub struct Timer
|
||||||
|
where Self: TimerOps;
|
||||||
|
|
||||||
pub trait TimerOps {
|
pub trait TimerOps {
|
||||||
fn init();
|
fn init();
|
||||||
fn read_cycle() -> u64;
|
fn read_cycle() -> u64;
|
||||||
fn tick();
|
fn set_next();
|
||||||
|
fn do_tick() {
|
||||||
|
CURRENT_TICK.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user