From b1fe9c6955ff857e3729a0bb4727247e050fb7ae Mon Sep 17 00:00:00 2001 From: Gerhard de Clercq <11624490+Gerharddc@users.noreply.github.com> Date: Fri, 12 Dec 2025 13:04:51 +0000 Subject: Add `run_until` function to std Executor as to support grafeul shutdown. --- examples/std/src/bin/tick_cancel.rs | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/std/src/bin/tick_cancel.rs (limited to 'examples') diff --git a/examples/std/src/bin/tick_cancel.rs b/examples/std/src/bin/tick_cancel.rs new file mode 100644 index 000000000..54e44790f --- /dev/null +++ b/examples/std/src/bin/tick_cancel.rs @@ -0,0 +1,47 @@ +use std::sync::atomic::{AtomicBool, Ordering}; +use std::thread; +use std::time::Duration; + +use embassy_executor::Executor; +use embassy_time::Timer; +use log::*; +use static_cell::StaticCell; + +#[embassy_executor::task] +async fn run() { + loop { + info!("tick"); + Timer::after_secs(1).await; + } +} + +static DONE: StaticCell = StaticCell::new(); +static EXECUTOR: StaticCell = StaticCell::new(); + +fn main() { + env_logger::builder() + .filter_level(log::LevelFilter::Debug) + .format_timestamp_nanos() + .init(); + + let done = DONE.init(AtomicBool::new(false)); + let done_cb = || done.load(Ordering::Relaxed); + + let server_thread = thread::spawn(move || { + let executor = EXECUTOR.init(Executor::new()); + executor.run_until( + |spawner| { + spawner.spawn(run().unwrap()); + }, + done_cb, + ); + info!("Executor finished"); + }); + + thread::sleep(Duration::from_secs(5)); + + info!("Cancelling executor"); + done.store(true, Ordering::Relaxed); + + server_thread.join().unwrap(); +} -- cgit