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
use std::thread;
use std::time::{Instant, Duration};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SpinSleeper {
native_accuracy_ns: u32,
}
impl SpinSleeper {
pub fn new(native_accuracy_ns: u32) -> SpinSleeper {
SpinSleeper { native_accuracy_ns }
}
pub fn native_accuracy_ns(&self) -> u32 {
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 {
println!("Sleeping for a bit...");
thread::sleep(duration - accuracy)
}
while start.elapsed() < duration {}
}
pub fn sleep_s(&self, seconds: f64) {
if seconds > 0.0 {
println!("{:?}", Duration::new(seconds.floor() as u64,
((seconds % 1f64) * 1_000_000_000f64).round() as u32));
self.sleep(Duration::new(seconds.floor() as u64,
((seconds % 1f64) * 1_000_000_000f64).round() as u32))
}
}
pub fn sleep_ns(&self, nanoseconds: u64) {
let nanoseconds: u64 = nanoseconds.into();
let subsec_ns = (nanoseconds % 1_000_000_000) as u32;
let seconds = nanoseconds / 1_000_000_000;
self.sleep(Duration::new(seconds, subsec_ns))
}
}
#[cfg(test)]
mod spin_sleep_test {
use super::*;
const ACCEPTABLE_DELTA_NS: u32 = 100_000;
#[test]
fn sleep_small() {
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() {
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() {
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() {
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));
}
}