mirror of
https://github.com/rtic-rs/rtic.git
synced 2025-12-19 06:15:45 +01:00
Removed Priority, simplified lifetime handling
This commit is contained in:
parent
53f3d397e7
commit
714020a624
15 changed files with 99 additions and 236 deletions
|
|
@ -0,0 +1 @@
|
|||
|
||||
|
|
@ -19,6 +19,10 @@ mod shared_resources_struct;
|
|||
mod software_tasks;
|
||||
mod util;
|
||||
|
||||
// TODO: organize codegen to actual parts of code
|
||||
// so `main::codegen` generates ALL the code for `fn main`,
|
||||
// `software_tasks::codegen` generates ALL the code for software tasks etc...
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
pub fn app(app: &App, analysis: &Analysis) -> TokenStream2 {
|
||||
let mut mod_app = vec![];
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
|
|||
for (name, _) in app.software_tasks.iter() {
|
||||
let type_name = util::internal_task_ident(name, "F");
|
||||
let exec_name = util::internal_task_ident(name, "EXEC");
|
||||
let prio_name = util::internal_task_ident(name, "PRIORITY");
|
||||
|
||||
items.push(quote!(
|
||||
#[allow(non_camel_case_types)]
|
||||
|
|
@ -22,12 +21,6 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
|
|||
static #exec_name:
|
||||
rtic::RacyCell<rtic::export::executor::AsyncTaskExecutor<#type_name>> =
|
||||
rtic::RacyCell::new(rtic::export::executor::AsyncTaskExecutor::new());
|
||||
|
||||
// The executors priority, this can be any value - we will overwrite it when we
|
||||
// start a task
|
||||
#[allow(non_upper_case_globals)]
|
||||
static #prio_name: rtic::RacyCell<rtic::export::Priority> =
|
||||
unsafe { rtic::RacyCell::new(rtic::export::Priority::new(0)) };
|
||||
));
|
||||
}
|
||||
|
||||
|
|
@ -39,7 +32,6 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
|
|||
|
||||
for name in channel.tasks.iter() {
|
||||
let exec_name = util::internal_task_ident(name, "EXEC");
|
||||
let prio_name = util::internal_task_ident(name, "PRIORITY");
|
||||
// let task = &app.software_tasks[name];
|
||||
// let cfgs = &task.cfgs;
|
||||
let executor_run_ident = util::executor_run_ident(name);
|
||||
|
|
@ -57,14 +49,9 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
|
|||
if !(&*#exec_name.get()).is_running() {
|
||||
// TODO Fix this to be compare and swap
|
||||
if #rq.load(core::sync::atomic::Ordering::Relaxed) {
|
||||
#rq.store(false, core::sync::atomic::Ordering::Relaxed);
|
||||
#rq.store(false, core::sync::atomic::Ordering::Relaxed);
|
||||
|
||||
|
||||
// The async executor needs a static priority
|
||||
#prio_name.get_mut().write(rtic::export::Priority::new(PRIORITY));
|
||||
let priority: &'static _ = &*#prio_name.get();
|
||||
|
||||
(&mut *#exec_name.get_mut()).spawn(#name(#name::Context::new(priority)));
|
||||
(&mut *#exec_name.get_mut()).spawn(#name(#name::Context::new()));
|
||||
#executor_run_ident.store(true, core::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,22 +41,16 @@ pub fn codegen(
|
|||
|
||||
rtic::export::run(PRIORITY, || {
|
||||
#name(
|
||||
#name::Context::new(&rtic::export::Priority::new(PRIORITY))
|
||||
#name::Context::new()
|
||||
)
|
||||
});
|
||||
}
|
||||
));
|
||||
|
||||
let mut shared_needs_lt = false;
|
||||
let mut local_needs_lt = false;
|
||||
|
||||
// `${task}Locals`
|
||||
if !task.args.local_resources.is_empty() {
|
||||
let (item, constructor) = local_resources_struct::codegen(
|
||||
Context::HardwareTask(name),
|
||||
&mut local_needs_lt,
|
||||
app,
|
||||
);
|
||||
let (item, constructor) =
|
||||
local_resources_struct::codegen(Context::HardwareTask(name), app);
|
||||
|
||||
root.push(item);
|
||||
|
||||
|
|
@ -65,24 +59,19 @@ pub fn codegen(
|
|||
|
||||
// `${task}Resources`
|
||||
if !task.args.shared_resources.is_empty() {
|
||||
let (item, constructor) = shared_resources_struct::codegen(
|
||||
Context::HardwareTask(name),
|
||||
&mut shared_needs_lt,
|
||||
app,
|
||||
);
|
||||
let (item, constructor) =
|
||||
shared_resources_struct::codegen(Context::HardwareTask(name), app);
|
||||
|
||||
root.push(item);
|
||||
|
||||
mod_app.push(constructor);
|
||||
}
|
||||
|
||||
root.push(module::codegen(
|
||||
Context::HardwareTask(name),
|
||||
shared_needs_lt,
|
||||
local_needs_lt,
|
||||
app,
|
||||
analysis,
|
||||
));
|
||||
// Module generation...
|
||||
|
||||
root.push(module::codegen(Context::HardwareTask(name), app, analysis));
|
||||
|
||||
// End module generation
|
||||
|
||||
if !task.is_extern {
|
||||
let attrs = &task.attrs;
|
||||
|
|
|
|||
|
|
@ -24,37 +24,27 @@ pub fn codegen(
|
|||
TokenStream2,
|
||||
) {
|
||||
if let Some(idle) = &app.idle {
|
||||
let mut shared_needs_lt = false;
|
||||
let mut local_needs_lt = false;
|
||||
let mut mod_app = vec![];
|
||||
let mut root_idle = vec![];
|
||||
|
||||
let name = &idle.name;
|
||||
|
||||
if !idle.args.shared_resources.is_empty() {
|
||||
let (item, constructor) =
|
||||
shared_resources_struct::codegen(Context::Idle, &mut shared_needs_lt, app);
|
||||
let (item, constructor) = shared_resources_struct::codegen(Context::Idle, app);
|
||||
|
||||
root_idle.push(item);
|
||||
mod_app.push(constructor);
|
||||
}
|
||||
|
||||
if !idle.args.local_resources.is_empty() {
|
||||
let (item, constructor) =
|
||||
local_resources_struct::codegen(Context::Idle, &mut local_needs_lt, app);
|
||||
let (item, constructor) = local_resources_struct::codegen(Context::Idle, app);
|
||||
|
||||
root_idle.push(item);
|
||||
|
||||
mod_app.push(constructor);
|
||||
}
|
||||
|
||||
root_idle.push(module::codegen(
|
||||
Context::Idle,
|
||||
shared_needs_lt,
|
||||
local_needs_lt,
|
||||
app,
|
||||
analysis,
|
||||
));
|
||||
root_idle.push(module::codegen(Context::Idle, app, analysis));
|
||||
|
||||
let attrs = &idle.attrs;
|
||||
let context = &idle.context;
|
||||
|
|
@ -71,7 +61,7 @@ pub fn codegen(
|
|||
));
|
||||
|
||||
let call_idle = quote!(#name(
|
||||
#name::Context::new(&rtic::export::Priority::new(0))
|
||||
#name::Context::new()
|
||||
));
|
||||
|
||||
(mod_app, root_idle, user_idle, call_idle)
|
||||
|
|
|
|||
|
|
@ -91,8 +91,7 @@ pub fn codegen(app: &App, analysis: &Analysis) -> CodegenResult {
|
|||
|
||||
// `${task}Locals`
|
||||
if !init.args.local_resources.is_empty() {
|
||||
let (item, constructor) =
|
||||
local_resources_struct::codegen(Context::Init, &mut local_needs_lt, app);
|
||||
let (item, constructor) = local_resources_struct::codegen(Context::Init, app);
|
||||
|
||||
root_init.push(item);
|
||||
|
||||
|
|
@ -103,13 +102,7 @@ pub fn codegen(app: &App, analysis: &Analysis) -> CodegenResult {
|
|||
let (shared_resources, local_resources) = #name(#name::Context::new(core.into()));
|
||||
};
|
||||
|
||||
root_init.push(module::codegen(
|
||||
Context::Init,
|
||||
false,
|
||||
local_needs_lt,
|
||||
app,
|
||||
analysis,
|
||||
));
|
||||
root_init.push(module::codegen(Context::Init, app, analysis));
|
||||
|
||||
(mod_app, root_init, user_init, call_init)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use quote::quote;
|
|||
use crate::codegen::util;
|
||||
|
||||
/// Generates local resources structs
|
||||
pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, TokenStream2) {
|
||||
pub fn codegen(ctxt: Context, app: &App) -> (TokenStream2, TokenStream2) {
|
||||
let mut lt = None;
|
||||
|
||||
let resources = match ctxt {
|
||||
|
|
@ -74,16 +74,14 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
|
|||
}
|
||||
|
||||
if lt.is_some() {
|
||||
*needs_lt = true;
|
||||
|
||||
// The struct could end up empty due to `cfg`s leading to an error due to `'a` being unused
|
||||
if has_cfgs {
|
||||
fields.push(quote!(
|
||||
#[doc(hidden)]
|
||||
pub __marker__: core::marker::PhantomData<&'a ()>
|
||||
pub __rtic_internal_marker: ::core::marker::PhantomData<&'a ()>
|
||||
));
|
||||
|
||||
values.push(quote!(__marker__: core::marker::PhantomData));
|
||||
values.push(quote!(__rtic_internal_marker: ::core::marker::PhantomData));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,13 +4,7 @@ use proc_macro2::TokenStream as TokenStream2;
|
|||
use quote::quote;
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
pub fn codegen(
|
||||
ctxt: Context,
|
||||
shared_resources_tick: bool,
|
||||
local_resources_tick: bool,
|
||||
app: &App,
|
||||
analysis: &Analysis,
|
||||
) -> TokenStream2 {
|
||||
pub fn codegen(ctxt: Context, app: &App, analysis: &Analysis) -> TokenStream2 {
|
||||
let mut items = vec![];
|
||||
let mut module_items = vec![];
|
||||
let mut fields = vec![];
|
||||
|
|
@ -20,7 +14,6 @@ pub fn codegen(
|
|||
|
||||
let name = ctxt.ident(app);
|
||||
|
||||
let mut lt = None;
|
||||
match ctxt {
|
||||
Context::Init => {
|
||||
fields.push(quote!(
|
||||
|
|
@ -39,10 +32,9 @@ pub fn codegen(
|
|||
values.push(quote!(device: #device::Peripherals::steal()));
|
||||
}
|
||||
|
||||
lt = Some(quote!('a));
|
||||
fields.push(quote!(
|
||||
/// Critical section token for init
|
||||
pub cs: rtic::export::CriticalSection<#lt>
|
||||
pub cs: rtic::export::CriticalSection<'a>
|
||||
));
|
||||
|
||||
values.push(quote!(cs: rtic::export::CriticalSection::new()));
|
||||
|
|
@ -55,12 +47,6 @@ pub fn codegen(
|
|||
|
||||
if ctxt.has_local_resources(app) {
|
||||
let ident = util::local_resources_ident(ctxt, app);
|
||||
let lt = if local_resources_tick {
|
||||
lt = Some(quote!('a));
|
||||
Some(quote!('a))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
module_items.push(quote!(
|
||||
#[doc(inline)]
|
||||
|
|
@ -69,7 +55,7 @@ pub fn codegen(
|
|||
|
||||
fields.push(quote!(
|
||||
/// Local Resources this task has access to
|
||||
pub local: #name::LocalResources<#lt>
|
||||
pub local: #name::LocalResources<'a>
|
||||
));
|
||||
|
||||
values.push(quote!(local: #name::LocalResources::new()));
|
||||
|
|
@ -77,12 +63,6 @@ pub fn codegen(
|
|||
|
||||
if ctxt.has_shared_resources(app) {
|
||||
let ident = util::shared_resources_ident(ctxt, app);
|
||||
let lt = if shared_resources_tick {
|
||||
lt = Some(quote!('a));
|
||||
Some(quote!('a))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
module_items.push(quote!(
|
||||
#[doc(inline)]
|
||||
|
|
@ -91,15 +71,10 @@ pub fn codegen(
|
|||
|
||||
fields.push(quote!(
|
||||
/// Shared Resources this task has access to
|
||||
pub shared: #name::SharedResources<#lt>
|
||||
pub shared: #name::SharedResources<'a>
|
||||
));
|
||||
|
||||
let priority = if ctxt.is_init() {
|
||||
None
|
||||
} else {
|
||||
Some(quote!(priority))
|
||||
};
|
||||
values.push(quote!(shared: #name::SharedResources::new(#priority)));
|
||||
values.push(quote!(shared: #name::SharedResources::new()));
|
||||
}
|
||||
|
||||
let doc = match ctxt {
|
||||
|
|
@ -122,12 +97,6 @@ pub fn codegen(
|
|||
None
|
||||
};
|
||||
|
||||
let priority = if ctxt.is_init() {
|
||||
None
|
||||
} else {
|
||||
Some(quote!(priority: &#lt rtic::export::Priority))
|
||||
};
|
||||
|
||||
let internal_context_name = util::internal_task_ident(name, "Context");
|
||||
|
||||
items.push(quote!(
|
||||
|
|
@ -135,15 +104,18 @@ pub fn codegen(
|
|||
/// Execution context
|
||||
#[allow(non_snake_case)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub struct #internal_context_name<#lt> {
|
||||
pub struct #internal_context_name<'a> {
|
||||
#[doc(hidden)]
|
||||
__rtic_internal_p: ::core::marker::PhantomData<&'a ()>,
|
||||
#(#fields,)*
|
||||
}
|
||||
|
||||
#(#cfgs)*
|
||||
impl<#lt> #internal_context_name<#lt> {
|
||||
impl<'a> #internal_context_name<'a> {
|
||||
#[inline(always)]
|
||||
pub unsafe fn new(#core #priority) -> Self {
|
||||
pub unsafe fn new(#core) -> Self {
|
||||
#internal_context_name {
|
||||
__rtic_internal_p: ::core::marker::PhantomData,
|
||||
#(#values,)*
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,19 +57,14 @@ pub fn codegen(
|
|||
#[allow(non_camel_case_types)]
|
||||
#(#cfgs)*
|
||||
pub struct #shared_name<'a> {
|
||||
priority: &'a Priority,
|
||||
__rtic_internal_p: ::core::marker::PhantomData<&'a ()>,
|
||||
}
|
||||
|
||||
#(#cfgs)*
|
||||
impl<'a> #shared_name<'a> {
|
||||
#[inline(always)]
|
||||
pub unsafe fn new(priority: &'a Priority) -> Self {
|
||||
#shared_name { priority }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub unsafe fn priority(&self) -> &Priority {
|
||||
self.priority
|
||||
pub unsafe fn new() -> Self {
|
||||
#shared_name { __rtic_internal_p: ::core::marker::PhantomData }
|
||||
}
|
||||
}
|
||||
));
|
||||
|
|
@ -104,8 +99,6 @@ pub fn codegen(
|
|||
quote!()
|
||||
} else {
|
||||
quote!(mod shared_resources {
|
||||
use rtic::export::Priority;
|
||||
|
||||
#(#mod_resources)*
|
||||
})
|
||||
};
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use quote::quote;
|
|||
use crate::codegen::util;
|
||||
|
||||
/// Generate shared resources structs
|
||||
pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, TokenStream2) {
|
||||
pub fn codegen(ctxt: Context, app: &App) -> (TokenStream2, TokenStream2) {
|
||||
let mut lt = None;
|
||||
|
||||
let resources = match ctxt {
|
||||
|
|
@ -72,7 +72,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
|
|||
|
||||
values.push(quote!(
|
||||
#(#cfgs)*
|
||||
#name: shared_resources::#shared_name::new(priority)
|
||||
#name: shared_resources::#shared_name::new()
|
||||
|
||||
));
|
||||
|
||||
|
|
@ -93,8 +93,6 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
|
|||
}
|
||||
|
||||
if lt.is_some() {
|
||||
*needs_lt = true;
|
||||
|
||||
// The struct could end up empty due to `cfg`s leading to an error due to `'a` being unused
|
||||
if has_cfgs {
|
||||
fields.push(quote!(
|
||||
|
|
@ -117,15 +115,10 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
|
|||
}
|
||||
);
|
||||
|
||||
let arg = if ctxt.is_init() {
|
||||
None
|
||||
} else {
|
||||
Some(quote!(priority: &#lt rtic::export::Priority))
|
||||
};
|
||||
let constructor = quote!(
|
||||
impl<#lt> #ident<#lt> {
|
||||
#[inline(always)]
|
||||
pub unsafe fn new(#arg) -> Self {
|
||||
pub unsafe fn new() -> Self {
|
||||
#ident {
|
||||
#(#values,)*
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,16 +36,11 @@ pub fn codegen(
|
|||
));
|
||||
|
||||
// `${task}Resources`
|
||||
let mut shared_needs_lt = false;
|
||||
let mut local_needs_lt = false;
|
||||
|
||||
// `${task}Locals`
|
||||
if !task.args.local_resources.is_empty() {
|
||||
let (item, constructor) = local_resources_struct::codegen(
|
||||
Context::SoftwareTask(name),
|
||||
&mut local_needs_lt,
|
||||
app,
|
||||
);
|
||||
let (item, constructor) =
|
||||
local_resources_struct::codegen(Context::SoftwareTask(name), app);
|
||||
|
||||
root.push(item);
|
||||
|
||||
|
|
@ -53,11 +48,8 @@ pub fn codegen(
|
|||
}
|
||||
|
||||
if !task.args.shared_resources.is_empty() {
|
||||
let (item, constructor) = shared_resources_struct::codegen(
|
||||
Context::SoftwareTask(name),
|
||||
&mut shared_needs_lt,
|
||||
app,
|
||||
);
|
||||
let (item, constructor) =
|
||||
shared_resources_struct::codegen(Context::SoftwareTask(name), app);
|
||||
|
||||
root.push(item);
|
||||
|
||||
|
|
@ -69,17 +61,12 @@ pub fn codegen(
|
|||
let attrs = &task.attrs;
|
||||
let cfgs = &task.cfgs;
|
||||
let stmts = &task.stmts;
|
||||
let context_lifetime = if shared_needs_lt || local_needs_lt {
|
||||
quote!(<'static>)
|
||||
} else {
|
||||
quote!()
|
||||
};
|
||||
|
||||
user_tasks.push(quote!(
|
||||
#(#attrs)*
|
||||
#(#cfgs)*
|
||||
#[allow(non_snake_case)]
|
||||
async fn #name(#context: #name::Context #context_lifetime) {
|
||||
async fn #name(#context: #name::Context<'static>) {
|
||||
use rtic::Mutex as _;
|
||||
use rtic::mutex::prelude::*;
|
||||
|
||||
|
|
@ -88,13 +75,7 @@ pub fn codegen(
|
|||
));
|
||||
}
|
||||
|
||||
root.push(module::codegen(
|
||||
Context::SoftwareTask(name),
|
||||
shared_needs_lt,
|
||||
local_needs_lt,
|
||||
app,
|
||||
analysis,
|
||||
));
|
||||
root.push(module::codegen(Context::SoftwareTask(name), app, analysis));
|
||||
}
|
||||
|
||||
(mod_app, root, user_tasks)
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@ pub fn impl_mutex(
|
|||
ceiling: u8,
|
||||
ptr: &TokenStream2,
|
||||
) -> TokenStream2 {
|
||||
let (path, priority) = if resources_prefix {
|
||||
(quote!(shared_resources::#name), quote!(self.priority()))
|
||||
let path = if resources_prefix {
|
||||
quote!(shared_resources::#name)
|
||||
} else {
|
||||
(quote!(#name), quote!(self.priority))
|
||||
quote!(#name)
|
||||
};
|
||||
|
||||
let device = &app.args.device;
|
||||
|
|
@ -38,7 +38,6 @@ pub fn impl_mutex(
|
|||
unsafe {
|
||||
rtic::export::lock(
|
||||
#ptr,
|
||||
#priority,
|
||||
CEILING,
|
||||
#device::NVIC_PRIO_BITS,
|
||||
&#masks_name,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue