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};
let executor = deterministic::Runner::default();
executor.start(|context| 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, Metrics};
use futures::channel::oneshot;
use std::time::Duration;
let executor = deterministic::Runner::default();
executor.start(|context| 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.with_label("waiter").spawn(|context| 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
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more