diff options
Diffstat (limited to 'embassy-time-queue-driver/src/queue_integrated.rs')
| -rw-r--r-- | embassy-time-queue-driver/src/queue_integrated.rs | 78 |
1 files changed, 78 insertions, 0 deletions
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..cb0f79356 --- /dev/null +++ b/embassy-time-queue-driver/src/queue_integrated.rs | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | //! Timer queue operations. | ||
| 2 | use core::cell::Cell; | ||
| 3 | use core::cmp::min; | ||
| 4 | |||
| 5 | use embassy_executor::raw::TaskRef; | ||
| 6 | |||
| 7 | /// A timer queue, with items integrated into tasks. | ||
| 8 | pub struct TimerQueue { | ||
| 9 | head: Cell<Option<TaskRef>>, | ||
| 10 | } | ||
| 11 | |||
| 12 | impl TimerQueue { | ||
| 13 | /// Creates a new timer queue. | ||
| 14 | pub const fn new() -> Self { | ||
| 15 | Self { head: Cell::new(None) } | ||
| 16 | } | ||
| 17 | |||
| 18 | /// Schedules a task to run at a specific time. | ||
| 19 | /// | ||
| 20 | /// If this function returns `true`, the called should find the next expiration time and set | ||
| 21 | /// a new alarm for that time. | ||
| 22 | pub fn schedule_wake(&mut self, at: u64, p: TaskRef) -> bool { | ||
| 23 | let item = p.timer_queue_item(); | ||
| 24 | if item.next.get().is_none() { | ||
| 25 | // If not in the queue, add it and update. | ||
| 26 | let prev = self.head.replace(Some(p)); | ||
| 27 | item.next.set(prev); | ||
| 28 | } else if at <= item.expires_at.get() { | ||
| 29 | // If expiration is sooner than previously set, update. | ||
| 30 | } else { | ||
| 31 | // Task does not need to be updated. | ||
| 32 | return false; | ||
| 33 | } | ||
| 34 | |||
| 35 | item.expires_at.set(at); | ||
| 36 | true | ||
| 37 | } | ||
| 38 | |||
| 39 | /// Dequeues expired timers and returns the next alarm time. | ||
| 40 | /// | ||
| 41 | /// The provided callback will be called for each expired task. Tasks that never expire | ||
| 42 | /// will be removed, but the callback will not be called. | ||
| 43 | pub fn next_expiration(&mut self, now: u64) -> u64 { | ||
| 44 | let mut next_expiration = u64::MAX; | ||
| 45 | |||
| 46 | self.retain(|p| { | ||
| 47 | let item = p.timer_queue_item(); | ||
| 48 | let expires = item.expires_at.get(); | ||
| 49 | |||
| 50 | if expires <= now { | ||
| 51 | // Timer expired, process task. | ||
| 52 | embassy_executor::raw::wake_task(p); | ||
| 53 | false | ||
| 54 | } else { | ||
| 55 | // Timer didn't yet expire, or never expires. | ||
| 56 | next_expiration = min(next_expiration, expires); | ||
| 57 | expires != u64::MAX | ||
| 58 | } | ||
| 59 | }); | ||
| 60 | |||
| 61 | next_expiration | ||
| 62 | } | ||
| 63 | |||
| 64 | fn retain(&self, mut f: impl FnMut(TaskRef) -> bool) { | ||
| 65 | let mut prev = &self.head; | ||
| 66 | while let Some(p) = prev.get() { | ||
| 67 | let item = p.timer_queue_item(); | ||
| 68 | if f(p) { | ||
| 69 | // Skip to next | ||
| 70 | prev = &item.next; | ||
| 71 | } else { | ||
| 72 | // Remove it | ||
| 73 | prev.set(item.next.get()); | ||
| 74 | item.next.set(None); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
