Skip to content

Commit

Permalink
refac: move timer static from arch to rt (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Dec 1, 2022
1 parent ce1155b commit 68a2145
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
4 changes: 0 additions & 4 deletions src/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ mod tests;

pub type MinPageSize = mm::size::Size4Kb;

pub fn tick_timer() {
interrupt::TIMER.advance_ticks(0);
}

#[cfg(target_os = "none")]
bootloader::entry_point!(arch_entry);

Expand Down
7 changes: 2 additions & 5 deletions src/arch/x86_64/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ pub fn enable_hardware_interrupts(acpi: Option<&acpi::InterruptModel>) {
controller
.start_periodic_timer(TIMER_INTERVAL)
.expect("10ms should be a reasonable interval for the PIT or local APIC timer...");
time::set_global_timer(&TIMER)
.expect("`enable_hardware_interrupts` should only be called once!");
tracing::info!(granularity = ?TIMER_INTERVAL, "global timer initialized")
}

Expand Down Expand Up @@ -56,8 +54,7 @@ static TSS: sync::Lazy<task::StateSegment> = sync::Lazy::new(|| {

pub(in crate::arch) static GDT: sync::InitOnce<Gdt> = sync::InitOnce::uninitialized();

const TIMER_INTERVAL: time::Duration = time::Duration::from_millis(10);
pub(super) static TIMER: time::Timer = time::Timer::new(TIMER_INTERVAL);
pub const TIMER_INTERVAL: time::Duration = time::Duration::from_millis(10);

static TEST_INTERRUPT_WAS_FIRED: AtomicUsize = AtomicUsize::new(0);

Expand Down Expand Up @@ -96,7 +93,7 @@ impl hal_core::interrupt::Handlers<Registers> for InterruptHandlers {
}

fn timer_tick() {
TIMER.pend_ticks(1);
crate::rt::TIMER.pend_ticks(1);
}

fn ps2_keyboard(scancode: u8) {
Expand Down
5 changes: 0 additions & 5 deletions src/arch/x86_64/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,3 @@ pub const DUMP_ARCH: Command = Command::new("arch")
Ok(())
}),
]);

pub fn dump_timer(_: &str) -> Result<(), shell::Error> {
tracing::info!(timer = ?super::interrupt::TIMER);
Ok(())
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ pub fn kernel_start(bootinfo: impl BootInfo, archinfo: crate::arch::ArchInfo) ->
// tracing.
arch::init(&bootinfo, &archinfo);

// initialize the kernel runtime.
rt::init();

#[cfg(test)]
arch::run_tests();

Expand Down
16 changes: 14 additions & 2 deletions src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use core::{
future::Future,
sync::atomic::{AtomicBool, AtomicUsize, Ordering::*},
};
use maitake::scheduler::{self, StaticScheduler, Stealer};
use maitake::{
scheduler::{self, StaticScheduler, Stealer},
time,
};
use mycelium_util::sync::InitOnce;
use rand::Rng;

Expand Down Expand Up @@ -49,6 +52,8 @@ struct Runtime {
/// 512 CPU cores ought to be enough for anybody...
pub const MAX_CORES: usize = 512;

pub static TIMER: time::Timer = time::Timer::new(arch::interrupt::TIMER_INTERVAL);

static RUNTIME: Runtime = {
// This constant is used as an array initializer; the clippy warning that it
// contains interior mutability is not actually a problem here, since we
Expand Down Expand Up @@ -83,6 +88,13 @@ where
})
}

/// Initialize the kernel runtime.
pub fn init() {
time::set_global_timer(&TIMER).expect("`rt::init` should only be called once!");

tracing::info!("kernel runtime initialized");
}

static SCHEDULER: arch::LocalKey<Cell<Option<&'static StaticScheduler>>> =
arch::LocalKey::new(|| Cell::new(None));

Expand All @@ -108,7 +120,7 @@ impl Core {

// turn the timer wheel if it wasn't turned recently and no one else is
// holding a lock, ensuring any pending timer ticks are consumed.
arch::tick_timer();
TIMER.advance_ticks(0);

// if there are remaining tasks to poll, continue without stealing.
if tick.has_remaining {
Expand Down
8 changes: 4 additions & 4 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ pub fn eval(line: &str) {
.with_fn(|line| Err(Error::other(line, "not yet implemented"))),
Command::new("timer")
.with_help("print the timer wheel")
.with_fn(crate::arch::shell::dump_timer),
Command::new("timer")
.with_help("print the timer wheel")
.with_fn(crate::arch::shell::dump_timer),
.with_fn(|_| {
tracing::info!(timer = ?crate::rt::TIMER);
Ok(())
}),
crate::arch::shell::DUMP_ARCH,
Command::new("heap")
.with_help("print kernel heap statistics")
Expand Down

0 comments on commit 68a2145

Please sign in to comment.