use core::mem::size_of;
#[cfg(all(target_arch = "aarch64", target_os = "none"))]
use core::sync::atomic::{AtomicU64, Ordering};
use crate::errno::SysResult;
use crate::raw::{nr, syscall2, syscall4};
pub const CLOCK_REALTIME: i32 = 0;
pub const CLOCK_MONOTONIC: i32 = 1;
pub const TIMER_ABSTIME: i32 = 1;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Timespec {
pub tv_sec: i64,
pub tv_nsec: i64,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Timeval {
pub tv_sec: i64,
pub tv_usec: i64,
}
const _: () = {
assert!(size_of::<Timespec>() == 16);
assert!(size_of::<Timeval>() == 16);
};
pub fn clock_gettime_syscall(clock_id: i32) -> SysResult<Timespec> {
let mut ts = Timespec::default();
unsafe {
syscall2(
nr::CLOCK_GETTIME,
clock_id as usize,
core::ptr::from_mut(&mut ts) as usize,
)?;
}
Ok(ts)
}
pub fn clock_gettime(clock_id: i32) -> SysResult<Timespec> {
#[cfg(all(target_arch = "aarch64", target_os = "none"))]
if clock_id == CLOCK_MONOTONIC {
static CACHED_HZ: AtomicU64 = AtomicU64::new(0);
let mut hz = CACHED_HZ.load(Ordering::Relaxed);
if hz == 0 {
hz = crate::raw::read_cntfrq_el0();
if hz != 0 {
CACHED_HZ.store(hz, Ordering::Relaxed);
}
}
let ticks = crate::raw::read_cntvct_el0();
if let Some(secs) = ticks.checked_div(hz) {
let rem = ticks - secs * hz;
let tv_sec = secs as i64;
let tv_nsec = ((rem as u128 * 1_000_000_000) / (hz as u128)) as i64;
return Ok(Timespec { tv_sec, tv_nsec });
}
}
clock_gettime_syscall(clock_id)
}
pub fn clock_settime(clock_id: i32, ts: &Timespec) -> SysResult<()> {
unsafe {
syscall2(
nr::CLOCK_SETTIME,
clock_id as usize,
core::ptr::from_ref(ts) as usize,
)?;
}
Ok(())
}
pub fn nanosleep(req: &Timespec, rem: Option<&mut Timespec>) -> SysResult<()> {
let rem_ptr = rem.map_or(0, |r| core::ptr::from_mut(r) as usize);
unsafe {
syscall4(
nr::CLOCK_NANOSLEEP,
CLOCK_MONOTONIC as usize,
0,
core::ptr::from_ref(req) as usize,
rem_ptr,
)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::raw::with_mock_syscall;
#[test]
fn clock_gettime_and_nanosleep_pass_expected_arguments() {
let ts = with_mock_syscall(
|sysno, args| {
assert_eq!(sysno, nr::CLOCK_GETTIME);
assert_eq!(args[0], CLOCK_MONOTONIC as usize);
let out = args[1] as *mut Timespec;
unsafe {
*out = Timespec {
tv_sec: 42,
tv_nsec: 123_456_789,
};
}
Ok(0)
},
|| clock_gettime(CLOCK_MONOTONIC).unwrap(),
);
assert_eq!(
ts,
Timespec {
tv_sec: 42,
tv_nsec: 123_456_789,
}
);
const REQ: Timespec = Timespec {
tv_sec: 1,
tv_nsec: 500,
};
let mut rem = Timespec::default();
with_mock_syscall(
|sysno, args| {
assert_eq!(sysno, nr::CLOCK_NANOSLEEP);
assert_eq!(args[0], CLOCK_MONOTONIC as usize);
assert_eq!(args[1], 0);
let req_ptr = args[2] as *const Timespec;
assert_eq!(unsafe { *req_ptr }, REQ);
assert!(args[3] != 0);
Ok(0)
},
|| nanosleep(&REQ, Some(&mut rem)).unwrap(),
);
}
}