diff options
Diffstat (limited to 'embassy-executor/src/raw/timer_queue.rs')
| -rw-r--r-- | embassy-executor/src/raw/timer_queue.rs | 76 |
1 files changed, 0 insertions, 76 deletions
diff --git a/embassy-executor/src/raw/timer_queue.rs b/embassy-executor/src/raw/timer_queue.rs deleted file mode 100644 index 94a5f340b..000000000 --- a/embassy-executor/src/raw/timer_queue.rs +++ /dev/null | |||
| @@ -1,76 +0,0 @@ | |||
| 1 | use core::cmp::min; | ||
| 2 | |||
| 3 | use super::TaskRef; | ||
| 4 | use crate::raw::util::SyncUnsafeCell; | ||
| 5 | |||
| 6 | pub(crate) struct TimerQueueItem { | ||
| 7 | next: SyncUnsafeCell<Option<TaskRef>>, | ||
| 8 | } | ||
| 9 | |||
| 10 | impl TimerQueueItem { | ||
| 11 | pub const fn new() -> Self { | ||
| 12 | Self { | ||
| 13 | next: SyncUnsafeCell::new(None), | ||
| 14 | } | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | pub(crate) struct TimerQueue { | ||
| 19 | head: SyncUnsafeCell<Option<TaskRef>>, | ||
| 20 | } | ||
| 21 | |||
| 22 | impl TimerQueue { | ||
| 23 | pub const fn new() -> Self { | ||
| 24 | Self { | ||
| 25 | head: SyncUnsafeCell::new(None), | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | pub(crate) unsafe fn update(&self, p: TaskRef) { | ||
| 30 | let task = p.header(); | ||
| 31 | if task.expires_at.get() != u64::MAX { | ||
| 32 | if task.state.timer_enqueue() { | ||
| 33 | task.timer_queue_item.next.set(self.head.get()); | ||
| 34 | self.head.set(Some(p)); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | pub(crate) unsafe fn next_expiration(&self) -> u64 { | ||
| 40 | let mut res = u64::MAX; | ||
| 41 | self.retain(|p| { | ||
| 42 | let task = p.header(); | ||
| 43 | let expires = task.expires_at.get(); | ||
| 44 | res = min(res, expires); | ||
| 45 | expires != u64::MAX | ||
| 46 | }); | ||
| 47 | res | ||
| 48 | } | ||
| 49 | |||
| 50 | pub(crate) unsafe fn dequeue_expired(&self, now: u64, on_task: impl Fn(TaskRef)) { | ||
| 51 | self.retain(|p| { | ||
| 52 | let task = p.header(); | ||
| 53 | if task.expires_at.get() <= now { | ||
| 54 | on_task(p); | ||
| 55 | false | ||
| 56 | } else { | ||
| 57 | true | ||
| 58 | } | ||
| 59 | }); | ||
| 60 | } | ||
| 61 | |||
| 62 | pub(crate) unsafe fn retain(&self, mut f: impl FnMut(TaskRef) -> bool) { | ||
| 63 | let mut prev = &self.head; | ||
| 64 | while let Some(p) = prev.get() { | ||
| 65 | let task = p.header(); | ||
| 66 | if f(p) { | ||
| 67 | // Skip to next | ||
| 68 | prev = &task.timer_queue_item.next; | ||
| 69 | } else { | ||
| 70 | // Remove it | ||
| 71 | prev.set(task.timer_queue_item.next.get()); | ||
| 72 | task.state.timer_dequeue(); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
