Merge pull request #3547 from bugadani/callback

Executor: Only set callback once
This commit is contained in:
Dario Nieuwenhuis 2024-11-20 23:14:02 +00:00 committed by GitHub
commit 66756af2f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 57 additions and 6 deletions

View File

@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
- `raw::Executor` now has an `fn initialize` that must be called once before starting to poll it.
## 0.6.3 - 2024-11-12
- Building with the `nightly` feature now works with the Xtensa Rust compiler 1.82.

View File

@ -53,6 +53,10 @@ mod thread {
///
/// This function never returns.
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
unsafe {
self.inner.initialize();
}
init(self.inner.spawner());
loop {

View File

@ -98,6 +98,9 @@ mod thread {
///
/// This function never returns.
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
unsafe {
self.inner.initialize();
}
init(self.inner.spawner());
loop {
@ -207,6 +210,9 @@ mod interrupt {
}
let executor = unsafe { (&*self.executor.get()).assume_init_ref() };
unsafe {
executor.initialize();
}
unsafe { NVIC::unmask(irq) }

View File

@ -54,6 +54,10 @@ mod thread {
///
/// This function never returns.
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
unsafe {
self.inner.initialize();
}
init(self.inner.spawner());
loop {

View File

@ -48,6 +48,10 @@ mod thread {
///
/// This function never returns.
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
unsafe {
self.inner.initialize();
}
init(self.inner.spawner());
loop {

View File

@ -55,6 +55,10 @@ mod thread {
///
/// This function never returns.
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
unsafe {
self.inner.initialize();
}
init(self.inner.spawner());
loop {

View File

@ -70,6 +70,10 @@ mod thread {
/// - a `static mut` (unsafe)
/// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe)
pub fn start(&'static mut self, init: impl FnOnce(Spawner)) {
unsafe {
self.inner.initialize();
}
unsafe {
let executor = &self.inner;
let future = Closure::new(move |_| {

View File

@ -339,6 +339,11 @@ impl SyncExecutor {
}
}
pub(crate) unsafe fn initialize(&'static self) {
#[cfg(feature = "integrated-timers")]
embassy_time_driver::set_alarm_callback(self.alarm, Self::alarm_callback, self as *const _ as *mut ());
}
/// Enqueue a task in the task queue
///
/// # Safety
@ -374,9 +379,6 @@ impl SyncExecutor {
///
/// Same as [`Executor::poll`], plus you must only call this on the thread this executor was created.
pub(crate) unsafe fn poll(&'static self) {
#[cfg(feature = "integrated-timers")]
embassy_time_driver::set_alarm_callback(self.alarm, Self::alarm_callback, self as *const _ as *mut ());
#[allow(clippy::never_loop)]
loop {
#[cfg(feature = "integrated-timers")]
@ -492,6 +494,15 @@ impl Executor {
}
}
/// Initializes the executor.
///
/// # Safety
///
/// This function must be called once before any other method is called.
pub unsafe fn initialize(&'static self) {
self.inner.initialize();
}
/// Spawn a task in this executor.
///
/// # Safety
@ -516,6 +527,8 @@ impl Executor {
///
/// # Safety
///
/// You must call `initialize` before calling this method.
///
/// You must NOT call `poll` reentrantly on the same executor.
///
/// In particular, note that `poll` may call the pender synchronously. Therefore, you

View File

@ -40,6 +40,10 @@ fn setup() -> (&'static Executor, Trace) {
let trace = Trace::new();
let context = Box::leak(Box::new(trace.clone())) as *mut _ as *mut ();
let executor = &*Box::leak(Box::new(Executor::new(context)));
unsafe {
executor.initialize();
}
(executor, trace)
}

View File

@ -155,7 +155,9 @@ impl Executor {
time_driver: get_driver(),
});
EXECUTOR.as_mut().unwrap()
let executor = EXECUTOR.as_mut().unwrap();
executor
})
}
@ -241,11 +243,15 @@ impl Executor {
///
/// This function never returns.
pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
init(unsafe { EXECUTOR.as_mut().unwrap() }.inner.spawner());
let executor = unsafe { EXECUTOR.as_mut().unwrap() };
unsafe {
executor.inner.initialize();
}
init(executor.inner.spawner());
loop {
unsafe {
EXECUTOR.as_mut().unwrap().inner.poll();
executor.inner.poll();
self.configure_pwr();
asm!("wfe");
};