mirror of
https://github.com/rtic-rs/rtic.git
synced 2025-12-16 12:55:23 +01:00
feat: example of wait-queue
This commit is contained in:
parent
14803e6639
commit
6a68e8e54d
3 changed files with 69 additions and 0 deletions
4
ci/expected/lm3s6965/wait-queue.run
Normal file
4
ci/expected/lm3s6965/wait-queue.run
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Inc
|
||||
Inc
|
||||
Inc
|
||||
Got 3
|
||||
|
|
@ -16,6 +16,7 @@ bare-metal = "1.0.0"
|
|||
cortex-m-semihosting = "0.5.0"
|
||||
rtic-time = { path = "../../rtic-time" }
|
||||
rtic-sync = { path = "../../rtic-sync" }
|
||||
rtic-common = { path = "../../rtic-common" }
|
||||
rtic-monotonics = { path = "../../rtic-monotonics", features = ["cortex-m-systick"] }
|
||||
rtic = { path = "../../rtic" }
|
||||
cfg-if = "1.0"
|
||||
|
|
|
|||
64
examples/lm3s6965/examples/wait-queue.rs
Normal file
64
examples/lm3s6965/examples/wait-queue.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
//! examples/wait-queue.rs
|
||||
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
#![deny(warnings)]
|
||||
#![deny(unsafe_code)]
|
||||
#![deny(missing_docs)]
|
||||
|
||||
use panic_semihosting as _;
|
||||
|
||||
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
|
||||
mod app {
|
||||
use cortex_m_semihosting::{debug, hprintln};
|
||||
use rtic_common::wait_queue::WaitQueue;
|
||||
|
||||
use rtic_monotonics::systick::prelude::*;
|
||||
systick_monotonic!(Mono, 100);
|
||||
|
||||
#[shared]
|
||||
struct Shared {
|
||||
count: u32,
|
||||
}
|
||||
|
||||
#[local]
|
||||
struct Local {}
|
||||
|
||||
#[init(local = [wait_queue: WaitQueue = WaitQueue::new()])]
|
||||
fn init(cx: init::Context) -> (Shared, Local) {
|
||||
Mono::start(cx.core.SYST, 12_000_000);
|
||||
|
||||
incrementer::spawn(cx.local.wait_queue).ok().unwrap();
|
||||
waiter::spawn(cx.local.wait_queue).ok().unwrap();
|
||||
|
||||
let count = 0;
|
||||
|
||||
(Shared { count }, Local {})
|
||||
}
|
||||
|
||||
#[task(shared = [count])]
|
||||
async fn incrementer(mut c: incrementer::Context, wait_queue: &'static WaitQueue) {
|
||||
loop {
|
||||
hprintln!("Inc");
|
||||
|
||||
c.shared.count.lock(|c| *c += 1);
|
||||
|
||||
while let Some(waker) = wait_queue.pop() {
|
||||
waker.wake();
|
||||
}
|
||||
|
||||
Mono::delay(10.millis()).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[task(shared = [count])]
|
||||
async fn waiter(mut c: waiter::Context, wait_queue: &'static WaitQueue) {
|
||||
let value = wait_queue
|
||||
.wait_until(|| c.shared.count.lock(|c| (*c >= 3).then_some(*c)))
|
||||
.await;
|
||||
|
||||
hprintln!("Got {}", value);
|
||||
|
||||
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue