abi: implement time functions
This commit is contained in:
parent
89f4965460
commit
dc76c5b7a8
@ -18,7 +18,7 @@ pub struct DebugOptions {
|
|||||||
impl Default for DebugOptions {
|
impl Default for DebugOptions {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
serial_level: LogLevel::Debug,
|
serial_level: LogLevel::Info,
|
||||||
display_level: LogLevel::Info,
|
display_level: LogLevel::Info,
|
||||||
disable_program_trace: false,
|
disable_program_trace: false,
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ pub struct SleepFuture {
|
|||||||
impl SleepFuture {
|
impl SleepFuture {
|
||||||
pub fn remaining(&self) -> Option<Duration> {
|
pub fn remaining(&self) -> Option<Duration> {
|
||||||
let now = monotonic_time();
|
let now = monotonic_time();
|
||||||
match self.deadline.checked_sub(&now) {
|
match self.deadline.checked_sub_time(&now) {
|
||||||
Some(duration) if !duration.is_zero() => Some(duration),
|
Some(duration) if !duration.is_zero() => Some(duration),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
@ -179,7 +179,7 @@ impl Scheduler for CpuQueue {
|
|||||||
|
|
||||||
let t = monotonic_time();
|
let t = monotonic_time();
|
||||||
if let Some(t0) = self.last_stats_measure.get() {
|
if let Some(t0) = self.last_stats_measure.get() {
|
||||||
if let Some(dt) = t.checked_sub(&t0) {
|
if let Some(dt) = t.checked_sub_time(&t0) {
|
||||||
self.update_stats(dt, current.as_ref());
|
self.update_stats(dt, current.as_ref());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -242,7 +242,7 @@ pub(crate) fn nanosleep(
|
|||||||
Ok(()) => Ok(()),
|
Ok(()) => Ok(()),
|
||||||
Err(Error::Interrupted) => {
|
Err(Error::Interrupted) => {
|
||||||
let now = monotonic_time();
|
let now = monotonic_time();
|
||||||
let rem = deadline.checked_sub(&now).unwrap_or_default();
|
let rem = deadline.checked_sub_time(&now).unwrap_or_default();
|
||||||
remaining.write(rem);
|
remaining.write(rem);
|
||||||
Err(Error::Interrupted)
|
Err(Error::Interrupted)
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,48 @@ impl SystemTime {
|
|||||||
+ (self.nanoseconds as u128 / 1000000)
|
+ (self.nanoseconds as u128 / 1000000)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_sub(&self, other: &SystemTime) -> Option<Duration> {
|
pub fn checked_add_duration(&self, other: &Duration) -> Option<Self> {
|
||||||
|
let nanoseconds = self.nanoseconds.checked_add(other.subsec_nanos() as u64)?;
|
||||||
|
let seconds = nanoseconds / NANOSECONDS_IN_SECOND;
|
||||||
|
let nanoseconds = nanoseconds % NANOSECONDS_IN_SECOND;
|
||||||
|
let seconds = self
|
||||||
|
.seconds
|
||||||
|
.checked_add(other.as_secs())?
|
||||||
|
.checked_add(seconds)?;
|
||||||
|
|
||||||
|
Some(Self {
|
||||||
|
seconds,
|
||||||
|
nanoseconds,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Self> {
|
||||||
|
let mut seconds = self.seconds.checked_sub(other.as_secs())?;
|
||||||
|
let nanoseconds = if self.nanoseconds < other.subsec_nanos() as u64 {
|
||||||
|
seconds = seconds.checked_sub(1)?;
|
||||||
|
NANOSECONDS_IN_SECOND + self.seconds - other.subsec_nanos() as u64
|
||||||
|
} else {
|
||||||
|
self.nanoseconds - other.subsec_nanos() as u64
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(Self {
|
||||||
|
seconds,
|
||||||
|
nanoseconds,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||||
|
if self >= other {
|
||||||
|
Ok(self.checked_sub_time(other).unwrap())
|
||||||
|
} else {
|
||||||
|
match other.sub_time(self) {
|
||||||
|
Ok(d) => Err(d),
|
||||||
|
Err(d) => Ok(d),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn checked_sub_time(&self, other: &SystemTime) -> Option<Duration> {
|
||||||
let mut seconds = self.seconds.checked_sub(other.seconds)?;
|
let mut seconds = self.seconds.checked_sub(other.seconds)?;
|
||||||
let nanoseconds = if self.nanoseconds < other.nanoseconds {
|
let nanoseconds = if self.nanoseconds < other.nanoseconds {
|
||||||
seconds = seconds.checked_sub(1)?;
|
seconds = seconds.checked_sub(1)?;
|
||||||
@ -92,7 +133,6 @@ mod tests {
|
|||||||
seconds: 1,
|
seconds: 1,
|
||||||
nanoseconds: 143000000,
|
nanoseconds: 143000000,
|
||||||
};
|
};
|
||||||
assert_eq!(t1.checked_sub(&t0), Some(Duration::from_millis(20)));
|
|
||||||
assert_eq!(t0 + Duration::from_millis(20), t1);
|
assert_eq!(t0 + Duration::from_millis(20), t1);
|
||||||
assert_eq!(t1.as_millis(), 1143);
|
assert_eq!(t1.as_millis(), 1143);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user