std\sys\pal\windows/
time.rs

1use core::hash::{Hash, Hasher};
2use core::ops::Neg;
3
4use crate::cmp::Ordering;
5use crate::ptr::null;
6use crate::sys::c;
7use crate::sys_common::IntoInner;
8use crate::time::Duration;
9use crate::{fmt, mem};
10
11const NANOS_PER_SEC: u64 = 1_000_000_000;
12const INTERVALS_PER_SEC: u64 = NANOS_PER_SEC / 100;
13
14#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
15pub struct Instant {
16    // This duration is relative to an arbitrary microsecond epoch
17    // from the winapi QueryPerformanceCounter function.
18    t: Duration,
19}
20
21#[derive(Copy, Clone)]
22pub struct SystemTime {
23    t: c::FILETIME,
24}
25
26const INTERVALS_TO_UNIX_EPOCH: u64 = 11_644_473_600 * INTERVALS_PER_SEC;
27
28pub const UNIX_EPOCH: SystemTime = SystemTime {
29    t: c::FILETIME {
30        dwLowDateTime: INTERVALS_TO_UNIX_EPOCH as u32,
31        dwHighDateTime: (INTERVALS_TO_UNIX_EPOCH >> 32) as u32,
32    },
33};
34
35impl Instant {
36    pub fn now() -> Instant {
37        // High precision timing on windows operates in "Performance Counter"
38        // units, as returned by the WINAPI QueryPerformanceCounter function.
39        // These relate to seconds by a factor of QueryPerformanceFrequency.
40        // In order to keep unit conversions out of normal interval math, we
41        // measure in QPC units and immediately convert to nanoseconds.
42        perf_counter::PerformanceCounterInstant::now().into()
43    }
44
45    pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
46        // On windows there's a threshold below which we consider two timestamps
47        // equivalent due to measurement error. For more details + doc link,
48        // check the docs on epsilon.
49        let epsilon = perf_counter::PerformanceCounterInstant::epsilon();
50        if other.t > self.t && other.t - self.t <= epsilon {
51            Some(Duration::new(0, 0))
52        } else {
53            self.t.checked_sub(other.t)
54        }
55    }
56
57    pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
58        Some(Instant { t: self.t.checked_add(*other)? })
59    }
60
61    pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
62        Some(Instant { t: self.t.checked_sub(*other)? })
63    }
64}
65
66impl SystemTime {
67    pub fn now() -> SystemTime {
68        unsafe {
69            let mut t: SystemTime = mem::zeroed();
70            c::GetSystemTimePreciseAsFileTime(&mut t.t);
71            t
72        }
73    }
74
75    fn from_intervals(intervals: i64) -> SystemTime {
76        SystemTime {
77            t: c::FILETIME {
78                dwLowDateTime: intervals as u32,
79                dwHighDateTime: (intervals >> 32) as u32,
80            },
81        }
82    }
83
84    fn intervals(&self) -> i64 {
85        (self.t.dwLowDateTime as i64) | ((self.t.dwHighDateTime as i64) << 32)
86    }
87
88    pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
89        let me = self.intervals();
90        let other = other.intervals();
91        if me >= other {
92            Ok(intervals2dur((me - other) as u64))
93        } else {
94            Err(intervals2dur((other - me) as u64))
95        }
96    }
97
98    pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
99        let intervals = self.intervals().checked_add(checked_dur2intervals(other)?)?;
100        Some(SystemTime::from_intervals(intervals))
101    }
102
103    pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
104        let intervals = self.intervals().checked_sub(checked_dur2intervals(other)?)?;
105        Some(SystemTime::from_intervals(intervals))
106    }
107}
108
109impl PartialEq for SystemTime {
110    fn eq(&self, other: &SystemTime) -> bool {
111        self.intervals() == other.intervals()
112    }
113}
114
115impl Eq for SystemTime {}
116
117impl PartialOrd for SystemTime {
118    fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> {
119        Some(self.cmp(other))
120    }
121}
122
123impl Ord for SystemTime {
124    fn cmp(&self, other: &SystemTime) -> Ordering {
125        self.intervals().cmp(&other.intervals())
126    }
127}
128
129impl fmt::Debug for SystemTime {
130    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131        f.debug_struct("SystemTime").field("intervals", &self.intervals()).finish()
132    }
133}
134
135impl From<c::FILETIME> for SystemTime {
136    fn from(t: c::FILETIME) -> SystemTime {
137        SystemTime { t }
138    }
139}
140
141impl IntoInner<c::FILETIME> for SystemTime {
142    fn into_inner(self) -> c::FILETIME {
143        self.t
144    }
145}
146
147impl Hash for SystemTime {
148    fn hash<H: Hasher>(&self, state: &mut H) {
149        self.intervals().hash(state)
150    }
151}
152
153fn checked_dur2intervals(dur: &Duration) -> Option<i64> {
154    dur.as_secs()
155        .checked_mul(INTERVALS_PER_SEC)?
156        .checked_add(dur.subsec_nanos() as u64 / 100)?
157        .try_into()
158        .ok()
159}
160
161fn intervals2dur(intervals: u64) -> Duration {
162    Duration::new(intervals / INTERVALS_PER_SEC, ((intervals % INTERVALS_PER_SEC) * 100) as u32)
163}
164
165mod perf_counter {
166    use super::NANOS_PER_SEC;
167    use crate::sync::atomic::{Atomic, AtomicU64, Ordering};
168    use crate::sys::{c, cvt};
169    use crate::sys_common::mul_div_u64;
170    use crate::time::Duration;
171
172    pub struct PerformanceCounterInstant {
173        ts: i64,
174    }
175    impl PerformanceCounterInstant {
176        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
184        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 {
190        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
197    fn frequency() -> i64 {
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: Atomic<u64> = 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 i64;
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
219    fn query() -> i64 {
220        let mut qpc_value: i64 = 0;
221        cvt(unsafe { c::QueryPerformanceCounter(&mut qpc_value) }).unwrap();
222        qpc_value
223    }
224}
225
226/// A timer you can wait on.
227pub(super) struct WaitableTimer {
228    handle: c::HANDLE,
229}
230impl WaitableTimer {
231    /// Creates a high-resolution timer. Will fail before Windows 10, version 1803.
232    pub fn high_resolution() -> Result<Self, ()> {
233        let handle = unsafe {
234            c::CreateWaitableTimerExW(
235                null(),
236                null(),
237                c::CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
238                c::TIMER_ALL_ACCESS,
239            )
240        };
241        if !handle.is_null() { Ok(Self { handle }) } else { Err(()) }
242    }
243    pub fn set(&self, duration: Duration) -> Result<(), ()> {
244        // Convert the Duration to a format similar to FILETIME.
245        // Negative values are relative times whereas positive values are absolute.
246        // Therefore we negate the relative duration.
247        let time = checked_dur2intervals(&duration).ok_or(())?.neg();
248        let result = unsafe { c::SetWaitableTimer(self.handle, &time, 0, None, null(), c::FALSE) };
249        if result != 0 { Ok(()) } else { Err(()) }
250    }
251    pub fn wait(&self) -> Result<(), ()> {
252        let result = unsafe { c::WaitForSingleObject(self.handle, c::INFINITE) };
253        if result != c::WAIT_FAILED { Ok(()) } else { Err(()) }
254    }
255}
256impl Drop for WaitableTimer {
257    fn drop(&mut self) {
258        unsafe { c::CloseHandle(self.handle) };
259    }
260}