mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 06:17:49 +00:00
Put the CPU to sleep when no task is ready
This commit is contained in:
@@ -32,6 +32,7 @@ impl Executor {
|
||||
loop {
|
||||
self.wake_tasks();
|
||||
self.run_ready_tasks();
|
||||
self.sleep_if_idle();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +66,22 @@ impl Executor {
|
||||
}
|
||||
}
|
||||
|
||||
fn sleep_if_idle(&self) {
|
||||
use x86_64::instructions::interrupts::{self, enable_interrupts_and_hlt};
|
||||
|
||||
// fast path
|
||||
if !self.wake_queue.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
interrupts::disable();
|
||||
if self.wake_queue.is_empty() {
|
||||
enable_interrupts_and_hlt();
|
||||
} else {
|
||||
interrupts::enable();
|
||||
}
|
||||
}
|
||||
|
||||
fn create_waker(&self, task_id: TaskId) -> Waker {
|
||||
Waker::from(Arc::new(TaskWaker {
|
||||
task_id,
|
||||
|
||||
Reference in New Issue
Block a user