pub struct Signaler { /* private fields */ }
Expand description
Coordinates a one-time signal across many tasks.
§Example
§Basic Usage
use commonware_runtime::{Spawner, Runner, Signaler, deterministic::Executor};
let (executor, _, _) = Executor::default();
executor.start(async move {
// Setup signaler and get future
let (mut signaler, signal) = Signaler::new();
// Signal shutdown
signaler.signal(2);
// Wait for shutdown in task
let sig = signal.await.unwrap();
println!("Received signal: {}", sig);
});
§Advanced Usage
While Futures::Shared
is efficient, there is still meaningful overhead
to cloning it (i.e. in each iteration of a loop). To avoid
a performance regression from introducing Signaler
, it is recommended
to wait on a reference to Signal
(i.e. &mut signal
).
use commonware_macros::select;
use commonware_runtime::{Clock, Spawner, Runner, Signaler, deterministic::Executor};
use futures::channel::oneshot;
use std::time::Duration;
let (executor, context, _) = Executor::default();
executor.start(async move {
// Setup signaler and get future
let (mut signaler, mut signal) = Signaler::new();
// Loop on the signal until resolved
let (tx, rx) = oneshot::channel();
context.spawn("task", {
let context = context.clone();
async move {
loop {
// Wait for signal or sleep
select! {
sig = &mut signal => {
println!("Received signal: {}", sig.unwrap());
break;
},
_ = context.sleep(Duration::from_secs(1)) => {},
};
}
let _ = tx.send(());
}
});
// Send signal
signaler.signal(9);
// Wait for task
rx.await.expect("shutdown signaled");
});
Implementations§
Auto Trait Implementations§
impl Freeze for Signaler
impl !RefUnwindSafe for Signaler
impl Send for Signaler
impl Sync for Signaler
impl Unpin for Signaler
impl !UnwindSafe for Signaler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more