diff options
Diffstat (limited to 'embassy-time-queue-driver/src/queue_generic.rs')
| -rw-r--r-- | embassy-time-queue-driver/src/queue_generic.rs | 146 |
1 files changed, 146 insertions, 0 deletions
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 | } | ||
