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/run_queue.rs | |
| parent | 8745d646f0976791b7098456aa61adb983fb1c18 (diff) | |
Split embassy crate into embassy-executor, embassy-util.
Diffstat (limited to 'embassy-executor/src/executor/raw/run_queue.rs')
| -rw-r--r-- | embassy-executor/src/executor/raw/run_queue.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/embassy-executor/src/executor/raw/run_queue.rs b/embassy-executor/src/executor/raw/run_queue.rs new file mode 100644 index 000000000..31615da7e --- /dev/null +++ b/embassy-executor/src/executor/raw/run_queue.rs | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | use core::ptr; | ||
| 2 | use core::ptr::NonNull; | ||
| 3 | |||
| 4 | use atomic_polyfill::{AtomicPtr, Ordering}; | ||
| 5 | use critical_section::CriticalSection; | ||
| 6 | |||
| 7 | use super::TaskHeader; | ||
| 8 | |||
| 9 | pub(crate) struct RunQueueItem { | ||
| 10 | next: AtomicPtr<TaskHeader>, | ||
| 11 | } | ||
| 12 | |||
| 13 | impl RunQueueItem { | ||
| 14 | pub const fn new() -> Self { | ||
| 15 | Self { | ||
| 16 | next: AtomicPtr::new(ptr::null_mut()), | ||
| 17 | } | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | /// Atomic task queue using a very, very simple lock-free linked-list queue: | ||
| 22 | /// | ||
| 23 | /// To enqueue a task, task.next is set to the old head, and head is atomically set to task. | ||
| 24 | /// | ||
| 25 | /// Dequeuing is done in batches: the queue is emptied by atomically replacing head with | ||
| 26 | /// null. Then the batch is iterated following the next pointers until null is reached. | ||
| 27 | /// | ||
| 28 | /// Note that batches will be iterated in the reverse order as they were enqueued. This is OK | ||
| 29 | /// for our purposes: it can't create fairness problems since the next batch won't run until the | ||
| 30 | /// current batch is completely processed, so even if a task enqueues itself instantly (for example | ||
| 31 | /// by waking its own waker) can't prevent other tasks from running. | ||
| 32 | pub(crate) struct RunQueue { | ||
| 33 | head: AtomicPtr<TaskHeader>, | ||
| 34 | } | ||
| 35 | |||
| 36 | impl RunQueue { | ||
| 37 | pub const fn new() -> Self { | ||
| 38 | Self { | ||
| 39 | head: AtomicPtr::new(ptr::null_mut()), | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | /// Enqueues an item. Returns true if the queue was empty. | ||
| 44 | /// | ||
| 45 | /// # Safety | ||
| 46 | /// | ||
| 47 | /// `item` must NOT be already enqueued in any queue. | ||
| 48 | #[inline(always)] | ||
| 49 | pub(crate) unsafe fn enqueue(&self, _cs: CriticalSection, task: *mut TaskHeader) -> bool { | ||
| 50 | let prev = self.head.load(Ordering::Relaxed); | ||
| 51 | (*task).run_queue_item.next.store(prev, Ordering::Relaxed); | ||
| 52 | self.head.store(task, Ordering::Relaxed); | ||
| 53 | prev.is_null() | ||
| 54 | } | ||
| 55 | |||
| 56 | /// Empty the queue, then call `on_task` for each task that was in the queue. | ||
| 57 | /// NOTE: It is OK for `on_task` to enqueue more tasks. In this case they're left in the queue | ||
| 58 | /// and will be processed by the *next* call to `dequeue_all`, *not* the current one. | ||
| 59 | pub(crate) fn dequeue_all(&self, on_task: impl Fn(NonNull<TaskHeader>)) { | ||
| 60 | // Atomically empty the queue. | ||
| 61 | let mut ptr = self.head.swap(ptr::null_mut(), Ordering::AcqRel); | ||
| 62 | |||
| 63 | // Iterate the linked list of tasks that were previously in the queue. | ||
| 64 | while let Some(task) = NonNull::new(ptr) { | ||
| 65 | // If the task re-enqueues itself, the `next` pointer will get overwritten. | ||
| 66 | // Therefore, first read the next pointer, and only then process the task. | ||
| 67 | let next = unsafe { task.as_ref() }.run_queue_item.next.load(Ordering::Relaxed); | ||
| 68 | |||
| 69 | on_task(task); | ||
| 70 | |||
| 71 | ptr = next | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
