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 <dirbaio@dirbaio.net>
This commit is contained in:
44
examples/nrf/src/bin/mutex.rs
Normal file
44
examples/nrf/src/bin/mutex.rs
Normal file
@ -0,0 +1,44 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use defmt::{info, unwrap};
|
||||
use embassy::blocking_mutex::raw::ThreadModeRawMutex;
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::mutex::Mutex;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_nrf::Peripherals;
|
||||
|
||||
use defmt_rtt as _; // global logger
|
||||
use panic_probe as _;
|
||||
|
||||
static MUTEX: Mutex<ThreadModeRawMutex, u32> = Mutex::new(0);
|
||||
|
||||
#[embassy::task]
|
||||
async fn my_task() {
|
||||
loop {
|
||||
{
|
||||
let mut m = MUTEX.lock().await;
|
||||
info!("start long operation");
|
||||
*m += 1000;
|
||||
|
||||
// Hold the mutex for a long time.
|
||||
Timer::after(Duration::from_secs(1)).await;
|
||||
info!("end long operation: count = {}", *m);
|
||||
}
|
||||
|
||||
Timer::after(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(spawner: Spawner, _p: Peripherals) {
|
||||
unwrap!(spawner.spawn(my_task()));
|
||||
|
||||
loop {
|
||||
Timer::after(Duration::from_millis(300)).await;
|
||||
let mut m = MUTEX.lock().await;
|
||||
*m += 1;
|
||||
info!("short operation: count = {}", *m);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user