diff options
| author | Dániel Buga <[email protected]> | 2024-11-26 23:54:21 +0100 |
|---|---|---|
| committer | Dániel Buga <[email protected]> | 2024-12-10 21:31:42 +0100 |
| commit | 5a5495aac43d75610735f2ca80fb6c8e8f31ed71 (patch) | |
| tree | 7a4336917894730692589359e9d1a285ec5a0a05 /embassy-time-queue-driver/src/lib.rs | |
| parent | 406d377b7564d16e12b7fae4f42c0c709bf4f243 (diff) | |
Refactor integrated-timers
Diffstat (limited to 'embassy-time-queue-driver/src/lib.rs')
| -rw-r--r-- | embassy-time-queue-driver/src/lib.rs | 136 |
1 files changed, 134 insertions, 2 deletions
diff --git a/embassy-time-queue-driver/src/lib.rs b/embassy-time-queue-driver/src/lib.rs index 50736e8c7..c5e989854 100644 --- a/embassy-time-queue-driver/src/lib.rs +++ b/embassy-time-queue-driver/src/lib.rs | |||
| @@ -6,7 +6,29 @@ | |||
| 6 | //! | 6 | //! |
| 7 | //! - Define a struct `MyTimerQueue` | 7 | //! - Define a struct `MyTimerQueue` |
| 8 | //! - Implement [`TimerQueue`] for it | 8 | //! - Implement [`TimerQueue`] for it |
| 9 | //! - Register it as the global timer queue with [`timer_queue_impl`](crate::timer_queue_impl). | 9 | //! - Register it as the global timer queue with [`timer_queue_impl`]. |
| 10 | //! - Ensure that you process the timer queue when `schedule_wake` is due. This usually involves | ||
| 11 | //! waking expired tasks, finding the next expiration time and setting an alarm. | ||
| 12 | //! | ||
| 13 | //! If a single global timer queue is sufficient for you, you can use the | ||
| 14 | //! [`GlobalTimerQueue`] type, which is a wrapper around a global timer queue | ||
| 15 | //! protected by a critical section. | ||
| 16 | //! | ||
| 17 | //! ``` | ||
| 18 | //! use embassy_time_queue_driver::GlobalTimerQueue; | ||
| 19 | //! embassy_time_queue_driver::timer_queue_impl!( | ||
| 20 | //! static TIMER_QUEUE_DRIVER: GlobalTimerQueue | ||
| 21 | //! = GlobalTimerQueue::new(|next_expiration| todo!("Set an alarm")) | ||
| 22 | //! ); | ||
| 23 | //! ``` | ||
| 24 | //! | ||
| 25 | //! You can also use the `queue_generic` or the `embassy_executor::raw::timer_queue` modules to | ||
| 26 | //! implement your own timer queue. These modules contain queue implementations which you can wrap | ||
| 27 | //! and tailor to your needs. | ||
| 28 | //! | ||
| 29 | //! If you are providing an embassy-executor implementation besides a timer queue, you can choose to | ||
| 30 | //! expose the `integrated-timers` feature in your implementation. This feature stores timer items | ||
| 31 | //! in the tasks themselves, so you don't need a fixed-size queue or dynamic memory allocation. | ||
| 10 | //! | 32 | //! |
| 11 | //! ## Example | 33 | //! ## Example |
| 12 | //! | 34 | //! |
| @@ -14,7 +36,7 @@ | |||
| 14 | //! use core::task::Waker; | 36 | //! use core::task::Waker; |
| 15 | //! | 37 | //! |
| 16 | //! use embassy_time::Instant; | 38 | //! use embassy_time::Instant; |
| 17 | //! use embassy_time::queue::{TimerQueue}; | 39 | //! use embassy_time::queue::TimerQueue; |
| 18 | //! | 40 | //! |
| 19 | //! struct MyTimerQueue{}; // not public! | 41 | //! struct MyTimerQueue{}; // not public! |
| 20 | //! | 42 | //! |
| @@ -26,11 +48,18 @@ | |||
| 26 | //! | 48 | //! |
| 27 | //! embassy_time_queue_driver::timer_queue_impl!(static QUEUE: MyTimerQueue = MyTimerQueue{}); | 49 | //! embassy_time_queue_driver::timer_queue_impl!(static QUEUE: MyTimerQueue = MyTimerQueue{}); |
| 28 | //! ``` | 50 | //! ``` |
| 51 | |||
| 52 | pub mod queue_generic; | ||
| 53 | |||
| 54 | use core::cell::RefCell; | ||
| 29 | use core::task::Waker; | 55 | use core::task::Waker; |
| 30 | 56 | ||
| 57 | use critical_section::Mutex; | ||
| 58 | |||
| 31 | /// Timer queue | 59 | /// Timer queue |
| 32 | pub trait TimerQueue { | 60 | pub trait TimerQueue { |
| 33 | /// Schedules a waker in the queue to be awoken at moment `at`. | 61 | /// Schedules a waker in the queue to be awoken at moment `at`. |
| 62 | /// | ||
| 34 | /// If this moment is in the past, the waker might be awoken immediately. | 63 | /// If this moment is in the past, the waker might be awoken immediately. |
| 35 | fn schedule_wake(&'static self, at: u64, waker: &Waker); | 64 | fn schedule_wake(&'static self, at: u64, waker: &Waker); |
| 36 | } | 65 | } |
| @@ -58,3 +87,106 @@ macro_rules! timer_queue_impl { | |||
| 58 | } | 87 | } |
| 59 | }; | 88 | }; |
| 60 | } | 89 | } |
| 90 | |||
| 91 | #[cfg(feature = "integrated-timers")] | ||
| 92 | type InnerQueue = embassy_executor::raw::timer_queue::TimerQueue; | ||
| 93 | |||
| 94 | #[cfg(not(feature = "integrated-timers"))] | ||
| 95 | type InnerQueue = queue_generic::Queue; | ||
| 96 | |||
| 97 | /// A timer queue implementation that can be used as a global timer queue. | ||
| 98 | /// | ||
| 99 | /// This implementation is not thread-safe, and should be protected by a mutex of some sort. | ||
| 100 | pub struct GenericTimerQueue<F: Fn(u64) -> bool> { | ||
| 101 | queue: InnerQueue, | ||
| 102 | set_alarm: F, | ||
| 103 | } | ||
| 104 | |||
| 105 | impl<F: Fn(u64) -> bool> GenericTimerQueue<F> { | ||
| 106 | /// Creates a new timer queue. | ||
| 107 | /// | ||
| 108 | /// `set_alarm` is a function that should set the next alarm time. The function should | ||
| 109 | /// return `true` if the alarm was set, and `false` if the alarm was in the past. | ||
| 110 | pub const fn new(set_alarm: F) -> Self { | ||
| 111 | Self { | ||
| 112 | queue: InnerQueue::new(), | ||
| 113 | set_alarm, | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | /// Schedules a task to run at a specific time, and returns whether any changes were made. | ||
| 118 | pub fn schedule_wake(&mut self, at: u64, waker: &core::task::Waker) { | ||
| 119 | #[cfg(feature = "integrated-timers")] | ||
| 120 | let waker = embassy_executor::raw::task_from_waker(waker); | ||
| 121 | |||
| 122 | if self.queue.schedule_wake(at, waker) { | ||
| 123 | self.dispatch() | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | /// Dequeues expired timers and returns the next alarm time. | ||
| 128 | pub fn next_expiration(&mut self, now: u64) -> u64 { | ||
| 129 | self.queue.next_expiration(now) | ||
| 130 | } | ||
| 131 | |||
| 132 | /// Handle the alarm. | ||
| 133 | /// | ||
| 134 | /// Call this function when the next alarm is due. | ||
| 135 | pub fn dispatch(&mut self) { | ||
| 136 | let mut next_expiration = self.next_expiration(embassy_time_driver::now()); | ||
| 137 | |||
| 138 | while !(self.set_alarm)(next_expiration) { | ||
| 139 | // next_expiration is in the past, dequeue and find a new expiration | ||
| 140 | next_expiration = self.next_expiration(next_expiration); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | /// A [`GenericTimerQueue`] protected by a critical section. Directly useable as a [`TimerQueue`]. | ||
| 146 | pub struct GlobalTimerQueue { | ||
| 147 | inner: Mutex<RefCell<GenericTimerQueue<fn(u64) -> bool>>>, | ||
| 148 | } | ||
| 149 | |||
| 150 | impl GlobalTimerQueue { | ||
| 151 | /// Creates a new timer queue. | ||
| 152 | /// | ||
| 153 | /// `set_alarm` is a function that should set the next alarm time. The function should | ||
| 154 | /// return `true` if the alarm was set, and `false` if the alarm was in the past. | ||
| 155 | pub const fn new(set_alarm: fn(u64) -> bool) -> Self { | ||
| 156 | Self { | ||
| 157 | inner: Mutex::new(RefCell::new(GenericTimerQueue::new(set_alarm))), | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | /// Schedules a task to run at a specific time, and returns whether any changes were made. | ||
| 162 | pub fn schedule_wake(&self, at: u64, waker: &core::task::Waker) { | ||
| 163 | critical_section::with(|cs| { | ||
| 164 | let mut inner = self.inner.borrow_ref_mut(cs); | ||
| 165 | inner.schedule_wake(at, waker); | ||
| 166 | }); | ||
| 167 | } | ||
| 168 | |||
| 169 | /// Dequeues expired timers and returns the next alarm time. | ||
| 170 | pub fn next_expiration(&self, now: u64) -> u64 { | ||
| 171 | critical_section::with(|cs| { | ||
| 172 | let mut inner = self.inner.borrow_ref_mut(cs); | ||
| 173 | inner.next_expiration(now) | ||
| 174 | }) | ||
| 175 | } | ||
| 176 | |||
| 177 | /// Handle the alarm. | ||
| 178 | /// | ||
| 179 | /// Call this function when the next alarm is due. | ||
| 180 | pub fn dispatch(&self) { | ||
| 181 | critical_section::with(|cs| { | ||
| 182 | let mut inner = self.inner.borrow_ref_mut(cs); | ||
| 183 | inner.dispatch() | ||
| 184 | }) | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | impl TimerQueue for GlobalTimerQueue { | ||
| 189 | fn schedule_wake(&'static self, at: u64, waker: &Waker) { | ||
| 190 | GlobalTimerQueue::schedule_wake(self, at, waker) | ||
| 191 | } | ||
| 192 | } | ||
