1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use core::mem::MaybeUninit;

use super::abi::*;
use crate::timespec;

pub const CLOCK_REALTIME: i32 = 0;
pub const CLOCK_MONOTONIC: i32 = 1;
pub const CLOCK_PROCESS_CPUTIME_ID: i32 = 2;
pub const CLOCK_THREAD_CPUTIME_ID: i32 = 3;
pub const CLOCK_MONOTONIC_RAW: i32 = 4;
pub const CLOCK_REALTIME_COARSE: i32 = 5;
pub const CLOCK_MONOTONIC_COARSE: i32 = 6;
pub const CLOCK_BOOTTIME: i32 = 7;
pub const CLOCK_REALTIME_ALARM: i32 = 8;
pub const CLOCK_BOOTTIME_ALARM: i32 = 9;
pub const CLOCK_SGI_CYCLE: i32 = 10;
pub const CLOCK_TAI: i32 = 11;

#[inline]
pub fn clock_gettime(clock: i32) -> Result<timespec, i32> {
    let mut out: MaybeUninit<timespec> = MaybeUninit::uninit();
    let ret = unsafe { syscall_2(228, clock as usize, out.as_mut_ptr() as usize) as i32 };
    if ret < 0 {
        return Err(ret);
    }
    // SAFETY: The kernel filled the structure or there would have been an error returned.
    Ok(unsafe { out.assume_init() })
}