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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/// Multithreaded notifier
pub mod sync {
super::impl_notify!(sync);
impl crate::AssertMt for AsyncFlag {}
impl crate::AssertMt for AsyncFlagHandle {}
}
/// Singlethreaded notifier
pub mod unsync {
super::impl_notify!(unsync);
}
macro_rules! impl_notify {
($sync:ident) => {
use std::{
pin::Pin,
task::{Context, Poll},
};
use crate::$sync::{flag::Flag, shared::Shared, waker_slot::WakerSlot};
#[derive(Debug)]
struct Inner {
waker: WakerSlot,
set: Flag,
}
#[derive(Debug, Clone)]
struct AsyncFlagImpl(Shared<Inner>);
impl AsyncFlagImpl {
pub fn new() -> Self {
Self(Shared::new(Inner {
waker: WakerSlot::new(),
set: Flag::new(false),
}))
}
pub fn notify(&self) {
self.0.set.swap(true);
self.0.waker.wake();
}
pub fn notified(&self) -> bool {
self.0.set.get()
}
}
impl Future for AsyncFlagImpl {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
// quick check to avoid registration if already done.
if self.0.set.get() {
return Poll::Ready(());
}
self.0.waker.register(cx.waker());
// Need to check condition **after** `register` to avoid a race
// condition that would result in lost notifications.
if self.0.set.get() {
Poll::Ready(())
} else {
Poll::Pending
}
}
}
/// An event that won't wake until [`AsyncFlagHandle::notify`] is called
/// successfully.
#[derive(Debug)]
pub struct AsyncFlag {
flag: AsyncFlagImpl,
}
impl Default for AsyncFlag {
fn default() -> Self {
Self::new()
}
}
impl AsyncFlag {
/// Create [`AsyncFlag`].
pub fn new() -> Self {
Self {
flag: AsyncFlagImpl::new(),
}
}
/// Get a handle to notify the flag.
pub fn handle(&self) -> AsyncFlagHandle {
AsyncFlagHandle::new(self.flag.clone())
}
/// Returns whether the event has been notified.
pub fn notified(&self) -> bool {
self.flag.notified()
}
/// Wait for [`AsyncFlagHandle::notify`] to be called.
pub async fn wait(self) {
self.flag.await
}
}
/// A wake up handle to [`AsyncFlag`].
#[derive(Debug, Clone)]
pub struct AsyncFlagHandle {
flag: AsyncFlagImpl,
}
impl AsyncFlagHandle {
fn new(flag: AsyncFlagImpl) -> Self {
Self { flag }
}
/// Notify the event.
pub fn notify(self) {
self.flag.notify()
}
}
};
}
use impl_notify;