diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-07-29 21:58:35 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-07-29 23:40:36 +0200 |
| commit | a0f1b0ee01d461607660d2d56b5b1bdc57e0d3fb (patch) | |
| tree | e60fc8f8db8ec07e55d655c1a830b07f4db0b7d2 /embassy-executor/src/executor/raw/timer_queue.rs | |
| parent | 8745d646f0976791b7098456aa61adb983fb1c18 (diff) | |
Split embassy crate into embassy-executor, embassy-util.
Diffstat (limited to 'embassy-executor/src/executor/raw/timer_queue.rs')
| -rw-r--r-- | embassy-executor/src/executor/raw/timer_queue.rs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/embassy-executor/src/executor/raw/timer_queue.rs b/embassy-executor/src/executor/raw/timer_queue.rs new file mode 100644 index 000000000..62fcfc531 --- /dev/null +++ b/embassy-executor/src/executor/raw/timer_queue.rs | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | use core::cell::Cell; | ||
| 2 | use core::cmp::min; | ||
| 3 | use core::ptr; | ||
| 4 | use core::ptr::NonNull; | ||
| 5 | |||
| 6 | use atomic_polyfill::Ordering; | ||
| 7 | |||
| 8 | use super::{TaskHeader, STATE_TIMER_QUEUED}; | ||
| 9 | use crate::time::Instant; | ||
| 10 | |||
| 11 | pub(crate) struct TimerQueueItem { | ||
| 12 | next: Cell<*mut TaskHeader>, | ||
| 13 | } | ||
| 14 | |||
| 15 | impl TimerQueueItem { | ||
| 16 | pub const fn new() -> Self { | ||
| 17 | Self { | ||
| 18 | next: Cell::new(ptr::null_mut()), | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | pub(crate) struct TimerQueue { | ||
| 24 | head: Cell<*mut TaskHeader>, | ||
| 25 | } | ||
| 26 | |||
| 27 | impl TimerQueue { | ||
| 28 | pub const fn new() -> Self { | ||
| 29 | Self { | ||
| 30 | head: Cell::new(ptr::null_mut()), | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | pub(crate) unsafe fn update(&self, p: NonNull<TaskHeader>) { | ||
| 35 | let task = p.as_ref(); | ||
| 36 | if task.expires_at.get() != Instant::MAX { | ||
| 37 | let old_state = task.state.fetch_or(STATE_TIMER_QUEUED, Ordering::AcqRel); | ||
| 38 | let is_new = old_state & STATE_TIMER_QUEUED == 0; | ||
| 39 | |||
| 40 | if is_new { | ||
| 41 | task.timer_queue_item.next.set(self.head.get()); | ||
| 42 | self.head.set(p.as_ptr()); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | pub(crate) unsafe fn next_expiration(&self) -> Instant { | ||
| 48 | let mut res = Instant::MAX; | ||
| 49 | self.retain(|p| { | ||
| 50 | let task = p.as_ref(); | ||
| 51 | let expires = task.expires_at.get(); | ||
| 52 | res = min(res, expires); | ||
| 53 | expires != Instant::MAX | ||
| 54 | }); | ||
| 55 | res | ||
| 56 | } | ||
| 57 | |||
| 58 | pub(crate) unsafe fn dequeue_expired(&self, now: Instant, on_task: impl Fn(NonNull<TaskHeader>)) { | ||
| 59 | self.retain(|p| { | ||
| 60 | let task = p.as_ref(); | ||
| 61 | if task.expires_at.get() <= now { | ||
| 62 | on_task(p); | ||
| 63 | false | ||
| 64 | } else { | ||
| 65 | true | ||
| 66 | } | ||
| 67 | }); | ||
| 68 | } | ||
| 69 | |||
| 70 | pub(crate) unsafe fn retain(&self, mut f: impl FnMut(NonNull<TaskHeader>) -> bool) { | ||
| 71 | let mut prev = &self.head; | ||
| 72 | while !prev.get().is_null() { | ||
| 73 | let p = NonNull::new_unchecked(prev.get()); | ||
| 74 | let task = &*p.as_ptr(); | ||
| 75 | if f(p) { | ||
| 76 | // Skip to next | ||
| 77 | prev = &task.timer_queue_item.next; | ||
| 78 | } else { | ||
| 79 | // Remove it | ||
| 80 | prev.set(task.timer_queue_item.next.get()); | ||
| 81 | task.state.fetch_and(!STATE_TIMER_QUEUED, Ordering::AcqRel); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
