parent
0c12d43b61
commit
1bc53c0b5f
@ -0,0 +1,34 @@
|
||||
use super::TaskControlBlock;
|
||||
use alloc::collections::VecDeque;
|
||||
use alloc::sync::Arc;
|
||||
use spin::Mutex;
|
||||
use lazy_static::*;
|
||||
|
||||
pub struct TaskManager {
|
||||
ready_queue: VecDeque<Arc<Mutex<TaskControlBlock>>>,
|
||||
}
|
||||
|
||||
/// A simple FIFO scheduler.
|
||||
impl TaskManager {
|
||||
pub fn new() -> Self {
|
||||
Self { ready_queue: VecDeque::new(), }
|
||||
}
|
||||
pub fn add(&mut self, task: Arc<Mutex<TaskControlBlock>>) {
|
||||
self.ready_queue.push_back(task);
|
||||
}
|
||||
pub fn fetch(&mut self) -> Option<Arc<Mutex<TaskControlBlock>>> {
|
||||
self.ready_queue.pop_front()
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref TASK_MANAGER: Mutex<TaskManager> = Mutex::new(TaskManager::new());
|
||||
}
|
||||
|
||||
pub fn add_task(task: Arc<Mutex<TaskControlBlock>>) {
|
||||
TASK_MANAGER.lock().add(task);
|
||||
}
|
||||
|
||||
pub fn fetch_task() -> Option<Arc<Mutex<TaskControlBlock>>> {
|
||||
TASK_MANAGER.lock().fetch()
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
use super::TaskControlBlock;
|
||||
use alloc::sync::Arc;
|
||||
use spin::Mutex;
|
||||
use lazy_static::*;
|
||||
use super::{add_task, fetch_task};
|
||||
use super::__switch;
|
||||
use crate::trap::TrapContext;
|
||||
|
||||
pub struct Processor {
|
||||
inner: Mutex<ProcessorInner>,
|
||||
}
|
||||
|
||||
unsafe impl Sync for Processor {}
|
||||
|
||||
struct ProcessorInner {
|
||||
current: Option<Arc<Mutex<TaskControlBlock>>>,
|
||||
idle_task_cx_ptr: usize,
|
||||
}
|
||||
|
||||
impl Processor {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: Mutex::new(ProcessorInner {
|
||||
current: None,
|
||||
idle_task_cx_ptr: 0,
|
||||
}),
|
||||
}
|
||||
}
|
||||
fn get_idle_task_cx_ptr2(&self) -> *const usize {
|
||||
let inner = self.inner.lock();
|
||||
&inner.idle_task_cx_ptr as *const usize
|
||||
}
|
||||
pub fn run(&self) {
|
||||
//println!("into Processor::run");
|
||||
loop {
|
||||
if let Some(task) = fetch_task() {
|
||||
//println!("found task!");
|
||||
let idle_task_cx_ptr = self.get_idle_task_cx_ptr2();
|
||||
let next_task_cx_ptr = task.lock().get_task_cx_ptr2();
|
||||
//println!("next_task_cx_ptr={:p}", next_task_cx_ptr);
|
||||
self.inner.lock().current = Some(task);
|
||||
unsafe {
|
||||
__switch(
|
||||
idle_task_cx_ptr,
|
||||
next_task_cx_ptr,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn take_current(&self) -> Option<Arc<Mutex<TaskControlBlock>>> {
|
||||
self.inner.lock().current.take()
|
||||
}
|
||||
pub fn current(&self) -> Option<Arc<Mutex<TaskControlBlock>>> {
|
||||
self.inner.lock().current.as_ref().map(|task| task.clone())
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref PROCESSOR: Processor = Processor::new();
|
||||
}
|
||||
|
||||
pub fn run_tasks() {
|
||||
PROCESSOR.run();
|
||||
}
|
||||
|
||||
pub fn take_current_task() -> Option<Arc<Mutex<TaskControlBlock>>> {
|
||||
PROCESSOR.take_current()
|
||||
}
|
||||
|
||||
pub fn current_task() -> Option<Arc<Mutex<TaskControlBlock>>> {
|
||||
//println!("into current_task!");
|
||||
PROCESSOR.current()
|
||||
}
|
||||
|
||||
pub fn current_user_token() -> usize {
|
||||
//println!("into current_user_token!");
|
||||
let task = current_task().unwrap();
|
||||
//println!("Got task in current_user_token!");
|
||||
let token = task.lock().get_user_token();
|
||||
token
|
||||
}
|
||||
|
||||
pub fn current_trap_cx() -> &'static mut TrapContext {
|
||||
current_task().unwrap().as_ref().lock().get_trap_cx()
|
||||
}
|
||||
|
||||
pub fn schedule(switched_task_cx_ptr2: *const usize) {
|
||||
let idle_task_cx_ptr2 = PROCESSOR.get_idle_task_cx_ptr2();
|
||||
unsafe {
|
||||
__switch(
|
||||
switched_task_cx_ptr2,
|
||||
idle_task_cx_ptr2,
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue