refactor: tasks code cleanup
This commit is contained in:
+8
-31
@@ -1,16 +1,22 @@
|
||||
use crate::{
|
||||
arch::ThreadContext,
|
||||
memory::{AddressSpace, KernelStack, VirtualAddr},
|
||||
println,
|
||||
};
|
||||
|
||||
// Thread Control Block
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ExitReason {
|
||||
Exited(usize),
|
||||
Killed,
|
||||
Fault,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ThreadState {
|
||||
Ready,
|
||||
Running,
|
||||
Blocked,
|
||||
Dead,
|
||||
Dead(ExitReason),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -41,32 +47,3 @@ impl Tcb {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn run_first_user(user_tcb: &Tcb) {
|
||||
let previous_interrupts = crate::arch::disable_interrupts_and_save();
|
||||
|
||||
let mut boot_thread_ctx = ThreadContext::empty();
|
||||
unsafe {
|
||||
user_tcb.address_space.activate();
|
||||
crate::arch::set_kernel_stack(user_tcb.kernel_stack.top());
|
||||
crate::arch::switch_context(&mut boot_thread_ctx, &user_tcb.context);
|
||||
}
|
||||
|
||||
crate::arch::restore_interrupts(previous_interrupts);
|
||||
}
|
||||
|
||||
pub unsafe fn switch(prev: &mut Tcb, next: &Tcb) {
|
||||
let previous_interrupts = crate::arch::disable_interrupts_and_save();
|
||||
|
||||
if prev.address_space != next.address_space {
|
||||
unsafe { next.address_space.activate() };
|
||||
}
|
||||
|
||||
crate::arch::set_kernel_stack(next.kernel_stack.top());
|
||||
|
||||
unsafe {
|
||||
crate::arch::switch_context(&mut prev.context, &next.context);
|
||||
}
|
||||
|
||||
crate::arch::restore_interrupts(previous_interrupts);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user