mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-23 20:22:51 +01:00
examples/lock fixed
This commit is contained in:
parent
bd20d0d89e
commit
b054e871d4
2 changed files with 11 additions and 9 deletions
|
@ -4,6 +4,7 @@
|
||||||
#![deny(warnings)]
|
#![deny(warnings)]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
#![feature(type_alias_impl_trait)]
|
||||||
|
|
||||||
use panic_semihosting as _;
|
use panic_semihosting as _;
|
||||||
|
|
||||||
|
@ -21,14 +22,14 @@ mod app {
|
||||||
struct Local {}
|
struct Local {}
|
||||||
|
|
||||||
#[init]
|
#[init]
|
||||||
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
|
fn init(_: init::Context) -> (Shared, Local) {
|
||||||
foo::spawn().unwrap();
|
foo::spawn().unwrap();
|
||||||
|
|
||||||
(Shared { counter: 0 }, Local {}, init::Monotonics())
|
(Shared { counter: 0 }, Local {})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(shared = [counter])] // <- same priority
|
#[task(shared = [counter])] // <- same priority
|
||||||
fn foo(c: foo::Context) {
|
async fn foo(c: foo::Context) {
|
||||||
bar::spawn().unwrap();
|
bar::spawn().unwrap();
|
||||||
|
|
||||||
*c.shared.counter += 1; // <- no lock API required
|
*c.shared.counter += 1; // <- no lock API required
|
||||||
|
@ -37,7 +38,7 @@ mod app {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(shared = [counter])] // <- same priority
|
#[task(shared = [counter])] // <- same priority
|
||||||
fn bar(c: bar::Context) {
|
async fn bar(c: bar::Context) {
|
||||||
foo::spawn().unwrap();
|
foo::spawn().unwrap();
|
||||||
|
|
||||||
*c.shared.counter += 1; // <- no lock API required
|
*c.shared.counter += 1; // <- no lock API required
|
|
@ -4,6 +4,7 @@
|
||||||
#![deny(warnings)]
|
#![deny(warnings)]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
#![feature(type_alias_impl_trait)]
|
||||||
|
|
||||||
use panic_semihosting as _;
|
use panic_semihosting as _;
|
||||||
|
|
||||||
|
@ -20,15 +21,15 @@ mod app {
|
||||||
struct Local {}
|
struct Local {}
|
||||||
|
|
||||||
#[init]
|
#[init]
|
||||||
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
|
fn init(_: init::Context) -> (Shared, Local) {
|
||||||
foo::spawn().unwrap();
|
foo::spawn().unwrap();
|
||||||
|
|
||||||
(Shared { shared: 0 }, Local {}, init::Monotonics())
|
(Shared { shared: 0 }, Local {})
|
||||||
}
|
}
|
||||||
|
|
||||||
// when omitted priority is assumed to be `1`
|
// when omitted priority is assumed to be `1`
|
||||||
#[task(shared = [shared])]
|
#[task(shared = [shared])]
|
||||||
fn foo(mut c: foo::Context) {
|
async fn foo(mut c: foo::Context) {
|
||||||
hprintln!("A").unwrap();
|
hprintln!("A").unwrap();
|
||||||
|
|
||||||
// the lower priority task requires a critical section to access the data
|
// the lower priority task requires a critical section to access the data
|
||||||
|
@ -53,7 +54,7 @@ mod app {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(priority = 2, shared = [shared])]
|
#[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
|
// the higher priority task does still need a critical section
|
||||||
let shared = c.shared.shared.lock(|shared| {
|
let shared = c.shared.shared.lock(|shared| {
|
||||||
*shared += 1;
|
*shared += 1;
|
||||||
|
@ -65,7 +66,7 @@ mod app {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[task(priority = 3)]
|
#[task(priority = 3)]
|
||||||
fn baz(_: baz::Context) {
|
async fn baz(_: baz::Context) {
|
||||||
hprintln!("C").unwrap();
|
hprintln!("C").unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue