2018-11-03 17:02:41 +01:00
|
|
|
//! examples/lock.rs
|
|
|
|
|
|
|
|
#![deny(unsafe_code)]
|
|
|
|
#![deny(warnings)]
|
2023-01-21 23:10:43 +01:00
|
|
|
#![deny(missing_docs)]
|
2018-11-03 17:02:41 +01:00
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
use panic_semihosting as _;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA, GPIOB, GPIOC])]
|
2020-05-19 20:00:13 +02:00
|
|
|
mod app {
|
2020-10-15 18:50:17 +02:00
|
|
|
use cortex_m_semihosting::{debug, hprintln};
|
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
#[shared]
|
|
|
|
struct Shared {
|
2019-07-10 22:42:44 +02:00
|
|
|
shared: u32,
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
#[local]
|
|
|
|
struct Local {}
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
#[init]
|
2021-07-07 22:50:59 +02:00
|
|
|
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
|
2021-09-22 13:22:45 +02:00
|
|
|
foo::spawn().unwrap();
|
2020-10-01 19:38:49 +02:00
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
(Shared { shared: 0 }, Local {}, init::Monotonics())
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// when omitted priority is assumed to be `1`
|
2021-09-22 13:22:45 +02:00
|
|
|
#[task(shared = [shared])]
|
|
|
|
fn foo(mut c: foo::Context) {
|
2023-01-11 21:33:44 +01:00
|
|
|
hprintln!("A");
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
// the lower priority task requires a critical section to access the data
|
2021-07-07 22:50:59 +02:00
|
|
|
c.shared.shared.lock(|shared| {
|
2018-11-03 17:02:41 +01:00
|
|
|
// data can only be modified within this critical section (closure)
|
|
|
|
*shared += 1;
|
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
// bar will *not* run right now due to the critical section
|
|
|
|
bar::spawn().unwrap();
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2023-01-11 21:33:44 +01:00
|
|
|
hprintln!("B - shared = {}", *shared);
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
// baz does not contend for `shared` so it's allowed to run now
|
|
|
|
baz::spawn().unwrap();
|
2018-11-03 17:02:41 +01:00
|
|
|
});
|
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
// critical section is over: bar can now start
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2023-01-11 21:33:44 +01:00
|
|
|
hprintln!("E");
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[task(priority = 2, shared = [shared])]
|
|
|
|
fn bar(mut c: bar::Context) {
|
2020-10-22 21:36:32 +02:00
|
|
|
// the higher priority task does still need a critical section
|
2021-07-07 22:50:59 +02:00
|
|
|
let shared = c.shared.shared.lock(|shared| {
|
2020-10-22 21:36:32 +02:00
|
|
|
*shared += 1;
|
|
|
|
|
|
|
|
*shared
|
|
|
|
});
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2023-01-11 21:33:44 +01:00
|
|
|
hprintln!("D - shared = {}", shared);
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[task(priority = 3)]
|
|
|
|
fn baz(_: baz::Context) {
|
2023-01-11 21:33:44 +01:00
|
|
|
hprintln!("C");
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
2020-04-22 12:58:14 +02:00
|
|
|
}
|