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
use super::executor::with_executor;
use core::{
    future::Future,
    hint::unreachable_unchecked,
    marker::Unpin,
    mem::{align_of, MaybeUninit},
    pin::Pin,
    ptr::{null, NonNull},
    task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};

/// The importance of a task which
/// may be used to influence scheduling.
pub enum Priority {
    Low = 0,
    Normal = 1,
    High = 2,
}

impl Priority {
    const MASK: usize = 3;
}

/// Minimal state used to represent a generic
/// unit of execution used by the executors in the runtime.
pub struct Task {
    next: usize,
    resume: unsafe fn(*mut Task),
}

impl Task {
    /// Create a new task with the given priority and resume function.
    /// The resume function is called when the thread is woken up by the
    /// scheduler internally to start executing its corresponding unit (not limited to futures).
    pub fn new(priority: Priority, resume: unsafe fn(*mut Task)) -> Self {
        assert!(align_of::<Self>() > Priority::MASK);
        let next = priority as usize;
        Self { next, resume }
    }

    /// Get the link provided by the task as it can function as a linked list node.
    pub fn next(&self) -> Option<NonNull<Self>> {
        NonNull::new((self.next & !Priority::MASK) as *mut Self)
    }

    /// Get the next task link having the task act as a linked list node.
    pub fn set_next(&mut self, next: Option<NonNull<Self>>) {
        self.next = (self.next & Priority::MASK)
            | match next {
                Some(ptr) => ptr.as_ptr() as usize,
                None => null::<Self>() as usize,
            };
    }

    /// Modify the runtime priority of the task.
    /// Should only be called before the task is resumed as
    /// the scheduler could also have ownership internally.
    pub fn set_priority(&mut self, priority: Priority) {
        self.next = (self.next & !Priority::MASK) | (priority as usize);
    }

    /// Get the assigned priority of the task.
    pub fn priority(&self) -> Priority {
        match self.next & Priority::MASK {
            0 => Priority::Low,
            1 => Priority::Normal,
            2 => Priority::High,
            _ => unsafe { unreachable_unchecked() },
        }
    }

    /// Call the resume function provided by [`new`]
    /// using the given task as a parameter.
    pub fn resume(&mut self) {
        unsafe { (self.resume)(self) }
    }
}

#[doc(hidden)]
#[derive(Default)]
pub(super) struct List {
    head: Option<NonNull<Task>>,
    tail: Option<NonNull<Task>>,
}

impl List {
    pub fn pop<'a>(&mut self) -> Option<&'a mut Task> {
        self.head.map(|task| {
            let task = unsafe { &mut *task.as_ptr() };
            self.head = task.next();
            if self.head.is_none() {
                self.tail = None;
            }
            task
        })
    }

    pub fn push(&mut self, priority_list: &PriorityList) {
        self.push_front(&priority_list.front);
        self.push_back(&priority_list.back);
    }

    pub fn push_front(&mut self, list: &Self) {
        if let Some(tail) = list.tail {
            unsafe { (&mut *tail.as_ptr()).set_next(self.head) };
        }
        if self.tail.is_none() {
            self.tail = list.tail;
        }
        self.head = list.head;
    }

    pub fn push_back(&mut self, list: &Self) {
        if let Some(tail) = self.tail {
            unsafe { (&mut *tail.as_ptr()).set_next(list.head) };
        }
        if self.head.is_none() {
            self.head = list.head;
        }
        self.tail = list.tail;
    }
}

#[doc(hidden)]
#[derive(Default)]
pub(super) struct PriorityList {
    front: List,
    back: List,
    pub size: usize,
}

impl PriorityList {
    /*
    pub fn pop<'a>(&mut self) -> Option<&'a mut Task> {
        self.front.pop().or_else(|| self.back.pop()).map(|task| {
            self.size -= 1;
            task
        })
    }
    */

    pub fn push(&mut self, task: &mut Task) {
        self.size += 1;
        let list = List {
            head: NonNull::new(task),
            tail: NonNull::new(task),
        };
        match task.priority() {
            Priority::Low | Priority::Normal => self.back.push_back(&list),
            Priority::High => self.front.push_back(&list),
        }
    }
}

