diff options
| author | Dario Nieuwenhuis <[email protected]> | 2020-12-30 00:56:32 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2020-12-30 00:57:35 +0100 |
| commit | 015b6bbce4fa93a58458bd72a5f7168d746f1e37 (patch) | |
| tree | a12a7ef8d4ee0e837bf4cf9f3d0bc8fbb6e5b62d | |
| parent | 2bf9b14ef07c4d2a33ee8a45b2f07b4cdd050e9e (diff) | |
Ensure timers always yield at least once.
This prevents a task that's constantly running late from monopolizing the CPU.
Add executor_fairness_test example showcasing it.
| -rw-r--r-- | embassy/src/executor/timer.rs | 12 | ||||
| -rw-r--r-- | examples/src/bin/executor_fairness_test.rs | 74 |
2 files changed, 83 insertions, 3 deletions
diff --git a/embassy/src/executor/timer.rs b/embassy/src/executor/timer.rs index 05c14b880..56236a058 100644 --- a/embassy/src/executor/timer.rs +++ b/embassy/src/executor/timer.rs | |||
| @@ -7,16 +7,21 @@ use crate::time::{Duration, Instant}; | |||
| 7 | 7 | ||
| 8 | pub struct Timer { | 8 | pub struct Timer { |
| 9 | expires_at: Instant, | 9 | expires_at: Instant, |
| 10 | yielded_once: bool, | ||
| 10 | } | 11 | } |
| 11 | 12 | ||
| 12 | impl Timer { | 13 | impl Timer { |
| 13 | pub fn at(expires_at: Instant) -> Self { | 14 | pub fn at(expires_at: Instant) -> Self { |
| 14 | Self { expires_at } | 15 | Self { |
| 16 | expires_at, | ||
| 17 | yielded_once: false, | ||
| 18 | } | ||
| 15 | } | 19 | } |
| 16 | 20 | ||
| 17 | pub fn after(duration: Duration) -> Self { | 21 | pub fn after(duration: Duration) -> Self { |
| 18 | Self { | 22 | Self { |
| 19 | expires_at: Instant::now() + duration, | 23 | expires_at: Instant::now() + duration, |
| 24 | yielded_once: false, | ||
| 20 | } | 25 | } |
| 21 | } | 26 | } |
| 22 | } | 27 | } |
| @@ -25,11 +30,12 @@ impl Unpin for Timer {} | |||
| 25 | 30 | ||
| 26 | impl Future for Timer { | 31 | impl Future for Timer { |
| 27 | type Output = (); | 32 | type Output = (); |
| 28 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | 33 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 29 | if self.expires_at <= Instant::now() { | 34 | if self.yielded_once && self.expires_at <= Instant::now() { |
| 30 | Poll::Ready(()) | 35 | Poll::Ready(()) |
| 31 | } else { | 36 | } else { |
| 32 | unsafe { super::register_timer(self.expires_at, cx.waker()) }; | 37 | unsafe { super::register_timer(self.expires_at, cx.waker()) }; |
| 38 | self.yielded_once = true; | ||
| 33 | Poll::Pending | 39 | Poll::Pending |
| 34 | } | 40 | } |
| 35 | } | 41 | } |
diff --git a/examples/src/bin/executor_fairness_test.rs b/examples/src/bin/executor_fairness_test.rs new file mode 100644 index 000000000..9b2c1bd26 --- /dev/null +++ b/examples/src/bin/executor_fairness_test.rs | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use example_common::*; | ||
| 8 | |||
| 9 | use core::task::Poll; | ||
| 10 | use cortex_m_rt::entry; | ||
| 11 | use defmt::panic; | ||
| 12 | use embassy::executor::{task, Executor}; | ||
| 13 | use embassy::time::{Duration, Instant, Timer}; | ||
| 14 | use embassy::util::Forever; | ||
| 15 | use embassy_nrf::pac; | ||
| 16 | use embassy_nrf::{interrupt, rtc}; | ||
| 17 | use nrf52840_hal::clocks; | ||
| 18 | |||
| 19 | #[task] | ||
| 20 | async fn run1() { | ||
| 21 | loop { | ||
| 22 | info!("DING DONG"); | ||
| 23 | Timer::after(Duration::from_ticks(16000)).await; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | #[task] | ||
| 28 | async fn run2() { | ||
| 29 | loop { | ||
| 30 | Timer::at(Instant::from_ticks(0)).await; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | #[task] | ||
| 35 | async fn run3() { | ||
| 36 | futures::future::poll_fn(|cx| { | ||
| 37 | cx.waker().wake_by_ref(); | ||
| 38 | Poll::<()>::Pending | ||
| 39 | }) | ||
| 40 | .await; | ||
| 41 | } | ||
| 42 | |||
| 43 | static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new(); | ||
| 44 | static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); | ||
| 45 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 46 | |||
| 47 | #[entry] | ||
| 48 | fn main() -> ! { | ||
| 49 | info!("Hello World!"); | ||
| 50 | |||
| 51 | let p = unwrap!(embassy_nrf::pac::Peripherals::take()); | ||
| 52 | |||
| 53 | clocks::Clocks::new(p.CLOCK) | ||
| 54 | .enable_ext_hfosc() | ||
| 55 | .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) | ||
| 56 | .start_lfclk(); | ||
| 57 | |||
| 58 | let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); | ||
| 59 | rtc.start(); | ||
| 60 | |||
| 61 | unsafe { embassy::time::set_clock(rtc) }; | ||
| 62 | |||
| 63 | let alarm = ALARM.put(rtc.alarm0()); | ||
| 64 | let executor = EXECUTOR.put(Executor::new_with_alarm(alarm, cortex_m::asm::sev)); | ||
| 65 | |||
| 66 | unwrap!(executor.spawn(run1())); | ||
| 67 | unwrap!(executor.spawn(run2())); | ||
| 68 | unwrap!(executor.spawn(run3())); | ||
| 69 | |||
| 70 | loop { | ||
| 71 | executor.run(); | ||
| 72 | cortex_m::asm::wfe(); | ||
| 73 | } | ||
| 74 | } | ||
