diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-12-16 12:30:30 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-12-16 12:30:30 +0000 |
| commit | 2c3bc75da6008afa7cacc1045954cef7e3d8740f (patch) | |
| tree | 47661322d49d3e38717e2fc3f38e920c222138f7 /embassy-time-queue-driver | |
| parent | 99ad61cecf4fe098feeced5524d3e60625137457 (diff) | |
| parent | e1c00613288024623f7fde61f65c4c40c9a5381a (diff) | |
Merge pull request #3593 from bugadani/refactor
Rework time-driver contract.
Diffstat (limited to 'embassy-time-queue-driver')
| -rw-r--r-- | embassy-time-queue-driver/CHANGELOG.md | 15 | ||||
| -rw-r--r-- | embassy-time-queue-driver/Cargo.toml | 32 | ||||
| -rw-r--r-- | embassy-time-queue-driver/src/lib.rs | 75 | ||||
| -rw-r--r-- | embassy-time-queue-driver/src/queue_generic.rs | 146 | ||||
| -rw-r--r-- | embassy-time-queue-driver/src/queue_integrated.rs | 90 |
5 files changed, 315 insertions, 43 deletions
diff --git a/embassy-time-queue-driver/CHANGELOG.md b/embassy-time-queue-driver/CHANGELOG.md new file mode 100644 index 000000000..a99f250ed --- /dev/null +++ b/embassy-time-queue-driver/CHANGELOG.md | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | # Changelog for embassy-time-queue-driver | ||
| 2 | |||
| 3 | All notable changes to this project will be documented in this file. | ||
| 4 | |||
| 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
| 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
| 7 | |||
| 8 | ## Unreleased | ||
| 9 | |||
| 10 | - Added `generic-queue-N` features. | ||
| 11 | - Added `embassy_time_queue_driver::Queue` struct which uses integrated or a generic storage (configured using `generic-queue-N`). | ||
| 12 | |||
| 13 | ## 0.1.0 - 2024-01-11 | ||
| 14 | |||
| 15 | Initial release | ||
diff --git a/embassy-time-queue-driver/Cargo.toml b/embassy-time-queue-driver/Cargo.toml index 9ce9d79bb..a104f5c39 100644 --- a/embassy-time-queue-driver/Cargo.toml +++ b/embassy-time-queue-driver/Cargo.toml | |||
| @@ -20,6 +20,38 @@ categories = [ | |||
| 20 | # This is especially common when mixing crates from crates.io and git. | 20 | # This is especially common when mixing crates from crates.io and git. |
| 21 | links = "embassy-time-queue" | 21 | links = "embassy-time-queue" |
| 22 | 22 | ||
| 23 | [dependencies] | ||
| 24 | heapless = "0.8" | ||
| 25 | embassy-executor = { version = "0.6.3", path = "../embassy-executor" } | ||
| 26 | |||
| 27 | [features] | ||
| 28 | #! ### Generic Queue | ||
| 29 | |||
| 30 | #! By default this crate uses a timer queue implementation that is faster but depends on `embassy-executor`. | ||
| 31 | #! It will panic if you try to await any timer when using another executor. | ||
| 32 | #! | ||
| 33 | #! Alternatively, you can choose to use a "generic" timer queue implementation that works on any executor. | ||
| 34 | #! To enable it, enable any of the features below. | ||
| 35 | #! | ||
| 36 | #! The features also set how many timers are used for the generic queue. At most one | ||
| 37 | #! `generic-queue-*` feature can be enabled. If none is enabled, a default of 64 timers is used. | ||
| 38 | #! | ||
| 39 | #! When using embassy-time from libraries, you should *not* enable any `generic-queue-*` feature, to allow the | ||
| 40 | #! end user to pick. | ||
| 41 | |||
| 42 | ## Generic Queue with 8 timers | ||
| 43 | generic-queue-8 = ["_generic-queue"] | ||
| 44 | ## Generic Queue with 16 timers | ||
| 45 | generic-queue-16 = ["_generic-queue"] | ||
| 46 | ## Generic Queue with 32 timers | ||
| 47 | generic-queue-32 = ["_generic-queue"] | ||
| 48 | ## Generic Queue with 64 timers | ||
| 49 | generic-queue-64 = ["_generic-queue"] | ||
| 50 | ## Generic Queue with 128 timers | ||
| 51 | generic-queue-128 = ["_generic-queue"] | ||
| 52 | |||
| 53 | _generic-queue = [] | ||
| 54 | |||
| 23 | [package.metadata.embassy_docs] | 55 | [package.metadata.embassy_docs] |
| 24 | src_base = "https://github.com/embassy-rs/embassy/blob/embassy-time-queue-driver-v$VERSION/embassy-time-queue-driver/src/" | 56 | src_base = "https://github.com/embassy-rs/embassy/blob/embassy-time-queue-driver-v$VERSION/embassy-time-queue-driver/src/" |
| 25 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-time-queue-driver/src/" | 57 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-time-queue-driver/src/" |
diff --git a/embassy-time-queue-driver/src/lib.rs b/embassy-time-queue-driver/src/lib.rs index 50736e8c7..97c81a124 100644 --- a/embassy-time-queue-driver/src/lib.rs +++ b/embassy-time-queue-driver/src/lib.rs | |||
| @@ -2,38 +2,25 @@ | |||
| 2 | #![doc = include_str!("../README.md")] | 2 | #![doc = include_str!("../README.md")] |
| 3 | #![warn(missing_docs)] | 3 | #![warn(missing_docs)] |
| 4 | 4 | ||
| 5 | //! ## Implementing a timer queue | 5 | //! This crate is an implementation detail of `embassy-time-driver`. |
| 6 | //! | 6 | //! |
| 7 | //! - Define a struct `MyTimerQueue` | 7 | //! As a HAL user, you should only depend on this crate if your application does not use |
| 8 | //! - Implement [`TimerQueue`] for it | 8 | //! `embassy-executor` and your HAL does not configure a generic queue by itself. |
| 9 | //! - Register it as the global timer queue with [`timer_queue_impl`](crate::timer_queue_impl). | ||
| 10 | //! | 9 | //! |
| 11 | //! ## Example | 10 | //! As a HAL implementer, you need to depend on this crate if you want to implement a time driver, |
| 12 | //! | 11 | //! but how you should do so is documented in `embassy-time-driver`. |
| 13 | //! ``` | 12 | |
| 14 | //! use core::task::Waker; | ||
| 15 | //! | ||
| 16 | //! use embassy_time::Instant; | ||
| 17 | //! use embassy_time::queue::{TimerQueue}; | ||
| 18 | //! | ||
| 19 | //! struct MyTimerQueue{}; // not public! | ||
| 20 | //! | ||
| 21 | //! impl TimerQueue for MyTimerQueue { | ||
| 22 | //! fn schedule_wake(&'static self, at: u64, waker: &Waker) { | ||
| 23 | //! todo!() | ||
| 24 | //! } | ||
| 25 | //! } | ||
| 26 | //! | ||
| 27 | //! embassy_time_queue_driver::timer_queue_impl!(static QUEUE: MyTimerQueue = MyTimerQueue{}); | ||
| 28 | //! ``` | ||
| 29 | use core::task::Waker; | 13 | use core::task::Waker; |
| 30 | 14 | ||
| 31 | /// Timer queue | 15 | #[cfg(feature = "_generic-queue")] |
| 32 | pub trait TimerQueue { | 16 | pub mod queue_generic; |
| 33 | /// Schedules a waker in the queue to be awoken at moment `at`. | 17 | #[cfg(not(feature = "_generic-queue"))] |
| 34 | /// If this moment is in the past, the waker might be awoken immediately. | 18 | pub mod queue_integrated; |
| 35 | fn schedule_wake(&'static self, at: u64, waker: &Waker); | 19 | |
| 36 | } | 20 | #[cfg(feature = "_generic-queue")] |
| 21 | pub use queue_generic::Queue; | ||
| 22 | #[cfg(not(feature = "_generic-queue"))] | ||
| 23 | pub use queue_integrated::Queue; | ||
| 37 | 24 | ||
| 38 | extern "Rust" { | 25 | extern "Rust" { |
| 39 | fn _embassy_time_schedule_wake(at: u64, waker: &Waker); | 26 | fn _embassy_time_schedule_wake(at: u64, waker: &Waker); |
| @@ -41,20 +28,22 @@ extern "Rust" { | |||
| 41 | 28 | ||
| 42 | /// Schedule the given waker to be woken at `at`. | 29 | /// Schedule the given waker to be woken at `at`. |
| 43 | pub fn schedule_wake(at: u64, waker: &Waker) { | 30 | pub fn schedule_wake(at: u64, waker: &Waker) { |
| 44 | unsafe { _embassy_time_schedule_wake(at, waker) } | 31 | // This function is not implemented in embassy-time-driver because it needs access to executor |
| 45 | } | 32 | // internals. The function updates task state, then delegates to the implementation provided |
| 46 | 33 | // by the time driver. | |
| 47 | /// Set the TimerQueue implementation. | 34 | #[cfg(not(feature = "_generic-queue"))] |
| 48 | /// | 35 | { |
| 49 | /// See the module documentation for an example. | 36 | use embassy_executor::raw::task_from_waker; |
| 50 | #[macro_export] | 37 | use embassy_executor::raw::timer_queue::TimerEnqueueOperation; |
| 51 | macro_rules! timer_queue_impl { | 38 | // The very first thing we must do, before we even access the timer queue, is to |
| 52 | (static $name:ident: $t: ty = $val:expr) => { | 39 | // mark the task a TIMER_QUEUED. This ensures that the task that is being scheduled |
| 53 | static $name: $t = $val; | 40 | // can not be respawn while we are accessing the timer queue. |
| 54 | 41 | let task = task_from_waker(waker); | |
| 55 | #[no_mangle] | 42 | if unsafe { task.timer_enqueue() } == TimerEnqueueOperation::Ignore { |
| 56 | fn _embassy_time_schedule_wake(at: u64, waker: &core::task::Waker) { | 43 | // We are not allowed to enqueue the task in the timer queue. This is because the |
| 57 | <$t as $crate::TimerQueue>::schedule_wake(&$name, at, waker); | 44 | // task is not spawned, and so it makes no sense to schedule it. |
| 45 | return; | ||
| 58 | } | 46 | } |
| 59 | }; | 47 | } |
| 48 | unsafe { _embassy_time_schedule_wake(at, waker) } | ||
| 60 | } | 49 | } |
diff --git a/embassy-time-queue-driver/src/queue_generic.rs b/embassy-time-queue-driver/src/queue_generic.rs new file mode 100644 index 000000000..232035bc6 --- /dev/null +++ b/embassy-time-queue-driver/src/queue_generic.rs | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | //! Generic timer queue implementations. | ||
| 2 | //! | ||
| 3 | //! Time queue drivers may use this to simplify their implementation. | ||
| 4 | |||
| 5 | use core::cmp::{min, Ordering}; | ||
| 6 | use core::task::Waker; | ||
| 7 | |||
| 8 | use heapless::Vec; | ||
| 9 | |||
| 10 | #[derive(Debug)] | ||
| 11 | struct Timer { | ||
| 12 | at: u64, | ||
| 13 | waker: Waker, | ||
| 14 | } | ||
| 15 | |||
| 16 | impl PartialEq for Timer { | ||
| 17 | fn eq(&self, other: &Self) -> bool { | ||
| 18 | self.at == other.at | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | impl Eq for Timer {} | ||
| 23 | |||
| 24 | impl PartialOrd for Timer { | ||
| 25 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
| 26 | self.at.partial_cmp(&other.at) | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | impl Ord for Timer { | ||
| 31 | fn cmp(&self, other: &Self) -> Ordering { | ||
| 32 | self.at.cmp(&other.at) | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | /// A timer queue with a pre-determined capacity. | ||
| 37 | pub struct ConstGenericQueue<const QUEUE_SIZE: usize> { | ||
| 38 | queue: Vec<Timer, QUEUE_SIZE>, | ||
| 39 | } | ||
| 40 | |||
| 41 | impl<const QUEUE_SIZE: usize> ConstGenericQueue<QUEUE_SIZE> { | ||
| 42 | /// Creates a new timer queue. | ||
| 43 | pub const fn new() -> Self { | ||
| 44 | Self { queue: Vec::new() } | ||
| 45 | } | ||
| 46 | |||
| 47 | /// Schedules a task to run at a specific time, and returns whether any changes were made. | ||
| 48 | /// | ||
| 49 | /// If this function returns `true`, the called should find the next expiration time and set | ||
| 50 | /// a new alarm for that time. | ||
| 51 | pub fn schedule_wake(&mut self, at: u64, waker: &Waker) -> bool { | ||
| 52 | self.queue | ||
| 53 | .iter_mut() | ||
| 54 | .find(|timer| timer.waker.will_wake(waker)) | ||
| 55 | .map(|timer| { | ||
| 56 | if timer.at > at { | ||
| 57 | timer.at = at; | ||
| 58 | true | ||
| 59 | } else { | ||
| 60 | false | ||
| 61 | } | ||
| 62 | }) | ||
| 63 | .unwrap_or_else(|| { | ||
| 64 | let mut timer = Timer { | ||
| 65 | waker: waker.clone(), | ||
| 66 | at, | ||
| 67 | }; | ||
| 68 | |||
| 69 | loop { | ||
| 70 | match self.queue.push(timer) { | ||
| 71 | Ok(()) => break, | ||
| 72 | Err(e) => timer = e, | ||
| 73 | } | ||
| 74 | |||
| 75 | self.queue.pop().unwrap().waker.wake(); | ||
| 76 | } | ||
| 77 | |||
| 78 | true | ||
| 79 | }) | ||
| 80 | } | ||
| 81 | |||
| 82 | /// Dequeues expired timers and returns the next alarm time. | ||
| 83 | pub fn next_expiration(&mut self, now: u64) -> u64 { | ||
| 84 | let mut next_alarm = u64::MAX; | ||
| 85 | |||
| 86 | let mut i = 0; | ||
| 87 | while i < self.queue.len() { | ||
| 88 | let timer = &self.queue[i]; | ||
| 89 | if timer.at <= now { | ||
| 90 | let timer = self.queue.swap_remove(i); | ||
| 91 | timer.waker.wake(); | ||
| 92 | } else { | ||
| 93 | next_alarm = min(next_alarm, timer.at); | ||
| 94 | i += 1; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | next_alarm | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | #[cfg(feature = "generic-queue-8")] | ||
| 103 | const QUEUE_SIZE: usize = 8; | ||
| 104 | #[cfg(feature = "generic-queue-16")] | ||
| 105 | const QUEUE_SIZE: usize = 16; | ||
| 106 | #[cfg(feature = "generic-queue-32")] | ||
| 107 | const QUEUE_SIZE: usize = 32; | ||
| 108 | #[cfg(feature = "generic-queue-64")] | ||
| 109 | const QUEUE_SIZE: usize = 64; | ||
| 110 | #[cfg(feature = "generic-queue-128")] | ||
| 111 | const QUEUE_SIZE: usize = 128; | ||
| 112 | #[cfg(not(any( | ||
| 113 | feature = "generic-queue-8", | ||
| 114 | feature = "generic-queue-16", | ||
| 115 | feature = "generic-queue-32", | ||
| 116 | feature = "generic-queue-64", | ||
| 117 | feature = "generic-queue-128" | ||
| 118 | )))] | ||
| 119 | const QUEUE_SIZE: usize = 64; | ||
| 120 | |||
| 121 | /// A timer queue with a pre-determined capacity. | ||
| 122 | pub struct Queue { | ||
| 123 | queue: ConstGenericQueue<QUEUE_SIZE>, | ||
| 124 | } | ||
| 125 | |||
| 126 | impl Queue { | ||
| 127 | /// Creates a new timer queue. | ||
| 128 | pub const fn new() -> Self { | ||
| 129 | Self { | ||
| 130 | queue: ConstGenericQueue::new(), | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | /// Schedules a task to run at a specific time, and returns whether any changes were made. | ||
| 135 | /// | ||
| 136 | /// If this function returns `true`, the called should find the next expiration time and set | ||
| 137 | /// a new alarm for that time. | ||
| 138 | pub fn schedule_wake(&mut self, at: u64, waker: &Waker) -> bool { | ||
| 139 | self.queue.schedule_wake(at, waker) | ||
| 140 | } | ||
| 141 | |||
| 142 | /// Dequeues expired timers and returns the next alarm time. | ||
| 143 | pub fn next_expiration(&mut self, now: u64) -> u64 { | ||
| 144 | self.queue.next_expiration(now) | ||
| 145 | } | ||
| 146 | } | ||
diff --git a/embassy-time-queue-driver/src/queue_integrated.rs b/embassy-time-queue-driver/src/queue_integrated.rs new file mode 100644 index 000000000..6bb4c0c1a --- /dev/null +++ b/embassy-time-queue-driver/src/queue_integrated.rs | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | //! Timer queue operations. | ||
| 2 | use core::cell::Cell; | ||
| 3 | use core::cmp::min; | ||
| 4 | use core::task::Waker; | ||
| 5 | |||
| 6 | use embassy_executor::raw::TaskRef; | ||
| 7 | |||
| 8 | /// A timer queue, with items integrated into tasks. | ||
| 9 | pub struct Queue { | ||
| 10 | head: Cell<Option<TaskRef>>, | ||
| 11 | } | ||
| 12 | |||
| 13 | impl Queue { | ||
| 14 | /// Creates a new timer queue. | ||
| 15 | pub const fn new() -> Self { | ||
| 16 | Self { head: Cell::new(None) } | ||
| 17 | } | ||
| 18 | |||
| 19 | /// Schedules a task to run at a specific time. | ||
| 20 | /// | ||
| 21 | /// If this function returns `true`, the called should find the next expiration time and set | ||
| 22 | /// a new alarm for that time. | ||
| 23 | pub fn schedule_wake(&mut self, at: u64, waker: &Waker) -> bool { | ||
| 24 | let task = embassy_executor::raw::task_from_waker(waker); | ||
| 25 | let item = task.timer_queue_item(); | ||
| 26 | if item.next.get().is_none() { | ||
| 27 | // If not in the queue, add it and update. | ||
| 28 | let prev = self.head.replace(Some(task)); | ||
| 29 | item.next.set(if prev.is_none() { | ||
| 30 | Some(unsafe { TaskRef::dangling() }) | ||
| 31 | } else { | ||
| 32 | prev | ||
| 33 | }); | ||
| 34 | item.expires_at.set(at); | ||
| 35 | true | ||
| 36 | } else if at <= item.expires_at.get() { | ||
| 37 | // If expiration is sooner than previously set, update. | ||
| 38 | item.expires_at.set(at); | ||
| 39 | true | ||
| 40 | } else { | ||
| 41 | // Task does not need to be updated. | ||
| 42 | false | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | /// Dequeues expired timers and returns the next alarm time. | ||
| 47 | /// | ||
| 48 | /// The provided callback will be called for each expired task. Tasks that never expire | ||
| 49 | /// will be removed, but the callback will not be called. | ||
| 50 | pub fn next_expiration(&mut self, now: u64) -> u64 { | ||
| 51 | let mut next_expiration = u64::MAX; | ||
| 52 | |||
| 53 | self.retain(|p| { | ||
| 54 | let item = p.timer_queue_item(); | ||
| 55 | let expires = item.expires_at.get(); | ||
| 56 | |||
| 57 | if expires <= now { | ||
| 58 | // Timer expired, process task. | ||
| 59 | embassy_executor::raw::wake_task(p); | ||
| 60 | false | ||
| 61 | } else { | ||
| 62 | // Timer didn't yet expire, or never expires. | ||
| 63 | next_expiration = min(next_expiration, expires); | ||
| 64 | expires != u64::MAX | ||
| 65 | } | ||
| 66 | }); | ||
| 67 | |||
| 68 | next_expiration | ||
| 69 | } | ||
| 70 | |||
| 71 | fn retain(&self, mut f: impl FnMut(TaskRef) -> bool) { | ||
| 72 | let mut prev = &self.head; | ||
| 73 | while let Some(p) = prev.get() { | ||
| 74 | if unsafe { p == TaskRef::dangling() } { | ||
| 75 | // prev was the last item, stop | ||
| 76 | break; | ||
| 77 | } | ||
| 78 | let item = p.timer_queue_item(); | ||
| 79 | if f(p) { | ||
| 80 | // Skip to next | ||
| 81 | prev = &item.next; | ||
| 82 | } else { | ||
| 83 | // Remove it | ||
| 84 | prev.set(item.next.get()); | ||
| 85 | item.next.set(None); | ||
| 86 | unsafe { p.timer_dequeue() }; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
