• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::cmp::Ordering;
2 use crate::fmt;
3 use crate::mem;
4 use crate::sys::c;
5 use crate::sys_common::IntoInner;
6 use crate::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 { t: self.t.checked_add(*other)? })
58     }
59 
checked_sub_duration(&self, other: &Duration) -> Option<Instant>60     pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
61         Some(Instant { t: self.t.checked_sub(*other)? })
62     }
63 }
64 
65 impl SystemTime {
now() -> SystemTime66     pub fn now() -> SystemTime {
67         unsafe {
68             let mut t: SystemTime = mem::zeroed();
69             c::GetSystemTimePreciseAsFileTime(&mut t.t);
70             t
71         }
72     }
73 
from_intervals(intervals: i64) -> SystemTime74     fn from_intervals(intervals: i64) -> SystemTime {
75         SystemTime {
76             t: c::FILETIME {
77                 dwLowDateTime: intervals as c::DWORD,
78                 dwHighDateTime: (intervals >> 32) as c::DWORD,
79             },
80         }
81     }
82 
intervals(&self) -> i6483     fn intervals(&self) -> i64 {
84         (self.t.dwLowDateTime as i64) | ((self.t.dwHighDateTime as i64) << 32)
85     }
86 
sub_time(&self, other: &SystemTime) -> Result<Duration, Duration>87     pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
88         let me = self.intervals();
89         let other = other.intervals();
90         if me >= other {
91             Ok(intervals2dur((me - other) as u64))
92         } else {
93             Err(intervals2dur((other - me) as u64))
94         }
95     }
96 
checked_add_duration(&self, other: &Duration) -> Option<SystemTime>97     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
98         let intervals = self.intervals().checked_add(checked_dur2intervals(other)?)?;
99         Some(SystemTime::from_intervals(intervals))
100     }
101 
checked_sub_duration(&self, other: &Duration) -> Option<SystemTime>102     pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
103         let intervals = self.intervals().checked_sub(checked_dur2intervals(other)?)?;
104         Some(SystemTime::from_intervals(intervals))
105     }
106 }
107 
108 impl PartialEq for SystemTime {
eq(&self, other: &SystemTime) -> bool109     fn eq(&self, other: &SystemTime) -> bool {
110         self.intervals() == other.intervals()
111     }
112 }
113 
114 impl Eq for SystemTime {}
115 
116 impl PartialOrd for SystemTime {
partial_cmp(&self, other: &SystemTime) -> Option<Ordering>117     fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> {
118         Some(self.cmp(other))
119     }
120 }
121 
122 impl Ord for SystemTime {
cmp(&self, other: &SystemTime) -> Ordering123     fn cmp(&self, other: &SystemTime) -> Ordering {
124         self.intervals().cmp(&other.intervals())
125     }
126 }
127 
128 impl fmt::Debug for SystemTime {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result129     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130         f.debug_struct("SystemTime").field("intervals", &self.intervals()).finish()
131     }
132 }
133 
134 impl From<c::FILETIME> for SystemTime {
from(t: c::FILETIME) -> SystemTime135     fn from(t: c::FILETIME) -> SystemTime {
136         SystemTime { t }
137     }
138 }
139 
140 impl IntoInner<c::FILETIME> for SystemTime {
into_inner(self) -> c::FILETIME141     fn into_inner(self) -> c::FILETIME {
142         self.t
143     }
144 }
145 
146 impl Hash for SystemTime {
hash<H: Hasher>(&self, state: &mut H)147     fn hash<H: Hasher>(&self, state: &mut H) {
148         self.intervals().hash(state)
149     }
150 }
151 
checked_dur2intervals(dur: &Duration) -> Option<i64>152 fn checked_dur2intervals(dur: &Duration) -> Option<i64> {
153     dur.as_secs()
154         .checked_mul(INTERVALS_PER_SEC)?
155         .checked_add(dur.subsec_nanos() as u64 / 100)?
156         .try_into()
157         .ok()
158 }
159 
intervals2dur(intervals: u64) -> Duration160 fn intervals2dur(intervals: u64) -> Duration {
161     Duration::new(intervals / INTERVALS_PER_SEC, ((intervals % INTERVALS_PER_SEC) * 100) as u32)
162 }
163 
164 mod perf_counter {
165     use super::NANOS_PER_SEC;
166     use crate::sync::atomic::{AtomicU64, Ordering};
167     use crate::sys::c;
168     use crate::sys::cvt;
169     use crate::sys_common::mul_div_u64;
170     use crate::time::Duration;
171 
172     pub struct PerformanceCounterInstant {
173         ts: c::LARGE_INTEGER,
174     }
175     impl PerformanceCounterInstant {
now() -> Self176         pub fn now() -> Self {
177             Self { ts: query() }
178         }
179 
180         // Per microsoft docs, the margin of error for cross-thread time comparisons
181         // using QueryPerformanceCounter is 1 "tick" -- defined as 1/frequency().
182         // Reference: https://docs.microsoft.com/en-us/windows/desktop/SysInfo
183         //                   /acquiring-high-resolution-time-stamps
epsilon() -> Duration184         pub fn epsilon() -> Duration {
185             let epsilon = NANOS_PER_SEC / (frequency() as u64);
186             Duration::from_nanos(epsilon)
187         }
188     }
189     impl From<PerformanceCounterInstant> for super::Instant {
from(other: PerformanceCounterInstant) -> Self190         fn from(other: PerformanceCounterInstant) -> Self {
191             let freq = frequency() as u64;
192             let instant_nsec = mul_div_u64(other.ts as u64, NANOS_PER_SEC, freq);
193             Self { t: Duration::from_nanos(instant_nsec) }
194         }
195     }
196 
frequency() -> c::LARGE_INTEGER197     fn frequency() -> c::LARGE_INTEGER {
198         // Either the cached result of `QueryPerformanceFrequency` or `0` for
199         // uninitialized. Storing this as a single `AtomicU64` allows us to use
200         // `Relaxed` operations, as we are only interested in the effects on a
201         // single memory location.
202         static FREQUENCY: AtomicU64 = AtomicU64::new(0);
203 
204         let cached = FREQUENCY.load(Ordering::Relaxed);
205         // If a previous thread has filled in this global state, use that.
206         if cached != 0 {
207             return cached as c::LARGE_INTEGER;
208         }
209         // ... otherwise learn for ourselves ...
210         let mut frequency = 0;
211         unsafe {
212             cvt(c::QueryPerformanceFrequency(&mut frequency)).unwrap();
213         }
214 
215         FREQUENCY.store(frequency as u64, Ordering::Relaxed);
216         frequency
217     }
218 
query() -> c::LARGE_INTEGER219     fn query() -> c::LARGE_INTEGER {
220         let mut qpc_value: c::LARGE_INTEGER = 0;
221         cvt(unsafe { c::QueryPerformanceCounter(&mut qpc_value) }).unwrap();
222         qpc_value
223     }
224 }
225