diff options
Diffstat (limited to 'embassy-time/src/queue_generic.rs')
| -rw-r--r-- | embassy-time/src/queue_generic.rs | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/embassy-time/src/queue_generic.rs b/embassy-time/src/queue_generic.rs index 6769d6a58..8a355b327 100644 --- a/embassy-time/src/queue_generic.rs +++ b/embassy-time/src/queue_generic.rs | |||
| @@ -57,19 +57,11 @@ impl Ord for Timer { | |||
| 57 | 57 | ||
| 58 | struct InnerQueue { | 58 | struct InnerQueue { |
| 59 | queue: SortedLinkedList<Timer, LinkedIndexU8, Min, { QUEUE_SIZE }>, | 59 | queue: SortedLinkedList<Timer, LinkedIndexU8, Min, { QUEUE_SIZE }>, |
| 60 | alarm: Option<AlarmHandle>, | 60 | alarm: AlarmHandle, |
| 61 | alarm_at: Instant, | 61 | alarm_at: Instant, |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | impl InnerQueue { | 64 | impl InnerQueue { |
| 65 | const fn new() -> Self { | ||
| 66 | Self { | ||
| 67 | queue: SortedLinkedList::new_u8(), | ||
| 68 | alarm: None, | ||
| 69 | alarm_at: Instant::MAX, | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | fn schedule_wake(&mut self, at: Instant, waker: &Waker) { | 65 | fn schedule_wake(&mut self, at: Instant, waker: &Waker) { |
| 74 | self.queue | 66 | self.queue |
| 75 | .find_mut(|timer| timer.waker.will_wake(waker)) | 67 | .find_mut(|timer| timer.waker.will_wake(waker)) |
| @@ -121,7 +113,7 @@ impl InnerQueue { | |||
| 121 | if self.alarm_at != new_at { | 113 | if self.alarm_at != new_at { |
| 122 | self.alarm_at = new_at; | 114 | self.alarm_at = new_at; |
| 123 | 115 | ||
| 124 | return set_alarm(self.alarm.unwrap(), self.alarm_at.as_ticks()); | 116 | return set_alarm(self.alarm, self.alarm_at.as_ticks()); |
| 125 | } | 117 | } |
| 126 | } else { | 118 | } else { |
| 127 | self.alarm_at = Instant::MAX; | 119 | self.alarm_at = Instant::MAX; |
| @@ -138,13 +130,13 @@ impl InnerQueue { | |||
| 138 | } | 130 | } |
| 139 | 131 | ||
| 140 | struct Queue { | 132 | struct Queue { |
| 141 | inner: Mutex<CriticalSectionRawMutex, RefCell<InnerQueue>>, | 133 | inner: Mutex<CriticalSectionRawMutex, RefCell<Option<InnerQueue>>>, |
| 142 | } | 134 | } |
| 143 | 135 | ||
| 144 | impl Queue { | 136 | impl Queue { |
| 145 | const fn new() -> Self { | 137 | const fn new() -> Self { |
| 146 | Self { | 138 | Self { |
| 147 | inner: Mutex::new(RefCell::new(InnerQueue::new())), | 139 | inner: Mutex::new(RefCell::new(None)), |
| 148 | } | 140 | } |
| 149 | } | 141 | } |
| 150 | 142 | ||
| @@ -152,19 +144,25 @@ impl Queue { | |||
| 152 | self.inner.lock(|inner| { | 144 | self.inner.lock(|inner| { |
| 153 | let mut inner = inner.borrow_mut(); | 145 | let mut inner = inner.borrow_mut(); |
| 154 | 146 | ||
| 155 | if inner.alarm.is_none() { | 147 | if inner.is_none() {} |
| 156 | let handle = unsafe { allocate_alarm() }.unwrap(); | ||
| 157 | inner.alarm = Some(handle); | ||
| 158 | |||
| 159 | set_alarm_callback(handle, Self::handle_alarm_callback, self as *const _ as _); | ||
| 160 | } | ||
| 161 | 148 | ||
| 162 | inner.schedule_wake(at, waker) | 149 | inner |
| 150 | .get_or_insert_with(|| { | ||
| 151 | let handle = unsafe { allocate_alarm() }.unwrap(); | ||
| 152 | set_alarm_callback(handle, Self::handle_alarm_callback, self as *const _ as _); | ||
| 153 | InnerQueue { | ||
| 154 | queue: SortedLinkedList::new_u8(), | ||
| 155 | alarm: handle, | ||
| 156 | alarm_at: Instant::MAX, | ||
| 157 | } | ||
| 158 | }) | ||
| 159 | .schedule_wake(at, waker) | ||
| 163 | }); | 160 | }); |
| 164 | } | 161 | } |
| 165 | 162 | ||
| 166 | fn handle_alarm(&self) { | 163 | fn handle_alarm(&self) { |
| 167 | self.inner.lock(|inner| inner.borrow_mut().handle_alarm()); | 164 | self.inner |
| 165 | .lock(|inner| inner.borrow_mut().as_mut().unwrap().handle_alarm()); | ||
| 168 | } | 166 | } |
| 169 | 167 | ||
| 170 | fn handle_alarm_callback(ctx: *mut ()) { | 168 | fn handle_alarm_callback(ctx: *mut ()) { |
