Implement an executor with waker support

This commit is contained in:
Philipp Oppermann
2020-03-27 17:01:09 +01:00
parent d7b144364d
commit 50b4b89ac2
4 changed files with 112 additions and 8 deletions

View File

@@ -6,6 +6,7 @@
#![feature(const_fn)] #![feature(const_fn)]
#![feature(alloc_layout_extra)] #![feature(alloc_layout_extra)]
#![feature(const_in_array_repeat_expressions)] #![feature(const_in_array_repeat_expressions)]
#![feature(wake_trait)]
#![test_runner(crate::test_runner)] #![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"] #![reexport_test_harness_main = "test_main"]

View File

@@ -7,7 +7,7 @@
extern crate alloc; extern crate alloc;
use blog_os::println; use blog_os::println;
use blog_os::task::{keyboard, simple_executor::SimpleExecutor, Task}; use blog_os::task::{executor::Executor, keyboard, Task};
use bootloader::{entry_point, BootInfo}; use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo; use core::panic::PanicInfo;
@@ -27,16 +27,13 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed"); allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
let mut executor = SimpleExecutor::new();
executor.spawn(Task::new(example_task()));
executor.spawn(Task::new(keyboard::print_keypresses()));
executor.run();
#[cfg(test)] #[cfg(test)]
test_main(); test_main();
println!("It did not crash!"); let mut executor = Executor::new();
blog_os::hlt_loop(); executor.spawn(Task::new(example_task()));
executor.spawn(Task::new(keyboard::print_keypresses()));
executor.run();
} }
/// This function is called on panic. /// This function is called on panic.

95
src/task/executor.rs Normal file
View File

@@ -0,0 +1,95 @@
use super::{Task, TaskId};
use alloc::{
collections::{BTreeMap, VecDeque},
sync::Arc,
task::Wake,
};
use core::task::{Context, Poll, Waker};
use crossbeam_queue::ArrayQueue;
pub struct Executor {
task_queue: VecDeque<Task>,
waiting_tasks: BTreeMap<TaskId, Task>,
wake_queue: Arc<ArrayQueue<TaskId>>,
waker_cache: BTreeMap<TaskId, Waker>,
}
impl Executor {
pub fn new() -> Self {
Executor {
task_queue: VecDeque::new(),
waiting_tasks: BTreeMap::new(),
wake_queue: Arc::new(ArrayQueue::new(100)),
waker_cache: BTreeMap::new(),
}
}
pub fn spawn(&mut self, task: Task) {
self.task_queue.push_back(task)
}
pub fn run(&mut self) -> ! {
loop {
self.wake_tasks();
self.run_ready_tasks();
}
}
fn run_ready_tasks(&mut self) {
while let Some(mut task) = self.task_queue.pop_front() {
let task_id = task.id();
if !self.waker_cache.contains_key(&task_id) {
self.waker_cache.insert(task_id, self.create_waker(task_id));
}
let waker = self.waker_cache.get(&task_id).expect("should exist");
let mut context = Context::from_waker(waker);
match task.poll(&mut context) {
Poll::Ready(()) => {
// task done -> remove cached waker
self.waker_cache.remove(&task_id);
}
Poll::Pending => {
if self.waiting_tasks.insert(task_id, task).is_some() {
panic!("task with same ID already in waiting_tasks");
}
}
}
}
}
fn wake_tasks(&mut self) {
while let Ok(task_id) = self.wake_queue.pop() {
if let Some(task) = self.waiting_tasks.remove(&task_id) {
self.task_queue.push_back(task);
}
}
}
fn create_waker(&self, task_id: TaskId) -> Waker {
Waker::from(Arc::new(TaskWaker {
task_id,
wake_queue: self.wake_queue.clone(),
}))
}
}
struct TaskWaker {
task_id: TaskId,
wake_queue: Arc<ArrayQueue<TaskId>>,
}
impl TaskWaker {
fn wake_task(&self) {
self.wake_queue.push(self.task_id).expect("wake_queue full");
}
}
impl Wake for TaskWaker {
fn wake(self: Arc<Self>) {
self.wake_task();
}
fn wake_by_ref(self: &Arc<Self>) {
self.wake_task();
}
}

View File

@@ -5,6 +5,7 @@ use core::{
task::{Context, Poll}, task::{Context, Poll},
}; };
pub mod executor;
pub mod keyboard; pub mod keyboard;
pub mod simple_executor; pub mod simple_executor;
@@ -22,4 +23,14 @@ impl Task {
fn poll(&mut self, context: &mut Context) -> Poll<()> { fn poll(&mut self, context: &mut Context) -> Poll<()> {
self.future.as_mut().poll(context) self.future.as_mut().poll(context)
} }
fn id(&self) -> TaskId {
use core::ops::Deref;
let addr = Pin::deref(&self.future) as *const _ as *const () as usize;
TaskId(addr)
}
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct TaskId(usize);