1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Temporal quantification.
15 pub use self::duration::Duration;
19 /// Returns the current value of a high-resolution performance counter
20 /// in nanoseconds since an unspecified epoch.
21 // NB: this is intentionally not public, this is not ready to stabilize its api.
22 fn precise_time_ns() -> u64 {
23 return os_precise_time_ns();
26 fn os_precise_time_ns() -> u64 {
27 let mut ticks_per_s = 0;
29 libc::QueryPerformanceFrequency(&mut ticks_per_s)
31 let ticks_per_s = if ticks_per_s == 0 {1} else {ticks_per_s};
34 libc::QueryPerformanceCounter(&mut ticks)
37 return (ticks as u64 * 1000000000) / (ticks_per_s as u64);
40 #[cfg(any(target_os = "macos", target_os = "ios"))]
41 fn os_precise_time_ns() -> u64 {
44 static mut TIMEBASE: libc::mach_timebase_info = libc::mach_timebase_info { numer: 0,
46 static ONCE: sync::Once = sync::ONCE_INIT;
49 imp::mach_timebase_info(&mut TIMEBASE);
51 let time = imp::mach_absolute_time();
52 time * TIMEBASE.numer as u64 / TIMEBASE.denom as u64
56 #[cfg(not(any(windows, target_os = "macos", target_os = "ios")))]
57 fn os_precise_time_ns() -> u64 {
58 let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
60 imp::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
62 return (ts.tv_sec as u64) * 1000000000 + (ts.tv_nsec as u64)
66 #[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios")))]
68 use libc::{c_int, timespec};
70 // Apparently android provides this in some other library?
71 #[cfg(not(target_os = "android"))]
76 pub fn clock_gettime(clk_id: c_int, tp: *mut timespec) -> c_int;
80 #[cfg(any(target_os = "macos", target_os = "ios"))]
82 use libc::{c_int, mach_timebase_info};
85 pub fn mach_absolute_time() -> u64;
86 pub fn mach_timebase_info(info: *mut mach_timebase_info) -> c_int;