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
use super::{Continue, Effect, Effectful, Poll};
use std::cell::Cell;
use std::fmt;
use std::pin::Pin;
use std::sync::Arc;
use crossbeam_channel::{bounded, Receiver, Sender};
thread_local! {
static TLS_CX: Cell<Option<&'static Context>> = Cell::new(None);
}
#[derive(Clone)]
pub struct Context {
notify: Arc<dyn Notify>,
}
impl Context {
pub fn from_notify(notify: Arc<dyn Notify>) -> Self {
Context { notify }
}
pub fn notify(&self) -> &Arc<dyn Notify> {
&self.notify
}
}
pub trait Notify: Send + Sync {
fn wake(&self);
}
pub struct Waker<E: Effect> {
sender: Sender<E::Output>,
notify: Arc<dyn Notify>,
}
impl<E: Effect> fmt::Debug for Waker<E>
where
E::Output: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Waker")
.field("sender", &self.sender)
.field("notify", &"<...>")
.finish()
}
}
impl<E: Effect> Clone for Waker<E> {
fn clone(&self) -> Self {
Self {
sender: self.sender.clone(),
notify: Arc::clone(&self.notify),
}
}
}
impl<E: Effect> Waker<E> {
fn new(sender: Sender<E::Output>, notify: Arc<dyn Notify>) -> Self {
Self { sender, notify }
}
pub fn wake(&self, v: E::Output) {
if let Ok(()) = self.sender.send(v) {
self.notify.wake();
}
}
pub fn try_wake(&self, v: E::Output) -> Result<(), E::Output> {
match self.sender.try_send(v) {
Ok(()) => {
self.notify.wake();
Ok(())
}
Err(e) => Err(e.into_inner()),
}
}
}
pub fn channel<E: Effect>(cx: &Context) -> (TypedContext<E>, Receiver<E::Output>) {
let (sender, receiver) = bounded(1);
let waker = Waker::new(sender, Arc::clone(&cx.notify));
(TypedContext(waker), receiver)
}
pub struct TypedContext<E: Effect>(Waker<E>);
impl<E: Effect> fmt::Debug for TypedContext<E>
where
E::Output: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("TypedContext").field(&self.0).finish()
}
}
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()
}
}
struct SetContextOnDrop(Option<&'static Context>);
impl Drop for SetContextOnDrop {
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_cx = SetContextOnDrop(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")
}
pub fn poll_with_task_context<C: Effectful>(comp: Pin<&mut C>) -> Poll<C::Output, C::Effect> {
comp.poll(get_task_context())
}