use std::{
future::Future,
pin::Pin,
sync::atomic::Ordering,
sync::Arc,
task::{Context, Poll}
};
use super::Shared;
pub struct KillTrig(pub(super) Arc<Shared>);
impl KillTrig {
pub fn trigger(&self) {
self.0.triggered.store(true, Ordering::SeqCst);
let mut state = self.0.state.lock();
for (_, waker) in state.waiting.drain() {
waker.wake();
}
}
}
impl Future for KillTrig {
type Output = ();
fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
let mut state = self.0.state.lock();
if Arc::<super::Shared>::strong_count(&self.0) == 1 {
Poll::Ready(())
} else {
state.waker = Some(ctx.waker().clone());
Poll::Pending
}
}
}