Add timeouts to more driver spin loops
This commit is contained in:
@@ -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};
|
||||
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user