]>
Commit | Line | Data |
---|---|---|
85aaf69f SL |
1 | // Copyright 2015 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. | |
4 | // | |
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. | |
10 | use libc; | |
11 | use ops::Sub; | |
12 | use time::Duration; | |
13 | use sync::{Once, ONCE_INIT}; | |
14 | ||
c34b1796 AL |
15 | const NANOS_PER_SEC: i64 = 1_000_000_000; |
16 | ||
85aaf69f SL |
17 | pub struct SteadyTime { |
18 | t: libc::LARGE_INTEGER, | |
19 | } | |
20 | ||
21 | impl SteadyTime { | |
22 | pub fn now() -> SteadyTime { | |
23 | let mut t = SteadyTime { t: 0 }; | |
24 | unsafe { libc::QueryPerformanceCounter(&mut t.t); } | |
25 | t | |
26 | } | |
27 | ||
28 | pub fn ns(&self) -> u64 { | |
c34b1796 | 29 | mul_div_i64(self.t as i64, NANOS_PER_SEC, frequency() as i64) as u64 |
85aaf69f SL |
30 | } |
31 | } | |
32 | ||
33 | fn frequency() -> libc::LARGE_INTEGER { | |
34 | static mut FREQUENCY: libc::LARGE_INTEGER = 0; | |
35 | static ONCE: Once = ONCE_INIT; | |
36 | ||
37 | unsafe { | |
38 | ONCE.call_once(|| { | |
39 | libc::QueryPerformanceFrequency(&mut FREQUENCY); | |
40 | }); | |
41 | FREQUENCY | |
42 | } | |
43 | } | |
44 | ||
45 | impl<'a> Sub for &'a SteadyTime { | |
46 | type Output = Duration; | |
47 | ||
48 | fn sub(self, other: &SteadyTime) -> Duration { | |
49 | let diff = self.t as i64 - other.t as i64; | |
c34b1796 | 50 | Duration::nanoseconds(mul_div_i64(diff, NANOS_PER_SEC, frequency() as i64)) |
85aaf69f SL |
51 | } |
52 | } | |
c34b1796 AL |
53 | |
54 | // Computes (value*numer)/denom without overflow, as long as both | |
55 | // (numer*denom) and the overall result fit into i64 (which is the case | |
56 | // for our time conversions). | |
57 | fn mul_div_i64(value: i64, numer: i64, denom: i64) -> i64 { | |
58 | let q = value / denom; | |
59 | let r = value % denom; | |
60 | // Decompose value as (value/denom*denom + value%denom), | |
61 | // substitute into (value*numer)/denom and simplify. | |
62 | // r < denom, so (denom*numer) is the upper bound of (r*numer) | |
63 | q * numer + r * numer / denom | |
64 | } | |
65 | ||
66 | #[test] | |
67 | fn test_muldiv() { | |
68 | assert_eq!(mul_div_i64( 1_000_000_000_001, 1_000_000_000, 1_000_000), 1_000_000_000_001_000); | |
69 | assert_eq!(mul_div_i64(-1_000_000_000_001, 1_000_000_000, 1_000_000), -1_000_000_000_001_000); | |
70 | assert_eq!(mul_div_i64(-1_000_000_000_001,-1_000_000_000, 1_000_000), 1_000_000_000_001_000); | |
71 | assert_eq!(mul_div_i64( 1_000_000_000_001, 1_000_000_000,-1_000_000), -1_000_000_000_001_000); | |
72 | assert_eq!(mul_div_i64( 1_000_000_000_001,-1_000_000_000,-1_000_000), 1_000_000_000_001_000); | |
73 | } |