1 use crate::std::cmp::Ordering;
2 use crate::std::fmt;
3 use crate::std::mem;
4 use crate::std::sys::c;
5 use crate::std::sys_common::IntoInner;
6 use crate::std::time::Duration;
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 {
now() -> Instant35 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
checked_sub_instant(&self, other: &Instant) -> Option<Duration>44 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
45 // On windows there's a threshold below which we consider two timestamps
46 // equivalent due to measurement error. For more details + doc link,
47 // check the docs on epsilon.
48 let epsilon = perf_counter::PerformanceCounterInstant::epsilon();
49 if other.t > self.t && other.t - self.t <= epsilon {
50 Some(Duration::new(0, 0))
51 } else {
52 self.t.checked_sub(other.t)
53 }
54 }
55
checked_add_duration(&self, other: &Duration) -> Option<Instant>56 pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
57 Some(Instant {
58 t: self.t.checked_add(*other)?,
59 })
60 }
61
checked_sub_duration(&self, other: &Duration) -> Option<Instant>62 pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
63 Some(Instant {
64 t: self.t.checked_sub(*other)?,
65 })
66 }
67 }
68
69 impl SystemTime {
now() -> SystemTime70 pub fn now() -> SystemTime {
71 unsafe {
72 let mut t: SystemTime = mem::zeroed();
73 c::GetSystemTimePreciseAsFileTime(&mut t.t);
74 t
75 }
76 }
77
from_intervals(intervals: i64) -> SystemTime78 fn from_intervals(intervals: i64) -> SystemTime {
79 SystemTime {
80 t: c::FILETIME {
81 dwLowDateTime: intervals as c::DWORD,
82 dwHighDateTime: (intervals >> 32) as c::DWORD,
83 },
84 }
85 }
86
intervals(&self) -> i6487 fn intervals(&self) -> i64 {
88 (self.t.dwLowDateTime as i64) | ((self.t.dwHighDateTime as i64) << 32)
89 }
90
sub_time(&self, other: &SystemTime) -> Result<Duration, Duration>91 pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
92 let me = self.intervals();
93 let other = other.intervals();
94 if me >= other {
95 Ok(intervals2dur((me - other) as u64))
96 } else {
97 Err(intervals2dur((other - me) as u64))
98 }
99 }
100
checked_add_duration(&self, other: &Duration) -> Option<SystemTime>101 pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
102 let intervals = self
103 .intervals()
104 .checked_add(checked_dur2intervals(other)?)?;
105 Some(SystemTime::from_intervals(intervals))
106 }
107
checked_sub_duration(&self, other: &Duration) -> Option<SystemTime>108 pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
109 let intervals = self
110 .intervals()
111 .checked_sub(checked_dur2intervals(other)?)?;
112 Some(SystemTime::from_intervals(intervals))
113 }
114 }
115
116 impl PartialEq for SystemTime {
eq(&self, other: &SystemTime) -> bool117 fn eq(&self, other: &SystemTime) -> bool {
118 self.intervals() == other.intervals()
119 }
120 }
121
122 impl Eq for SystemTime {}
123
124 impl PartialOrd for SystemTime {
partial_cmp(&self, other: &SystemTime) -> Option<Ordering>125 fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> {
126 Some(self.cmp(other))
127 }
128 }
129
130 impl Ord for SystemTime {
cmp(&self, other: &SystemTime) -> Ordering131 fn cmp(&self, other: &SystemTime) -> Ordering {
132 self.intervals().cmp(&other.intervals())
133 }
134 }
135
136 impl fmt::Debug for SystemTime {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result137 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138 f.debug_struct("SystemTime")
139 .field("intervals", &self.intervals())
140 .finish()
141 }
142 }
143
144 impl From<c::FILETIME> for SystemTime {
from(t: c::FILETIME) -> SystemTime145 fn from(t: c::FILETIME) -> SystemTime {
146 SystemTime { t }
147 }
148 }
149
150 impl IntoInner<c::FILETIME> for SystemTime {
into_inner(self) -> c::FILETIME151 fn into_inner(self) -> c::FILETIME {
152 self.t
153 }
154 }
155
156 impl Hash for SystemTime {
hash<H: Hasher>(&self, state: &mut H)157 fn hash<H: Hasher>(&self, state: &mut H) {
158 self.intervals().hash(state)
159 }
160 }
161
checked_dur2intervals(dur: &Duration) -> Option<i64>162 fn checked_dur2intervals(dur: &Duration) -> Option<i64> {
163 dur.as_secs()
164 .checked_mul(INTERVALS_PER_SEC)?
165 .checked_add(dur.subsec_nanos() as u64 / 100)?
166 .try_into()
167 .ok()
168 }
169
intervals2dur(intervals: u64) -> Duration170 fn intervals2dur(intervals: u64) -> Duration {
171 Duration::new(
172 intervals / INTERVALS_PER_SEC,
173 ((intervals % INTERVALS_PER_SEC) * 100) as u32,
174 )
175 }
176
177 mod perf_counter {
178 use super::NANOS_PER_SEC;
179 use crate::std::sync::atomic::{AtomicU64, Ordering};
180 use crate::std::sys::c;
181 use crate::std::sys::cvt;
182 use crate::std::sys_common::mul_div_u64;
183 use crate::std::time::Duration;
184
185 pub struct PerformanceCounterInstant {
186 ts: c::LARGE_INTEGER,
187 }
188 impl PerformanceCounterInstant {
now() -> Self189 pub fn now() -> Self {
190 Self { ts: query() }
191 }
192
193 // Per microsoft docs, the margin of error for cross-thread time comparisons
194 // using QueryPerformanceCounter is 1 "tick" -- defined as 1/frequency().
195 // Reference: https://docs.microsoft.com/en-us/windows/desktop/SysInfo
196 // /acquiring-high-resolution-time-stamps
epsilon() -> Duration197 pub fn epsilon() -> Duration {
198 let epsilon = NANOS_PER_SEC / (frequency() as u64);
199 Duration::from_nanos(epsilon)
200 }
201 }
202 impl From<PerformanceCounterInstant> for super::Instant {
from(other: PerformanceCounterInstant) -> Self203 fn from(other: PerformanceCounterInstant) -> Self {
204 let freq = frequency() as u64;
205 let instant_nsec = mul_div_u64(other.ts as u64, NANOS_PER_SEC, freq);
206 Self {
207 t: Duration::from_nanos(instant_nsec),
208 }
209 }
210 }
211
frequency() -> c::LARGE_INTEGER212 fn frequency() -> c::LARGE_INTEGER {
213 // Either the cached result of `QueryPerformanceFrequency` or `0` for
214 // uninitialized. Storing this as a single `AtomicU64` allows us to use
215 // `Relaxed` operations, as we are only interested in the effects on a
216 // single memory location.
217 static FREQUENCY: AtomicU64 = AtomicU64::new(0);
218
219 let cached = FREQUENCY.load(Ordering::Relaxed);
220 // If a previous thread has filled in this global state, use that.
221 if cached != 0 {
222 return cached as c::LARGE_INTEGER;
223 }
224 // ... otherwise learn for ourselves ...
225 let mut frequency = 0;
226 unsafe {
227 cvt(c::QueryPerformanceFrequency(&mut frequency)).unwrap();
228 }
229
230 FREQUENCY.store(frequency as u64, Ordering::Relaxed);
231 frequency
232 }
233
query() -> c::LARGE_INTEGER234 fn query() -> c::LARGE_INTEGER {
235 let mut qpc_value: c::LARGE_INTEGER = 0;
236 cvt(unsafe { c::QueryPerformanceCounter(&mut qpc_value) }).unwrap();
237 qpc_value
238 }
239 }
240