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,17 +222,18 @@ impl Tasking {
} }
pub fn task_yield(&self) { pub fn task_yield(&self) {
loop {
self.freeable_kstacks.lock().clear(); self.freeable_kstacks.lock().clear();
let Some(current_pid) = *self.current_pid.read() else { let Some(current_pid) = *self.current_pid.read() else {
self.wfi_loop.store(false, Ordering::Relaxed); self.wfi_loop.store(false, Ordering::Relaxed);
return; break;
}; };
let next_process_pid = self.ready_to_run.lock().pop_front(); let next_process_pid = self.ready_to_run.lock().pop_front();
if let Some(next_process_pid) = next_process_pid { if let Some(next_process_pid) = next_process_pid {
self.wfi_loop.store(false, Ordering::Relaxed); self.wfi_loop.store(false, Ordering::Relaxed);
if next_process_pid == self.current_pid().unwrap() { if next_process_pid == self.current_pid().unwrap() {
println!("Yielding to currect process! Returning"); println!("Yielding to currect process! Returning");
return; break;
} }
#[expect( #[expect(
clippy::expect_used, clippy::expect_used,
@ -264,6 +265,7 @@ impl Tasking {
let curr_stack = addr_of_mut!(processes[previous_process].kernel_esp); let curr_stack = addr_of_mut!(processes[previous_process].kernel_esp);
core::mem::drop(processes); core::mem::drop(processes);
switch_to_asm(curr_stack, kernel_esp); switch_to_asm(curr_stack, kernel_esp);
break;
} else if { } else if {
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")] #[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let res = self.processes.read()[current_pid].sleeping.read().is_some(); let res = self.processes.read()[current_pid].sleeping.read().is_some();
@ -276,9 +278,10 @@ impl Tasking {
self.wfi_loop.store(true, Ordering::Relaxed); self.wfi_loop.store(true, Ordering::Relaxed);
x86_64::instructions::interrupts::enable_and_hlt(); x86_64::instructions::interrupts::enable_and_hlt();
x86_64::instructions::interrupts::disable(); x86_64::instructions::interrupts::disable();
self.task_yield();
} else { } else {
self.wfi_loop.store(false, Ordering::Relaxed); self.wfi_loop.store(false, Ordering::Relaxed);
break;
}
} }
} }
@ -287,6 +290,7 @@ impl Tasking {
} }
pub fn exit(&self) -> ! { pub fn exit(&self) -> ! {
loop {
let next_process_pid = self.ready_to_run.lock().pop_front(); let next_process_pid = self.ready_to_run.lock().pop_front();
if let Some(next_process_pid) = next_process_pid { if let Some(next_process_pid) = next_process_pid {
self.wfi_loop.store(false, Ordering::Relaxed); self.wfi_loop.store(false, Ordering::Relaxed);
@ -317,7 +321,8 @@ impl Tasking {
self.wfi_loop.store(true, Ordering::Relaxed); self.wfi_loop.store(true, Ordering::Relaxed);
x86_64::instructions::interrupts::enable_and_hlt(); x86_64::instructions::interrupts::enable_and_hlt();
x86_64::instructions::interrupts::disable(); x86_64::instructions::interrupts::disable();
self.exit(); //self.exit();
}
} }
} }