From b054e871d486e8eb35e3c98a73652640238c5e7d Mon Sep 17 00:00:00 2001 From: Per Lindgren Date: Sat, 7 Jan 2023 14:23:32 +0100 Subject: examples/lock fixed --- examples/lock-free.no_rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ examples/lock-free.rs | 49 ----------------------------------------------- examples/lock.rs | 11 ++++++----- 3 files changed, 56 insertions(+), 54 deletions(-) create mode 100644 examples/lock-free.no_rs delete mode 100644 examples/lock-free.rs (limited to 'examples') diff --git a/examples/lock-free.no_rs b/examples/lock-free.no_rs new file mode 100644 index 0000000..053307c --- /dev/null +++ b/examples/lock-free.no_rs @@ -0,0 +1,50 @@ +//! examples/lock-free.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] +#![feature(type_alias_impl_trait)] + +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])] +mod app { + use cortex_m_semihosting::{debug, hprintln}; + + #[shared] + struct Shared { + #[lock_free] // <- lock-free shared resource + counter: u64, + } + + #[local] + struct Local {} + + #[init] + fn init(_: init::Context) -> (Shared, Local) { + foo::spawn().unwrap(); + + (Shared { counter: 0 }, Local {}) + } + + #[task(shared = [counter])] // <- same priority + async fn foo(c: foo::Context) { + bar::spawn().unwrap(); + + *c.shared.counter += 1; // <- no lock API required + let counter = *c.shared.counter; + hprintln!(" foo = {}", counter).unwrap(); + } + + #[task(shared = [counter])] // <- same priority + async fn bar(c: bar::Context) { + foo::spawn().unwrap(); + + *c.shared.counter += 1; // <- no lock API required + let counter = *c.shared.counter; + hprintln!(" bar = {}", counter).unwrap(); + + debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator + } +} diff --git a/examples/lock-free.rs b/examples/lock-free.rs deleted file mode 100644 index ea6ff1b..0000000 --- a/examples/lock-free.rs +++ /dev/null @@ -1,49 +0,0 @@ -//! examples/lock-free.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 { - #[lock_free] // <- lock-free shared resource - counter: u64, - } - - #[local] - struct Local {} - - #[init] - fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { - foo::spawn().unwrap(); - - (Shared { counter: 0 }, Local {}, init::Monotonics()) - } - - #[task(shared = [counter])] // <- same priority - fn foo(c: foo::Context) { - bar::spawn().unwrap(); - - *c.shared.counter += 1; // <- no lock API required - let counter = *c.shared.counter; - hprintln!(" foo = {}", counter).unwrap(); - } - - #[task(shared = [counter])] // <- same priority - fn bar(c: bar::Context) { - foo::spawn().unwrap(); - - *c.shared.counter += 1; // <- no lock API required - let counter = *c.shared.counter; - hprintln!(" bar = {}", counter).unwrap(); - - debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator - } -} diff --git a/examples/lock.rs b/examples/lock.rs index f1a1696..50b6aaa 100644 --- a/examples/lock.rs +++ b/examples/lock.rs @@ -4,6 +4,7 @@ #![deny(warnings)] #![no_main] #![no_std] +#![feature(type_alias_impl_trait)] use panic_semihosting as _; @@ -20,15 +21,15 @@ mod app { struct Local {} #[init] - fn init(_: init::Context) -> (Shared, Local, init::Monotonics) { + fn init(_: init::Context) -> (Shared, Local) { foo::spawn().unwrap(); - (Shared { shared: 0 }, Local {}, init::Monotonics()) + (Shared { shared: 0 }, Local {}) } // when omitted priority is assumed to be `1` #[task(shared = [shared])] - fn foo(mut c: foo::Context) { + async fn foo(mut c: foo::Context) { hprintln!("A").unwrap(); // the lower priority task requires a critical section to access the data @@ -53,7 +54,7 @@ mod app { } #[task(priority = 2, shared = [shared])] - fn bar(mut c: bar::Context) { + async fn bar(mut c: bar::Context) { // the higher priority task does still need a critical section let shared = c.shared.shared.lock(|shared| { *shared += 1; @@ -65,7 +66,7 @@ mod app { } #[task(priority = 3)] - fn baz(_: baz::Context) { + async fn baz(_: baz::Context) { hprintln!("C").unwrap(); } } -- cgit v1.2.3