1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![no_std]
use async_ach_waker::{WakerPool, WakerToken};
use core::future::Future;
use core::pin::Pin;
use core::sync::atomic::{AtomicUsize, Ordering::Relaxed};
use core::task::{Context, Poll};
pub struct Notify<const W: usize> {
version: AtomicUsize,
wakers: WakerPool<W>,
}
impl<const W: usize> Notify<W> {
pub const fn new() -> Self {
Self {
version: AtomicUsize::new(0),
wakers: WakerPool::new(),
}
}
pub fn notify_waiters(&self) {
self.version.fetch_add(1, Relaxed);
self.wakers.wake();
}
pub fn notified(&self) -> Notified<'_, W> {
Notified {
parent: self,
version: self.version.load(Relaxed),
token: None,
}
}
}
pub struct Notified<'a, const W: usize> {
parent: &'a Notify<W>,
version: usize,
token: Option<WakerToken<'a, W>>,
}
impl<'a, const W: usize> Future for Notified<'a, W> {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let new_version = self.parent.version.load(Relaxed);
if self.version < new_version {
self.version = new_version;
Poll::Ready(())
} else {
let waker = cx.waker();
if let Some(token) = &self.token {
token.swap(waker);
} else {
if let Ok(token) = self.parent.wakers.register(waker) {
self.token = Some(token);
} else {
waker.wake_by_ref();
}
}
Poll::Pending
}
}
}