aboutsummaryrefslogtreecommitdiff
path: root/embassy-executor/src/executor/raw/run_queue.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-executor/src/executor/raw/run_queue.rs')
-rw-r--r--embassy-executor/src/executor/raw/run_queue.rs74
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 @@
1use core::ptr;
2use core::ptr::NonNull;
3
4use atomic_polyfill::{AtomicPtr, Ordering};
5use critical_section::CriticalSection;
6
7use super::TaskHeader;
8
9pub(crate) struct RunQueueItem {
10 next: AtomicPtr<TaskHeader>,
11}
12
13impl 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.
32pub(crate) struct RunQueue {
33 head: AtomicPtr<TaskHeader>,
34}
35
36impl 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}