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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Runs `!Send` futures on the current thread.
use crate::fiber::inner::{self as fiber, queue::MpscQueues, JoinHandle, Schedule, Fiber};
use crate::fiber::runtime::RuntimeInner;
use crate::krse::task::AtomicWaker;
use std::cell::Cell;
use std::future::Future;
use std::pin::Pin;
use std::ptr::{self, NonNull};
use std::rc::Rc;
use std::task::{Context, Poll};
use pin_project_lite::pin_project;
#[derive(Debug)]
pub struct LocalSet {
scheduler: Rc<Scheduler>,
}
#[derive(Debug)]
struct Scheduler {
tick: Cell<u8>,
queues: MpscQueues<Self>,
/// Used to notify the `LocalFuture` when a task in the local task set is
/// notified.
waker: AtomicWaker,
}
pin_project! {
struct LocalFuture<F> {
scheduler: Rc<Scheduler>,
#[pin]
future: F,
}
}
thread_local! {
static CURRENT_TASK_SET: Cell<Option<NonNull<Scheduler>>> = Cell::new(None);
}
pub fn spawn_local<F>(future: F) -> JoinHandle<F::Output>
where
F: Future + 'static,
F::Output: 'static,
{
CURRENT_TASK_SET.with(|current| {
let current = current
.get()
.expect("`spawn_local` called from outside of a task::LocalSet!");
let (task, handle) = fiber::joinable_local(future);
unsafe {
// safety: this function is unsafe to call outside of the local
// thread. Since the call above to get the current task set
// would not succeed if we were outside of a local set, this is
// safe.
current.as_ref().queues.push_local(task);
}
handle
})
}
/// Max number of tasks to poll per tick.
const MAX_TASKS_PER_TICK: usize = 61;
impl LocalSet {
/// Returns a new local task set.
pub fn new() -> Self {
Self {
scheduler: Rc::new(Scheduler::new()),
}
}
pub fn spawn_local<F>(&self, future: F) -> JoinHandle<F::Output>
where
F: Future + 'static,
F::Output: 'static,
{
let (task, handle) = fiber::joinable_local(future);
unsafe {
// safety: since `LocalSet` is not Send or Sync, this is
// always being called from the local thread.
self.scheduler.queues.push_local(task);
}
handle
}
pub(crate) fn block_on<F>(&self, rt: &mut RuntimeInner, future: F) -> F::Output
where
F: Future,
{
let scheduler = self.scheduler.clone();
self.scheduler
.with(move || rt.block_on(LocalFuture { scheduler, future }))
}
}
impl Default for LocalSet {
fn default() -> Self {
Self::new()
}
}
impl<F: Future> Future for LocalFuture<F> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let scheduler = this.scheduler;
let mut future = this.future;
scheduler.waker.register_by_ref(cx.waker());
if let Poll::Ready(output) = future.as_mut().poll(cx) {
return Poll::Ready(output);
}
if scheduler.tick() {
// If `tick` returns true, we need to notify the local future again:
// there are still tasks remaining in the run queue.
cx.waker().wake_by_ref();
}
Poll::Pending
}
}
// === impl Scheduler ===
impl Schedule for Scheduler {
fn bind(&self, task: &Fiber<Self>) {
assert!(self.is_current());
unsafe {
self.queues.add_task(task);
}
}
fn release(&self, task: Fiber<Self>) {
// This will be called when dropping the local runtime.
self.queues.release_remote(task);
}
fn release_local(&self, task: &Fiber<Self>) {
debug_assert!(self.is_current());
unsafe {
self.queues.release_local(task);
}
}
fn schedule(&self, task: Fiber<Self>) {
if self.is_current() {
unsafe { self.queues.push_local(task) };
} else {
let mut lock = self.queues.remote();
lock.schedule(task, false);
self.waker.wake();
drop(lock);
}
}
}
impl Scheduler {
fn new() -> Self {
Self {
tick: Cell::new(0),
queues: MpscQueues::new(),
waker: AtomicWaker::new(),
}
}
fn with<F>(&self, f: impl FnOnce() -> F) -> F {
struct Entered<'a> {
current: &'a Cell<Option<NonNull<Scheduler>>>,
}
impl<'a> Drop for Entered<'a> {
fn drop(&mut self) {
self.current.set(None);
}
}
CURRENT_TASK_SET.with(|current| {
let prev = current.replace(Some(NonNull::from(self)));
assert!(prev.is_none(), "nested call to local::Scheduler::with");
let _entered = Entered { current };
f()
})
}
fn is_current(&self) -> bool {
CURRENT_TASK_SET
.try_with(|current| {
current
.get()
.iter()
.any(|current| ptr::eq(current.as_ptr(), self as *const _))
})
.unwrap_or(false)
}
/// Tick the scheduler, returning whether the local future needs to be
/// notified again.
fn tick(&self) -> bool {
assert!(self.is_current());
for _ in 0..MAX_TASKS_PER_TICK {
let tick = self.tick.get().wrapping_add(1);
self.tick.set(tick);
let task = match unsafe {
// safety: we must be on the local thread to call this. The assertion
// the top of this method ensures that `tick` is only called locally.
self.queues.next_task(tick)
} {
Some(task) => task,
// We have fully drained the queue of notified tasks, so the
// local future doesn't need to be notified again — it can wait
// until something else wakes a task in the local set.
None => return false,
};
if let Some(task) = task.run(&mut || Some(self.into())) {
unsafe {
// safety: we must be on the local thread to call this. The
// the top of this method ensures that `tick` is only called locally.
self.queues.push_local(task);
}
}
}
true
}
}
impl Drop for Scheduler {
fn drop(&mut self) {
unsafe {
// safety: these functions are unsafe to call outside of the local
// thread. Since the `Scheduler` type is not `Send` or `Sync`, we
// know it will be dropped only from the local thread.
self.queues.shutdown();
// Wait until all tasks have been released.
// XXX: this is a busy loop, but we don't really have any way to park
// the thread here?
loop {
self.queues.drain_pending_drop();
self.queues.drain_queues();
if !self.queues.has_tasks_remaining() {
break;
}
std::thread::yield_now();
}
}
}
}