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
128
129
130
131
132
133
134
135
136
137
138
139
140
pub use core::task::*;
#[cfg(target_has_atomic = "ptr")]
pub use self::if_arc::*;
#[cfg(target_has_atomic = "ptr")]
mod if_arc {
use super::*;
use arc::Arc;
use core::marker::PhantomData;
use core::mem;
use core::ptr::{self, NonNull};
pub trait Wake: Send + Sync {
fn wake(arc_self: &Arc<Self>);
#[inline]
unsafe fn wake_local(arc_self: &Arc<Self>) {
Self::wake(arc_self);
}
}
#[cfg(target_has_atomic = "ptr")]
struct ArcWrapped<T>(PhantomData<T>);
unsafe impl<T: Wake + 'static> UnsafeWake for ArcWrapped<T> {
#[inline]
unsafe fn clone_raw(&self) -> Waker {
let me: *const ArcWrapped<T> = self;
let arc = (*(&me as *const *const ArcWrapped<T> as *const Arc<T>)).clone();
Waker::from(arc)
}
#[inline]
unsafe fn drop_raw(&self) {
let mut me: *const ArcWrapped<T> = self;
let me = &mut me as *mut *const ArcWrapped<T> as *mut Arc<T>;
ptr::drop_in_place(me);
}
#[inline]
unsafe fn wake(&self) {
let me: *const ArcWrapped<T> = self;
T::wake(&*(&me as *const *const ArcWrapped<T> as *const Arc<T>))
}
#[inline]
unsafe fn wake_local(&self) {
let me: *const ArcWrapped<T> = self;
T::wake_local(&*(&me as *const *const ArcWrapped<T> as *const Arc<T>))
}
}
impl<T> From<Arc<T>> for Waker
where T: Wake + 'static,
{
fn from(rc: Arc<T>) -> Self {
unsafe {
let ptr = mem::transmute::<Arc<T>, NonNull<ArcWrapped<T>>>(rc);
Waker::new(ptr)
}
}
}
#[inline]
pub unsafe fn local_waker<W: Wake + 'static>(wake: Arc<W>) -> LocalWaker {
let ptr = mem::transmute::<Arc<W>, NonNull<ArcWrapped<W>>>(wake);
LocalWaker::new(ptr)
}
struct NonLocalAsLocal<T>(ArcWrapped<T>);
unsafe impl<T: Wake + 'static> UnsafeWake for NonLocalAsLocal<T> {
#[inline]
unsafe fn clone_raw(&self) -> Waker {
self.0.clone_raw()
}
#[inline]
unsafe fn drop_raw(&self) {
self.0.drop_raw()
}
#[inline]
unsafe fn wake(&self) {
self.0.wake()
}
#[inline]
unsafe fn wake_local(&self) {
self.0.wake()
}
}
#[inline]
pub fn local_waker_from_nonlocal<W: Wake + 'static>(wake: Arc<W>) -> LocalWaker {
unsafe {
let ptr = mem::transmute::<Arc<W>, NonNull<NonLocalAsLocal<W>>>(wake);
LocalWaker::new(ptr)
}
}
}