-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2-share.rs
65 lines (55 loc) · 1.76 KB
/
2-share.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Sharing state between tasks using [`Cell`] and [`RefCell`].
//!
//! # Expected output
//!
//! ```
//! Task `b` will set cell now.
//! Task `b` will set ref_cell now.
//! Task `b` will yield now.
//! Task `a`: cell = Cell { value: 42 }
//! Task `a`: ref_cell = RefCell { value: Some(42) }
//! Task `a` will yield now.
//! Task `b` will not yield again.
//! ```
#![no_main]
#![no_std]
use async_embedded::task;
use core::cell::{Cell, RefCell};
use cortex_m_rt::entry;
use defmt::{consts, info, Debug2Format};
use defmt_rtt as _; // global logger
use panic_probe as _;
use stm32f1xx_hal as _; // memory layout // panic handler
#[entry]
fn main() -> ! {
static mut CELL: Cell<u32> = Cell::new(0);
static mut REF_CELL: RefCell<Option<u32>> = RefCell::new(None);
// only references with `'static` lifetimes can be sent to `spawn`-ed tasks
// NOTE we coerce these to a shared (`&-`) reference to avoid the `move` blocks taking ownership
// of the owning static (`&'static mut`) reference
let cell: &'static Cell<_> = CELL;
let ref_cell: &'static RefCell<_> = REF_CELL;
let a = async move {
info!("Task `a`: cell = {:?}", Debug2Format::<consts::U64>(cell));
info!(
"Task `a`: ref_cell = {:?}",
Debug2Format::<consts::U64>(ref_cell)
);
loop {
info!("Task `a` will yield now.");
task::r#yield().await;
}
};
task::spawn(a);
let b = async {
info!("Task `b` will set cell now.");
cell.set(42);
info!("Task `b` will set ref_cell now.");
*ref_cell.borrow_mut() = Some(42);
info!("Task `b` will yield now.");
task::r#yield().await;
info!("Task `b` will not yield again.");
loop {}
};
task::block_on(b)
}