| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use std::mem; |
| 6 |
|
| 7 |
use super::{RustFutureContinuationCallback, RustFuturePoll}; |
| 8 |
|
| 9 |
/// Schedules a [crate::RustFuture] by managing the continuation data |
| 10 |
/// |
| 11 |
/// This struct manages the continuation callback and data that comes from the foreign side. It |
| 12 |
/// is responsible for calling the continuation callback when the future is ready to be woken up. |
| 13 |
/// |
| 14 |
/// The basic guarantees are: |
| 15 |
/// |
| 16 |
/// * Each callback will be invoked exactly once, with its associated data. |
| 17 |
/// * If `wake()` is called, the callback will be invoked to wake up the future -- either |
| 18 |
/// immediately or the next time we get a callback. |
| 19 |
/// * If `cancel()` is called, the same will happen and the schedule will stay in the cancelled |
| 20 |
/// state, invoking any future callbacks as soon as they're stored. |
| 21 |
|
| 22 |
#[derive(Debug)] |
| 23 |
pub(super) enum Scheduler { |
| 24 |
/// No continuations set, neither wake() nor cancel() called. |
| 25 |
Empty, |
| 26 |
/// `wake()` was called when there was no continuation set. The next time `store` is called, |
| 27 |
/// the continuation should be immediately invoked with `RustFuturePoll::MaybeReady` |
| 28 |
Waked, |
| 29 |
/// The future has been cancelled, any future `store` calls should immediately result in the |
| 30 |
/// continuation being called with `RustFuturePoll::Ready`. |
| 31 |
Cancelled, |
| 32 |
/// Continuation set, the next time `wake()` is called is called, we should invoke it. |
| 33 |
Set(RustFutureContinuationCallback, u64), |
| 34 |
} |
| 35 |
|
| 36 |
impl Scheduler { |
| 37 |
pub(super) fn new() -> Self { |
| 38 |
Self::Empty |
| 39 |
} |
| 40 |
|
| 41 |
/// Store new continuation data if we are in the `Empty` state. If we are in the `Waked` or |
| 42 |
/// `Cancelled` state, call the continuation immediately with the data. |
| 43 |
pub(super) fn store(&mut self, callback: RustFutureContinuationCallback, data: u64) { |
| 44 |
match self { |
| 45 |
Self::Empty => *self = Self::Set(callback, data), |
| 46 |
Self::Set(old_callback, old_data) => { |
| 47 |
trace!( |
| 48 |
"store: observed `Self::Set` state. Is poll() being called from multiple threads at once?" |
| 49 |
); |
| 50 |
old_callback(*old_data, RustFuturePoll::Ready); |
| 51 |
*self = Self::Set(callback, data); |
| 52 |
} |
| 53 |
Self::Waked => { |
| 54 |
*self = Self::Empty; |
| 55 |
callback(data, RustFuturePoll::MaybeReady); |
| 56 |
} |
| 57 |
Self::Cancelled => { |
| 58 |
callback(data, RustFuturePoll::Ready); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
pub(super) fn wake(&mut self) { |
| 64 |
match self { |
| 65 |
// If we had a continuation set, then call it and transition to the `Empty` state. |
| 66 |
Self::Set(callback, old_data) => { |
| 67 |
let old_data = *old_data; |
| 68 |
let callback = *callback; |
| 69 |
*self = Self::Empty; |
| 70 |
callback(old_data, RustFuturePoll::MaybeReady); |
| 71 |
} |
| 72 |
// If we were in the `Empty` state, then transition to `Waked`. The next time `store` |
| 73 |
// is called, we will immediately call the continuation. |
| 74 |
Self::Empty => *self = Self::Waked, |
| 75 |
// This is a no-op if we were in the `Cancelled` or `Waked` state. |
| 76 |
_ => (), |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
pub(super) fn cancel(&mut self) { |
| 81 |
if let Self::Set(callback, old_data) = mem::replace(self, Self::Cancelled) { |
| 82 |
callback(old_data, RustFuturePoll::Ready); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
pub(super) fn is_cancelled(&self) -> bool { |
| 87 |
matches!(self, Self::Cancelled) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// The `*const ()` data pointer references an object on the foreign side. |
| 92 |
// This object must be `Sync` in Rust terminology -- it must be safe for us to pass the pointer to the continuation callback from any thread. |
| 93 |
// If the foreign side upholds their side of the contract, then `Scheduler` is Send + Sync. |
| 94 |
|
| 95 |
unsafe impl Send for Scheduler {} |
| 96 |
unsafe impl Sync for Scheduler {} |