/// A combination of a task and a given future
/// which implements [`Task`] based waking and polling.
///
/// For convenience purposes in regards to the executors,
/// the task also stores and caches the output of the future.
pub struct FutureTask<F: Future> {
    future: F,
    pub task: Task,
    output: Option<F::Output>,
}

// TODO: is this safe? as I understand it, the FutureTask
//       shouldnt be moved in memory considering it uses
//       the task pointer in order to compute its reference.
impl<F: Future> Unpin for FutureTask<F> {}

impl<F: Future> Future for FutureTask<F> {
    type Output = NonNull<F::Output>;

    /// Polls the FutureTask for the result or returns the cached output as a NonNull pointer.
    /// This returns a pointer instead of an immutable reference since rust doesnt allow
    /// references for type annotations for [`Future::Output`].
    fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.get_mut();
        loop {
            // check if the output is already cached
            if let Some(output) = this.output.as_mut() {
                return Poll::Ready(unsafe { NonNull::new_unchecked(output) });
            }

            let pinned = unsafe { Pin::new_unchecked(&mut this.future) };
            match pinned.poll(ctx) {
                Poll::Pending => return Poll::Pending,
                Poll::Ready(output) => {
                    this.output = Some(output);
                    continue;
                }
            }
        }
    }
}

impl<F: Future> FutureTask<F> {
    /// Create a new future + task wrapper.
    pub fn new(priority: Priority, future: F) -> Self {
        Self {
            future,
            task: Task::new(priority, Self::on_resume),
            output: None,
        }
    }

    /// Convert the FutureTask into its underlying output value.
    pub fn into_output(self) -> Option<F::Output> {
        self.output
    }

    unsafe fn on_resume(task: *mut Task) {
        // Given a pointer to a task, get it's corresponding FutureTask.
        // This uses a `MaybeUninit::zeroed()` instance in order to
        // compute the task field offset but should be replaced if this is merged:
        // https://internals.rust-lang.org/t/pre-rfc-add-a-new-offset-of-macro-to-core-mem/9273
        let this = {
            let stub = MaybeUninit::<Self>::zeroed();
            let base_ptr = stub.as_ptr() as usize;
            let task_ptr = &(&*stub.as_ptr()).task as *const _ as usize;
            &mut *(((task as usize) - (task_ptr - base_ptr)) as *mut Self)
        };
        let _ = this.resume();
    }

    /// Resume the future wrapped task, returning the resulting output.
    ///
    /// This effectively calls `Future::poll()` with the waker and context
    /// setup to resume the future using the task as a reference if the
    /// futures output is not already polled() and stored.
    pub fn resume(&mut self) -> Poll<&F::Output> {
        // check if the output is cached before polling down below
        if self.output.is_some() {
            return Poll::Ready(self.output.as_ref().unwrap());
        }

        const WAKE: unsafe fn(*const ()) =
            |ptr| with_executor(|executor| unsafe { executor.schedule(&mut *(ptr as *mut Task)) });

        const VTABLE: RawWakerVTable =
            RawWakerVTable::new(|ptr| RawWaker::new(ptr, &VTABLE), WAKE, WAKE, |_| {});

        let ptr = &self.task as *const Task as *const ();
        let waker = unsafe { Waker::from_raw(RawWaker::new(ptr, &VTABLE)) };
        let pinned = unsafe { Pin::new_unchecked(self) };

        match pinned.poll(&mut Context::from_waker(&waker)) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(ptr) => Poll::Ready(unsafe { &*ptr.as_ptr() }),
        }
    }
}

/// Returns a future which can be used to yield the current
/// future to the executor in order to let another task run.
///
/// Takes in a `Priority` to potentially influence
/// the re-scheduling of the current future task.
pub fn yield_now(priority: Priority) -> impl Future {
    struct YieldTask {
        did_yield: bool,
    };

    impl Future for YieldTask {
        type Output = ();

        fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
            if self.did_yield {
                return Poll::Ready(());
            }

            self.did_yield = true;
            ctx.waker().wake_by_ref();
            Poll::Pending
        }
    }

    FutureTask::new(priority, YieldTask { did_yield: false })
}