Skip to content

Commit

Permalink
refac(hal-x86_64): move pit and tsc into time (#356)
Browse files Browse the repository at this point in the history
This commit moves the `pit` and `tsc` modules out of `interrupt` and
`cpu`, and into a new `time` module. This feels like a better
organization of things, IMO.
  • Loading branch information
hawkw authored Oct 22, 2022
1 parent 9994cd5 commit b99fb81
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 151 deletions.
4 changes: 1 addition & 3 deletions hal-x86_64/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use mycelium_util::bits;
pub mod entropy;
pub mod intrinsics;
mod msr;
mod tsc;
pub use self::msr::Msr;
pub use self::tsc::Rdtsc;

#[repr(transparent)]
pub struct Port {
Expand Down Expand Up @@ -246,7 +244,7 @@ impl FeatureNotSupported {
self.0
}

pub(in crate::cpu) fn new(feature_name: &'static str) -> Self {
pub(crate) fn new(feature_name: &'static str) -> Self {
Self(feature_name)
}
}
2 changes: 1 addition & 1 deletion hal-x86_64/src/cpu/entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use mycelium_util::sync::spin;
use raw_cpuid::CpuId;

#[cfg(feature = "rand_core")]
use super::Rdtsc;
use crate::time::Rdtsc;
#[cfg(feature = "rand_core")]
use mycelium_util::sync::Lazy;
#[cfg(feature = "rand_core")]
Expand Down
4 changes: 1 addition & 3 deletions hal-x86_64/src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use mycelium_util::{bits, fmt};

pub mod idt;
pub mod pic;
pub mod pit;

pub use idt::Idt;
pub use pic::CascadedPic;
pub use pit::PIT;

pub type Control = &'static mut Idt;

Expand Down Expand Up @@ -227,7 +225,7 @@ impl hal_core::interrupt::Control for Idt {
use core::sync::atomic::Ordering;
// if we weren't trying to do a PIT sleep, handle the timer tick
// instead.
let was_sleeping = pit::SLEEPING
let was_sleeping = crate::time::pit::SLEEPING
.compare_exchange(true, false, Ordering::AcqRel, Ordering::Acquire)
.is_ok();
if !was_sleeping {
Expand Down
1 change: 1 addition & 0 deletions hal-x86_64/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod mm;
pub mod segment;
pub mod serial;
pub mod task;
pub mod time;
pub mod vga;

pub const NAME: &str = "x86_64";
38 changes: 38 additions & 0 deletions hal-x86_64/src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! x86 hardware timers and timekeeping functionality.
pub(crate) mod pit;
mod tsc;
pub use self::{
pit::{Pit, PIT},
tsc::Rdtsc,
};
use core::fmt;
pub use core::time::Duration;

/// Error indicating that a [`Duration`] was invalid for a particular use.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct InvalidDuration {
duration: Duration,
message: &'static str,
}

// === impl InvalidDuration ===

impl fmt::Display for InvalidDuration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self { duration, message } = self;
write!(f, "invalid duration {duration:?}: {message}")
}
}

impl InvalidDuration {
/// Returns the [`Duration`] that was invalid.
#[must_use]
pub fn duration(self) -> Duration {
self.duration
}

#[must_use]
pub(crate) fn new(duration: Duration, message: &'static str) -> Self {
Self { duration, message }
}
}
Loading

0 comments on commit b99fb81

Please sign in to comment.