aboutsummaryrefslogtreecommitdiff
path: root/embassy-executor/src
diff options
context:
space:
mode:
authorGrant Miller <[email protected]>2023-01-31 17:49:18 -0600
committerGrant Miller <[email protected]>2023-01-31 18:59:03 -0600
commitfb1946be7fa38eecb36711a1257f89dae3714b61 (patch)
tree4ccfd365854f55ad638762fb4f0c23ee2e3576b5 /embassy-executor/src
parenta697f1517a9c54ba042bbf70e0b2ed762d300471 (diff)
Replace the pointer in `TaskHeader` with an `Option<&Executor>`
Diffstat (limited to 'embassy-executor/src')
-rw-r--r--embassy-executor/src/raw/mod.rs10
-rw-r--r--embassy-executor/src/spawner.rs12
2 files changed, 11 insertions, 11 deletions
diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs
index 8cdce92ec..6783c4853 100644
--- a/embassy-executor/src/raw/mod.rs
+++ b/embassy-executor/src/raw/mod.rs
@@ -15,10 +15,10 @@ mod waker;
15 15
16use core::cell::Cell; 16use core::cell::Cell;
17use core::future::Future; 17use core::future::Future;
18use core::mem;
18use core::pin::Pin; 19use core::pin::Pin;
19use core::ptr::NonNull; 20use core::ptr::NonNull;
20use core::task::{Context, Poll}; 21use core::task::{Context, Poll};
21use core::{mem, ptr};
22 22
23use atomic_polyfill::{AtomicU32, Ordering}; 23use atomic_polyfill::{AtomicU32, Ordering};
24use critical_section::CriticalSection; 24use critical_section::CriticalSection;
@@ -46,7 +46,7 @@ pub(crate) const STATE_TIMER_QUEUED: u32 = 1 << 2;
46pub(crate) struct TaskHeader { 46pub(crate) struct TaskHeader {
47 pub(crate) state: AtomicU32, 47 pub(crate) state: AtomicU32,
48 pub(crate) run_queue_item: RunQueueItem, 48 pub(crate) run_queue_item: RunQueueItem,
49 pub(crate) executor: Cell<*const Executor>, // Valid if state != 0 49 pub(crate) executor: Cell<Option<&'static Executor>>,
50 poll_fn: unsafe fn(TaskRef), 50 poll_fn: unsafe fn(TaskRef),
51 51
52 #[cfg(feature = "integrated-timers")] 52 #[cfg(feature = "integrated-timers")]
@@ -115,7 +115,7 @@ impl<F: Future + 'static> TaskStorage<F> {
115 raw: TaskHeader { 115 raw: TaskHeader {
116 state: AtomicU32::new(0), 116 state: AtomicU32::new(0),
117 run_queue_item: RunQueueItem::new(), 117 run_queue_item: RunQueueItem::new(),
118 executor: Cell::new(ptr::null()), 118 executor: Cell::new(None),
119 poll_fn: Self::poll, 119 poll_fn: Self::poll,
120 120
121 #[cfg(feature = "integrated-timers")] 121 #[cfg(feature = "integrated-timers")]
@@ -346,7 +346,7 @@ impl Executor {
346 /// In this case, the task's Future must be Send. This is because this is effectively 346 /// In this case, the task's Future must be Send. This is because this is effectively
347 /// sending the task to the executor thread. 347 /// sending the task to the executor thread.
348 pub(super) unsafe fn spawn(&'static self, task: TaskRef) { 348 pub(super) unsafe fn spawn(&'static self, task: TaskRef) {
349 task.header().executor.set(self); 349 task.header().executor.set(Some(self));
350 350
351 #[cfg(feature = "rtos-trace")] 351 #[cfg(feature = "rtos-trace")]
352 trace::task_new(task.as_ptr() as u32); 352 trace::task_new(task.as_ptr() as u32);
@@ -455,7 +455,7 @@ pub fn wake_task(task: TaskRef) {
455 455
456 // We have just marked the task as scheduled, so enqueue it. 456 // We have just marked the task as scheduled, so enqueue it.
457 unsafe { 457 unsafe {
458 let executor = &*header.executor.get(); 458 let executor = header.executor.get().unwrap_unchecked();
459 executor.enqueue(cs, task); 459 executor.enqueue(cs, task);
460 } 460 }
461 }) 461 })
diff --git a/embassy-executor/src/spawner.rs b/embassy-executor/src/spawner.rs
index 650ea06cb..7c0a0183c 100644
--- a/embassy-executor/src/spawner.rs
+++ b/embassy-executor/src/spawner.rs
@@ -89,10 +89,10 @@ impl Spawner {
89 /// 89 ///
90 /// Panics if the current executor is not an Embassy executor. 90 /// Panics if the current executor is not an Embassy executor.
91 pub async fn for_current_executor() -> Self { 91 pub async fn for_current_executor() -> Self {
92 poll_fn(|cx| unsafe { 92 poll_fn(|cx| {
93 let task = raw::task_from_waker(cx.waker()); 93 let task = raw::task_from_waker(cx.waker());
94 let executor = task.header().executor.get(); 94 let executor = unsafe { task.header().executor.get().unwrap_unchecked() };
95 Poll::Ready(Self::new(&*executor)) 95 Poll::Ready(Self::new(executor))
96 }) 96 })
97 .await 97 .await
98 } 98 }
@@ -165,10 +165,10 @@ impl SendSpawner {
165 /// 165 ///
166 /// Panics if the current executor is not an Embassy executor. 166 /// Panics if the current executor is not an Embassy executor.
167 pub async fn for_current_executor() -> Self { 167 pub async fn for_current_executor() -> Self {
168 poll_fn(|cx| unsafe { 168 poll_fn(|cx| {
169 let task = raw::task_from_waker(cx.waker()); 169 let task = raw::task_from_waker(cx.waker());
170 let executor = task.header().executor.get(); 170 let executor = unsafe { task.header().executor.get().unwrap_unchecked() };
171 Poll::Ready(Self::new(&*executor)) 171 Poll::Ready(Self::new(executor))
172 }) 172 })
173 .await 173 .await
174 } 174 }