tg-rcore-tutorial-syscall-zgy 0.0.1

System call definitions and interfaces for rCore tutorial OS.
Documentation
//! 时间相关共享类型定义。
//!
//! 参考 Linux UAPI:
//! <https://github.com/torvalds/linux/blob/master/include/uapi/linux/time.h>.

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(transparent)]
/// 时钟类型标识(与 `clock_gettime` 等接口配合使用)。
pub struct ClockId(pub usize);

impl ClockId {
    /// 实时时钟(受系统时间校正影响)。
    pub const CLOCK_REALTIME: Self = Self(0);
    /// 单调时钟(不受 wall-clock 调整影响,常用于计时)。
    pub const CLOCK_MONOTONIC: Self = Self(1);
    pub const CLOCK_PROCESS_CPUTIME_ID: Self = Self(2);
    pub const CLOCK_THREAD_CPUTIME_ID: Self = Self(3);
    pub const CLOCK_MONOTONIC_RAW: Self = Self(4);
    pub const CLOCK_REALTIME_COARSE: Self = Self(5);
    pub const CLOCK_MONOTONIC_COARSE: Self = Self(6);
    pub const CLOCK_BOOTTIME: Self = Self(7);
    pub const CLOCK_REALTIME_ALARM: Self = Self(8);
    pub const CLOCK_BOOTTIME_ALARM: Self = Self(9);
    pub const CLOCK_SGI_CYCLE: Self = Self(10);
    pub const CLOCK_TAI: Self = Self(11);
}

#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug)]
#[repr(C)]
/// 秒 + 纳秒表示的时间结构。
pub struct TimeSpec {
    ///    pub tv_sec: usize,
    /// 纳秒
    pub tv_nsec: usize,
}

impl TimeSpec {
    /// 0 秒。
    pub const ZERO: Self = Self {
        tv_sec: 0,
        tv_nsec: 0,
    };
    /// 1 秒。
    pub const SECOND: Self = Self {
        tv_sec: 1,
        tv_nsec: 0,
    };
    /// 1 毫秒。
    pub const MILLSECOND: Self = Self {
        tv_sec: 0,
        tv_nsec: 1_000_000,
    };
    /// 1 微秒。
    pub const MICROSECOND: Self = Self {
        tv_sec: 0,
        tv_nsec: 1_000,
    };
    /// 1 纳秒。
    pub const NANOSECOND: Self = Self {
        tv_sec: 0,
        tv_nsec: 1,
    };
    /// 从毫秒构造 `TimeSpec`。
    pub fn from_millsecond(millsecond: usize) -> Self {
        Self {
            tv_sec: millsecond / 1_000,
            tv_nsec: millsecond % 1_000 * 1_000_000,
        }
    }
}

impl core::ops::Add<TimeSpec> for TimeSpec {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        let mut ans = Self {
            tv_sec: self.tv_sec + rhs.tv_sec,
            tv_nsec: self.tv_nsec + rhs.tv_nsec,
        };
        // 纳秒进位归一化到 [0, 1e9) 区间。
        if ans.tv_nsec > 1_000_000_000 {
            ans.tv_sec += 1;
            ans.tv_nsec -= 1_000_000_000;
        }
        ans
    }
}

impl core::fmt::Display for TimeSpec {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "TimeSpec({}.{:09})", self.tv_sec, self.tv_nsec)
    }
}