1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
mod loop_helper;
pub use crate::loop_helper::*;
use std::{
thread,
time::{Duration, Instant},
};
pub type Seconds = f64;
pub type RatePerSecond = f64;
pub type Nanoseconds = u64;
pub type SubsecondNanoseconds = u32;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SpinSleeper {
native_accuracy_ns: u32,
}
#[cfg(not(windows))]
const DEFAULT_NATIVE_SLEEP_ACCURACY: SubsecondNanoseconds = 125_000;
#[cfg(not(windows))]
#[inline]
pub(crate) fn thread_sleep(duration: Duration) {
thread::sleep(duration)
}
#[cfg(windows)]
static MIN_TIME_PERIOD: once_cell::sync::Lazy<winapi::shared::minwindef::UINT> =
once_cell::sync::Lazy::new(|| unsafe {
use std::mem;
use winapi::um::{mmsystem::*, timeapi::timeGetDevCaps};
let tc_size = mem::size_of::<TIMECAPS>() as u32;
let mut tc = TIMECAPS {
wPeriodMin: 0,
wPeriodMax: 0,
};
if timeGetDevCaps(&mut tc as *mut TIMECAPS, tc_size) == TIMERR_NOERROR {
tc.wPeriodMin
} else {
1
}
});
#[cfg(windows)]
#[inline]
pub(crate) fn thread_sleep(duration: Duration) {
unsafe {
use winapi::um::timeapi::{timeBeginPeriod, timeEndPeriod};
timeBeginPeriod(*MIN_TIME_PERIOD);
thread::sleep(duration);
timeEndPeriod(*MIN_TIME_PERIOD);
}
}
impl Default for SpinSleeper {
#[inline]
fn default() -> Self {
#[cfg(windows)]
let accuracy = *MIN_TIME_PERIOD * 1_000_000;
#[cfg(not(windows))]
let accuracy = DEFAULT_NATIVE_SLEEP_ACCURACY;
SpinSleeper::new(accuracy)
}
}
impl SpinSleeper {
#[inline]
pub fn new(native_accuracy_ns: SubsecondNanoseconds) -> SpinSleeper {
SpinSleeper { native_accuracy_ns }
}
pub fn native_accuracy_ns(self) -> SubsecondNanoseconds {
self.native_accuracy_ns
}
pub fn sleep(self, duration: Duration) {
let start = Instant::now();
let accuracy = Duration::new(0, self.native_accuracy_ns);
if duration > accuracy {
thread_sleep(duration - accuracy);
}
while start.elapsed() < duration {
thread::yield_now();
}
}
pub fn sleep_s(self, seconds: Seconds) {
if seconds > 0.0 {
self.sleep(Duration::from_secs_f64(seconds));
}
}
pub fn sleep_ns(self, nanoseconds: Nanoseconds) {
let subsec_ns = (nanoseconds % 1_000_000_000) as u32;
let seconds = nanoseconds / 1_000_000_000;
self.sleep(Duration::new(seconds, subsec_ns))
}
}
pub fn sleep(duration: Duration) {
SpinSleeper::default().sleep(duration);
}
#[cfg(feature = "nondeterministic_tests")]
#[cfg(test)]
mod spin_sleep_test {
use super::*;
const ACCEPTABLE_DELTA_NS: SubsecondNanoseconds = 50_000;
macro_rules! passes_eventually {
($test:stmt) => {{
let mut error = None;
for _ in 0..50 {
match ::std::panic::catch_unwind(|| {
$test;
}) {
Ok(_) => break,
Err(err) => {
error = error.or(Some(err));
thread::sleep(Duration::new(0, 1000));
}
}
}
assert!(
error.is_none(),
"Test failed 50/50 times: {:?}",
error.unwrap()
);
}};
}
#[test]
fn sleep_small() {
passes_eventually!({
let ns_duration = 12_345_678;
let ps = SpinSleeper::new(20_000_000);
ps.sleep(Duration::new(0, 1000));
let before = Instant::now();
ps.sleep(Duration::new(0, ns_duration));
let after = Instant::now();
println!("Actual: {:?}", after.duration_since(before));
assert!(
after.duration_since(before) <= Duration::new(0, ns_duration + ACCEPTABLE_DELTA_NS)
);
assert!(
after.duration_since(before) >= Duration::new(0, ns_duration - ACCEPTABLE_DELTA_NS)
);
});
}
#[test]
fn sleep_big() {
passes_eventually!({
let ns_duration = 212_345_678;
let ps = SpinSleeper::new(20_000_000);
ps.sleep(Duration::new(0, 1000));
let before = Instant::now();
ps.sleep(Duration::new(1, ns_duration));
let after = Instant::now();
println!("Actual: {:?}", after.duration_since(before));
assert!(
after.duration_since(before) <= Duration::new(1, ns_duration + ACCEPTABLE_DELTA_NS)
);
assert!(
after.duration_since(before) >= Duration::new(1, ns_duration - ACCEPTABLE_DELTA_NS)
);
});
}
#[test]
fn sleep_s() {
passes_eventually!({
let ns_duration = 12_345_678_f64;
let ps = SpinSleeper::new(20_000_000);
ps.sleep_s(0.000001);
let before = Instant::now();
ps.sleep_s(ns_duration / 1_000_000_000_f64);
let after = Instant::now();
println!("Actual: {:?}", after.duration_since(before));
assert!(
after.duration_since(before)
<= Duration::new(0, ns_duration.round() as u32 + ACCEPTABLE_DELTA_NS)
);
assert!(
after.duration_since(before)
>= Duration::new(0, ns_duration.round() as u32 - ACCEPTABLE_DELTA_NS)
);
});
}
#[test]
fn sleep_ns() {
passes_eventually!({
let ns_duration: u32 = 12_345_678;
let ps = SpinSleeper::new(20_000_000);
ps.sleep_ns(1000);
let before = Instant::now();
ps.sleep_ns(ns_duration as u64);
let after = Instant::now();
println!("Actual: {:?}", after.duration_since(before));
assert!(
after.duration_since(before) <= Duration::new(0, ns_duration + ACCEPTABLE_DELTA_NS)
);
assert!(
after.duration_since(before) >= Duration::new(0, ns_duration - ACCEPTABLE_DELTA_NS)
);
});
}
}
#[test]
#[ignore]
fn print_estimated_thread_sleep_accuracy() {
let mut best = Duration::from_secs(100);
let mut sum = Duration::from_secs(0);
let mut worst = Duration::from_secs(0);
for _ in 0..100 {
let before = Instant::now();
thread_sleep(Duration::new(0, 1));
let elapsed = before.elapsed();
sum += elapsed;
if elapsed < best {
best = elapsed;
}
if elapsed > worst {
worst = elapsed;
}
}
println!(
"average: {:?}, best : {:?}, worst: {:?}",
Duration::from_nanos((sum.subsec_nanos() / 100).into()),
best,
worst,
);
panic!("Manual use only, ignore when done");
}