2022-09-22 16:42:49 +02:00
|
|
|
use core::future::poll_fn;
|
2021-08-25 00:20:29 +02:00
|
|
|
use core::marker::PhantomData;
|
|
|
|
use core::mem;
|
2022-04-26 19:08:18 +02:00
|
|
|
use core::task::Poll;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2021-08-25 00:20:29 +02:00
|
|
|
use super::raw;
|
|
|
|
|
2021-08-26 00:20:52 +02:00
|
|
|
/// Token to spawn a newly-created task in an executor.
|
|
|
|
///
|
2022-07-29 21:58:35 +02:00
|
|
|
/// When calling a task function (like `#[embassy_executor::task] async fn my_task() { ... }`), the returned
|
2021-08-26 00:20:52 +02:00
|
|
|
/// value is a `SpawnToken` that represents an instance of the task, ready to spawn. You must
|
|
|
|
/// then spawn it into an executor, typically with [`Spawner::spawn()`].
|
|
|
|
///
|
2022-04-27 04:27:42 +02:00
|
|
|
/// The generic parameter `S` determines whether the task can be spawned in executors
|
|
|
|
/// in other threads or not. If `S: Send`, it can, which allows spawning it into a [`SendSpawner`].
|
|
|
|
/// If not, it can't, so it can only be spawned into the current thread's executor, with [`Spawner`].
|
|
|
|
///
|
2021-08-26 00:20:52 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Dropping a SpawnToken instance panics. You may not "abort" spawning a task in this way.
|
|
|
|
/// Once you've invoked a task function and obtained a SpawnToken, you *must* spawn it.
|
|
|
|
#[must_use = "Calling a task function does nothing on its own. You must spawn the returned SpawnToken, typically with Spawner::spawn()"]
|
2022-04-27 04:27:42 +02:00
|
|
|
pub struct SpawnToken<S> {
|
2023-01-29 19:55:06 +01:00
|
|
|
raw_task: Option<raw::TaskRef>,
|
2022-04-27 04:27:42 +02:00
|
|
|
phantom: PhantomData<*mut S>,
|
2021-08-25 00:20:29 +02:00
|
|
|
}
|
|
|
|
|
2022-04-27 04:27:42 +02:00
|
|
|
impl<S> SpawnToken<S> {
|
2023-01-29 19:55:06 +01:00
|
|
|
pub(crate) unsafe fn new(raw_task: raw::TaskRef) -> Self {
|
2021-08-25 00:20:29 +02:00
|
|
|
Self {
|
|
|
|
raw_task: Some(raw_task),
|
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn new_failed() -> Self {
|
|
|
|
Self {
|
|
|
|
raw_task: None,
|
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 04:27:42 +02:00
|
|
|
impl<S> Drop for SpawnToken<S> {
|
2021-08-25 00:20:29 +02:00
|
|
|
fn drop(&mut self) {
|
|
|
|
// TODO deallocate the task instead.
|
2021-08-26 00:20:52 +02:00
|
|
|
panic!("SpawnToken instances may not be dropped. You must pass them to Spawner::spawn()")
|
2021-08-25 00:20:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:20:52 +02:00
|
|
|
/// Error returned when spawning a task.
|
2021-08-25 00:20:29 +02:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum SpawnError {
|
2021-08-26 00:20:52 +02:00
|
|
|
/// Too many instances of this task are already running.
|
|
|
|
///
|
2022-07-29 21:58:35 +02:00
|
|
|
/// By default, a task marked with `#[embassy_executor::task]` can only have one instance
|
2021-08-26 00:20:52 +02:00
|
|
|
/// running at a time. You may allow multiple instances to run in parallel with
|
2022-07-29 21:58:35 +02:00
|
|
|
/// `#[embassy_executor::task(pool_size = 4)]`, at the cost of higher RAM usage.
|
2021-08-25 00:20:29 +02:00
|
|
|
Busy,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle to spawn tasks into an executor.
|
|
|
|
///
|
|
|
|
/// This Spawner can spawn any task (Send and non-Send ones), but it can
|
|
|
|
/// only be used in the executor thread (it is not Send itself).
|
|
|
|
///
|
|
|
|
/// If you want to spawn tasks from another thread, use [SendSpawner].
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Spawner {
|
|
|
|
executor: &'static raw::Executor,
|
|
|
|
not_send: PhantomData<*mut ()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Spawner {
|
2021-08-26 00:20:52 +02:00
|
|
|
pub(crate) fn new(executor: &'static raw::Executor) -> Self {
|
2021-08-25 00:20:29 +02:00
|
|
|
Self {
|
|
|
|
executor,
|
|
|
|
not_send: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:08:18 +02:00
|
|
|
/// Get a Spawner for the current executor.
|
|
|
|
///
|
|
|
|
/// This function is `async` just to get access to the current async
|
|
|
|
/// context. It returns instantly, it does not block/yield.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the current executor is not an Embassy executor.
|
|
|
|
pub async fn for_current_executor() -> Self {
|
|
|
|
poll_fn(|cx| unsafe {
|
|
|
|
let task = raw::task_from_waker(cx.waker());
|
2023-01-29 19:55:06 +01:00
|
|
|
let executor = task.header().executor.get();
|
2022-04-26 19:08:18 +02:00
|
|
|
Poll::Ready(Self::new(&*executor))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:20:52 +02:00
|
|
|
/// Spawn a task into an executor.
|
|
|
|
///
|
2022-07-29 21:58:35 +02:00
|
|
|
/// You obtain the `token` by calling a task function (i.e. one marked with `#[embassy_executor::task]`).
|
2022-04-27 04:27:42 +02:00
|
|
|
pub fn spawn<S>(&self, token: SpawnToken<S>) -> Result<(), SpawnError> {
|
2021-08-25 00:20:29 +02:00
|
|
|
let task = token.raw_task;
|
|
|
|
mem::forget(token);
|
|
|
|
|
|
|
|
match task {
|
|
|
|
Some(task) => {
|
|
|
|
unsafe { self.executor.spawn(task) };
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
None => Err(SpawnError::Busy),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:08:18 +02:00
|
|
|
// Used by the `embassy_macros::main!` macro to throw an error when spawn
|
|
|
|
// fails. This is here to allow conditional use of `defmt::unwrap!`
|
|
|
|
// without introducing a `defmt` feature in the `embassy_macros` package,
|
|
|
|
// which would require use of `-Z namespaced-features`.
|
|
|
|
/// Spawn a task into an executor, panicking on failure.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the spawning fails.
|
2022-04-27 04:27:42 +02:00
|
|
|
pub fn must_spawn<S>(&self, token: SpawnToken<S>) {
|
2021-08-25 00:20:29 +02:00
|
|
|
unwrap!(self.spawn(token));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert this Spawner to a SendSpawner. This allows you to send the
|
|
|
|
/// spawner to other threads, but the spawner loses the ability to spawn
|
|
|
|
/// non-Send tasks.
|
|
|
|
pub fn make_send(&self) -> SendSpawner {
|
|
|
|
SendSpawner {
|
|
|
|
executor: self.executor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle to spawn tasks into an executor from any thread.
|
|
|
|
///
|
2021-08-26 00:20:52 +02:00
|
|
|
/// This Spawner can be used from any thread (it is Send), but it can
|
|
|
|
/// only spawn Send tasks. The reason for this is spawning is effectively
|
|
|
|
/// "sending" the tasks to the executor thread.
|
2021-08-25 00:20:29 +02:00
|
|
|
///
|
2021-08-26 00:20:52 +02:00
|
|
|
/// If you want to spawn non-Send tasks, use [Spawner].
|
2021-08-25 00:20:29 +02:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct SendSpawner {
|
|
|
|
executor: &'static raw::Executor,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for SendSpawner {}
|
|
|
|
unsafe impl Sync for SendSpawner {}
|
|
|
|
|
|
|
|
impl SendSpawner {
|
2022-04-26 19:08:18 +02:00
|
|
|
pub(crate) fn new(executor: &'static raw::Executor) -> Self {
|
|
|
|
Self { executor }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a Spawner for the current executor.
|
|
|
|
///
|
|
|
|
/// This function is `async` just to get access to the current async
|
|
|
|
/// context. It returns instantly, it does not block/yield.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the current executor is not an Embassy executor.
|
|
|
|
pub async fn for_current_executor() -> Self {
|
|
|
|
poll_fn(|cx| unsafe {
|
|
|
|
let task = raw::task_from_waker(cx.waker());
|
2023-01-29 19:55:06 +01:00
|
|
|
let executor = task.header().executor.get();
|
2022-04-26 19:08:18 +02:00
|
|
|
Poll::Ready(Self::new(&*executor))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:20:52 +02:00
|
|
|
/// Spawn a task into an executor.
|
|
|
|
///
|
2022-07-29 21:58:35 +02:00
|
|
|
/// You obtain the `token` by calling a task function (i.e. one marked with `#[embassy_executor::task]`).
|
2022-04-27 04:27:42 +02:00
|
|
|
pub fn spawn<S: Send>(&self, token: SpawnToken<S>) -> Result<(), SpawnError> {
|
2021-08-25 00:20:29 +02:00
|
|
|
let header = token.raw_task;
|
|
|
|
mem::forget(token);
|
|
|
|
|
|
|
|
match header {
|
|
|
|
Some(header) => {
|
|
|
|
unsafe { self.executor.spawn(header) };
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
None => Err(SpawnError::Busy),
|
|
|
|
}
|
|
|
|
}
|
2022-04-26 19:08:18 +02:00
|
|
|
|
|
|
|
/// Spawn a task into an executor, panicking on failure.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the spawning fails.
|
2022-04-27 04:27:42 +02:00
|
|
|
pub fn must_spawn<S: Send>(&self, token: SpawnToken<S>) {
|
2022-04-26 19:08:18 +02:00
|
|
|
unwrap!(self.spawn(token));
|
|
|
|
}
|
2021-08-25 00:20:29 +02:00
|
|
|
}
|