From 6a68e8e54d2b006a888edab0f75e99a04675bb0a Mon Sep 17 00:00:00 2001 From: Oleksandr Babak Date: Thu, 15 May 2025 14:52:26 +0200 Subject: [PATCH] feat: example of wait-queue --- ci/expected/lm3s6965/wait-queue.run | 4 ++ examples/lm3s6965/Cargo.toml | 1 + examples/lm3s6965/examples/wait-queue.rs | 64 ++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 ci/expected/lm3s6965/wait-queue.run create mode 100644 examples/lm3s6965/examples/wait-queue.rs diff --git a/ci/expected/lm3s6965/wait-queue.run b/ci/expected/lm3s6965/wait-queue.run new file mode 100644 index 00000000000..ed8f8f09f7d --- /dev/null +++ b/ci/expected/lm3s6965/wait-queue.run @@ -0,0 +1,4 @@ +Inc +Inc +Inc +Got 3 diff --git a/examples/lm3s6965/Cargo.toml b/examples/lm3s6965/Cargo.toml index 5e2cc5034de..78ae168e119 100644 --- a/examples/lm3s6965/Cargo.toml +++ b/examples/lm3s6965/Cargo.toml @@ -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" diff --git a/examples/lm3s6965/examples/wait-queue.rs b/examples/lm3s6965/examples/wait-queue.rs new file mode 100644 index 00000000000..7be1c0a89cc --- /dev/null +++ b/examples/lm3s6965/examples/wait-queue.rs @@ -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 + } +}