preemptive_threads/time/mod.rs
1//! Time management and timer interrupt handling.
2//!
3//! This module provides timer interrupt handling, time slice accounting,
4//! and preemption support for the threading system.
5
6pub mod tick;
7pub mod timer;
8
9#[cfg(feature = "x86_64")]
10pub mod x86_64_timer;
11
12pub use tick::{TickCounter, TimeSlice};
13pub use timer::{Timer, TimerConfig, TimerError, PreemptGuard, IrqGuard};
14
15/// Get monotonic time - alias for Instant::now() for compatibility
16pub fn get_monotonic_time() -> Instant {
17 Instant::now()
18}
19
20/// Nanoseconds since some arbitrary epoch.
21///
22/// This is used for high-resolution timing and scheduling decisions.
23/// The actual epoch is implementation-defined and may vary between architectures.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
25pub struct Instant(u64);
26
27impl Instant {
28 /// Create a new instant from nanoseconds since epoch.
29 pub fn from_nanos(nanos: u64) -> Self {
30 Self(nanos)
31 }
32
33 /// Get nanoseconds since epoch.
34 pub fn as_nanos(self) -> u64 {
35 self.0
36 }
37
38 /// Get nanoseconds since epoch as u128 for calculations.
39 pub fn as_nanos_u128(self) -> u128 {
40 self.0 as u128
41 }
42
43 /// Get the current instant.
44 ///
45 /// This reads the current time from the architecture-specific timer.
46 pub fn now() -> Self {
47 #[cfg(feature = "x86_64")]
48 {
49 x86_64_timer::read_tsc()
50 }
51
52 #[cfg(not(feature = "x86_64"))]
53 {
54 // Fallback for other architectures
55 Self(0)
56 }
57 }
58
59 /// Calculate duration since another instant.
60 ///
61 /// # Panics
62 ///
63 /// Panics if `earlier` is after `self`.
64 pub fn duration_since(self, earlier: Instant) -> Duration {
65 Duration::from_nanos(self.0 - earlier.0)
66 }
67
68 /// Add a duration to this instant.
69 pub fn add(self, duration: Duration) -> Self {
70 Self(self.0 + duration.as_nanos())
71 }
72}
73
74/// A duration of time.
75#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
76pub struct Duration(u64);
77
78impl Duration {
79 /// Create a duration from nanoseconds.
80 pub fn from_nanos(nanos: u64) -> Self {
81 Self(nanos)
82 }
83
84 /// Create a duration from microseconds.
85 pub fn from_micros(micros: u64) -> Self {
86 Self(micros * 1_000)
87 }
88
89 /// Create a duration from milliseconds.
90 pub fn from_millis(millis: u64) -> Self {
91 Self(millis * 1_000_000)
92 }
93
94 /// Get nanoseconds in this duration.
95 pub fn as_nanos(self) -> u64 {
96 self.0
97 }
98
99 /// Get nanoseconds as u128 for calculations.
100 pub fn as_nanos_u128(self) -> u128 {
101 self.0 as u128
102 }
103
104 /// Get microseconds in this duration.
105 pub fn as_micros(self) -> u64 {
106 self.0 / 1_000
107 }
108
109 /// Get milliseconds in this duration.
110 pub fn as_millis(self) -> u64 {
111 self.0 / 1_000_000
112 }
113}
114
115/// Frequency in Hz for timer interrupts.
116pub const TIMER_FREQUENCY_HZ: u32 = 1000; // 1 kHz = 1ms time slices
117
118/// Default quantum duration in nanoseconds (1ms).
119pub const DEFAULT_QUANTUM_NS: u64 = 1_000_000;