pub enum Signal {
Open(Receiver),
Closed(i32),
}Expand description
A one-time broadcast that can be awaited by many tasks. It is often used for coordinating shutdown across many tasks.
Each Signal tracks its lifecycle to enable proper shutdown coordination.
To minimize overhead, it is recommended to wait on a reference to it
(i.e. &mut signal) in loops rather than creating multiple Signals.
§Example
§Basic Usage
use commonware_runtime::{Spawner, Runner, deterministic, signal::Signaler};
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Setup signaler and get future
let (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).
Note: Polling the same Signal after it has resolved will always panic.
When waiting on a reference to a Signal, ensure it is either fused
or not polled again after it has yielded a result.
use commonware_macros::select;
use commonware_runtime::{Clock, Spawner, Runner, deterministic, Metrics, signal::Signaler};
use futures::channel::oneshot;
use std::time::Duration;
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Setup signaler and get future
let (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");
});Variants§
Open(Receiver)
A signal that will resolve when the signaler marks it as resolved.
Closed(i32)
A signal that has been resolved with a known value.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Signal
impl !RefUnwindSafe for Signal
impl Send for Signal
impl Sync for Signal
impl Unpin for Signal
impl !UnwindSafe for Signal
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
Source§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f. Read moreSource§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
Source§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll will never again be called once it has
completed. This method can be used to turn any Future into a
FusedFuture. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
Source§fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
() on completion and sends
its output to another future on a separate task. Read moreSource§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
Source§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
Source§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = ()>.Source§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = Never>.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>
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>
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