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
|
//! A test that verifies the correctness of the [`TimerQueue`].
//!
//! To run this test, you need to activate the `critical-section/std` feature.
use std::{
fmt::Debug,
task::{Poll, Waker},
};
use cassette::Cassette;
use parking_lot::Mutex;
use rtic_time::{Monotonic, TimerQueue};
static NOW: Mutex<Option<Instant>> = Mutex::new(None);
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub struct Duration(u64);
impl Duration {
pub fn from_ticks(millis: u64) -> Self {
Self(millis)
}
pub fn as_ticks(&self) -> u64 {
self.0
}
}
impl core::ops::Add<Duration> for Duration {
type Output = Duration;
fn add(self, rhs: Duration) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl From<Duration> for Instant {
fn from(value: Duration) -> Self {
Instant(value.0)
}
}
static WAKERS: Mutex<Vec<Waker>> = Mutex::new(Vec::new());
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub struct Instant(u64);
impl Instant {
const ZERO: Self = Self(0);
pub fn tick() -> bool {
// If we've never ticked before, initialize the clock.
if NOW.lock().is_none() {
*NOW.lock() = Some(Instant::ZERO);
}
// We've ticked before, add one to the clock
else {
let now = Instant::now();
let new_time = now + Duration(1);
*NOW.lock() = Some(new_time);
}
let had_wakers = !WAKERS.lock().is_empty();
// Wake up all things waiting for a specific time to happen.
for waker in WAKERS.lock().drain(..) {
waker.wake_by_ref();
}
let had_interrupt = TestMono::tick(false);
had_interrupt || had_wakers
}
pub fn now() -> Self {
NOW.lock().clone().unwrap_or(Instant::ZERO)
}
pub fn elapsed(&self) -> Duration {
Duration(Self::now().0 - self.0)
}
pub async fn wait_until(time: Instant) {
core::future::poll_fn(|ctx| {
if Instant::now() >= time {
Poll::Ready(())
} else {
WAKERS.lock().push(ctx.waker().clone());
Poll::Pending
}
})
.await;
}
}
impl From<u64> for Instant {
fn from(value: u64) -> Self {
Self(value)
}
}
impl core::ops::Add<Duration> for Instant {
type Output = Instant;
fn add(self, rhs: Duration) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl core::ops::Sub<Duration> for Instant {
type Output = Instant;
fn sub(self, rhs: Duration) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl core::ops::Sub<Instant> for Instant {
type Output = Duration;
fn sub(self, rhs: Instant) -> Self::Output {
Duration(self.0 - rhs.0)
}
}
static COMPARE: Mutex<Option<Instant>> = Mutex::new(None);
static TIMER_QUEUE: TimerQueue<TestMono> = TimerQueue::new();
pub struct TestMono;
impl TestMono {
pub fn tick(force_interrupt: bool) -> bool {
let now = Instant::now();
let compare_reached = Some(now) == Self::compare();
let interrupt = compare_reached || force_interrupt;
if interrupt {
unsafe {
TestMono::queue().on_monotonic_interrupt();
}
true
} else {
false
}
}
/// Initialize the monotonic.
pub fn init() {
Self::queue().initialize(Self);
}
/// Used to access the underlying timer queue
pub fn queue() -> &'static TimerQueue<TestMono> {
&TIMER_QUEUE
}
pub fn compare() -> Option<Instant> {
COMPARE.lock().clone()
}
}
impl Monotonic for TestMono {
const ZERO: Self::Instant = Instant::ZERO;
type Instant = Instant;
type Duration = Duration;
fn now() -> Self::Instant {
Instant::now()
}
fn set_compare(instant: Self::Instant) {
let _ = COMPARE.lock().insert(instant);
}
fn clear_compare_flag() {}
fn pend_interrupt() {
Self::tick(true);
}
}
#[test]
fn timer_queue() {
TestMono::init();
let start = Instant::ZERO;
let build_delay_test = |pre_delay: Option<u64>, delay: u64| {
let delay = Duration::from_ticks(delay);
let pre_delay = pre_delay.map(Duration::from_ticks);
let total = if let Some(pre_delay) = pre_delay {
pre_delay + delay
} else {
delay
};
let total_millis = total.as_ticks();
async move {
// A `pre_delay` simulates a delay in scheduling,
// without the `pre_delay` being present in the timer
// queue
if let Some(pre_delay) = pre_delay {
Instant::wait_until(start + pre_delay).await;
}
TestMono::queue().delay(delay).await;
let elapsed = start.elapsed().as_ticks();
println!("{total_millis} ticks delay reached after {elapsed} ticks");
if elapsed != total_millis {
panic!(
"{total_millis} ticks delay was not on time ({elapsed} ticks passed instead)"
);
}
}
};
macro_rules! cassette {
($($x:ident),* $(,)?) => { $(
// Move the value to ensure that it is owned
let mut $x = $x;
// Shadow the original binding so that it can't be directly accessed
// ever again.
#[allow(unused_mut)]
let mut $x = unsafe {
core::pin::Pin::new_unchecked(&mut $x)
};
let mut $x = Cassette::new($x);
)* }
}
let d1 = build_delay_test(Some(100), 100);
cassette!(d1);
let d2 = build_delay_test(None, 300);
cassette!(d2);
let d3 = build_delay_test(None, 400);
cassette!(d3);
macro_rules! poll {
($($fut:ident),*) => {
$(if !$fut.is_done() {
$fut.poll_on();
})*
};
}
// Do an initial poll to set up all of the waiting futures
poll!(d1, d2, d3);
for _ in 0..500 {
// We only poll the waiting futures if an
// interrupt occured or if an artificial delay
// has passed.
if Instant::tick() {
poll!(d1, d2, d3);
}
if Instant::now() == 0.into() {
// First, we want to be waiting for our 300 tick delay
assert_eq!(TestMono::compare(), Some(300.into()));
}
if Instant::now() == 100.into() {
// After 100 ticks, we enqueue a new delay that is supposed to last
// until the 200-tick-mark
assert_eq!(TestMono::compare(), Some(200.into()));
}
if Instant::now() == 200.into() {
// After 200 ticks, we dequeue the 200-tick-mark delay and
// requeue the 300 tick delay
assert_eq!(TestMono::compare(), Some(300.into()));
}
if Instant::now() == 300.into() {
// After 300 ticks, we dequeue the 300-tick-mark delay and
// go to the 400 tick delay that is already enqueued
assert_eq!(TestMono::compare(), Some(400.into()));
}
}
assert!(d1.is_done() && d2.is_done() && d3.is_done());
}
|