aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf/src/bin
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-04-06 10:20:43 +0000
committerGitHub <[email protected]>2022-04-06 10:20:43 +0000
commitfee0aef076718adcb2c654920712aa20786c94be (patch)
tree62a5bc0631310a37dfa6292dc475e227b1793fce /examples/nrf/src/bin
parentc1b382296434e762d16a36d658d2f308358e3f87 (diff)
parent67319480568a4548f2c784282c8844cd08cda7aa (diff)
Merge #696
696: Add async Mutex. r=Dirbaio a=Dirbaio What it says on the tin :) It allows sharing data between tasks when you want to `.await` stuff while holding it locked. Co-authored-by: Dario Nieuwenhuis <[email protected]>
Diffstat (limited to 'examples/nrf/src/bin')
-rw-r--r--examples/nrf/src/bin/mutex.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/mutex.rs b/examples/nrf/src/bin/mutex.rs
new file mode 100644
index 000000000..db1b72f6d
--- /dev/null
+++ b/examples/nrf/src/bin/mutex.rs
@@ -0,0 +1,44 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{info, unwrap};
6use embassy::blocking_mutex::raw::ThreadModeRawMutex;
7use embassy::executor::Spawner;
8use embassy::mutex::Mutex;
9use embassy::time::{Duration, Timer};
10use embassy_nrf::Peripherals;
11
12use defmt_rtt as _; // global logger
13use panic_probe as _;
14
15static MUTEX: Mutex<ThreadModeRawMutex, u32> = Mutex::new(0);
16
17#[embassy::task]
18async fn my_task() {
19 loop {
20 {
21 let mut m = MUTEX.lock().await;
22 info!("start long operation");
23 *m += 1000;
24
25 // Hold the mutex for a long time.
26 Timer::after(Duration::from_secs(1)).await;
27 info!("end long operation: count = {}", *m);
28 }
29
30 Timer::after(Duration::from_secs(1)).await;
31 }
32}
33
34#[embassy::main]
35async fn main(spawner: Spawner, _p: Peripherals) {
36 unwrap!(spawner.spawn(my_task()));
37
38 loop {
39 Timer::after(Duration::from_millis(300)).await;
40 let mut m = MUTEX.lock().await;
41 *m += 1;
42 info!("short operation: count = {}", *m);
43 }
44}