mikros: Add support for Instant

This commit is contained in:
pjht 2024-11-28 14:05:11 -06:00
parent 68a58e1dae
commit f4ab2ecd1b
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A
3 changed files with 54 additions and 1 deletions

View File

@ -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;

View File

@ -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)
}

View 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)?))
}
}