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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use super::{Continue, Effect};
use std::cell::Cell;
use std::intrinsics::type_name;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex};
use std::thread::{self, Thread};
use log::debug;
thread_local! {
static TLS_CX: Cell<Option<&'static Context>> = Cell::new(None);
}
extern "Rust" {
type Whatever;
}
#[derive(Debug)]
pub struct Context {
storage: Arc<Mutex<Option<NonNull<Whatever>>>>,
thread: Thread,
}
unsafe impl Send for Context {}
unsafe impl Sync for Context {}
impl Context {
pub fn current() -> Self {
Context {
storage: Arc::new(Mutex::new(None)),
thread: thread::current(),
}
}
pub fn contains(&self) -> bool {
self.storage.lock().unwrap().is_some()
}
pub fn take<T>(&self) -> Option<T> {
unsafe {
debug!(
"Context::get: {}, from {:?}",
type_name::<T>(),
self.storage
);
self.storage
.lock()
.unwrap()
.take()
.map(|non_null| *(Box::from_raw(non_null.as_ptr() as *mut T)))
}
}
pub fn set<T>(&self, v: T) {
unsafe {
debug!("Context::set: {}", type_name::<T>());
*self.storage.lock().unwrap() = Some(NonNull::new_unchecked(
Box::into_raw(Box::new(v)) as *mut Whatever,
));
}
}
pub fn typed<E: Effect>(&self) -> TypedContext<E> {
let ptr =
Arc::into_raw(Arc::clone(&self.storage)) as *const Mutex<Option<NonNull<E::Output>>>;
unsafe {
TypedContext(Waker {
storage: Arc::from_raw(ptr),
thread: self.thread.clone(),
})
}
}
}
pub struct Waker<E: Effect> {
storage: Arc<Mutex<Option<NonNull<E::Output>>>>,
thread: Thread,
}
impl<E: Effect> Clone for Waker<E> {
fn clone(&self) -> Self {
Self {
storage: Arc::clone(&self.storage),
thread: self.thread.clone(),
}
}
}
impl<E: Effect> Waker<E> {
pub fn wake(&self, v: E::Output) {
unsafe {
debug!(
"Waker::set: {} -> {}, to: {:?}",
type_name::<E>(),
type_name::<E::Output>(),
self.storage,
);
*self.storage.lock().unwrap() = Some(NonNull::new_unchecked(
Box::into_raw(Box::new(v)) as *mut E::Output,
));
}
self.thread.unpark()
}
}
unsafe impl<E> Send for Waker<E>
where
E: Effect,
E::Output: Send,
{
}
unsafe impl<E> Sync for Waker<E>
where
E: Effect,
E::Output: Sync,
{
}
pub struct TypedContext<E: Effect>(Waker<E>);
impl<E: Effect> TypedContext<E> {
pub fn waker(&self) -> Waker<E> {
self.0.clone()
}
pub fn continuation<R>(self) -> Continue<R> {
Continue::new()
}
pub fn resume<R>(self, v: E::Output) -> Continue<R> {
self.0.wake(v);
self.continuation()
}
}
pub fn gen_taker<E>(_e: &E) -> fn(&Context) -> Option<E::Output>
where
E: Effect,
{
Context::take::<E::Output>
}
struct SetOnDrop(Option<&'static Context>);
impl Drop for SetOnDrop {
fn drop(&mut self) {
TLS_CX.with(|tls_cx| {
tls_cx.replace(self.0.take());
})
}
}
pub fn set_task_context<F, R>(cx: &Context, f: F) -> R
where
F: FnOnce() -> R,
{
let old_cx = TLS_CX.with(|tls_cx| {
tls_cx.replace(Some(unsafe {
&*(cx as *const Context) as &'static Context
}))
});
let _reset = SetOnDrop(old_cx);
f()
}
pub fn get_task_context<'a>() -> &'a Context {
TLS_CX
.with(|tls_cx| tls_cx.get().take())
.expect("thread local context must be set")
}