Add timeouts to more driver spin loops

This commit is contained in:
Jeremy Soller
2025-11-26 09:30:45 -07:00
parent dd41c4f13e
commit d07a33ec0b
8 changed files with 125 additions and 84 deletions
+2
View File
@@ -16,6 +16,8 @@ pub mod io;
mod logger;
/// The Scatter Gather List (SGL) API for drivers.
pub mod sgl;
/// Low latency timeout for driver loops
pub mod timeout;
pub use logger::{output_level, file_level, setup_logging};
+45
View File
@@ -0,0 +1,45 @@
use std::{thread, time::{Duration, Instant}};
pub struct Timeout {
instant: Instant,
duration: Duration,
}
impl Timeout {
#[inline]
pub fn new(duration: Duration) -> Self {
Self {
instant: Instant::now(),
duration,
}
}
#[inline]
pub fn from_micros(micros: u64) -> Self {
Self::new(Duration::from_micros(micros))
}
#[inline]
pub fn from_millis(millis: u64) -> Self {
Self::new(Duration::from_millis(millis))
}
#[inline]
pub fn from_secs(secs: u64) -> Self {
Self::new(Duration::from_secs(secs))
}
#[inline]
pub fn run(&self) -> Result<(), ()> {
if self.instant.elapsed() < self.duration {
// Sleeps in Redox are only evaluated on PIT ticks (a few ms), which is not
// short enough for a reasonably responsive timeout. However, the clock is
// highly accurate. So, we yield instead of sleep to reduce latency.
//TODO: allow timeout that spins instead of yields?
std::thread::yield_now();
Ok(())
} else {
Err(())
}
}
}