diff options
author | 2020-10-15 16:42:30 +0000 | |
---|---|---|
committer | 2020-10-15 16:42:30 +0000 | |
commit | 355cb82d0693fe108ac28ec8a0d77e8aab4e6e06 (patch) | |
tree | 67c0eadf6d8a98a2691b816d46a5103d159d6bbc /macros/src/codegen/spawn.rs | |
parent | f9303cef1cf5b2d57d26e7667289fbdccf959ea8 (diff) | |
parent | 6808cc7cdf1512a7b10dd43f268f430a676c606e (diff) | |
download | rtic-355cb82d0693fe108ac28ec8a0d77e8aab4e6e06.tar.gz rtic-355cb82d0693fe108ac28ec8a0d77e8aab4e6e06.tar.zst rtic-355cb82d0693fe108ac28ec8a0d77e8aab4e6e06.zip |
Merge #390
390: Spawn and schedule from anywhere r=AfoHT a=korken89
This PR moves RTIC to the spawn and schedule from anywhere syntax.
Notable changes:
* We do no longer support non-`Send` types.
* Some extra code is generated as any task may spawn/schedule any task. However Rust/LLVM does a great job optimizing away non used instantiations (no real code-size difference observed).
* Worst case priority inversion has increased, but it is now predictable.
Upsides:
* With this we should be able to support async/await.
* RTIC tasks can now be callbacks (spawned and scheduled).
* RTIC tasks can be stored.
Needs the following PR to land first: https://github.com/rtic-rs/rtic-syntax/pull/34
The following now works:
```rust
#[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)]
mod app {
#[init]
fn init(mut cx: init::Context) -> init::LateResources {
// Init stuff...
// New spawn syntax
foo::spawn().unwrap();
// New schedule syntax
bar::schedule(now + 4_000_000.cycles()).unwrap();
init::LateResources {}
}
#[task]
fn foo(_: foo::Context) {}
#[task]
fn bar(_: bar::Context) {}
extern "C" {
fn SSI0();
}
}
```
Co-authored-by: Per Lindgren <per.lindgren@ltu.se>
Co-authored-by: Emil Fresk <emil.fresk@gmail.com>
Diffstat (limited to 'macros/src/codegen/spawn.rs')
-rw-r--r-- | macros/src/codegen/spawn.rs | 121 |
1 files changed, 0 insertions, 121 deletions
diff --git a/macros/src/codegen/spawn.rs b/macros/src/codegen/spawn.rs deleted file mode 100644 index da281516..00000000 --- a/macros/src/codegen/spawn.rs +++ /dev/null @@ -1,121 +0,0 @@ -use std::collections::HashSet; - -use proc_macro2::TokenStream as TokenStream2; -use quote::quote; -use rtic_syntax::ast::App; - -use crate::{ - analyze::Analysis, - check::Extra, - codegen::{spawn_body, util}, -}; - -/// Generates all `${ctxt}::Spawn` methods -pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> { - let mut items = vec![]; - - let mut seen = HashSet::<_>::new(); - for (spawner, spawnees) in app.spawn_callers() { - let mut methods = vec![]; - - for name in spawnees { - let spawnee = &app.software_tasks[name]; - let cfgs = &spawnee.cfgs; - let (args, _, untupled, ty) = util::regroup_inputs(&spawnee.inputs); - let args = &args; - - if spawner.is_init() { - // `init` uses a special spawn implementation; it doesn't use the `spawn_${name}` - // functions which are shared by other contexts - - let body = spawn_body::codegen(spawner, &name, app, analysis, extra); - - let let_instant = if app.uses_schedule() { - let m = extra.monotonic(); - - Some(quote!(let instant = unsafe { <#m as rtic::Monotonic>::zero() };)) - } else { - None - }; - - methods.push(quote!( - #(#cfgs)* - pub fn #name(&self #(,#args)*) -> Result<(), #ty> { - #let_instant - #body - } - )); - } else { - let spawn = util::spawn_ident(name); - - if !seen.contains(name) { - // Generate a `spawn_${name}_S${sender}` function - seen.insert(name); - - let instant = if app.uses_schedule() { - let m = extra.monotonic(); - - Some(quote!(, instant: <#m as rtic::Monotonic>::Instant)) - } else { - None - }; - - let body = spawn_body::codegen(spawner, &name, app, analysis, extra); - - items.push(quote!( - #(#cfgs)* - unsafe fn #spawn( - priority: &rtic::export::Priority - #instant - #(,#args)* - ) -> Result<(), #ty> { - #body - } - )); - } - - let (let_instant, instant) = if app.uses_schedule() { - let m = extra.monotonic(); - - ( - Some(if spawner.is_idle() { - quote!(let instant = <#m as rtic::Monotonic>::now();) - } else { - quote!(let instant = self.instant();) - }), - Some(quote!(, instant)), - ) - } else { - (None, None) - }; - - methods.push(quote!( - #(#cfgs)* - #[inline(always)] - pub fn #name(&self #(,#args)*) -> Result<(), #ty> { - unsafe { - #let_instant - #spawn(self.priority() #instant #(,#untupled)*) - } - } - )); - } - } - - let lt = if spawner.is_init() { - None - } else { - Some(quote!('a)) - }; - - let spawner = spawner.ident(app); - debug_assert!(!methods.is_empty()); - items.push(quote!( - impl<#lt> #spawner::Spawn<#lt> { - #(#methods)* - } - )); - } - - items -} |