api test, all implemented, TODO hide priority?

This commit is contained in:
Per Lindgren 2021-10-18 17:59:09 +02:00 committed by Henrik Tjäder
parent f9d05fdad0
commit 9d92f2ab70
2 changed files with 79 additions and 39 deletions

View file

@ -1,13 +1,13 @@
//! examples/lock.rs //! examples/lockall.rs
// #![deny(unsafe_code)] #![deny(unsafe_code)]
// #![deny(warnings)] #![deny(warnings)]
#![no_main] #![no_main]
#![no_std] #![no_std]
use panic_semihosting as _; use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])] #[rtic::app(device = lm3s6965, dispatchers = [GPIOA, GPIOB, GPIOC])]
mod app { mod app {
use cortex_m_semihosting::{debug, hprintln}; use cortex_m_semihosting::{debug, hprintln};
@ -30,44 +30,31 @@ mod app {
// when omitted priority is assumed to be `1` // when omitted priority is assumed to be `1`
#[task(shared = [a, b])] #[task(shared = [a, b])]
fn foo(mut c: foo::Context) { fn foo(mut c: foo::Context) {
static mut X: Option<&'static mut u32> = None;
static mut Y: u32 = 0;
let _ = hprintln!("before lock");
c.shared.lock(|s| { c.shared.lock(|s| {
let _ = hprintln!("in lock"); hprintln!("foo: a = {}, b = {}", s.a, s.b).ok();
let _ = hprintln!("here {}, {}", s.a, s.b);
*s.a += 1; *s.a += 1;
bar::spawn().unwrap();
// soundness check baz::spawn().unwrap();
// c.shared.lock(|s| {}); // borrow error hprintln!("still in foo::lock").ok();
// c.shared.a.lock(|s| {}); // borrow error
unsafe {
X = Some(&mut Y);
// X = Some(s.a); // lifetime issue
// X = Some(&mut *s.a); // lifetime issue
// X = Some(&'static mut *s.a); // not rust
}
let _ = hprintln!("here {}, {}", s.a, s.b);
}); });
// the lower priority task requires a critical section to access the data hprintln!("still in foo").ok();
// c.shared.shared.lock(|shared| {
// // data can only be modified within this critical section (closure)
// *shared += 1;
// // bar will *not* run right now due to the critical section
// bar::spawn().unwrap();
// hprintln!("B - shared = {}", *shared).unwrap();
// // baz does not contend for `shared` so it's allowed to run now
// baz::spawn().unwrap();
// });
// // critical section is over: bar can now start
// hprintln!("E").unwrap();
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
} }
#[task(priority = 2, shared = [a])]
fn bar(mut c: bar::Context) {
// the higher priority task does still need a critical section
let a = c.shared.lock(|s| {
*s.a += 1;
// *s.b += 1; `b` not accessible
*s.a
});
hprintln!("bar: a = {}", a).unwrap();
}
#[task(priority = 3)]
fn baz(_: baz::Context) {
hprintln!("baz").unwrap();
}
} }

View file

@ -0,0 +1,53 @@
//! examples/lockall_soundness.rs
// #![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
use cortex_m_semihosting::{debug, hprintln};
#[shared]
struct Shared {
a: u32,
b: i64,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
foo::spawn().unwrap();
(Shared { a: 1, b: 2 }, Local {}, init::Monotonics())
}
// when omitted priority is assumed to be `1`
#[task(shared = [a, b])]
fn foo(mut c: foo::Context) {
static mut X: Option<&'static mut u32> = None;
static mut Y: u32 = 0;
c.shared.lock(|s| {
hprintln!("s.a = {}, s.b = {}", s.a, s.b).ok();
*s.a += 1;
// soundness check
// c.shared.lock(|s| {}); // borrow error
// c.shared.a.lock(|s| {}); // borrow error
unsafe {
X = Some(&mut Y);
// X = Some(s.a); // lifetime issue
// X = Some(&mut *s.a); // lifetime issue
// X = Some(&'static mut *s.a); // not rust
}
hprintln!("s.a = {}, s.b = {}", s.a, s.b).ok();
});
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
}
}