rtic/examples/lockall_soundness.rs
2022-02-05 01:10:28 +01:00

53 lines
1.3 KiB
Rust

//! 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
}
}