]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/time.rs
Improve some compiletest documentation
[rust.git] / src / libstd / sys / windows / time.rs
1 use crate::cmp::Ordering;
2 use crate::fmt;
3 use crate::mem;
4 use crate::sys::c;
5 use crate::time::Duration;
6 use crate::convert::TryInto;
7
8 use core::hash::{Hash, Hasher};
9
10 const NANOS_PER_SEC: u64 = 1_000_000_000;
11 const INTERVALS_PER_SEC: u64 = NANOS_PER_SEC / 100;
12
13 #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
14 pub struct Instant {
15     // This duration is relative to an arbitrary microsecond epoch
16     // from the winapi QueryPerformanceCounter function.
17     t: Duration,
18 }
19
20 #[derive(Copy, Clone)]
21 pub struct SystemTime {
22     t: c::FILETIME,
23 }
24
25 const INTERVALS_TO_UNIX_EPOCH: u64 = 11_644_473_600 * INTERVALS_PER_SEC;
26
27 pub const UNIX_EPOCH: SystemTime = SystemTime {
28     t: c::FILETIME {
29         dwLowDateTime: INTERVALS_TO_UNIX_EPOCH as u32,
30         dwHighDateTime: (INTERVALS_TO_UNIX_EPOCH >> 32) as u32,
31     },
32 };
33
34 impl Instant {
35     pub fn now() -> Instant {
36         // High precision timing on windows operates in "Performance Counter"
37         // units, as returned by the WINAPI QueryPerformanceCounter function.
38         // These relate to seconds by a factor of QueryPerformanceFrequency.
39         // In order to keep unit conversions out of normal interval math, we
40         // measure in QPC units and immediately convert to nanoseconds.
41         perf_counter::PerformanceCounterInstant::now().into()
42     }
43
44     pub fn actually_monotonic() -> bool {
45         false
46     }
47
48     pub const fn zero() -> Instant {
49         Instant { t: Duration::from_secs(0) }
50     }
51
52     pub fn sub_instant(&self, other: &Instant) -> Duration {
53         // On windows there's a threshold below which we consider two timestamps
54         // equivalent due to measurement error. For more details + doc link,
55         // check the docs on epsilon.
56         let epsilon =
57             perf_counter::PerformanceCounterInstant::epsilon();
58         if other.t > self.t && other.t - self.t <= epsilon {
59             return Duration::new(0, 0)
60         }
61         self.t.checked_sub(other.t)
62               .expect("specified instant was later than self")
63     }
64
65     pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
66         Some(Instant {
67             t: self.t.checked_add(*other)?
68         })
69     }
70
71     pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
72         Some(Instant {
73             t: self.t.checked_sub(*other)?
74         })
75     }
76 }
77
78 impl SystemTime {
79     pub fn now() -> SystemTime {
80         unsafe {
81             let mut t: SystemTime = mem::zeroed();
82             c::GetSystemTimeAsFileTime(&mut t.t);
83             return t
84         }
85     }
86
87     fn from_intervals(intervals: i64) -> SystemTime {
88         SystemTime {
89             t: c::FILETIME {
90                 dwLowDateTime: intervals as c::DWORD,
91                 dwHighDateTime: (intervals >> 32) as c::DWORD,
92             }
93         }
94     }
95
96     fn intervals(&self) -> i64 {
97         (self.t.dwLowDateTime as i64) | ((self.t.dwHighDateTime as i64) << 32)
98     }
99
100     pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
101         let me = self.intervals();
102         let other = other.intervals();
103         if me >= other {
104             Ok(intervals2dur((me - other) as u64))
105         } else {
106             Err(intervals2dur((other - me) as u64))
107         }
108     }
109
110     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
111         let intervals = self.intervals().checked_add(checked_dur2intervals(other)?)?;
112         Some(SystemTime::from_intervals(intervals))
113     }
114
115     pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
116         let intervals = self.intervals().checked_sub(checked_dur2intervals(other)?)?;
117         Some(SystemTime::from_intervals(intervals))
118     }
119 }
120
121 impl PartialEq for SystemTime {
122     fn eq(&self, other: &SystemTime) -> bool {
123         self.intervals() == other.intervals()
124     }
125 }
126
127 impl Eq for SystemTime {}
128
129 impl PartialOrd for SystemTime {
130     fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> {
131         Some(self.cmp(other))
132     }
133 }
134
135 impl Ord for SystemTime {
136     fn cmp(&self, other: &SystemTime) -> Ordering {
137         self.intervals().cmp(&other.intervals())
138     }
139 }
140
141 impl fmt::Debug for SystemTime {
142     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
143         f.debug_struct("SystemTime")
144          .field("intervals", &self.intervals())
145          .finish()
146     }
147 }
148
149 impl From<c::FILETIME> for SystemTime {
150     fn from(t: c::FILETIME) -> SystemTime {
151         SystemTime { t }
152     }
153 }
154
155 impl Hash for SystemTime {
156     fn hash<H : Hasher>(&self, state: &mut H) {
157         self.intervals().hash(state)
158     }
159 }
160
161 fn checked_dur2intervals(dur: &Duration) -> Option<i64> {
162     dur.as_secs()
163         .checked_mul(INTERVALS_PER_SEC)?
164         .checked_add(dur.subsec_nanos() as u64 / 100)?
165         .try_into()
166         .ok()
167 }
168
169 fn intervals2dur(intervals: u64) -> Duration {
170     Duration::new(intervals / INTERVALS_PER_SEC,
171                   ((intervals % INTERVALS_PER_SEC) * 100) as u32)
172 }
173
174 mod perf_counter {
175     use super::{NANOS_PER_SEC};
176     use crate::sync::Once;
177     use crate::sys_common::mul_div_u64;
178     use crate::sys::c;
179     use crate::sys::cvt;
180     use crate::time::Duration;
181
182     pub struct PerformanceCounterInstant {
183         ts: c::LARGE_INTEGER
184     }
185     impl PerformanceCounterInstant {
186         pub fn now() -> Self {
187             Self {
188                 ts: query()
189             }
190         }
191
192         // Per microsoft docs, the margin of error for cross-thread time comparisons
193         // using QueryPerformanceCounter is 1 "tick" -- defined as 1/frequency().
194         // Reference: https://docs.microsoft.com/en-us/windows/desktop/SysInfo
195         //                   /acquiring-high-resolution-time-stamps
196         pub fn epsilon() -> Duration {
197             let epsilon = NANOS_PER_SEC / (frequency() as u64);
198             Duration::from_nanos(epsilon)
199         }
200     }
201     impl From<PerformanceCounterInstant> for super::Instant {
202         fn from(other: PerformanceCounterInstant) -> Self {
203             let freq = frequency() as u64;
204             let instant_nsec = mul_div_u64(other.ts as u64, NANOS_PER_SEC, freq);
205             Self {
206                 t: Duration::from_nanos(instant_nsec)
207             }
208         }
209     }
210
211     fn frequency() -> c::LARGE_INTEGER {
212         static mut FREQUENCY: c::LARGE_INTEGER = 0;
213         static ONCE: Once = Once::new();
214
215         unsafe {
216             ONCE.call_once(|| {
217                 cvt(c::QueryPerformanceFrequency(&mut FREQUENCY)).unwrap();
218             });
219             FREQUENCY
220         }
221     }
222
223     fn query() -> c::LARGE_INTEGER {
224         let mut qpc_value: c::LARGE_INTEGER = 0;
225         cvt(unsafe {
226             c::QueryPerformanceCounter(&mut qpc_value)
227         }).unwrap();
228         qpc_value
229     }
230 }