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