Make task_yield and exit use a loop instead of a tail call (manual TCO)

This commit is contained in:
pjht 2024-09-23 13:25:15 -05:00
parent 499de08b62
commit 0abfd39522
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -222,63 +222,66 @@ impl Tasking {
} }
pub fn task_yield(&self) { pub fn task_yield(&self) {
self.freeable_kstacks.lock().clear(); loop {
let Some(current_pid) = *self.current_pid.read() else { self.freeable_kstacks.lock().clear();
self.wfi_loop.store(false, Ordering::Relaxed); let Some(current_pid) = *self.current_pid.read() else {
return; self.wfi_loop.store(false, Ordering::Relaxed);
}; break;
let next_process_pid = self.ready_to_run.lock().pop_front(); };
if let Some(next_process_pid) = next_process_pid { let next_process_pid = self.ready_to_run.lock().pop_front();
self.wfi_loop.store(false, Ordering::Relaxed); if let Some(next_process_pid) = next_process_pid {
if next_process_pid == self.current_pid().unwrap() { self.wfi_loop.store(false, Ordering::Relaxed);
println!("Yielding to currect process! Returning"); if next_process_pid == self.current_pid().unwrap() {
return; println!("Yielding to currect process! Returning");
break;
}
#[expect(
clippy::expect_used,
reason = "This expect checks a critical invariant. If this fails, the kernel MUST panic"
)]
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let current_address_space = self.processes.write()[next_process_pid]
.address_space
.take()
.expect("Non-current process has active page table")
.activate();
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
self.processes.write()[current_pid].address_space = Some(current_address_space);
let processes = self.processes.read();
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let current_process = &processes[current_pid];
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let next_process = &processes[next_process_pid];
gdt::set_tss_stack(next_process.kernel_esp_top);
if current_process.sleeping.read().is_none() {
self.ready_to_run.lock().push_back(current_pid);
}
let kernel_esp = next_process.kernel_esp;
let previous_process = current_pid;
*self.current_pid.write() = Some(next_process_pid);
core::mem::drop(processes);
let mut processes = self.processes.write();
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let curr_stack = addr_of_mut!(processes[previous_process].kernel_esp);
core::mem::drop(processes);
switch_to_asm(curr_stack, kernel_esp);
break;
} else if {
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let res = self.processes.read()[current_pid].sleeping.read().is_some();
res
} {
//println!("All processes sleeping, exiting QEMU");
//self.print_stats();
//qemu_exit::exit_qemu();
//println!("All processes sleeping, waiting for interrupt");
self.wfi_loop.store(true, Ordering::Relaxed);
x86_64::instructions::interrupts::enable_and_hlt();
x86_64::instructions::interrupts::disable();
} else {
self.wfi_loop.store(false, Ordering::Relaxed);
break;
} }
#[expect(
clippy::expect_used,
reason = "This expect checks a critical invariant. If this fails, the kernel MUST panic"
)]
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let current_address_space = self.processes.write()[next_process_pid]
.address_space
.take()
.expect("Non-current process has active page table")
.activate();
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
self.processes.write()[current_pid].address_space = Some(current_address_space);
let processes = self.processes.read();
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let current_process = &processes[current_pid];
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let next_process = &processes[next_process_pid];
gdt::set_tss_stack(next_process.kernel_esp_top);
if current_process.sleeping.read().is_none() {
self.ready_to_run.lock().push_back(current_pid);
}
let kernel_esp = next_process.kernel_esp;
let previous_process = current_pid;
*self.current_pid.write() = Some(next_process_pid);
core::mem::drop(processes);
let mut processes = self.processes.write();
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let curr_stack = addr_of_mut!(processes[previous_process].kernel_esp);
core::mem::drop(processes);
switch_to_asm(curr_stack, kernel_esp);
} else if {
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let res = self.processes.read()[current_pid].sleeping.read().is_some();
res
} {
//println!("All processes sleeping, exiting QEMU");
//self.print_stats();
//qemu_exit::exit_qemu();
//println!("All processes sleeping, waiting for interrupt");
self.wfi_loop.store(true, Ordering::Relaxed);
x86_64::instructions::interrupts::enable_and_hlt();
x86_64::instructions::interrupts::disable();
self.task_yield();
} else {
self.wfi_loop.store(false, Ordering::Relaxed);
} }
} }
@ -287,37 +290,39 @@ impl Tasking {
} }
pub fn exit(&self) -> ! { pub fn exit(&self) -> ! {
let next_process_pid = self.ready_to_run.lock().pop_front(); loop {
if let Some(next_process_pid) = next_process_pid { let next_process_pid = self.ready_to_run.lock().pop_front();
self.wfi_loop.store(false, Ordering::Relaxed); if let Some(next_process_pid) = next_process_pid {
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")] self.wfi_loop.store(false, Ordering::Relaxed);
let mut processes = self.processes.write(); #[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
if let Some(current_pid) = *self.current_pid.read() { let mut processes = self.processes.write();
self.freeable_kstacks.lock().push(processes.remove(current_pid).kernel_stack); if let Some(current_pid) = *self.current_pid.read() {
self.freeable_kstacks.lock().push(processes.remove(current_pid).kernel_stack);
}
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let next_process = &mut processes[next_process_pid];
#[expect(
clippy::expect_used,
reason = "This expect checks a critical invariant. If this fails, the kernel MUST panic"
)]
next_process
.address_space
.take()
.expect("Non-current process has active page table")
.activate();
gdt::set_tss_stack(next_process.kernel_esp_top);
let kernel_esp = next_process.kernel_esp;
*self.current_pid.write() = Some(next_process_pid);
core::mem::drop(processes);
switch_to_asm_exit(kernel_esp);
unreachable!()
} else {
//println!("Last non-sleeping process exited, exiting QEMU");
self.wfi_loop.store(true, Ordering::Relaxed);
x86_64::instructions::interrupts::enable_and_hlt();
x86_64::instructions::interrupts::disable();
//self.exit();
} }
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let next_process = &mut processes[next_process_pid];
#[expect(
clippy::expect_used,
reason = "This expect checks a critical invariant. If this fails, the kernel MUST panic"
)]
next_process
.address_space
.take()
.expect("Non-current process has active page table")
.activate();
gdt::set_tss_stack(next_process.kernel_esp_top);
let kernel_esp = next_process.kernel_esp;
*self.current_pid.write() = Some(next_process_pid);
core::mem::drop(processes);
switch_to_asm_exit(kernel_esp);
unreachable!()
} else {
//println!("Last non-sleeping process exited, exiting QEMU");
self.wfi_loop.store(true, Ordering::Relaxed);
x86_64::instructions::interrupts::enable_and_hlt();
x86_64::instructions::interrupts::disable();
self.exit();
} }
} }