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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
use super::job_queue::*;
use super::queue_state::*;
use super::core::*;
use super::active_queue::*;
use super::wake_queue::*;
use super::desync_scheduler::*;
use futures::prelude::*;
use futures::channel::oneshot;
use futures::task;
use std::mem;
use std::sync::*;
use std::pin::{Pin};
///
/// The possible states of a future result
///
enum FutureResultState<T> {
None,
Some(T),
ReturnedViaFuture
}
///
/// The possible states of a waker that will wake up a queue that's being drained as part of polling a future
///
enum DrainWakerState {
/// The drain waker has never been woken before
NotWoken,
/// This has been woken but the waker is not set
Woken,
/// This has been woken and the waker is set
WillWakeWithWaker(task::Waker)
}
///
/// Waker that will wake up a queue being drained. We set the waker with a delay so it's possible to switch to
/// a waker that will wake a future on a background queue if necessary.
///
struct DrainWaker {
state: Mutex<DrainWakerState>
}
impl DrainWaker {
///
/// Creates a new drain waker
///
fn new() -> DrainWaker {
DrainWaker {
state: Mutex::new(DrainWakerState::NotWoken)
}
}
///
/// Sets the waker to be called when this drain waker is woken
///
fn wake_with(&self, new_waker: task::Waker) {
use self::DrainWakerState::*;
// Update the state and determine if we need to invoke the waker immediately (if it's been woken before the waker was set)
let to_wake = {
// Fetch the current state
let mut new_state = self.state.lock().expect("Drain waker state");
let mut state = Woken;
mem::swap(&mut *new_state, &mut state);
// Update the state based on this action
match state {
Woken => { *new_state = Woken; Some(new_waker) },
NotWoken => { *new_state = WillWakeWithWaker(new_waker); None },
WillWakeWithWaker(_old_waker) => { *new_state = WillWakeWithWaker(new_waker); None }
}
};
// Wake up the waker, if we need to call it immediately
to_wake.map(|to_wake| to_wake.wake());
}
}
impl task::ArcWake for DrainWaker {
fn wake_by_ref(arc_self: &Arc<Self>) {
use self::DrainWakerState::*;
// If the current state contains a waker, we'll call it once we've unlocked the mutex
let to_wake = {
// Fetch the current state
let mut new_state = arc_self.state.lock().expect("Drain waker state");
let mut state = Woken;
mem::swap(&mut *new_state, &mut state);
// Update the state based on this action
match state {
NotWoken => { *new_state = Woken; None },
Woken => { *new_state = Woken; None },
WillWakeWithWaker(waker) => { *new_state = Woken; Some(waker) }
}
};
// Wake up the waker
to_wake.map(|to_wake| to_wake.wake());
}
}
///
/// Waker that wakes up two different wakers at once
///
struct DoubleWaker(Mutex<Option<(task::Waker, task::Waker)>>);
impl task::ArcWake for DoubleWaker {
fn wake_by_ref(arc_self: &Arc<Self>) {
let to_wake = arc_self.0.lock().unwrap().take();
if let Some((waker1, waker2)) = to_wake {
waker1.wake();
waker2.wake();
}
}
}
///
/// Signalling structure used to return the result of a scheduler future
///
struct SchedulerFutureResult<T> {
/// The result of the future, or None if it has not been generated yet
result: FutureResultState<Result<T, oneshot::Canceled>>,
/// The waker to be called when the future is available
waker: Option<task::Waker>
}
///
/// Wrapper that cancels the future if it is dropped before it is signalled
///
pub (super) struct SchedulerFutureSignaller<T>(Arc<Mutex<SchedulerFutureResult<T>>>);
///
/// Future representing a task pending on a scheduler
///
/// If polled when no threads are available, this future will run synchronously on the current thread
/// (stealing its execution time rather than blocking)
///
pub struct SchedulerFuture<T: Send> {
/// The unique ID of this future
id: FutureId,
/// The queue which will eventually evaluate the result of this future
queue: Arc<JobQueue>,
/// The scheduler core that this future belongs to
scheduler: Scheduler,
/// Set to true if this future has claimed ownership of the queue to drain it
draining: bool,
/// A container for the result of this scheduler future
result: Arc<Mutex<SchedulerFutureResult<T>>>
}
impl<T> FutureResultState<T> {
///
/// Returns true if no result has ever been generated for this future
///
fn is_none(&self) -> bool {
match self {
FutureResultState::None => true,
FutureResultState::Some(_) => false,
FutureResultState::ReturnedViaFuture => false
}
}
///
/// If a result has been generated by this future and has not previously been taken, returns it. Panics if the result has already
/// been returned and is no longer available.
///
fn take(&mut self) -> Option<T> {
// Move the value out of this object
let mut new_value = FutureResultState::ReturnedViaFuture;
mem::swap(self, &mut new_value);
// Return it if it contains a value
match new_value {
FutureResultState::None => { *self = FutureResultState::None; None },
FutureResultState::ReturnedViaFuture => { *self = FutureResultState::ReturnedViaFuture; panic!("Future result has already been returned") },
FutureResultState::Some(value) => { Some(value) }
}
}
}
impl<T> Drop for SchedulerFutureSignaller<T> {
fn drop(&mut self) {
let waker = {
let mut future_result = self.0.lock().expect("Scheduler future result");
// If no result has been generated, then mark the future as canceled
if future_result.result.is_none() {
// Mark the future as canceled
future_result.result = FutureResultState::Some(Err(oneshot::Canceled));
// Wake up anything that was polling it
let waker = future_result.waker.take();
waker
} else {
// Result is already set, so don't wake anything up
None
}
};
// If we need to wake the future, then do so here (note that we're outside of the lock when we do this)
waker.map(|waker| waker.wake());
}
}
impl<T> SchedulerFutureSignaller<T> {
///
/// Signals that the result of the calculation is available
///
pub (super) fn signal(self, result: T) {
let waker = {
let mut future_result = self.0.lock().expect("Scheduler future result");
// Set the result
future_result.result = FutureResultState::Some(Ok(result));
// Retrieve the waker
future_result.waker.take()
};
// If we retrieved a waker from the result, wake it up
waker.map(|waker| waker.wake());
}
}
///
/// Possible actions we can take based on the state of the signal for the future
///
enum SchedulerAction<T> {
/// Queue is running elsewhere: we should wait for it to reach the point where the future's result is available
WaitForCompletion,
/// Future has already completed: we should return the value to sender
ReturnValue(T),
/// We've claimed the 'running' state of the queue and should drain it
DrainQueue,
/// The queue has panicked
Panic
}
impl<T: Send> SchedulerFuture<T> {
///
/// Creates a new scheduler future and the result needed to signal it
///
pub (super) fn new(queue: &Arc<JobQueue>, core: Arc<SchedulerCore>) -> (SchedulerFuture<T>, SchedulerFutureSignaller<T>) {
// Create an unfinished result
let result = SchedulerFutureResult {
result: FutureResultState::None,
waker: None
};
let result = Arc::new(Mutex::new(result));
// Insert into a future
let future = SchedulerFuture {
id: FutureId::new(),
queue: Arc::clone(queue),
scheduler: Scheduler { core },
draining: false,
result: Arc::clone(&result)
};
(future, SchedulerFutureSignaller(result))
}
///
/// Detaches from this future, leaving it to run in the background on the main desync scheduler
///
#[inline]
pub fn detach(self) {
// Nothing to do, this just drops the future
}
///
/// Synchronously waits for this future to be completed by the scheduler, then returns the result
///
pub fn sync(self) -> Result<T, oneshot::Canceled> {
// See if the result has arrived yet
let result = self.result.lock().expect("Scheduler future result").result.take();
if let Some(result) = result {
return result;
}
// Synchronise reading the result with the queue
// TODO: if future tasks have been queued, this will wait for those as well
let result = self.scheduler.sync(&self.queue, || { self.result.lock().expect("Scheduler future result").result.take() });
// The result should now be available
if let Some(result) = result {
return result;
} else {
// The future never completed (or the result has been stolen somewhere else somehow)
debug_assert!(false, "Future never completed");
return Err(oneshot::Canceled);
}
}
///
/// We moved the queue into the running state and need to drain it until we've got a result
///
fn drain_queue(&mut self, context: &mut task::Context) -> task::Poll<Result<T, oneshot::Canceled>> {
debug_assert!(self.queue.core.lock().expect("JobQueue core lock").state.is_running());
// Set the queue as active
let _active = ActiveQueue { queue: &*self.queue };
let mut result;
self.draining = true;
// While there is no result, run a job from the queue
loop {
// See if the result has arrived yet
result = self.result.lock().expect("Scheduler future result").result.take();
if !result.is_none() { break; }
// Run the next job in the queue
if let Some(mut job) = self.queue.dequeue() {
// Queue is running
debug_assert!(self.queue.core.lock().expect("Job queue core").state.is_running());
// Create a context to poll in (we may need to reschedule in the background)
let waker = Arc::new(DrainWaker::new());
let waker_ref = task::waker_ref(&waker);
let mut drain_context = task::Context::from_waker(&waker_ref);
// Poll the queue
let poll_result = job.run(&mut drain_context);
match poll_result {
task::Poll::Ready(()) => {
// Keep running jobs and checking the results if ready
},
task::Poll::Pending => {
// Requeue the job
self.queue.requeue(job);
// If the result was supplied, break out of the loop and reschedule the queue
result = self.result.lock().expect("Scheduler future result").result.take();
if result.is_some() {
// Wake the queue in the background if needed (the result has arrived)
self.queue.core.lock().expect("JobQueue core lock").state = QueueState::WaitingForWake;
let queue_waker = WakeQueue(Arc::clone(&self.queue), Arc::clone(&self.scheduler.core));
let queue_waker = Arc::new(queue_waker);
let queue_waker = task::waker(queue_waker);
waker.wake_with(queue_waker);
// The future is ready (job will be rescheduled in the background)
self.draining = false;
return task::Poll::Ready(result.unwrap());
} else {
// Wait for the next poll
self.result.lock().expect("Scheduler future result").waker = Some(context.waker().clone());
self.queue.core.lock().expect("JobQueue core lock").state = QueueState::WaitingForPoll(self.id);
// Wake both the queue and the context
let context_waker = context.waker().clone();
let queue_waker = WakeQueue(Arc::clone(&self.queue), Arc::clone(&self.scheduler.core));
let queue_waker = Arc::new(queue_waker);
let queue_waker = task::waker(queue_waker);
let wake_both = DoubleWaker(Mutex::new(Some((queue_waker, context_waker))));
let wake_both = task::waker(Arc::new(wake_both));
waker.wake_with(wake_both);
// Result is pending
self.draining = true;
return task::Poll::Pending;
}
}
}
} else {
// Queue is empty and our result hasn't arrived yet?!
// Assume the future will resolve eventually: move the queue in to the background
self.result.lock().expect("Scheduler future result").waker = Some(context.waker().clone());
// Reschedule the queue
self.queue.core.lock().expect("JobQueue core lock").state = QueueState::Idle;
self.scheduler.core.reschedule_queue(&self.queue, Arc::clone(&self.scheduler.core));
self.draining = false;
return task::Poll::Pending;
}
}
// Reschedule the queue if there are any events left pending
// Note: the queue is already pending when we start running events from it here.
// This means it'll get dequeued by a thread eventually: maybe while it's running
// here. As we've set the queue state to running while we're busy, the thread won't
// start the queue while it's already running.
self.queue.core.lock().expect("JobQueue core lock").state = QueueState::Idle;
self.scheduler.core.reschedule_queue(&self.queue, Arc::clone(&self.scheduler.core));
// Result must be available by this point
self.draining = false;
task::Poll::Ready(result.unwrap())
}
}
impl<T: Send> Drop for SchedulerFuture<T> {
fn drop(&mut self) {
/* -- no need to reschedule manually any more, the queue will wake itself up
// Reschedule the queue in the background if we're draining the queue
if self.draining {
{
// The core should be in the 'waiting for poll' state
let mut core = self.queue.core.lock().expect("JobQueue core lock");
debug_assert!(match core.state { QueueState::WaitingForPoll(_) => true, _ => false });
// Core is now idle
core.state = QueueState::Idle;
}
// Reschedule the queue
self.scheduler.core.reschedule_queue(&self.queue, Arc::clone(&self.scheduler.core));
}
*/
}
}
impl<T: Send> Future for SchedulerFuture<T> {
type Output = Result<T, oneshot::Canceled>;
///
/// Polls this future
///
fn poll(mut self: Pin<&mut Self>, context: &mut task::Context) -> task::Poll<Self::Output> {
// Lock the result and determine which action to take
let next_action = {
let mut future_result = self.result.lock().expect("Scheduler future result");
if let Some(result) = future_result.result.take() {
// The result is available: we should return it immediately
SchedulerAction::ReturnValue(result)
} else {
// If the queue is idle when this is called, we need to schedule this task on this thread rather than one owned by the background process
let run_action = {
let mut core = self.queue.core.lock().expect("JobQueue core lock");
match core.state {
QueueState::Running => SchedulerAction::WaitForCompletion,
QueueState::WaitingForWake => SchedulerAction::WaitForCompletion,
QueueState::WaitingForUnpark => SchedulerAction::WaitForCompletion,
QueueState::AwokenWhileRunning => SchedulerAction::WaitForCompletion,
QueueState::Panicked => SchedulerAction::Panic,
QueueState::Pending => { core.state = QueueState::Running; SchedulerAction::DrainQueue },
QueueState::Idle => { core.state = QueueState::Running; SchedulerAction::DrainQueue }
QueueState::WaitingForPoll(owner_id) => {
if owner_id == self.id {
// Continue polling on this future
core.state = QueueState::Running; SchedulerAction::DrainQueue
} else {
// Wait for the owning future to complete
SchedulerAction::WaitForCompletion
}
},
}
};
// Wake up the calling context when the result becomes available
match &run_action {
SchedulerAction::DrainQueue => { }
SchedulerAction::WaitForCompletion |
SchedulerAction::ReturnValue(_) |
SchedulerAction::Panic => { future_result.waker = Some(context.waker().clone()); }
}
run_action
}
};
match next_action {
SchedulerAction::WaitForCompletion => task::Poll::Pending,
SchedulerAction::ReturnValue(value) => task::Poll::Ready(value),
SchedulerAction::DrainQueue => self.drain_queue(context),
SchedulerAction::Panic => panic!("Cannot schedule jobs on a panicked queue"),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn returns_immediately_if_signaled() {
use futures::executor;
let scheduler = scheduler();
let queue = queue();
let (future, signaller) = SchedulerFuture::new(&queue, Arc::clone(&scheduler.core));
signaller.signal(42);
assert!(executor::block_on(future) == Ok(42));
}
#[test]
fn cancels_when_dropped() {
use futures::executor;
let scheduler = scheduler();
let queue = queue();
let future = { SchedulerFuture::<i32>::new(&queue, Arc::clone(&scheduler.core)).0 };
assert!(executor::block_on(future) == Err(oneshot::Canceled));
}
#[test]
fn signals_from_another_thread() {
use futures::executor;
use std::thread;
use std::time::Duration;
let scheduler = scheduler();
let queue = queue();
let (future, signaller) = SchedulerFuture::new(&queue, Arc::clone(&scheduler.core));
thread::spawn(move || {
thread::sleep(Duration::from_millis(100));
signaller.signal(42);
});
assert!(executor::block_on(future) == Ok(42));
}
#[test]
fn forces_queue_drain() {
use futures::executor;
use std::thread;
use std::time::Duration;
let scheduler = scheduler();
let queue = queue();
scheduler.set_max_threads(0);
scheduler.despawn_threads_if_overloaded();
let (future, signaller) = SchedulerFuture::new(&queue, Arc::clone(&scheduler.core));
scheduler.desync(&queue, move || {
thread::sleep(Duration::from_millis(100));
signaller.signal(42);
});
assert!(executor::block_on(future) == Ok(42));
}
}