From 6eafcf10e944fb5875c086631dde7fad6f0a7b3b Mon Sep 17 00:00:00 2001 From: Per Date: Wed, 4 Mar 2020 15:06:03 +0100 Subject: task_local and lock_free analysis (take 1) --- macros/src/custom_local.rs | 178 +++++++++++++++++++++++++++++++++++++++++++++ macros/src/lib.rs | 15 ++++ 2 files changed, 193 insertions(+) create mode 100644 macros/src/custom_local.rs (limited to 'macros/src') diff --git a/macros/src/custom_local.rs b/macros/src/custom_local.rs new file mode 100644 index 00000000..33817905 --- /dev/null +++ b/macros/src/custom_local.rs @@ -0,0 +1,178 @@ +use syn::parse; +//use syn::Ident; +//use proc_macro2::{Ident, Span}; +use proc_macro2::Ident; +use rtfm_syntax::{ + analyze::{Analysis, Ownership}, + ast::App, +}; +use syn::Error; +// ast::{App, CustomArg}, + +type Idents<'a> = Vec<&'a Ident>; + +// Assign an `extern` interrupt to each priority level +pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { + // collect task local resources + let task_local: Idents = app + .resources + .iter() + .filter(|(_, r)| r.properties.task_local) + .map(|(i, _)| i) + .chain( + app.late_resources + .iter() + .filter(|(_, r)| r.properties.task_local) + .map(|(i, _)| i), + ) + .collect(); + + let lock_free: Idents = app + .resources + .iter() + .filter(|(_, r)| r.properties.lock_free) + .map(|(i, _)| i) + .chain( + app.late_resources + .iter() + .filter(|(_, r)| r.properties.lock_free) + .map(|(i, _)| i), + ) + .collect(); + + // collect all tasks into a vector + type Task = String; + + let all_tasks: Vec<(Task, Idents)> = app + .idles + .iter() + .map(|(core, ht)| { + ( + format!("Idle (core {})", core), + ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ) + }) + .chain(app.software_tasks.iter().map(|(name, ht)| { + ( + name.to_string(), + ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ) + })) + .chain(app.hardware_tasks.iter().map(|(name, ht)| { + ( + name.to_string(), + ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ) + })) + .collect(); + + // check that task_local resources is only used once + let mut error = vec![]; + for task_local_id in task_local.iter() { + let mut used = vec![]; + for (task, tr) in all_tasks.iter() { + for r in tr { + if task_local_id == r { + used.push((task, r)); + } + } + } + if used.len() > 1 { + error.push(Error::new( + task_local_id.span(), + format!( + "task local resource {:?} is used by multiple tasks", + task_local_id.to_string() + ), + )); + + used.iter().for_each(|(task, resource)| { + error.push(Error::new( + resource.span(), + format!( + "task local resource {:?} is used by task {:?}", + resource.to_string(), + task + ), + )) + }); + } + } + + // filter out contended resources + let contended: Vec<(&Ident, &Ownership)> = _analysis + .ownerships + .iter() + .filter(|(_id, own)| match own { + Ownership::Contended { .. } => true, + _ => false, + }) + .collect(); + + // filter out lock_free contended resources + let lock_free_violation: Vec<&(&Ident, &Ownership)> = contended + .iter() + .filter(|(cont_id, _)| lock_free.iter().any(|lf_id| cont_id == lf_id)) + .collect(); + + // report contention error + lock_free_violation.iter().for_each(|(lf_err_id, _)| { + error.push(Error::new( + lf_err_id.span(), + format!( + "lock_free resource {:?} is contended by higher priority task", + lf_err_id.to_string() + ), + )) + }); + + // collect errors + if error.is_empty() { + Ok(()) + } else { + let mut err = error.iter().next().unwrap().clone(); + error.iter().for_each(|e| err.combine(e.clone())); + Err(err) + } + + // for tl in task_local { + // println!("tl {:?}", tl); + // // let mut first_use = None; + // for i in _analysis.ownerships.iter() { + // println!("\nown: {:?}", i); + // } + + // // println!("analysis {:?}", _analysis.locations); + + // for i in _analysis.locations.iter() { + // println!("\nloc: {:?}", i); + // } + + // ErrorMessage { + // // Span is implemented as an index into a thread-local interner to keep the + // // size small. It is not safe to access from a different thread. We want + // // errors to be Send and Sync to play nicely with the Failure crate, so pin + // // the span we're given to its original thread and assume it is + // // Span::call_site if accessed from any other thread. + // start_span: ThreadBound, + // end_span: ThreadBound, + // message: String, + // } + // (app.name, "here"); + // let span = app.name.span(); + // let start = span.start(); + // Err(vec![ErrorMessage { start_span: app.name.}]); + + // let mut task_locals = Vec::new(); + // println!("-- app:late_resources"); + + // println!( + // "task_locals {:?}", + // app.late_resources.filter(|r| r.task_local) + // ); + + // println!("-- resources"); + // for i in app.resources.iter() { + // println!("res: {:?}", i); + // } +} diff --git a/macros/src/lib.rs b/macros/src/lib.rs index e659559e..2c81ede0 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -10,6 +10,7 @@ use rtic_syntax::Settings; mod analyze; mod check; mod codegen; +mod custom_local; #[cfg(test)] mod tests; @@ -214,13 +215,27 @@ pub fn app(args: TokenStream, input: TokenStream) -> TokenStream { Ok(x) => x, }; + match custom_local::app(&app, &analysis) { + Err(e) => return e.to_compile_error().into(), + Ok(_) => {} + } + let extra = match check::app(&app, &analysis) { Err(e) => return e.to_compile_error().into(), Ok(x) => x, }; + // println!("extra {:?}", extra); + let analysis = analyze::app(analysis, &app); + // println!("after analysis, extra {:?}", extra); + + // match custom_local::app(&app, &analysis) { + // Err(e) => return e.to_compile_error().into(), + // Ok(_) => {} + // } + let ts = codegen::app(&app, &analysis, &extra); // Try to write the expanded code to disk -- cgit v1.2.3 From d4439fe73be1467e8881f7d11941b2768c37fc21 Mon Sep 17 00:00:00 2001 From: Henrik Tjäder Date: Sat, 7 Mar 2020 23:39:20 +0100 Subject: Print module name and priority --- macros/src/custom_local.rs | 135 ++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 74 deletions(-) (limited to 'macros/src') diff --git a/macros/src/custom_local.rs b/macros/src/custom_local.rs index 33817905..f220c3d8 100644 --- a/macros/src/custom_local.rs +++ b/macros/src/custom_local.rs @@ -1,13 +1,11 @@ use syn::parse; -//use syn::Ident; -//use proc_macro2::{Ident, Span}; +use std::collections::HashMap; use proc_macro2::Ident; use rtfm_syntax::{ - analyze::{Analysis, Ownership}, + analyze::Analysis, ast::App, }; use syn::Error; -// ast::{App, CustomArg}, type Idents<'a> = Vec<&'a Ident>; @@ -42,26 +40,31 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { // collect all tasks into a vector type Task = String; + type Priority = u8; - let all_tasks: Vec<(Task, Idents)> = app + let all_tasks: Vec<(Task, Idents, Priority)> = app .idles .iter() .map(|(core, ht)| { ( format!("Idle (core {})", core), ht.args.resources.iter().map(|(v, _)| v).collect::>(), + 0 + ) }) .chain(app.software_tasks.iter().map(|(name, ht)| { ( name.to_string(), ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ht.args.priority ) })) .chain(app.hardware_tasks.iter().map(|(name, ht)| { ( name.to_string(), ht.args.resources.iter().map(|(v, _)| v).collect::>(), + ht.args.priority ) })) .collect(); @@ -70,10 +73,10 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { let mut error = vec![]; for task_local_id in task_local.iter() { let mut used = vec![]; - for (task, tr) in all_tasks.iter() { + for (task, tr, priority) in all_tasks.iter() { for r in tr { if task_local_id == r { - used.push((task, r)); + used.push((task, r, priority)); } } } @@ -86,46 +89,71 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { ), )); - used.iter().for_each(|(task, resource)| { + used.iter().for_each(|(task, resource, priority)| { error.push(Error::new( resource.span(), format!( - "task local resource {:?} is used by task {:?}", + "task local resource {:?} is used by task {:?} with priority {:?}", resource.to_string(), - task + task, + priority ), )) }); } } - // filter out contended resources - let contended: Vec<(&Ident, &Ownership)> = _analysis - .ownerships - .iter() - .filter(|(_id, own)| match own { - Ownership::Contended { .. } => true, - _ => false, - }) - .collect(); + let mut lf_res_with_error = vec![]; + let mut lf_hash = HashMap::new(); - // filter out lock_free contended resources - let lock_free_violation: Vec<&(&Ident, &Ownership)> = contended - .iter() - .filter(|(cont_id, _)| lock_free.iter().any(|lf_id| cont_id == lf_id)) - .collect(); + for lf_res in lock_free.iter() { + for (task, tr, priority) in all_tasks.iter() { + for r in tr { + // Get all uses of resources annotated lock_free + if lf_res == r { + // HashMap returns the previous existing object if old.key == new.key + if let Some(lf_res) = lf_hash.insert(r.to_string(), (task, r, priority)) { + // Check if priority differ, if it does, append to + // list of resources which will be annotated with errors + if priority != lf_res.2 { + lf_res_with_error.push(lf_res.1); + lf_res_with_error.push(r); + } + // If the resource already violates lock free properties + if lf_res_with_error.contains(&r) { + lf_res_with_error.push(lf_res.1); + lf_res_with_error.push(r); + } + } + } + } + } + } - // report contention error - lock_free_violation.iter().for_each(|(lf_err_id, _)| { + // Add error message in the resource struct + for r in lock_free { + if lf_res_with_error.contains(&&r) { + error.push(Error::new( + r.span(), + format!( + "Lock free resource {:?} is used by tasks at different priorities", + r.to_string(), + ), + )); + } + } + + // Add error message for each use of the resource + for resource in lf_res_with_error.clone() { error.push(Error::new( - lf_err_id.span(), + resource.span(), format!( - "lock_free resource {:?} is contended by higher priority task", - lf_err_id.to_string() + "Resource {:?} is declared lock free but used by tasks at different priorities", + resource.to_string(), ), - )) - }); - + )); + } + // collect errors if error.is_empty() { Ok(()) @@ -134,45 +162,4 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { error.iter().for_each(|e| err.combine(e.clone())); Err(err) } - - // for tl in task_local { - // println!("tl {:?}", tl); - // // let mut first_use = None; - // for i in _analysis.ownerships.iter() { - // println!("\nown: {:?}", i); - // } - - // // println!("analysis {:?}", _analysis.locations); - - // for i in _analysis.locations.iter() { - // println!("\nloc: {:?}", i); - // } - - // ErrorMessage { - // // Span is implemented as an index into a thread-local interner to keep the - // // size small. It is not safe to access from a different thread. We want - // // errors to be Send and Sync to play nicely with the Failure crate, so pin - // // the span we're given to its original thread and assume it is - // // Span::call_site if accessed from any other thread. - // start_span: ThreadBound, - // end_span: ThreadBound, - // message: String, - // } - // (app.name, "here"); - // let span = app.name.span(); - // let start = span.start(); - // Err(vec![ErrorMessage { start_span: app.name.}]); - - // let mut task_locals = Vec::new(); - // println!("-- app:late_resources"); - - // println!( - // "task_locals {:?}", - // app.late_resources.filter(|r| r.task_local) - // ); - - // println!("-- resources"); - // for i in app.resources.iter() { - // println!("res: {:?}", i); - // } -} +} \ No newline at end of file -- cgit v1.2.3 From e2364aae3eebf3326534bd4818d0312a03817538 Mon Sep 17 00:00:00 2001 From: Henrik Tjäder Date: Tue, 8 Sep 2020 17:51:10 +0000 Subject: Updated examples and rtic-name --- examples/local-cfg-task-local.rs | 65 ------------ examples/local-cfg.rs | 127 ----------------------- examples/local.rs | 79 -------------- examples/local_err.rs | 82 --------------- examples/local_minimal.rs | 30 ------ examples/static.rs | 15 ++- examples/task-local-minimal.rs | 33 ++++++ examples/task-local.rs | 82 +++++++++++++++ macros/Cargo.toml | 8 +- macros/src/custom_local.rs | 165 ------------------------------ macros/src/lib.rs | 15 --- ui/single/local-cfg-task-local-err.rs | 66 ++++++++++++ ui/single/local-cfg-task-local-err.stderr | 37 +++++++ ui/single/local-err.rs | 83 +++++++++++++++ ui/single/local-err.stderr | 60 +++++++++++ 15 files changed, 373 insertions(+), 574 deletions(-) delete mode 100644 examples/local-cfg-task-local.rs delete mode 100644 examples/local-cfg.rs delete mode 100644 examples/local.rs delete mode 100644 examples/local_err.rs delete mode 100644 examples/local_minimal.rs create mode 100644 examples/task-local-minimal.rs create mode 100644 examples/task-local.rs delete mode 100644 macros/src/custom_local.rs create mode 100644 ui/single/local-cfg-task-local-err.rs create mode 100644 ui/single/local-cfg-task-local-err.stderr create mode 100644 ui/single/local-err.rs create mode 100644 ui/single/local-err.stderr (limited to 'macros/src') diff --git a/examples/local-cfg-task-local.rs b/examples/local-cfg-task-local.rs deleted file mode 100644 index 49069113..00000000 --- a/examples/local-cfg-task-local.rs +++ /dev/null @@ -1,65 +0,0 @@ -//! examples/local-cfg-task-local.rs - -#![deny(unsafe_code)] -//#![deny(warnings)] -#![no_main] -#![no_std] - -use cortex_m_semihosting::hprintln; -use cortex_m_semihosting::debug; -use lm3s6965::Interrupt; -use panic_semihosting as _; - -#[rtfm::app(device = lm3s6965)] -const APP: () = { - struct Resources { - // A local (move), early resource - #[cfg(feature = "feature_l1")] - #[task_local] - #[init(1)] - l1: u32, - - // A local (move), late resource - #[task_local] - l2: u32, - } - - #[init] - fn init(_: init::Context) -> init::LateResources { - rtfm::pend(Interrupt::UART0); - rtfm::pend(Interrupt::UART1); - init::LateResources { - #[cfg(feature = "feature_l2")] - l2: 2, - #[cfg(not(feature = "feature_l2"))] - l2: 5 - } - } - - // l1 ok (task_local) - #[idle(resources =[#[cfg(feature = "feature_l1")]l1])] - fn idle(_cx: idle::Context) -> ! { - #[cfg(feature = "feature_l1")] - hprintln!("IDLE:l1 = {}", _cx.resources.l1).unwrap(); - debug::exit(debug::EXIT_SUCCESS); - loop {} - } - - // l2 ok (task_local) - #[task(priority = 1, binds = UART0, resources = [ - #[cfg(feature = "feature_l2")]l2, - ])] - fn uart0(_cx: uart0::Context) { - #[cfg(feature = "feature_l2")] - hprintln!("UART0:l2 = {}", _cx.resources.l2).unwrap(); - } - - // l2 error, conflicting with uart0 for l2 (task_local) - #[task(priority = 1, binds = UART1, resources = [ - #[cfg(not(feature = "feature_l2"))]l2 - ])] - fn uart1(_cx: uart1::Context) { - #[cfg(not(feature = "feature_l2"))] - hprintln!("UART0:l2 = {}", _cx.resources.l2).unwrap(); - } -}; diff --git a/examples/local-cfg.rs b/examples/local-cfg.rs deleted file mode 100644 index b9165508..00000000 --- a/examples/local-cfg.rs +++ /dev/null @@ -1,127 +0,0 @@ -//! examples/local.rs - -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -#[cfg( - any( - feature = "feature_s", - feature = "feature_e1", - feature = "feature_e2", - feature = "feature_l1", - feature = "feature_l2" - ) - ) -] -use cortex_m_semihosting::hprintln; -use cortex_m_semihosting::debug; -use lm3s6965::Interrupt; -use panic_semihosting as _; - -#[rtfm::app(device = lm3s6965)] -const APP: () = { - struct Resources { - // An early resource - #[cfg(feature = "feature_s")] - #[init(0)] - shared: u32, - - // A local (move), early resource - #[cfg(feature = "feature_l1")] - #[task_local] - #[init(1)] - l1: u32, - - // An exclusive, early resource - #[cfg(feature = "feature_e1")] - #[lock_free] - #[init(1)] - e1: u32, - - // A local (move), late resource - #[task_local] - #[cfg(feature = "feature_l2")] - l2: u32, - - // An exclusive, late resource - #[cfg(feature = "feature_e2")] - #[lock_free] - e2: u32, - } - - #[init] - fn init(_: init::Context) -> init::LateResources { - rtfm::pend(Interrupt::UART0); - rtfm::pend(Interrupt::UART1); - init::LateResources { - #[cfg(feature = "feature_e2")] - e2: 2, - #[cfg(feature = "feature_l2")] - l2: 2 - } - } - - // `shared` cannot be accessed from this context - // l1 ok (task_local) - // e2 ok (lock_free) - #[idle(resources =[#[cfg(feature = "feature_l1")]l1, e2])] - fn idle(_cx: idle::Context) -> ! { - #[cfg(feature = "feature_l1")] - hprintln!("IDLE:l1 = {}", _cx.resources.l1).unwrap(); - #[cfg(feature = "feature_e2")] - hprintln!("IDLE:e2 = {}", _cx.resources.e2).unwrap(); - debug::exit(debug::EXIT_SUCCESS); - loop {} - } - - // `shared` can be accessed from this context - // l2 ok (task_local) - // e1 ok (lock_free) - #[task(priority = 1, binds = UART0, resources = [shared, #[cfg(feature = "feature_l2")]l2, #[cfg(feature = "feature_e1")]e1])] - fn uart0(_cx: uart0::Context) { - #[cfg(feature = "feature_s")] - let shared: &mut u32 = _cx.resources.shared; - - #[cfg(feature = "feature_s")] - { - *shared += 1; - } - - #[cfg(feature = "feature_e1")] - { - // Unless put in a closure, the following error: - // error[E0658]: attributes on expressions are experimental - // --> examples/local-cfg.rs:69:9 - // 69 | #[cfg(feature = "feature_x")] - // | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - // = note: see issue #15701 - // for more information - *_cx.resources.e1 += 10; - } - #[cfg(feature = "feature_s")] - hprintln!("UART0: shared = {}", shared).unwrap(); - #[cfg(feature = "feature_l2")] - hprintln!("UART0:l2 = {}", _cx.resources.l2).unwrap(); - #[cfg(feature = "feature_e1")] - hprintln!("UART0:e1 = {}", _cx.resources.e1).unwrap(); - } - - // `shared` can be accessed from this context - // e1 ok (lock_free) - #[task(priority = 1, binds = UART1, resources = [#[cfg(feature = "feature_s")]shared, #[cfg(feature = "feature_e1")]e1])] - fn uart1(_cx: uart1::Context) { - #[cfg(feature = "feature_s")] - let shared: &mut u32 = _cx.resources.shared; - #[cfg(feature = "feature_s")] - { - *shared += 1; - } - - #[cfg(feature = "feature_s")] - hprintln!("UART1: shared = {}", shared).unwrap(); - #[cfg(feature = "feature_e1")] - hprintln!("UART1:e1 = {}", _cx.resources.e1).unwrap(); - } -}; diff --git a/examples/local.rs b/examples/local.rs deleted file mode 100644 index 802ab20f..00000000 --- a/examples/local.rs +++ /dev/null @@ -1,79 +0,0 @@ -//! examples/local.rs - -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -use cortex_m_semihosting::{debug, hprintln}; -use lm3s6965::Interrupt; -use panic_semihosting as _; - -#[rtfm::app(device = lm3s6965)] -const APP: () = { - struct Resources { - // An early resource - #[init(0)] - shared: u32, - - // A local (move), early resource - #[task_local] - #[init(1)] - l1: u32, - - // An exclusive, early resource - #[lock_free] - #[init(1)] - e1: u32, - - // A local (move), late resource - #[task_local] - l2: u32, - - // An exclusive, late resource - #[lock_free] - e2: u32, - } - - #[init] - fn init(_: init::Context) -> init::LateResources { - rtfm::pend(Interrupt::UART0); - rtfm::pend(Interrupt::UART1); - init::LateResources { e2: 2, l2: 2 } - } - - // `shared` cannot be accessed from this context - // l1 ok (task_local) - // e2 ok (lock_free) - #[idle(resources =[l1, e2])] - fn idle(cx: idle::Context) -> ! { - hprintln!("IDLE:l1 = {}", cx.resources.l1).unwrap(); - hprintln!("IDLE:e2 = {}", cx.resources.e2).unwrap(); - debug::exit(debug::EXIT_SUCCESS); - loop {} - } - - // `shared` can be accessed from this context - // l2 ok (task_local) - // e1 ok (lock_free) - #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] - fn uart0(cx: uart0::Context) { - let shared: &mut u32 = cx.resources.shared; - *shared += 1; - *cx.resources.e1 += 10; - hprintln!("UART0: shared = {}", shared).unwrap(); - hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap(); - hprintln!("UART0:e1 = {}", cx.resources.e1).unwrap(); - } - - // `shared` can be accessed from this context - // e1 ok (lock_free) - #[task(priority = 1, binds = UART1, resources = [shared, e1])] - fn uart1(cx: uart1::Context) { - let shared: &mut u32 = cx.resources.shared; - *shared += 1; - - hprintln!("UART1: shared = {}", shared).unwrap(); - hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap(); - } -}; diff --git a/examples/local_err.rs b/examples/local_err.rs deleted file mode 100644 index 3be593a8..00000000 --- a/examples/local_err.rs +++ /dev/null @@ -1,82 +0,0 @@ -//! examples/local_err.rs - -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -// errors here, since we cannot bail compilation or generate stubs -// run cargo expand, then you see the root of the problem... -use cortex_m_semihosting::{debug, hprintln}; -use lm3s6965::Interrupt; -use panic_semihosting as _; - -#[rtfm::app(device = lm3s6965)] -const APP: () = { - struct Resources { - // An early resource - #[init(0)] - shared: u32, - - // A local (move), early resource - #[task_local] - #[init(1)] - l1: u32, - - // An exclusive, early resource - #[lock_free] - #[init(1)] - e1: u32, - - // A local (move), late resource - #[task_local] - l2: u32, - - // An exclusive, late resource - #[lock_free] - e2: u32, - } - - #[init] - fn init(_: init::Context) -> init::LateResources { - rtfm::pend(Interrupt::UART0); - rtfm::pend(Interrupt::UART1); - init::LateResources { e2: 2, l2: 2 } - } - - // `shared` cannot be accessed from this context - // l1 ok - // l2 rejeceted (not task_local) - // e2 ok - #[idle(resources =[l1, l2, e2])] - fn idle(cx: idle::Context) -> ! { - hprintln!("IDLE:l1 = {}", cx.resources.l1).unwrap(); - hprintln!("IDLE:e2 = {}", cx.resources.e2).unwrap(); - debug::exit(debug::EXIT_SUCCESS); - loop {} - } - - // `shared` can be accessed from this context - // l2 rejected (not task_local) - // e1 rejected (not lock_free) - #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] - fn uart0(cx: uart0::Context) { - let shared: &mut u32 = cx.resources.shared; - *shared += 1; - *cx.resources.e1 += 10; - hprintln!("UART0: shared = {}", shared).unwrap(); - hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap(); - hprintln!("UART0:e1 = {}", cx.resources.e1).unwrap(); - } - - // l2 rejected (not task_local) - #[task(priority = 2, binds = UART1, resources = [shared, l2, e1])] - fn uart1(cx: uart1::Context) { - let shared: &mut u32 = cx.resources.shared; - *shared += 1; - - hprintln!("UART1: shared = {}", shared).unwrap(); - hprintln!("UART1:l2 = {}", cx.resources.l2).unwrap(); - hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap(); - } -}; diff --git a/examples/local_minimal.rs b/examples/local_minimal.rs deleted file mode 100644 index 13531c5e..00000000 --- a/examples/local_minimal.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! examples/local_minimal.rs -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -use cortex_m_semihosting::{debug, hprintln}; -use panic_semihosting as _; - -#[rtfm::app(device = lm3s6965)] -const APP: () = { - struct Resources { - // A local (move), late resource - #[task_local] - l: u32, - } - - #[init] - fn init(_: init::Context) -> init::LateResources { - init::LateResources { l: 42 } - } - - // l is task_local - #[idle(resources =[l])] - fn idle(cx: idle::Context) -> ! { - hprintln!("IDLE:l = {}", cx.resources.l).unwrap(); - debug::exit(debug::EXIT_SUCCESS); - loop {} - } -}; diff --git a/examples/static.rs b/examples/static.rs index ddcb11e6..9c3110c0 100644 --- a/examples/static.rs +++ b/examples/static.rs @@ -1,4 +1,4 @@ -//! examples/late.rs +//! examples/static.rs #![deny(unsafe_code)] #![deny(warnings)] @@ -14,9 +14,14 @@ use heapless::{ use lm3s6965::Interrupt; use panic_semihosting as _; -#[rtfm::app(device = lm3s6965)] -const APP: () = { +#[rtic::app(device = lm3s6965)] +mod app { + + use crate::U4; + use crate::{Consumer, Producer}; + // Late resources + #[resources] struct Resources { p: Producer<'static, u32, U4>, c: Consumer<'static, u32, U4>, @@ -40,7 +45,7 @@ const APP: () = { debug::exit(debug::EXIT_SUCCESS); } else { - rtfm::pend(Interrupt::UART0); + rtic::pend(Interrupt::UART0); } } } @@ -51,4 +56,4 @@ const APP: () = { *KALLE += 1; c.resources.p.enqueue(42).unwrap(); } -}; +} diff --git a/examples/task-local-minimal.rs b/examples/task-local-minimal.rs new file mode 100644 index 00000000..fd5ac68a --- /dev/null +++ b/examples/task-local-minimal.rs @@ -0,0 +1,33 @@ +//! examples/task-local_minimal.rs +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::{debug, hprintln}; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + #[resources] + struct Resources { + // A local (move), late resource + #[task_local] + l: u32, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + init::LateResources { l: 42 } + } + + // l is task_local + #[idle(resources =[l])] + fn idle(cx: idle::Context) -> ! { + hprintln!("IDLE:l = {}", cx.resources.l).unwrap(); + debug::exit(debug::EXIT_SUCCESS); + loop { + cortex_m::asm::nop(); + } + } +} diff --git a/examples/task-local.rs b/examples/task-local.rs new file mode 100644 index 00000000..8f0dfc79 --- /dev/null +++ b/examples/task-local.rs @@ -0,0 +1,82 @@ +//! examples/task-local.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::{debug, hprintln}; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + #[resources] + struct Resources { + // An early resource + #[init(0)] + shared: u32, + + // A local (move), early resource + #[task_local] + #[init(1)] + l1: u32, + + // An exclusive, early resource + #[lock_free] + #[init(1)] + e1: u32, + + // A local (move), late resource + #[task_local] + l2: u32, + + // An exclusive, late resource + #[lock_free] + e2: u32, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + rtic::pend(Interrupt::UART0); + rtic::pend(Interrupt::UART1); + init::LateResources { e2: 2, l2: 2 } + } + + // `shared` cannot be accessed from this context + // l1 ok (task_local) + // e2 ok (lock_free) + #[idle(resources =[l1, e2])] + fn idle(cx: idle::Context) -> ! { + hprintln!("IDLE:l1 = {}", cx.resources.l1).unwrap(); + hprintln!("IDLE:e2 = {}", cx.resources.e2).unwrap(); + debug::exit(debug::EXIT_SUCCESS); + loop { + cortex_m::asm::nop(); + } + } + + // `shared` can be accessed from this context + // l2 ok (task_local) + // e1 ok (lock_free) + #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] + fn uart0(cx: uart0::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; + *cx.resources.e1 += 10; + hprintln!("UART0: shared = {}", shared).unwrap(); + hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap(); + hprintln!("UART0:e1 = {}", cx.resources.e1).unwrap(); + } + + // `shared` can be accessed from this context + // e1 ok (lock_free) + #[task(priority = 1, binds = UART1, resources = [shared, e1])] + fn uart1(cx: uart1::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; + + hprintln!("UART1: shared = {}", shared).unwrap(); + hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap(); + } +} diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 74f54433..4fac48fe 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -21,10 +21,6 @@ proc-macro = true proc-macro2 = "1" quote = "1" syn = "1" -#rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "master", version = "0.4.0" } - -[dependencies.rtic-syntax] -git = "https://github.com/rtic-rs/rtic-syntax.git" -branch = "task_local_experiment" -version = "0.4.1" +#rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "task_local_experiment", version = "0.4.1" } +rtic-syntax = { git = "https://github.com/AfoHT/rtic-syntax", branch = "task_local_experiment", version = "0.4.1" } diff --git a/macros/src/custom_local.rs b/macros/src/custom_local.rs deleted file mode 100644 index f220c3d8..00000000 --- a/macros/src/custom_local.rs +++ /dev/null @@ -1,165 +0,0 @@ -use syn::parse; -use std::collections::HashMap; -use proc_macro2::Ident; -use rtfm_syntax::{ - analyze::Analysis, - ast::App, -}; -use syn::Error; - -type Idents<'a> = Vec<&'a Ident>; - -// Assign an `extern` interrupt to each priority level -pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<()> { - // collect task local resources - let task_local: Idents = app - .resources - .iter() - .filter(|(_, r)| r.properties.task_local) - .map(|(i, _)| i) - .chain( - app.late_resources - .iter() - .filter(|(_, r)| r.properties.task_local) - .map(|(i, _)| i), - ) - .collect(); - - let lock_free: Idents = app - .resources - .iter() - .filter(|(_, r)| r.properties.lock_free) - .map(|(i, _)| i) - .chain( - app.late_resources - .iter() - .filter(|(_, r)| r.properties.lock_free) - .map(|(i, _)| i), - ) - .collect(); - - // collect all tasks into a vector - type Task = String; - type Priority = u8; - - let all_tasks: Vec<(Task, Idents, Priority)> = app - .idles - .iter() - .map(|(core, ht)| { - ( - format!("Idle (core {})", core), - ht.args.resources.iter().map(|(v, _)| v).collect::>(), - 0 - - ) - }) - .chain(app.software_tasks.iter().map(|(name, ht)| { - ( - name.to_string(), - ht.args.resources.iter().map(|(v, _)| v).collect::>(), - ht.args.priority - ) - })) - .chain(app.hardware_tasks.iter().map(|(name, ht)| { - ( - name.to_string(), - ht.args.resources.iter().map(|(v, _)| v).collect::>(), - ht.args.priority - ) - })) - .collect(); - - // check that task_local resources is only used once - let mut error = vec![]; - for task_local_id in task_local.iter() { - let mut used = vec![]; - for (task, tr, priority) in all_tasks.iter() { - for r in tr { - if task_local_id == r { - used.push((task, r, priority)); - } - } - } - if used.len() > 1 { - error.push(Error::new( - task_local_id.span(), - format!( - "task local resource {:?} is used by multiple tasks", - task_local_id.to_string() - ), - )); - - used.iter().for_each(|(task, resource, priority)| { - error.push(Error::new( - resource.span(), - format!( - "task local resource {:?} is used by task {:?} with priority {:?}", - resource.to_string(), - task, - priority - ), - )) - }); - } - } - - let mut lf_res_with_error = vec![]; - let mut lf_hash = HashMap::new(); - - for lf_res in lock_free.iter() { - for (task, tr, priority) in all_tasks.iter() { - for r in tr { - // Get all uses of resources annotated lock_free - if lf_res == r { - // HashMap returns the previous existing object if old.key == new.key - if let Some(lf_res) = lf_hash.insert(r.to_string(), (task, r, priority)) { - // Check if priority differ, if it does, append to - // list of resources which will be annotated with errors - if priority != lf_res.2 { - lf_res_with_error.push(lf_res.1); - lf_res_with_error.push(r); - } - // If the resource already violates lock free properties - if lf_res_with_error.contains(&r) { - lf_res_with_error.push(lf_res.1); - lf_res_with_error.push(r); - } - } - } - } - } - } - - // Add error message in the resource struct - for r in lock_free { - if lf_res_with_error.contains(&&r) { - error.push(Error::new( - r.span(), - format!( - "Lock free resource {:?} is used by tasks at different priorities", - r.to_string(), - ), - )); - } - } - - // Add error message for each use of the resource - for resource in lf_res_with_error.clone() { - error.push(Error::new( - resource.span(), - format!( - "Resource {:?} is declared lock free but used by tasks at different priorities", - resource.to_string(), - ), - )); - } - - // collect errors - if error.is_empty() { - Ok(()) - } else { - let mut err = error.iter().next().unwrap().clone(); - error.iter().for_each(|e| err.combine(e.clone())); - Err(err) - } -} \ No newline at end of file diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 2c81ede0..e659559e 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -10,7 +10,6 @@ use rtic_syntax::Settings; mod analyze; mod check; mod codegen; -mod custom_local; #[cfg(test)] mod tests; @@ -215,27 +214,13 @@ pub fn app(args: TokenStream, input: TokenStream) -> TokenStream { Ok(x) => x, }; - match custom_local::app(&app, &analysis) { - Err(e) => return e.to_compile_error().into(), - Ok(_) => {} - } - let extra = match check::app(&app, &analysis) { Err(e) => return e.to_compile_error().into(), Ok(x) => x, }; - // println!("extra {:?}", extra); - let analysis = analyze::app(analysis, &app); - // println!("after analysis, extra {:?}", extra); - - // match custom_local::app(&app, &analysis) { - // Err(e) => return e.to_compile_error().into(), - // Ok(_) => {} - // } - let ts = codegen::app(&app, &analysis, &extra); // Try to write the expanded code to disk diff --git a/ui/single/local-cfg-task-local-err.rs b/ui/single/local-cfg-task-local-err.rs new file mode 100644 index 00000000..412f6142 --- /dev/null +++ b/ui/single/local-cfg-task-local-err.rs @@ -0,0 +1,66 @@ +//! examples/local-cfg-task-local.rs + +#![deny(unsafe_code)] +//#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::debug; +use cortex_m_semihosting::hprintln; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + #[resources] + struct Resources { + // A local (move), early resource + #[cfg(feature = "feature_l1")] + #[task_local] + #[init(1)] + l1: u32, + + // A local (move), late resource + #[task_local] + l2: u32, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + rtic::pend(Interrupt::UART0); + rtic::pend(Interrupt::UART1); + init::LateResources { + #[cfg(feature = "feature_l2")] + l2: 2, + #[cfg(not(feature = "feature_l2"))] + l2: 5, + } + } + + // l1 ok (task_local) + #[idle(resources =[#[cfg(feature = "feature_l1")]l1])] + fn idle(_cx: idle::Context) -> ! { + #[cfg(feature = "feature_l1")] + hprintln!("IDLE:l1 = {}", _cx.resources.l1).unwrap(); + debug::exit(debug::EXIT_SUCCESS); + loop {} + } + + // l2 ok (task_local) + #[task(priority = 1, binds = UART0, resources = [ + #[cfg(feature = "feature_l2")]l2, + ])] + fn uart0(_cx: uart0::Context) { + #[cfg(feature = "feature_l2")] + hprintln!("UART0:l2 = {}", _cx.resources.l2).unwrap(); + } + + // l2 error, conflicting with uart0 for l2 (task_local) + #[task(priority = 1, binds = UART1, resources = [ + #[cfg(not(feature = "feature_l2"))]l2 + ])] + fn uart1(_cx: uart1::Context) { + #[cfg(not(feature = "feature_l2"))] + hprintln!("UART0:l2 = {}", _cx.resources.l2).unwrap(); + } +} diff --git a/ui/single/local-cfg-task-local-err.stderr b/ui/single/local-cfg-task-local-err.stderr new file mode 100644 index 00000000..9a84ead4 --- /dev/null +++ b/ui/single/local-cfg-task-local-err.stderr @@ -0,0 +1,37 @@ +error: task local resource "l2" is used by multiple tasks + --> $DIR/local-cfg-task-local-err.rs:25:9 + | +25 | l2: u32, + | ^^ + +error: task local resource "l2" is used by task "uart0" with priority 1 + --> $DIR/local-cfg-task-local-err.rs:51:39 + | +51 | #[cfg(feature = "feature_l2")]l2, + | ^^ + +error: task local resource "l2" is used by task "uart1" with priority 1 + --> $DIR/local-cfg-task-local-err.rs:60:44 + | +60 | #[cfg(not(feature = "feature_l2"))]l2 + | ^^ + +warning: unused import: `cortex_m_semihosting::debug` + --> $DIR/local-cfg-task-local-err.rs:8:5 + | +8 | use cortex_m_semihosting::debug; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default + +warning: unused import: `cortex_m_semihosting::hprintln` + --> $DIR/local-cfg-task-local-err.rs:9:5 + | +9 | use cortex_m_semihosting::hprintln; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +warning: unused import: `lm3s6965::Interrupt` + --> $DIR/local-cfg-task-local-err.rs:10:5 + | +10 | use lm3s6965::Interrupt; + | ^^^^^^^^^^^^^^^^^^^ diff --git a/ui/single/local-err.rs b/ui/single/local-err.rs new file mode 100644 index 00000000..0fe98a4b --- /dev/null +++ b/ui/single/local-err.rs @@ -0,0 +1,83 @@ +//! examples/local_err.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +// errors here, since we cannot bail compilation or generate stubs +// run cargo expand, then you see the root of the problem... +use cortex_m_semihosting::{debug, hprintln}; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + #[resources] + struct Resources { + // An early resource + #[init(0)] + shared: u32, + + // A local (move), early resource + #[task_local] + #[init(1)] + l1: u32, + + // An exclusive, early resource + #[lock_free] + #[init(1)] + e1: u32, + + // A local (move), late resource + #[task_local] + l2: u32, + + // An exclusive, late resource + #[lock_free] + e2: u32, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + rtic::pend(Interrupt::UART0); + rtic::pend(Interrupt::UART1); + init::LateResources { e2: 2, l2: 2 } + } + + // `shared` cannot be accessed from this context + // l1 ok + // l2 rejeceted (not task_local) + // e2 ok + #[idle(resources =[l1, l2, e2])] + fn idle(cx: idle::Context) -> ! { + hprintln!("IDLE:l1 = {}", cx.resources.l1).unwrap(); + hprintln!("IDLE:e2 = {}", cx.resources.e2).unwrap(); + debug::exit(debug::EXIT_SUCCESS); + loop {} + } + + // `shared` can be accessed from this context + // l2 rejected (not task_local) + // e1 rejected (not lock_free) + #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] + fn uart0(cx: uart0::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; + *cx.resources.e1 += 10; + hprintln!("UART0: shared = {}", shared).unwrap(); + hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap(); + hprintln!("UART0:e1 = {}", cx.resources.e1).unwrap(); + } + + // l2 rejected (not task_local) + #[task(priority = 2, binds = UART1, resources = [shared, l2, e1])] + fn uart1(cx: uart1::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; + + hprintln!("UART1: shared = {}", shared).unwrap(); + hprintln!("UART1:l2 = {}", cx.resources.l2).unwrap(); + hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap(); + } +} diff --git a/ui/single/local-err.stderr b/ui/single/local-err.stderr new file mode 100644 index 00000000..88369d8e --- /dev/null +++ b/ui/single/local-err.stderr @@ -0,0 +1,60 @@ +error: task local resource "l2" is used by multiple tasks + --> $DIR/local-err.rs:34:9 + | +34 | l2: u32, + | ^^ + +error: task local resource "l2" is used by task "idle" with priority 0 + --> $DIR/local-err.rs:52:28 + | +52 | #[idle(resources =[l1, l2, e2])] + | ^^ + +error: task local resource "l2" is used by task "uart0" with priority 1 + --> $DIR/local-err.rs:63:62 + | +63 | #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] + | ^^ + +error: task local resource "l2" is used by task "uart1" with priority 2 + --> $DIR/local-err.rs:74:62 + | +74 | #[task(priority = 2, binds = UART1, resources = [shared, l2, e1])] + | ^^ + +error: Lock free resource "e1" is used by tasks at different priorities + --> $DIR/local-err.rs:30:9 + | +30 | e1: u32, + | ^^ + +error: Resource "e1" is declared lock free but used by tasks at different priorities + --> $DIR/local-err.rs:63:66 + | +63 | #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] + | ^^ + +error: Resource "e1" is declared lock free but used by tasks at different priorities + --> $DIR/local-err.rs:74:66 + | +74 | #[task(priority = 2, binds = UART1, resources = [shared, l2, e1])] + | ^^ + +error: unused imports: `debug`, `hprintln` + --> $DIR/local-err.rs:10:28 + | +10 | use cortex_m_semihosting::{debug, hprintln}; + | ^^^^^ ^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/local-err.rs:4:9 + | +4 | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(unused_imports)]` implied by `#[deny(warnings)]` + +error: unused import: `lm3s6965::Interrupt` + --> $DIR/local-err.rs:11:5 + | +11 | use lm3s6965::Interrupt; + | ^^^^^^^^^^^^^^^^^^^ -- cgit v1.2.3 From 37ee3a47afbbbf57751243d6d32aaac78073780c Mon Sep 17 00:00:00 2001 From: Henrik Tjäder Date: Wed, 14 Oct 2020 10:15:35 +0000 Subject: Create Enum containing all tasks --- macros/src/codegen.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'macros/src') diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index f230d395..a44266ad 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -126,6 +126,20 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { let user_code = app.user_code.clone(); let name = &app.name; let device = extra.device; + + // Get the list of all tasks + // Currently unused, might be useful + let task_list = analysis.tasks.clone(); + + let mut tasks = vec![]; + if !task_list.is_empty() { + tasks.push(quote!( + enum Tasks { + #(#task_list),* + } + )); + } + quote!( #(#user)* @@ -141,6 +155,9 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { #(#root_software_tasks)* + /// Unused + #(#tasks)* + /// Implementation details mod #name { /// Always include the device crate which contains the vector table -- cgit v1.2.3