-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathdrop.rs
More file actions
90 lines (73 loc) · 1.93 KB
/
drop.rs
File metadata and controls
90 lines (73 loc) · 1.93 KB
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
use std::{
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
thread::{self, ThreadId},
};
use futures_util::task::AtomicWaker;
struct DropWatcher {
waker: Arc<AtomicWaker>,
thread_id: ThreadId,
}
impl DropWatcher {
fn new(waker: Arc<AtomicWaker>) -> Self {
Self {
waker,
thread_id: thread::current().id(),
}
}
}
impl Future for DropWatcher {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
self.waker.register(cx.waker());
Poll::Pending
}
}
impl Drop for DropWatcher {
fn drop(&mut self) {
if self.thread_id != thread::current().id() {
panic!("DropWatcher dropped on a different thread!!!");
}
}
}
#[test]
fn test_drop_with_timer() {
compio_runtime::Runtime::new().unwrap().block_on(async {
compio_runtime::spawn(async {
loop {
compio_runtime::time::sleep(std::time::Duration::from_secs(1)).await;
}
})
.detach();
})
}
#[test]
fn test_wake_after_runtime_drop() {
let waker = Arc::new(AtomicWaker::new());
let waker_clone = waker.clone();
let rt = compio_runtime::Runtime::new().unwrap();
rt.block_on(async move {
compio_runtime::spawn(DropWatcher::new(waker_clone)).detach();
});
drop(rt);
// Use `unwrap()` to ensure there is a waker stored.
waker.take().unwrap().wake();
}
#[test]
fn test_wake_from_another_thread_after_runtime_drop() {
let waker = Arc::new(AtomicWaker::new());
let waker_clone = waker.clone();
let rt = compio_runtime::Runtime::new().unwrap();
rt.block_on(async move {
compio_runtime::spawn(DropWatcher::new(waker_clone)).detach();
});
drop(rt);
thread::spawn(move || {
// Use `unwrap()` to ensure there is a waker stored.
waker.take().unwrap().wake();
})
.join()
.unwrap();
}