|
| 1 | +use sys::time::TimeSpec; |
| 2 | +use {Result, Errno}; |
| 3 | +use libc; |
| 4 | + |
| 5 | +libc_enum! { |
| 6 | + #[repr(i32)] |
| 7 | + pub enum ClockId { |
| 8 | + #[cfg(any(target_os = "fuchsia", |
| 9 | + target_os = "linux", |
| 10 | + target_os = "android", |
| 11 | + target_os = "emscripten"))] |
| 12 | + CLOCK_BOOTTIME, |
| 13 | + #[cfg(any(target_os = "fuchsia", |
| 14 | + target_os = "linux", |
| 15 | + target_os = "android", |
| 16 | + target_os = "emscripten"))] |
| 17 | + CLOCK_BOOTTIME_ALARM, |
| 18 | + CLOCK_MONOTONIC, |
| 19 | + #[cfg(any(target_os = "fuchsia", |
| 20 | + target_os = "linux", |
| 21 | + target_os = "android", |
| 22 | + target_os = "emscripten"))] |
| 23 | + CLOCK_MONOTONIC_COARSE, |
| 24 | + #[cfg(any(target_os = "freebsd", |
| 25 | + target_os = "dragonfly"))] |
| 26 | + CLOCK_MONOTONIC_FAST, |
| 27 | + #[cfg(any(target_os = "freebsd", |
| 28 | + target_os = "dragonfly"))] |
| 29 | + CLOCK_MONOTONIC_PRECISE, |
| 30 | + #[cfg(any(target_os = "fuchsia", |
| 31 | + target_os = "linux", |
| 32 | + target_os = "android", |
| 33 | + target_os = "emscripten"))] |
| 34 | + CLOCK_MONOTONIC_RAW, |
| 35 | + #[cfg(any(target_os = "fuchsia", |
| 36 | + target_env = "uclibc", |
| 37 | + target_os = "macos", |
| 38 | + target_os = "ios", |
| 39 | + target_os = "freebsd", |
| 40 | + target_os = "dragonfly", |
| 41 | + target_os = "linux", |
| 42 | + target_os = "android", |
| 43 | + target_os = "emscripten"))] |
| 44 | + CLOCK_PROCESS_CPUTIME_ID, |
| 45 | + #[cfg(any(target_os = "freebsd", |
| 46 | + target_os = "dragonfly"))] |
| 47 | + CLOCK_PROF, |
| 48 | + CLOCK_REALTIME, |
| 49 | + #[cfg(any(target_os = "fuchsia", |
| 50 | + target_os = "linux", |
| 51 | + target_os = "android", |
| 52 | + target_os = "emscripten"))] |
| 53 | + CLOCK_REALTIME_ALARM, |
| 54 | + #[cfg(any(target_os = "fuchsia", |
| 55 | + target_os = "linux", |
| 56 | + target_os = "android", |
| 57 | + target_os = "emscripten"))] |
| 58 | + CLOCK_REALTIME_COARSE, |
| 59 | + #[cfg(any(target_os = "freebsd", |
| 60 | + target_os = "dragonfly"))] |
| 61 | + CLOCK_REALTIME_FAST, |
| 62 | + #[cfg(any(target_os = "freebsd", |
| 63 | + target_os = "dragonfly"))] |
| 64 | + CLOCK_REALTIME_PRECISE, |
| 65 | + #[cfg(any(target_os = "freebsd", |
| 66 | + target_os = "dragonfly"))] |
| 67 | + CLOCK_SECOND, |
| 68 | + #[cfg(any(target_os = "fuchsia", |
| 69 | + target_os = "emscripten", |
| 70 | + all(target_os = "linux", target_env = "musl")))] |
| 71 | + CLOCK_SGI_CYCLE, |
| 72 | + #[cfg(any(target_os = "fuchsia", |
| 73 | + target_os = "emscripten", |
| 74 | + all(target_os = "linux", target_env = "musl")))] |
| 75 | + CLOCK_TAI, |
| 76 | + #[cfg(any(target_os = "fuchsia", |
| 77 | + target_os = "ios", |
| 78 | + target_os = "macos", |
| 79 | + target_os = "freebsd", |
| 80 | + target_os = "dragonfly", |
| 81 | + target_os = "linux", |
| 82 | + target_os = "android", |
| 83 | + target_os = "emscripten", |
| 84 | + target_env = "uclibc"))] |
| 85 | + CLOCK_THREAD_CPUTIME_ID, |
| 86 | + #[cfg(any(target_os = "freebsd", |
| 87 | + target_os = "dragonfly"))] |
| 88 | + CLOCK_UPTIME, |
| 89 | + #[cfg(any(target_os = "freebsd", |
| 90 | + target_os = "dragonfly"))] |
| 91 | + CLOCK_UPTIME_FAST, |
| 92 | + #[cfg(any(target_os = "freebsd", |
| 93 | + target_os = "dragonfly"))] |
| 94 | + CLOCK_UPTIME_PRECISE, |
| 95 | + #[cfg(any(target_os = "freebsd", |
| 96 | + target_os = "dragonfly"))] |
| 97 | + CLOCK_VIRTUAL, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +pub fn clock_getres(clk_id: ClockId) -> Result<TimeSpec> { |
| 102 | + let mut c_time = libc::timespec {tv_sec: 0, tv_nsec: 0}; |
| 103 | + let res = unsafe { libc::clock_getres(clk_id as libc::c_int, &mut c_time) }; |
| 104 | + Errno::result(res)?; |
| 105 | + Ok(TimeSpec::from(c_time)) |
| 106 | +} |
| 107 | + |
| 108 | +pub fn clock_gettime(clk_id: ClockId) -> Result<TimeSpec> { |
| 109 | + let mut c_time = libc::timespec {tv_sec: 0, tv_nsec: 0}; |
| 110 | + let res = unsafe { libc::clock_gettime(clk_id as libc::c_int, &mut c_time) }; |
| 111 | + Errno::result(res)?; |
| 112 | + Ok(TimeSpec::from(c_time)) |
| 113 | +} |
| 114 | + |
| 115 | +pub fn clock_settime(clk_id: ClockId, timespec: TimeSpec) -> Result<()> { |
| 116 | + let res = unsafe { libc::clock_settime(clk_id as libc::c_int, timespec.as_ref()) }; |
| 117 | + Errno::result(res).map(drop) |
| 118 | +} |
0 commit comments