std\sys\pal\windows/
time.rs1use 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 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 perf_counter::PerformanceCounterInstant::now().into()
43 }
44
45 pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
46 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 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 static FREQUENCY: Atomic<u64> = AtomicU64::new(0);
203
204 let cached = FREQUENCY.load(Ordering::Relaxed);
205 if cached != 0 {
207 return cached as i64;
208 }
209 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
226pub(super) struct WaitableTimer {
228 handle: c::HANDLE,
229}
230impl WaitableTimer {
231 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 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}