mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-27 14:04:56 +01:00
task_local and lock_free analysis (take 1)
This commit is contained in:
parent
f5cf64e35c
commit
6eafcf10e9
7 changed files with 444 additions and 1 deletions
79
examples/local.rs
Normal file
79
examples/local.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
//! 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();
|
||||
}
|
||||
};
|
82
examples/local_err.rs
Normal file
82
examples/local_err.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
//! 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();
|
||||
}
|
||||
};
|
30
examples/local_minimal.rs
Normal file
30
examples/local_minimal.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
//! 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 {}
|
||||
}
|
||||
};
|
54
examples/static.rs
Normal file
54
examples/static.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
//! examples/late.rs
|
||||
|
||||
#![deny(unsafe_code)]
|
||||
#![deny(warnings)]
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use cortex_m_semihosting::{debug, hprintln};
|
||||
use heapless::{
|
||||
consts::*,
|
||||
i,
|
||||
spsc::{Consumer, Producer, Queue},
|
||||
};
|
||||
use lm3s6965::Interrupt;
|
||||
use panic_semihosting as _;
|
||||
|
||||
#[rtfm::app(device = lm3s6965)]
|
||||
const APP: () = {
|
||||
// Late resources
|
||||
struct Resources {
|
||||
p: Producer<'static, u32, U4>,
|
||||
c: Consumer<'static, u32, U4>,
|
||||
}
|
||||
|
||||
#[init]
|
||||
fn init(_: init::Context) -> init::LateResources {
|
||||
static mut Q: Queue<u32, U4> = Queue(i::Queue::new());
|
||||
|
||||
let (p, c) = Q.split();
|
||||
|
||||
// Initialization of late resources
|
||||
init::LateResources { p, c }
|
||||
}
|
||||
|
||||
#[idle(resources = [c])]
|
||||
fn idle(c: idle::Context) -> ! {
|
||||
loop {
|
||||
if let Some(byte) = c.resources.c.dequeue() {
|
||||
hprintln!("received message: {}", byte).unwrap();
|
||||
|
||||
debug::exit(debug::EXIT_SUCCESS);
|
||||
} else {
|
||||
rtfm::pend(Interrupt::UART0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[task(binds = UART0, resources = [p])]
|
||||
fn uart0(c: uart0::Context) {
|
||||
static mut KALLE: u32 = 0;
|
||||
*KALLE += 1;
|
||||
c.resources.p.enqueue(42).unwrap();
|
||||
}
|
||||
};
|
|
@ -21,5 +21,10 @@ 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" }
|
||||
#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"
|
||||
|
||||
|
|
178
macros/src/custom_local.rs
Normal file
178
macros/src/custom_local.rs
Normal file
|
@ -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::<Vec<_>>(),
|
||||
)
|
||||
})
|
||||
.chain(app.software_tasks.iter().map(|(name, ht)| {
|
||||
(
|
||||
name.to_string(),
|
||||
ht.args.resources.iter().map(|(v, _)| v).collect::<Vec<_>>(),
|
||||
)
|
||||
}))
|
||||
.chain(app.hardware_tasks.iter().map(|(name, ht)| {
|
||||
(
|
||||
name.to_string(),
|
||||
ht.args.resources.iter().map(|(v, _)| v).collect::<Vec<_>>(),
|
||||
)
|
||||
}))
|
||||
.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<Span>,
|
||||
// end_span: ThreadBound<Span>,
|
||||
// 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);
|
||||
// }
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue