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
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use tokio::sync::Notify;
#[derive(Clone)]
pub struct FutureBool {
notify_true: Arc<Notify>,
notify_false: Arc<Notify>,
inner: Arc<AtomicBool>,
}
impl FutureBool {
pub fn new(val: bool) -> Self {
Self {
notify_true: Arc::new(Notify::new()),
notify_false: Arc::new(Notify::new()),
inner: Arc::new(AtomicBool::new(val)),
}
}
pub fn set(&self) {
self.inner.store(true, Ordering::Release);
self.notify_true.notify_waiters();
}
pub fn unset(&self) {
self.inner.store(false, Ordering::Release);
self.notify_false.notify_waiters();
}
pub async fn wait_change(&self) -> bool {
let val = self.inner.load(Ordering::Acquire);
if val {
self.notify_false.notified().await;
} else {
self.notify_true.notified().await;
}
!val
}
pub async fn wait_true(&self) {
let val = self.inner.load(Ordering::Acquire);
if !val {
self.notify_true.notified().await;
}
}
pub async fn wait_false(&self) {
let val = self.inner.load(Ordering::Acquire);
if val {
self.notify_false.notified().await;
}
}
}