|
|
|
@ -45,32 +45,37 @@ lazy_static! {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TaskManager {
|
|
|
|
|
fn run_first_task(&self) {
|
|
|
|
|
let next_task = &mut self.inner.upsafe_access().tasks[0];
|
|
|
|
|
fn run_first_task(&self) -> ! {
|
|
|
|
|
let mut inner = self.inner.exclusive_access();
|
|
|
|
|
let next_task = &mut inner.tasks[0];
|
|
|
|
|
next_task.task_status = TaskStatus::Running;
|
|
|
|
|
let next_task_cx_ptr = &next_task.task_cx as *const TaskContext;
|
|
|
|
|
drop(next_task);
|
|
|
|
|
drop(inner);
|
|
|
|
|
let mut _unused = TaskContext::zero_init();
|
|
|
|
|
// before this, we should drop local variables that must be dropped manually
|
|
|
|
|
unsafe {
|
|
|
|
|
__switch(
|
|
|
|
|
&mut _unused as *mut _,
|
|
|
|
|
next_task_cx_ptr,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
panic!("unreachable in run_first_task!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mark_current_suspended(&self) {
|
|
|
|
|
let inner = self.inner.upsafe_access();
|
|
|
|
|
inner.tasks[inner.current_task].task_status = TaskStatus::Ready;
|
|
|
|
|
let mut inner = self.inner.exclusive_access();
|
|
|
|
|
let cur = inner.current_task;
|
|
|
|
|
inner.tasks[cur].task_status = TaskStatus::Ready;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mark_current_exited(&self) {
|
|
|
|
|
let inner = self.inner.upsafe_access();
|
|
|
|
|
inner.tasks[inner.current_task].task_status = TaskStatus::Exited;
|
|
|
|
|
let mut inner = self.inner.exclusive_access();
|
|
|
|
|
let cur = inner.current_task;
|
|
|
|
|
inner.tasks[cur].task_status = TaskStatus::Exited;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_next_task(&self) -> Option<usize> {
|
|
|
|
|
let inner = self.inner.upsafe_access();
|
|
|
|
|
let inner = self.inner.exclusive_access();
|
|
|
|
|
let current = inner.current_task;
|
|
|
|
|
(current + 1..current + self.num_app + 1)
|
|
|
|
|
.map(|id| id % self.num_app)
|
|
|
|
@ -80,30 +85,32 @@ impl TaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_current_token(&self) -> usize {
|
|
|
|
|
let inner = self.inner.upsafe_access();
|
|
|
|
|
let inner = self.inner.exclusive_access();
|
|
|
|
|
inner.tasks[inner.current_task].get_user_token()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_current_trap_cx(&self) -> &mut TrapContext {
|
|
|
|
|
let inner = self.inner.upsafe_access();
|
|
|
|
|
let inner = self.inner.exclusive_access();
|
|
|
|
|
inner.tasks[inner.current_task].get_trap_cx()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run_next_task(&self) {
|
|
|
|
|
if let Some(next) = self.find_next_task() {
|
|
|
|
|
let mut inner = self.inner.upsafe_access();
|
|
|
|
|
let mut inner = self.inner.exclusive_access();
|
|
|
|
|
let current = inner.current_task;
|
|
|
|
|
inner.tasks[next].task_status = TaskStatus::Running;
|
|
|
|
|
inner.current_task = next;
|
|
|
|
|
let current_task_cx_ptr = &mut inner.tasks[current].task_cx as *mut TaskContext;
|
|
|
|
|
let next_task_cx_ptr = &inner.tasks[next].task_cx as *const TaskContext;
|
|
|
|
|
drop(inner);
|
|
|
|
|
// before this, we should drop local variables that must be dropped manually
|
|
|
|
|
unsafe {
|
|
|
|
|
__switch(
|
|
|
|
|
current_task_cx_ptr,
|
|
|
|
|
next_task_cx_ptr,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// go back to user mode
|
|
|
|
|
} else {
|
|
|
|
|
panic!("All applications completed!");
|
|
|
|
|
}
|
|
|
|
|