aboutsummaryrefslogtreecommitdiff
path: root/embassy-executor/src/spawner.rs
diff options
context:
space:
mode:
authorQuentin Smith <[email protected]>2023-07-17 21:31:43 -0400
committerQuentin Smith <[email protected]>2023-07-17 21:31:43 -0400
commit6f02403184eb7fb7990fb88fc9df9c4328a690a3 (patch)
tree748f510e190bb2724750507a6e69ed1a8e08cb20 /embassy-executor/src/spawner.rs
parentd896f80405aa8963877049ed999e4aba25d6e2bb (diff)
parent6b5df4523aa1c4902f02e803450ae4b418e0e3ca (diff)
Merge remote-tracking branch 'origin/main' into nrf-pdm
Diffstat (limited to 'embassy-executor/src/spawner.rs')
-rw-r--r--embassy-executor/src/spawner.rs32
1 files changed, 13 insertions, 19 deletions
diff --git a/embassy-executor/src/spawner.rs b/embassy-executor/src/spawner.rs
index 25a0d7dbb..2b6224045 100644
--- a/embassy-executor/src/spawner.rs
+++ b/embassy-executor/src/spawner.rs
@@ -1,10 +1,8 @@
1use core::future::poll_fn;
1use core::marker::PhantomData; 2use core::marker::PhantomData;
2use core::mem; 3use core::mem;
3use core::ptr::NonNull;
4use core::task::Poll; 4use core::task::Poll;
5 5
6use futures_util::future::poll_fn;
7
8use super::raw; 6use super::raw;
9 7
10/// Token to spawn a newly-created task in an executor. 8/// Token to spawn a newly-created task in an executor.
@@ -23,12 +21,12 @@ use super::raw;
23/// Once you've invoked a task function and obtained a SpawnToken, you *must* spawn it. 21/// Once you've invoked a task function and obtained a SpawnToken, you *must* spawn it.
24#[must_use = "Calling a task function does nothing on its own. You must spawn the returned SpawnToken, typically with Spawner::spawn()"] 22#[must_use = "Calling a task function does nothing on its own. You must spawn the returned SpawnToken, typically with Spawner::spawn()"]
25pub struct SpawnToken<S> { 23pub struct SpawnToken<S> {
26 raw_task: Option<NonNull<raw::TaskHeader>>, 24 raw_task: Option<raw::TaskRef>,
27 phantom: PhantomData<*mut S>, 25 phantom: PhantomData<*mut S>,
28} 26}
29 27
30impl<S> SpawnToken<S> { 28impl<S> SpawnToken<S> {
31 pub(crate) unsafe fn new(raw_task: NonNull<raw::TaskHeader>) -> Self { 29 pub(crate) unsafe fn new(raw_task: raw::TaskRef) -> Self {
32 Self { 30 Self {
33 raw_task: Some(raw_task), 31 raw_task: Some(raw_task),
34 phantom: PhantomData, 32 phantom: PhantomData,
@@ -91,10 +89,11 @@ impl Spawner {
91 /// 89 ///
92 /// Panics if the current executor is not an Embassy executor. 90 /// Panics if the current executor is not an Embassy executor.
93 pub async fn for_current_executor() -> Self { 91 pub async fn for_current_executor() -> Self {
94 poll_fn(|cx| unsafe { 92 poll_fn(|cx| {
95 let task = raw::task_from_waker(cx.waker()); 93 let task = raw::task_from_waker(cx.waker());
96 let executor = (*task.as_ptr()).executor.get(); 94 let executor = unsafe { task.header().executor.get().unwrap_unchecked() };
97 Poll::Ready(Self::new(&*executor)) 95 let executor = unsafe { raw::Executor::wrap(executor) };
96 Poll::Ready(Self::new(executor))
98 }) 97 })
99 .await 98 .await
100 } 99 }
@@ -132,9 +131,7 @@ impl Spawner {
132 /// spawner to other threads, but the spawner loses the ability to spawn 131 /// spawner to other threads, but the spawner loses the ability to spawn
133 /// non-Send tasks. 132 /// non-Send tasks.
134 pub fn make_send(&self) -> SendSpawner { 133 pub fn make_send(&self) -> SendSpawner {
135 SendSpawner { 134 SendSpawner::new(&self.executor.inner)
136 executor: self.executor,
137 }
138 } 135 }
139} 136}
140 137
@@ -147,14 +144,11 @@ impl Spawner {
147/// If you want to spawn non-Send tasks, use [Spawner]. 144/// If you want to spawn non-Send tasks, use [Spawner].
148#[derive(Copy, Clone)] 145#[derive(Copy, Clone)]
149pub struct SendSpawner { 146pub struct SendSpawner {
150 executor: &'static raw::Executor, 147 executor: &'static raw::SyncExecutor,
151} 148}
152 149
153unsafe impl Send for SendSpawner {}
154unsafe impl Sync for SendSpawner {}
155
156impl SendSpawner { 150impl SendSpawner {
157 pub(crate) fn new(executor: &'static raw::Executor) -> Self { 151 pub(crate) fn new(executor: &'static raw::SyncExecutor) -> Self {
158 Self { executor } 152 Self { executor }
159 } 153 }
160 154
@@ -167,10 +161,10 @@ impl SendSpawner {
167 /// 161 ///
168 /// Panics if the current executor is not an Embassy executor. 162 /// Panics if the current executor is not an Embassy executor.
169 pub async fn for_current_executor() -> Self { 163 pub async fn for_current_executor() -> Self {
170 poll_fn(|cx| unsafe { 164 poll_fn(|cx| {
171 let task = raw::task_from_waker(cx.waker()); 165 let task = raw::task_from_waker(cx.waker());
172 let executor = (*task.as_ptr()).executor.get(); 166 let executor = unsafe { task.header().executor.get().unwrap_unchecked() };
173 Poll::Ready(Self::new(&*executor)) 167 Poll::Ready(Self::new(executor))
174 }) 168 })
175 .await 169 .await
176 } 170 }