mikros: Add support for Instant
This commit is contained in:
parent
68a58e1dae
commit
f4ab2ecd1b
@ -20,7 +20,6 @@ pub mod pipe;
|
||||
pub mod process;
|
||||
pub mod stdio;
|
||||
pub mod thread;
|
||||
#[path = "../unsupported/time.rs"]
|
||||
pub mod time;
|
||||
|
||||
mod common;
|
||||
|
@ -242,3 +242,11 @@ pub fn unlock_mutex(mutex: u64) {
|
||||
pub fn drop_mutex(mutex: u64) {
|
||||
syscall1(36, mutex);
|
||||
}
|
||||
|
||||
pub fn tick_freq() -> u64 {
|
||||
syscall0(37)
|
||||
}
|
||||
|
||||
pub fn num_ticks() -> u64 {
|
||||
syscall0(38)
|
||||
}
|
||||
|
46
library/std/src/sys/pal/mikros/time.rs
Normal file
46
library/std/src/sys/pal/mikros/time.rs
Normal file
@ -0,0 +1,46 @@
|
||||
use crate::time::Duration;
|
||||
use super::syscalls::{tick_freq, num_ticks};
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
||||
pub struct Instant(Duration);
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
||||
pub struct SystemTime(Duration);
|
||||
|
||||
pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
|
||||
|
||||
impl Instant {
|
||||
pub fn now() -> Instant {
|
||||
Self(Duration::from_secs(num_ticks()) / (tick_freq() as u32))
|
||||
}
|
||||
|
||||
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
|
||||
self.0.checked_sub(other.0)
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
|
||||
Some(Instant(self.0.checked_add(*other)?))
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
|
||||
Some(Instant(self.0.checked_sub(*other)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl SystemTime {
|
||||
pub fn now() -> SystemTime {
|
||||
panic!("time not implemented on this platform")
|
||||
}
|
||||
|
||||
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
|
||||
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
|
||||
}
|
||||
|
||||
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_add(*other)?))
|
||||
}
|
||||
|
||||
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
|
||||
Some(SystemTime(self.0.checked_sub(*other)?))
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user