sys_yield tests worked on qemu.
This commit is contained in:
parent
91043b08cd
commit
4590f233b5
10 changed files with 134 additions and 53 deletions
|
@ -1,22 +1,24 @@
|
|||
mod context;
|
||||
mod switch;
|
||||
mod task;
|
||||
|
||||
use crate::config::MAX_APP_NUM;
|
||||
use crate::loader::{get_num_app, init_app_cx};
|
||||
use core::cell::RefCell;
|
||||
use lazy_static::*;
|
||||
use switch::__switch;
|
||||
use task::{TaskControlBlock, TaskStatus};
|
||||
|
||||
pub use context::TaskContext;
|
||||
|
||||
struct TaskControlBlock {
|
||||
task_cx_ptr: usize,
|
||||
}
|
||||
|
||||
pub struct TaskManager {
|
||||
num_app: usize,
|
||||
inner: RefCell<TaskManagerInner>,
|
||||
}
|
||||
|
||||
struct TaskManagerInner {
|
||||
tasks: [TaskControlBlock; MAX_APP_NUM],
|
||||
current_task: RefCell<usize>,
|
||||
current_task: usize,
|
||||
}
|
||||
|
||||
unsafe impl Sync for TaskManager {}
|
||||
|
@ -24,30 +26,75 @@ unsafe impl Sync for TaskManager {}
|
|||
lazy_static! {
|
||||
pub static ref TASK_MANAGER: TaskManager = {
|
||||
let num_app = get_num_app();
|
||||
let mut tasks = [TaskControlBlock { task_cx_ptr: 0 }; MAX_APP_NUM];
|
||||
let mut tasks = [
|
||||
TaskControlBlock { task_cx_ptr: 0, task_status: TaskStatus::UnInit };
|
||||
MAX_APP_NUM
|
||||
];
|
||||
for i in 0..num_app {
|
||||
tasks[i] = TaskControlBlock { task_cx_ptr: init_app_cx(i) as *const _ as usize, };
|
||||
tasks[i].task_cx_ptr = init_app_cx(i) as * const _ as usize;
|
||||
tasks[i].task_status = TaskStatus::Ready;
|
||||
}
|
||||
TaskManager {
|
||||
num_app,
|
||||
tasks,
|
||||
current_task: RefCell::new(0),
|
||||
inner: RefCell::new(TaskManagerInner {
|
||||
tasks,
|
||||
current_task: 0,
|
||||
}),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl TaskManager {
|
||||
pub fn run_first_task(&self) {
|
||||
fn run_first_task(&self) {
|
||||
self.inner.borrow_mut().tasks[0].task_status = TaskStatus::Running;
|
||||
let next_task_cx = self.inner.borrow().tasks[0].get_task_cx_ptr2();
|
||||
unsafe {
|
||||
__switch(&0usize, &self.tasks[0].task_cx_ptr);
|
||||
__switch(
|
||||
&0usize as *const _,
|
||||
next_task_cx,
|
||||
);
|
||||
}
|
||||
}
|
||||
pub fn switch_to_next_task(&self) {
|
||||
let current = *self.current_task.borrow();
|
||||
let next = if current == self.num_app - 1 { 0 } else { current + 1 };
|
||||
*self.current_task.borrow_mut() = next;
|
||||
unsafe {
|
||||
__switch(&self.tasks[current].task_cx_ptr, &self.tasks[next].task_cx_ptr);
|
||||
|
||||
fn mark_current_suspended(&self) {
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
let current = inner.current_task;
|
||||
inner.tasks[current].task_status = TaskStatus::Ready;
|
||||
}
|
||||
|
||||
fn mark_current_exited(&self) {
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
let current = inner.current_task;
|
||||
inner.tasks[current].task_status = TaskStatus::Exited;
|
||||
}
|
||||
|
||||
fn find_next_task(&self) -> Option<usize> {
|
||||
let inner = self.inner.borrow();
|
||||
let current = inner.current_task;
|
||||
(current + 1..current + self.num_app + 1)
|
||||
.map(|id| id % self.num_app)
|
||||
.find(|id| {
|
||||
inner.tasks[*id].task_status == TaskStatus::Ready
|
||||
})
|
||||
}
|
||||
|
||||
fn run_next_task(&self) {
|
||||
if let Some(next) = self.find_next_task() {
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
let current = inner.current_task;
|
||||
inner.tasks[next].task_status = TaskStatus::Running;
|
||||
inner.current_task = next;
|
||||
let current_task_cx = inner.tasks[current].get_task_cx_ptr2();
|
||||
let next_task_cx = inner.tasks[next].get_task_cx_ptr2();
|
||||
core::mem::drop(inner);
|
||||
unsafe {
|
||||
__switch(
|
||||
current_task_cx,
|
||||
next_task_cx,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
panic!("[kernel] All applications completed!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,6 +103,14 @@ pub fn run_first_task() {
|
|||
TASK_MANAGER.run_first_task();
|
||||
}
|
||||
|
||||
pub fn switch_to_next_task() {
|
||||
TASK_MANAGER.switch_to_next_task();
|
||||
pub fn run_next_task() {
|
||||
TASK_MANAGER.run_next_task();
|
||||
}
|
||||
|
||||
pub fn mark_current_suspended() {
|
||||
TASK_MANAGER.mark_current_suspended();
|
||||
}
|
||||
|
||||
pub fn mark_current_exited() {
|
||||
TASK_MANAGER.mark_current_exited();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue