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
use std::thread;
use futures::task::{self, Task};
use futures::sync::BiLock;
use futures::{Async, Future};
use void::Void;
struct Inner {
dropped: bool,
task_rx: Option<Task>,
}
pub struct DropNotify {
inner: BiLock<Inner>,
}
pub struct DropNotice {
inner: BiLock<Inner>,
}
pub fn drop_notify() -> (DropNotify, DropNotice) {
let inner = Inner {
dropped: false,
task_rx: None,
};
let (lock_notify, lock_notice) = BiLock::new(inner);
let drop_notify = DropNotify {
inner: lock_notify,
};
let drop_notice = DropNotice {
inner: lock_notice,
};
(drop_notify, drop_notice)
}
impl Future for DropNotice {
type Item = ();
type Error = Void;
fn poll(&mut self) -> Result<Async<()>, Void> {
if let Async::Ready(ref mut inner) = self.inner.poll_lock() {
if inner.dropped {
return Ok(Async::Ready(()));
}
inner.task_rx = Some(task::current());
Ok(Async::NotReady)
} else {
Ok(Async::Ready(()))
}
}
}
impl Drop for DropNotify {
fn drop(&mut self) {
loop {
if let Async::Ready(ref mut inner) = self.inner.poll_lock() {
inner.dropped = true;
if let Some(task) = inner.task_rx.take() {
task.notify();
}
return;
}
thread::yield_now();
}
}
}