Update embassy-std to new executor api

This commit is contained in:
Dario Nieuwenhuis
2021-02-03 04:30:11 +01:00
parent edca627286
commit 4192e52629
4 changed files with 103 additions and 69 deletions

View File

@ -113,13 +113,6 @@ pub struct Spawner {
}
impl Spawner {
fn new(executor: &'static raw::Executor) -> Self {
Self {
executor,
not_send: PhantomData,
}
}
pub fn spawn<F>(&self, token: SpawnToken<F>) -> Result<(), SpawnError> {
let task = token.raw_task;
mem::forget(token);
@ -165,13 +158,6 @@ unsafe impl Sync for SendSpawner {}
///
/// If you want to spawn tasks from another thread, use [SendSpawner].
impl SendSpawner {
fn new(executor: &'static raw::Executor) -> Self {
Self {
executor,
not_send: PhantomData,
}
}
pub fn spawn<F: Send>(&self, token: SpawnToken<F>) -> Result<(), SpawnError> {
let header = token.raw_task;
mem::forget(token);
@ -207,7 +193,7 @@ impl Executor {
///
/// This function never returns.
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
init(Spawner::new(&self.inner));
init(unsafe { self.inner.spawner() });
loop {
unsafe { self.inner.run_queued() };
@ -253,7 +239,7 @@ impl<I: OwnedInterrupt> IrqExecutor<I> {
pub fn start(&'static mut self, init: impl FnOnce(Spawner) + Send) {
self.irq.disable();
init(Spawner::new(&self.inner));
init(unsafe { self.inner.spawner() });
self.irq.set_handler(
|ctx| unsafe {

View File

@ -1,5 +1,6 @@
use core::cell::Cell;
use core::cmp::min;
use core::marker::PhantomData;
use core::ptr;
use core::ptr::NonNull;
use core::sync::atomic::{AtomicU32, Ordering};
@ -67,7 +68,7 @@ impl Task {
}
}
pub(crate) struct Executor {
pub struct Executor {
run_queue: RunQueue,
timer_queue: TimerQueue,
signal_fn: fn(*mut ()),
@ -76,7 +77,7 @@ pub(crate) struct Executor {
}
impl Executor {
pub(crate) const fn new(signal_fn: fn(*mut ()), signal_ctx: *mut ()) -> Self {
pub const fn new(signal_fn: fn(*mut ()), signal_ctx: *mut ()) -> Self {
Self {
run_queue: RunQueue::new(),
timer_queue: TimerQueue::new(),
@ -86,23 +87,27 @@ impl Executor {
}
}
pub(crate) fn set_alarm(&mut self, alarm: &'static dyn Alarm) {
pub fn set_alarm(&mut self, alarm: &'static dyn Alarm) {
self.alarm = Some(alarm);
}
pub fn set_signal_ctx(&mut self, signal_ctx: *mut ()) {
self.signal_ctx = signal_ctx;
}
unsafe fn enqueue(&self, item: *mut Task) {
if self.run_queue.enqueue(item) {
(self.signal_fn)(self.signal_ctx)
}
}
pub(crate) unsafe fn spawn(&'static self, task: NonNull<Task>) {
pub unsafe fn spawn(&'static self, task: NonNull<Task>) {
let task = task.as_ref();
task.executor.set(self);
self.enqueue(task as *const _ as _);
}
pub(crate) unsafe fn run_queued(&self) {
pub unsafe fn run_queued(&'static self) {
if self.alarm.is_some() {
self.timer_queue.dequeue_expired(Instant::now(), |p| {
p.as_ref().enqueue();
@ -138,6 +143,13 @@ impl Executor {
alarm.set(next_expiration.as_ticks());
}
}
pub unsafe fn spawner(&'static self) -> super::Spawner {
super::Spawner {
executor: self,
not_send: PhantomData,
}
}
}
pub use super::waker::task_from_waker;