POC: allow custom executors

This commit is contained in:
Dániel Buga
2023-08-12 16:00:18 +02:00
parent 0727f8690c
commit 675b7fb605
10 changed files with 448 additions and 388 deletions

View File

@ -1,6 +1,9 @@
#[cfg(feature = "executor-interrupt")]
compile_error!("`executor-interrupt` is not supported with `arch-std`.");
#[cfg(not(feature = "thread-context"))]
compile_error!("`arch-std` requires `thread-context`.");
#[cfg(feature = "executor-thread")]
pub use thread::*;
#[cfg(feature = "executor-thread")]
@ -11,63 +14,40 @@ mod thread {
#[cfg(feature = "nightly")]
pub use embassy_macros::main_std as main;
use crate::raw::{Pender, PenderInner};
use crate::{raw, Spawner};
use crate::raw::OpaqueThreadContext;
use crate::thread::ThreadContext;
#[derive(Copy, Clone)]
pub(crate) struct ThreadPender(&'static Signaler);
impl ThreadPender {
#[allow(unused)]
pub(crate) fn pend(self) {
self.0.signal()
}
}
/// Single-threaded std-based executor.
pub struct Executor {
inner: raw::Executor,
not_send: PhantomData<*mut ()>,
/// TODO
// Name pending
pub struct StdThreadCtx {
_not_send: PhantomData<*mut ()>,
signaler: &'static Signaler,
}
impl Executor {
/// Create a new Executor.
pub fn new() -> Self {
impl Default for StdThreadCtx {
fn default() -> Self {
let signaler = &*Box::leak(Box::new(Signaler::new()));
Self {
inner: raw::Executor::new(Pender(PenderInner::Thread(ThreadPender(signaler)))),
not_send: PhantomData,
_not_send: PhantomData,
signaler,
}
}
}
/// Run the executor.
///
/// The `init` closure is called with a [`Spawner`] that spawns tasks on
/// this executor. Use it to spawn the initial task(s). After `init` returns,
/// the executor starts running the tasks.
///
/// To spawn more tasks later, you may keep copies of the [`Spawner`] (it is `Copy`),
/// for example by passing it as an argument to the initial tasks.
///
/// This function requires `&'static mut self`. This means you have to store the
/// Executor instance in a place where it'll live forever and grants you mutable
/// access. There's a few ways to do this:
///
/// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe)
/// - a `static mut` (unsafe)
/// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe)
///
/// This function never returns.
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
init(self.inner.spawner());
loop {
unsafe { self.inner.poll() };
self.signaler.wait()
}
impl ThreadContext for StdThreadCtx {
fn context(&self) -> OpaqueThreadContext {
OpaqueThreadContext(self.signaler as *const _ as usize)
}
fn wait(&mut self) {
self.signaler.wait()
}
}
#[export_name = "__thread_mode_pender"]
fn __thread_mode_pender(core_id: OpaqueThreadContext) {
let signaler: &'static Signaler = unsafe { std::mem::transmute(core_id) };
signaler.signal()
}
struct Signaler {
@ -97,4 +77,8 @@ mod thread {
self.condvar.notify_one();
}
}
/// TODO
// Type alias for backwards compatibility
pub type Executor = crate::thread::ThreadModeExecutor<StdThreadCtx>;
}