embassy-rs / embassy

Modern embedded framework, using Rust and async.
https://embassy.dev
Apache License 2.0
4.87k stars 661 forks source link

Get the current raw executor from sync code #2694

Open InBetweenNames opened 4 months ago

InBetweenNames commented 4 months ago

As of now it doesn't seem possible to get a pointer to the current executor from a sync context in Embassy. For example:

async fn foo(){

    bar();

}

fn bar()
{
  let p: *const raw::Executor = embassy_executor::get_current_executor_raw_ptr();

  // do stuff with the executor

  let ringbuf = RINGBUFS.get(executor); // from a hashmap for example

}

Is it possible to add this kind of functionality to Embassy? I have a bit of a proof of concept here, specific to the rp2040, that seems to work but is highly tailored to my use case (lock-free logging using defmt with only very brief disabling of interrupts). What I've done is basically store the current executor in a static, one for each core, and I have macros to ensure that this always gets saved and restored correctly (I think) based on which interrupt is currently executing. It seems to work, but is very kludgy. This relies on interrupts of the same priority not interrupting other interrupts of the same priority, which (correct me if I'm wrong) seems to be the case on this chip.

Thanks!

lulf commented 3 months ago

I think we'd want to avoid raw::Executor to become a public API. Is there a way you could implement this using a feature toggle in embassy-executor?