twizzler_abi/syscall/time/
clock.rsuse bitflags::bitflags;
use super::{ClockSource, FemtoSeconds, ReadClockFlags, ReadClockListFlags, TimeSpan};
bitflags! {
#[derive(Debug, Clone, Copy)]
pub struct ClockFlags: u32 {
const MONOTONIC = 1;
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct ClockInfo {
current: TimeSpan,
precision: FemtoSeconds,
resolution: FemtoSeconds,
flags: ClockFlags,
}
impl ClockInfo {
pub const ZERO: ClockInfo = ClockInfo::new(
TimeSpan::ZERO,
FemtoSeconds(0),
FemtoSeconds(0),
ClockFlags::MONOTONIC,
);
pub const fn new(
current: TimeSpan,
precision: FemtoSeconds,
resolution: FemtoSeconds,
flags: ClockFlags,
) -> Self {
Self {
current,
precision,
resolution,
flags,
}
}
pub fn precision(&self) -> FemtoSeconds {
self.precision
}
pub fn resolution(&self) -> FemtoSeconds {
self.resolution
}
pub fn current_value(&self) -> TimeSpan {
self.current
}
pub fn is_monotonic(&self) -> bool {
self.flags.contains(ClockFlags::MONOTONIC)
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub enum ClockKind {
Unknown,
Monotonic,
RealTime,
}
impl From<ClockKind> for u64 {
fn from(clock: ClockKind) -> Self {
match clock {
ClockKind::Monotonic => 0,
ClockKind::RealTime => 1,
ClockKind::Unknown => 2,
}
}
}
impl From<u64> for ClockKind {
fn from(x: u64) -> Self {
match x {
0 => ClockKind::Monotonic,
1 => ClockKind::RealTime,
_ => ClockKind::Unknown,
}
}
}
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct ClockID(pub u64);
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
pub struct Clock {
pub info: ClockInfo,
id: ClockID,
kind: ClockKind,
}
impl Clock {
pub const ZERO: Clock = Clock {
info: ClockInfo::ZERO,
id: ClockID(0),
kind: ClockKind::Unknown,
};
pub fn new(info: ClockInfo, id: ClockID, kind: ClockKind) -> Clock {
Self { info, id, kind }
}
pub fn read(&self) -> TimeSpan {
match super::sys_read_clock_info(ClockSource::ID(self.id), ReadClockFlags::empty()) {
Ok(ci) => ci.current_value(),
_ => TimeSpan::ZERO,
}
}
pub fn info(&self) -> ClockInfo {
self.info
}
pub fn get(kind: ClockKind) -> Clock {
let mut clk = [Clock::ZERO];
if let Ok(filled) =
super::sys_read_clock_list(kind, &mut clk, 0, ReadClockListFlags::FIRST_KIND)
{
if filled > 0 {
return clk[0];
}
}
Clock::ZERO
}
pub fn set(&mut self, info: ClockInfo, id: ClockID, kind: ClockKind) {
self.info = info;
self.id = id;
self.kind = kind;
}
}