Refactor Executor::run into separate methods

This commit is contained in:
Philipp Oppermann
2020-03-08 13:49:36 +01:00
parent 378159ce76
commit f75d63853f

View File

@@ -36,43 +36,70 @@ impl Executor {
pub fn run(&mut self) -> ! { pub fn run(&mut self) -> ! {
loop { loop {
// perform wakeups caused by interrupts self.run_ready_tasks();
// the interrupt handlers can't do it themselves since wakers might execute self.apply_interrupt_wakeups();
// arbitrary code, e.g. allocate self.wake_waiting_tasks();
while let Ok(waker) = interrupts::interrupt_wakeups().pop() { self.hlt_if_idle();
waker.wake(); }
} }
// wakeup waiting tasks
while let Ok(task_id) = self.wake_queue.pop() { fn run_ready_tasks(&mut self) {
if let Some(task) = self.pending_tasks.remove(&task_id) { while let Some(mut task) = self.task_queue.pop_front() {
self.task_queue.push_back(task); let waker = self.create_waker(&task).into();
} else { let mut context = Context::from_waker(&waker);
println!("WARNING: woken task not found in pending_tasks"); match task.as_mut().poll(&mut context) {
} Poll::Ready(()) => {} // task done
} Poll::Pending => {
// run ready tasks // add task to pending_tasks and wait for wakeup
while let Some(mut task) = self.task_queue.pop_front() { let task_id = Self::task_id(&task);
let waker = self.create_waker(&task).into(); if self.pending_tasks.insert(task_id, task).is_some() {
let mut context = Context::from_waker(&waker); panic!("Task with same ID already in pending_tasks");
match task.as_mut().poll(&mut context) {
Poll::Ready(()) => {} // task done
Poll::Pending => {
// add task to pending_tasks list and wait for wakeup
let task_id = Self::task_id(&task);
if self.pending_tasks.insert(task_id, task).is_some() {
panic!("Task with same ID already in pending_tasks queue");
}
} }
} }
} }
// wait for next interrupt if there is nothing left to do }
if self.wake_queue.is_empty() { }
unsafe { asm!("cli") };
if self.wake_queue.is_empty() { /// Invoke wakers for tasks woken by interrupts
unsafe { asm!("sti; hlt") }; ///
} else { /// The interrupt handlers can't invoke the waker directly since wakers
unsafe { asm!("sti") }; /// might execute arbitrary code, e.g. allocate, which should not be done
} /// in interrupt handlers to avoid deadlocks.
fn apply_interrupt_wakeups(&mut self) {
while let Ok(waker) = interrupts::interrupt_wakeups().pop() {
waker.wake();
}
}
fn wake_waiting_tasks(&mut self) {
while let Ok(task_id) = self.wake_queue.pop() {
if let Some(task) = self.pending_tasks.remove(&task_id) {
self.task_queue.push_back(task);
} else {
println!("WARNING: woken task not found in pending_tasks");
}
}
}
/// Executes the `hlt` instruction if there are no ready tasks
fn hlt_if_idle(&self) {
if self.task_queue.is_empty() {
// disable interrupts to avoid races
x86_64::instructions::interrupts::disable();
// check if relevant interrupts occured since the last check
if interrupts::interrupt_wakeups().is_empty() {
// no interrupts occured -> hlt to wait for next interrupt
//
// It's important to execute `hlt` directly after `sti` because
// otherwise we could miss interrupts between the two
// instructions. Since `sti` only enables interrupts after the
// subsequent instruction, we can be sure that we don't miss an
// interrupt. (One exception are non-maskable interrupts, which
// can occur even when interrupts are disabled.)
unsafe { asm!("sti; hlt") };
} else {
// there were some new wakeups -> continue execution
x86_64::instructions::interrupts::enable();
} }
} }
} }