use std::future::Future;
use std::ops::Deref;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::task::{Context, Poll, Waker};
#[derive(Default)]
pub(crate) struct Notify {
fired: AtomicBool,
waiters: Mutex<Waiters>,
}
#[derive(Default)]
struct Waiters {
slots: Vec<Option<Waker>>,
free: Vec<usize>,
}
impl Notify {
pub(crate) fn new() -> Self {
Notify::default()
}
pub(crate) fn fire(&self) {
if self.fired.swap(true, Ordering::AcqRel) {
return;
}
let slots = {
let mut waiters = self.waiters();
waiters.free.clear();
std::mem::take(&mut waiters.slots)
};
for waker in slots.into_iter().flatten() {
waker.wake();
}
}
pub(crate) fn is_fired(&self) -> bool {
self.fired.load(Ordering::Acquire)
}
pub(crate) fn listen(&self) -> Listen<'_> {
Listen { notify: Source::Borrowed(self), slot: None }
}
pub(crate) fn listen_owned(self: &Arc<Self>) -> Listen<'static> {
Listen { notify: Source::Owned(Arc::clone(self)), slot: None }
}
fn waiters(&self) -> MutexGuard<'_, Waiters> {
self.waiters.lock().unwrap_or_else(PoisonError::into_inner)
}
}
pub(crate) struct Listen<'a> {
notify: Source<'a>,
slot: Option<usize>,
}
enum Source<'a> {
Borrowed(&'a Notify),
Owned(Arc<Notify>),
}
impl Deref for Source<'_> {
type Target = Notify;
fn deref(&self) -> &Notify {
match self {
Source::Borrowed(notify) => notify,
Source::Owned(notify) => notify,
}
}
}
impl Future for Listen<'_> {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let this = &mut *self;
if this.notify.is_fired() {
return Poll::Ready(());
}
let mut waiters = this.notify.waiters();
if this.notify.is_fired() {
return Poll::Ready(());
}
match this.slot.filter(|&i| i < waiters.slots.len()) {
Some(i) => {
let slot = &mut waiters.slots[i];
if !slot.as_ref().is_some_and(|waker| waker.will_wake(cx.waker())) {
*slot = Some(cx.waker().clone());
}
}
None => {
let waker = Some(cx.waker().clone());
let index = match waiters.free.pop() {
Some(i) => {
waiters.slots[i] = waker;
i
}
None => {
waiters.slots.push(waker);
waiters.slots.len() - 1
}
};
this.slot = Some(index);
}
}
Poll::Pending
}
}
impl Drop for Listen<'_> {
fn drop(&mut self) {
let Some(index) = self.slot.take() else { return };
let mut waiters = self.notify.waiters();
if self.notify.is_fired() {
return;
}
if let Some(slot) = waiters.slots.get_mut(index) {
*slot = None;
waiters.free.push(index);
}
}
}
pub struct Cancelled<'a> {
inner: Listen<'a>,
}
impl<'a> Cancelled<'a> {
pub(crate) fn new(inner: Listen<'a>) -> Self {
Cancelled { inner }
}
}
impl Future for Cancelled<'_> {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
Pin::new(&mut self.inner).poll(cx)
}
}
pub struct Draining<'a> {
inner: Listen<'a>,
}
impl<'a> Draining<'a> {
pub(crate) fn new(inner: Listen<'a>) -> Self {
Draining { inner }
}
}
impl Future for Draining<'_> {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
Pin::new(&mut self.inner).poll(cx)
}
}