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::{IntoInner, c};
7use crate::time::Duration;
8use crate::{fmt, mem};
9
10const NANOS_PER_SEC: u64 = 1_000_000_000;
11const INTERVALS_PER_SEC: u64 = NANOS_PER_SEC / 100;
12
13#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
14pub 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)]
21pub struct SystemTime {
22    t: c::FILETIME,
23}
24
25const INTERVALS_TO_UNIX_EPOCH: u64 = 11_644_473_600 * INTERVALS_PER_SEC;
26
27pub 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
34impl 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 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
56    pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
57        Some(Instant { t: self.t.checked_add(*other)? })
58    }
59
60    pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
61        Some(Instant { t: self.t.checked_sub(*other)? })
62    }
63}
64
65impl SystemTime {
66    pub const MAX: SystemTime = SystemTime {
67        t: c::FILETIME {
68            dwLowDateTime: (i64::MAX & 0xFFFFFFFF) as u32,
69            dwHighDateTime: (i64::MAX >> 32) as u32,
70        },
71    };
72
73    pub const MIN: SystemTime =
74        SystemTime { t: c::FILETIME { dwLowDateTime: 0, dwHighDateTime: 0 } };
75
76    pub fn now() -> SystemTime {
77        unsafe {
78            let mut t: SystemTime = mem::zeroed();
79            c::GetSystemTimePreciseAsFileTime(&mut t.t);
80            t
81        }
82    }
83
84    fn from_intervals(intervals: i64) -> SystemTime {
85        SystemTime {
86            t: c::FILETIME {
87                dwLowDateTime: intervals as u32,
88                dwHighDateTime: (intervals >> 32) as u32,
89            },
90        }
91    }
92
93    fn intervals(&self) -> i64 {
94        (self.t.dwLowDateTime as i64) | ((self.t.dwHighDateTime as i64) << 32)
95    }
96
97    pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
98        let me = self.intervals();
99        let other = other.intervals();
100        if me >= other {
101            Ok(intervals2dur((me - other) as u64))
102        } else {
103            Err(intervals2dur((other - me) as u64))
104        }
105    }
106
107    pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
108        let intervals = self.intervals().checked_add(checked_dur2intervals(other)?)?;
109        Some(SystemTime::from_intervals(intervals))
110    }
111
112    pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
113        // Windows does not support times before 1601, hence why we don't
114        // support negatives. In order to tackle this, we try to convert the
115        // resulting value into an u64, which should obviously fail in the case
116        // that the value is below zero.
117        let intervals: u64 =
118            self.intervals().checked_sub(checked_dur2intervals(other)?)?.try_into().ok()?;
119        Some(SystemTime::from_intervals(intervals as i64))
120    }
121}
122
123impl PartialEq for SystemTime {
124    fn eq(&self, other: &SystemTime) -> bool {
125        self.intervals() == other.intervals()
126    }
127}
128
129impl Eq for SystemTime {}
130
131impl PartialOrd for SystemTime {
132    fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> {
133        Some(self.cmp(other))
134    }
135}
136
137impl Ord for SystemTime {
138    fn cmp(&self, other: &SystemTime) -> Ordering {
139        self.intervals().cmp(&other.intervals())
140    }
141}
142
143impl fmt::Debug for SystemTime {
144    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145        f.debug_struct("SystemTime").field("intervals", &self.intervals()).finish()
146    }
147}
148
149impl From<c::FILETIME> for SystemTime {
150    fn from(t: c::FILETIME) -> SystemTime {
151        SystemTime { t }
152    }
153}
154
155impl IntoInner<c::FILETIME> for SystemTime {
156    fn into_inner(self) -> c::FILETIME {
157        self.t
158    }
159}
160
161impl Hash for SystemTime {
162    fn hash<H: Hasher>(&self, state: &mut H) {
163        self.intervals().hash(state)
164    }
165}
166
167fn checked_dur2intervals(dur: &Duration) -> Option<i64> {
168    dur.as_secs()
169        .checked_mul(INTERVALS_PER_SEC)?
170        .checked_add(dur.subsec_nanos() as u64 / 100)?
171        .try_into()
172        .ok()
173}
174
175fn intervals2dur(intervals: u64) -> Duration {
176    Duration::new(intervals / INTERVALS_PER_SEC, ((intervals % INTERVALS_PER_SEC) * 100) as u32)
177}
178
179mod perf_counter {
180    use super::NANOS_PER_SEC;
181    use crate::sync::atomic::{Atomic, AtomicU64, Ordering};
182    use crate::sys::helpers::mul_div_u64;
183    use crate::sys::{c, cvt};
184    use crate::time::Duration;
185
186    pub struct PerformanceCounterInstant {
187        ts: i64,
188    }
189    impl PerformanceCounterInstant {
190        pub fn now() -> Self {
191            Self { ts: query() }
192        }
193
194        // Per microsoft docs, the margin of error for cross-thread time comparisons
195        // using QueryPerformanceCounter is 1 "tick" -- defined as 1/frequency().
196        // Reference: https://docs.microsoft.com/en-us/windows/desktop/SysInfo
197        //                   /acquiring-high-resolution-time-stamps
198        pub fn epsilon() -> Duration {
199            let epsilon = NANOS_PER_SEC / (frequency() as u64);
200            Duration::from_nanos(epsilon)
201        }
202    }
203    impl From<PerformanceCounterInstant> for super::Instant {
204        fn from(other: PerformanceCounterInstant) -> Self {
205            let freq = frequency() as u64;
206            let instant_nsec = mul_div_u64(other.ts as u64, NANOS_PER_SEC, freq);
207            Self { t: Duration::from_nanos(instant_nsec) }
208        }
209    }
210
211    fn frequency() -> i64 {
212        // Either the cached result of `QueryPerformanceFrequency` or `0` for
213        // uninitialized. Storing this as a single `AtomicU64` allows us to use
214        // `Relaxed` operations, as we are only interested in the effects on a
215        // single memory location.
216        static FREQUENCY: Atomic<u64> = AtomicU64::new(0);
217
218        let cached = FREQUENCY.load(Ordering::Relaxed);
219        // If a previous thread has filled in this global state, use that.
220        if cached != 0 {
221            return cached as i64;
222        }
223        // ... otherwise learn for ourselves ...
224        let mut frequency = 0;
225        unsafe {
226            cvt(c::QueryPerformanceFrequency(&mut frequency)).unwrap();
227        }
228
229        FREQUENCY.store(frequency as u64, Ordering::Relaxed);
230        frequency
231    }
232
233    fn query() -> i64 {
234        let mut qpc_value: i64 = 0;
235        cvt(unsafe { c::QueryPerformanceCounter(&mut qpc_value) }).unwrap();
236        qpc_value
237    }
238}
239
240/// A timer you can wait on.
241pub(crate) struct WaitableTimer {
242    handle: c::HANDLE,
243}
244impl WaitableTimer {
245    /// Creates a high-resolution timer. Will fail before Windows 10, version 1803.
246    pub fn high_resolution() -> Result<Self, ()> {
247        let handle = unsafe {
248            c::CreateWaitableTimerExW(
249                null(),
250                null(),
251                c::CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
252                c::TIMER_ALL_ACCESS,
253            )
254        };
255        if !handle.is_null() { Ok(Self { handle }) } else { Err(()) }
256    }
257    pub fn set(&self, duration: Duration) -> Result<(), ()> {
258        // Convert the Duration to a format similar to FILETIME.
259        // Negative values are relative times whereas positive values are absolute.
260        // Therefore we negate the relative duration.
261        let time = checked_dur2intervals(&duration).ok_or(())?.neg();
262        let result = unsafe { c::SetWaitableTimer(self.handle, &time, 0, None, null(), c::FALSE) };
263        if result != 0 { Ok(()) } else { Err(()) }
264    }
265    pub fn wait(&self) -> Result<(), ()> {
266        let result = unsafe { c::WaitForSingleObject(self.handle, c::INFINITE) };
267        if result != c::WAIT_FAILED { Ok(()) } else { Err(()) }
268    }
269}
270impl Drop for WaitableTimer {
271    fn drop(&mut self) {
272        unsafe { c::CloseHandle(self.handle) };
273    }
274}