Skip to content

Commit 53ff4fe

Browse files
thejpsterkorken89
authored andcommitted
Updates to "Delay and TImeout using Monotonics"
Some inconsistencies in the text caused confusion on my first few read-throughs, so I've tried to add some clarity.
1 parent 77a29b4 commit 53ff4fe

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

book/en/src/by-example/delay.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Tasks with delay
1+
# Delay and Timeout using Monotonics
22

33
A convenient way to express miniminal timing requirements is by delaying progression.
44

@@ -53,7 +53,7 @@ Rust [`Future`]s (underlying Rust `async`/`await`) are composable. This makes it
5353

5454
[`Future`]: https://doc.rust-lang.org/std/future/trait.Future.html
5555

56-
A common use case is transactions with an associated timeout. In the examples shown below, we introduce a fake HAL device that performs some transaction. We have modelled the time it takes based on the input parameter (`n`) as `350ms + n * 100ms`.
56+
A common use case is transactions with an associated timeout. In the examples shown below, we introduce a fake HAL device that performs some imagined transaction when you call `hal_get(n).await`. We have modelled the time it takes based on the input parameter (`n`) as `350ms + n * 100ms`.
5757

5858
Using the `select_biased` macro from the `futures` crate it may look like this:
5959

@@ -65,29 +65,23 @@ Assuming the `hal_get` will take 450ms to finish, a short timeout of 200ms will
6565

6666
Extending the timeout to 1000ms would cause `hal_get` will to complete first.
6767

68-
Using `select_biased` any number of futures can be combined, so its very powerful. However, as the timeout pattern is frequently used, more ergonomic support is baked into RTIC, provided by the [`rtic-monotonics`] and [`rtic-time`] crates.
69-
70-
Rewriting the second example from above using `timeout_after` gives:
68+
Using `select_biased` any number of futures can be combined, so its very powerful. However, as the timeout pattern is frequently used, more ergonomic support is baked into RTIC, provided by the [`rtic-monotonics`] and [`rtic-time`] crates. Here's another example, using `Mono::delay_until` and `Mono::timeout_after`:
7169

7270
```rust,noplayground
7371
{{#include ../../../../examples/lm3s6965/examples/async-timeout.rs:timeout_at_basic}}
7472
```
7573

7674
In cases where you want exact control over time without drift we can use exact points in time using `Instant`, and spans of time using `Duration`. Operations on the `Instant` and `Duration` types come from the [`fugit`] crate.
7775

78-
[fugit]: https://crates.io/crates/fugit
79-
80-
`let mut instant = Systick::now()` sets the starting time of execution.
81-
82-
We want to call `hal_get` after 1000ms relative to this starting time. This can be accomplished by using `Systick::delay_until(instant).await`.
76+
[`fugit`]: https://crates.io/crates/fugit
8377

84-
Then, we define a point in time called `timeout`, and call `Systick::timeout_at(timeout, hal_get(n)).await`.
78+
`let mut instant = Mono::now()` sets the starting time of execution.
8579

86-
For the first iteration of the loop, with `n == 0`, the `hal_get` will take 350ms (and finishes before the timeout).
80+
We want to call `hal_get` every 1000ms relative to this starting time. We accomplish this by incrementing our `instant` by 1000 ms and then using `Mono::delay_until(instant).await`. Any additional delays incurred as we iterate around this loop are compensated for by delaying until 'previous + 1000' as opposed to 'now + 1000' (which would cause our loop timing to drift).
8781

88-
For the second iteration, with `n == 1`, the `hal_get` will take 450ms (and again succeeds to finish before the timeout).
82+
To show an alternative to the `select!` async timeout example above, we define a future point in time as `timeout`, and call `Mono::timeout_at(timeout, hal_get(n)).await`.
8983

90-
For the third iteration, with `n == 2`, `hal_get` will take 550ms to finish, in which case we will run into a timeout.
84+
For the first iteration of the loop, with `n == 0`, the `hal_get` will take 350ms (as described above), and finishes before the timeout. For the second iteration, the delay is 450ms, which still finishes before the timeout. For the third iteration, with `n == 2`, `hal_get` will take 550ms to finish, in which case we will run into a timeout.
9185

9286
<details>
9387
<summary>A complete example</summary>

0 commit comments

Comments
 (0)