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
use core::sync::atomic::AtomicUsize;
use core::sync::atomic::{AtomicU32, AtomicU64, Ordering};
mod datetime;
mod duration;
mod instant;
#[macro_use]
mod macros;
mod units;
mod unix;
pub use datetime::*;
pub use duration::*;
pub use instant::*;
pub use units::*;
pub use unix::*;
pub(crate) const NANOS_PER_SEC: u64 = 1_000_000_000;
pub(crate) const NANOS_PER_MILLI: u64 = 1_000_000;
pub(crate) const NANOS_PER_MICRO: u64 = 1_000;
const UNINITIALIZED: usize = 0;
const INITIALIZED: usize = 1;
const REFRESHING: usize = 2;
static CLOCK: Clock = Clock::new();
pub fn refresh_clock() {
CLOCK.refresh()
}
struct Clock {
state: AtomicUsize,
coarse: Instant<Seconds<AtomicU32>>,
precise: Instant<Nanoseconds<AtomicU64>>,
coarse_unix: UnixInstant<Seconds<AtomicU32>>,
precise_unix: UnixInstant<Nanoseconds<AtomicU64>>,
}
impl Clock {
const fn new() -> Self {
Clock {
state: AtomicUsize::new(UNINITIALIZED),
coarse: Instant {
inner: Seconds {
inner: AtomicU32::new(0),
},
},
precise: Instant {
inner: Nanoseconds {
inner: AtomicU64::new(0),
},
},
coarse_unix: UnixInstant {
inner: Seconds {
inner: AtomicU32::new(0),
},
},
precise_unix: UnixInstant {
inner: Nanoseconds {
inner: AtomicU64::new(0),
},
},
}
}
fn initialize(&self) {
if self.state.load(Ordering::Relaxed) == UNINITIALIZED {
self.refresh();
}
}
fn refresh(&self) {
match self.state.load(Ordering::Relaxed) {
UNINITIALIZED => {
if self
.state
.compare_exchange(
UNINITIALIZED,
REFRESHING,
Ordering::Acquire,
Ordering::Acquire,
)
.is_ok()
{
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe {
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
}
self.coarse.store(
Instant {
inner: Seconds::from(ts),
},
Ordering::Release,
);
self.precise.store(
Instant {
inner: Nanoseconds::from(ts),
},
Ordering::Release,
);
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe {
libc::clock_gettime(libc::CLOCK_REALTIME, &mut ts);
}
self.coarse_unix.store(
UnixInstant {
inner: Seconds::from(ts),
},
Ordering::Release,
);
self.precise_unix.store(
UnixInstant {
inner: Nanoseconds::from(ts),
},
Ordering::Release,
);
self.state.store(INITIALIZED, Ordering::Release);
}
while self.state.load(Ordering::Relaxed) != INITIALIZED {}
}
INITIALIZED => {
if self
.state
.compare_exchange(
INITIALIZED,
REFRESHING,
Ordering::Acquire,
Ordering::Acquire,
)
.is_ok()
{
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe {
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
}
let now: Instant<Nanoseconds<u64>> = Instant {
inner: Nanoseconds::from(ts),
};
let previous = self.precise.load(Ordering::Acquire);
if now > previous {
self.precise.store(now, Ordering::Release);
self.coarse.store(
Instant {
inner: Seconds::from(ts),
},
Ordering::Release,
);
}
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe {
libc::clock_gettime(libc::CLOCK_REALTIME, &mut ts);
}
self.coarse_unix.store(
UnixInstant {
inner: Seconds::from(ts),
},
Ordering::Release,
);
self.precise_unix.store(
UnixInstant {
inner: Nanoseconds::from(ts),
},
Ordering::Release,
);
self.state.store(INITIALIZED, Ordering::Relaxed);
}
while self.state.load(Ordering::Relaxed) != INITIALIZED {}
}
REFRESHING => {
while self.state.load(Ordering::Relaxed) != INITIALIZED {}
}
_ => {
unreachable!()
}
}
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn basic() {
let now = Instant::<Nanoseconds<u64>>::now();
std::thread::sleep(std::time::Duration::new(1, 0));
let elapsed = now.elapsed();
assert!(elapsed.as_secs_f64() >= 1.0);
assert!(elapsed.as_secs() >= 1);
assert!(elapsed.as_nanos() >= NANOS_PER_SEC);
let t0 = Instant::<Nanoseconds<u64>>::recent();
std::thread::sleep(std::time::Duration::new(1, 0));
assert_eq!(Instant::<Nanoseconds<u64>>::recent(), t0);
refresh_clock();
let t1 = Instant::<Nanoseconds<u64>>::recent();
assert!((t1 - t0).as_secs_f64() >= 1.0);
assert!((t1 - t0).as_secs() >= 1);
}
}