diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-10-26 21:00:50 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-10-26 21:00:50 +0200 |
| commit | f9da6271cea7035b2c9f27cfe479aa81889168d1 (patch) | |
| tree | 14c8c0bfdb58887b0c14e7b703df38fc9feb8834 /embassy-time/src/queue_generic.rs | |
| parent | 4976cbbe6040d5e147e7c42bd29b72d6223b05b0 (diff) | |
time/generic_queue: use Vec instead of SortedLinkedList
Diffstat (limited to 'embassy-time/src/queue_generic.rs')
| -rw-r--r-- | embassy-time/src/queue_generic.rs | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/embassy-time/src/queue_generic.rs b/embassy-time/src/queue_generic.rs index 8a355b327..20ae7e6cc 100644 --- a/embassy-time/src/queue_generic.rs +++ b/embassy-time/src/queue_generic.rs | |||
| @@ -4,7 +4,7 @@ use core::task::Waker; | |||
| 4 | 4 | ||
| 5 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | 5 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; |
| 6 | use embassy_sync::blocking_mutex::Mutex; | 6 | use embassy_sync::blocking_mutex::Mutex; |
| 7 | use heapless::sorted_linked_list::{LinkedIndexU8, Min, SortedLinkedList}; | 7 | use heapless::Vec; |
| 8 | 8 | ||
| 9 | use crate::driver::{allocate_alarm, set_alarm, set_alarm_callback, AlarmHandle}; | 9 | use crate::driver::{allocate_alarm, set_alarm, set_alarm_callback, AlarmHandle}; |
| 10 | use crate::queue::TimerQueue; | 10 | use crate::queue::TimerQueue; |
| @@ -56,18 +56,17 @@ impl Ord for Timer { | |||
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | struct InnerQueue { | 58 | struct InnerQueue { |
| 59 | queue: SortedLinkedList<Timer, LinkedIndexU8, Min, { QUEUE_SIZE }>, | 59 | queue: Vec<Timer, QUEUE_SIZE>, |
| 60 | alarm: AlarmHandle, | 60 | alarm: AlarmHandle, |
| 61 | alarm_at: Instant, | ||
| 62 | } | 61 | } |
| 63 | 62 | ||
| 64 | impl InnerQueue { | 63 | impl InnerQueue { |
| 65 | fn schedule_wake(&mut self, at: Instant, waker: &Waker) { | 64 | fn schedule_wake(&mut self, at: Instant, waker: &Waker) { |
| 66 | self.queue | 65 | self.queue |
| 67 | .find_mut(|timer| timer.waker.will_wake(waker)) | 66 | .iter_mut() |
| 67 | .find(|timer| timer.waker.will_wake(waker)) | ||
| 68 | .map(|mut timer| { | 68 | .map(|mut timer| { |
| 69 | timer.at = min(timer.at, at); | 69 | timer.at = min(timer.at, at); |
| 70 | timer.finish(); | ||
| 71 | }) | 70 | }) |
| 72 | .unwrap_or_else(|| { | 71 | .unwrap_or_else(|| { |
| 73 | let mut timer = Timer { | 72 | let mut timer = Timer { |
| @@ -96,35 +95,35 @@ impl InnerQueue { | |||
| 96 | loop { | 95 | loop { |
| 97 | let now = Instant::now(); | 96 | let now = Instant::now(); |
| 98 | 97 | ||
| 99 | while self.queue.peek().filter(|timer| timer.at <= now).is_some() { | 98 | let mut next_alarm = Instant::MAX; |
| 100 | self.queue.pop().unwrap().waker.wake(); | 99 | |
| 100 | let mut i = 0; | ||
| 101 | while i < self.queue.len() { | ||
| 102 | let timer = &self.queue[i]; | ||
| 103 | if timer.at <= now { | ||
| 104 | let timer = self.queue.swap_remove(i); | ||
| 105 | timer.waker.wake(); | ||
| 106 | } else { | ||
| 107 | next_alarm = min(next_alarm, timer.at); | ||
| 108 | i += 1; | ||
| 109 | } | ||
| 101 | } | 110 | } |
| 102 | 111 | ||
| 103 | if self.update_alarm() { | 112 | if self.update_alarm(next_alarm) { |
| 104 | break; | 113 | break; |
| 105 | } | 114 | } |
| 106 | } | 115 | } |
| 107 | } | 116 | } |
| 108 | 117 | ||
| 109 | fn update_alarm(&mut self) -> bool { | 118 | fn update_alarm(&mut self, next_alarm: Instant) -> bool { |
| 110 | if let Some(timer) = self.queue.peek() { | 119 | if next_alarm == Instant::MAX { |
| 111 | let new_at = timer.at; | 120 | true |
| 112 | |||
| 113 | if self.alarm_at != new_at { | ||
| 114 | self.alarm_at = new_at; | ||
| 115 | |||
| 116 | return set_alarm(self.alarm, self.alarm_at.as_ticks()); | ||
| 117 | } | ||
| 118 | } else { | 121 | } else { |
| 119 | self.alarm_at = Instant::MAX; | 122 | set_alarm(self.alarm, next_alarm.as_ticks()) |
| 120 | } | 123 | } |
| 121 | |||
| 122 | true | ||
| 123 | } | 124 | } |
| 124 | 125 | ||
| 125 | fn handle_alarm(&mut self) { | 126 | fn handle_alarm(&mut self) { |
| 126 | self.alarm_at = Instant::MAX; | ||
| 127 | |||
| 128 | self.dispatch(); | 127 | self.dispatch(); |
| 129 | } | 128 | } |
| 130 | } | 129 | } |
| @@ -151,9 +150,8 @@ impl Queue { | |||
| 151 | let handle = unsafe { allocate_alarm() }.unwrap(); | 150 | let handle = unsafe { allocate_alarm() }.unwrap(); |
| 152 | set_alarm_callback(handle, Self::handle_alarm_callback, self as *const _ as _); | 151 | set_alarm_callback(handle, Self::handle_alarm_callback, self as *const _ as _); |
| 153 | InnerQueue { | 152 | InnerQueue { |
| 154 | queue: SortedLinkedList::new_u8(), | 153 | queue: Vec::new(), |
| 155 | alarm: handle, | 154 | alarm: handle, |
| 156 | alarm_at: Instant::MAX, | ||
| 157 | } | 155 | } |
| 158 | }) | 156 | }) |
| 159 | .schedule_wake(at, waker) | 157 | .schedule_wake(at, waker) |
