simple_executor.rs
1 use super::Task; 2 use alloc::collections::VecDeque; 3 use core::task::{Waker, RawWaker}; 4 use core::task::RawWakerVTable; 5 use core::task::{Context, Poll}; 6 7 pub struct SimpleExecutor { 8 task_queue: VecDeque<Task>, 9 } 10 11 impl SimpleExecutor { 12 pub fn new() -> SimpleExecutor { 13 SimpleExecutor { 14 task_queue: VecDeque::new(), 15 } 16 } 17 18 pub fn spawn(&mut self, task: Task) { 19 self.task_queue.push_back(task) 20 } 21 22 pub fn run(&mut self) { 23 while let Some(mut task) = self.task_queue.pop_front() { 24 let waker = dummy_water(); 25 let mut context = Context::from_waker(&waker); 26 match task.poll(&mut context) { 27 Poll::Ready(()) => {} 28 Poll::Pending => self.task_queue.push_back(task), 29 } 30 } 31 } 32 } 33 34 fn dummy_raw_waker() -> RawWaker { 35 fn no_op(_: *const ()) {} 36 fn clone(_: *const ()) -> RawWaker { 37 dummy_raw_waker() 38 } 39 40 let vtable = &RawWakerVTable::new(clone, no_op, no_op, no_op); 41 RawWaker::new(0 as *const (), vtable) 42 } 43 44 fn dummy_water() -> Waker { 45 unsafe { Waker::from_raw(dummy_raw_waker()) } 46 }