1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//!Implementation of [`TaskManager`]
use core::borrow::Borrow;
use super::TaskControlBlock;
use crate::sync::UPSafeCell;
use alloc::collections::VecDeque;
use alloc::sync::Arc;
use lazy_static::*;

///A array of `TaskControlBlock` that is thread-safe
#[repr(C)]
pub struct TaskManager {
    /// upper_border
    pub upper_border:usize,
    /// ready_queue
    pub ready_queue: VecDeque<Arc<TaskControlBlock>>,
    /// lower_border
    pub lower_border:usize,
}

/// A simple FIFO scheduler.
impl TaskManager {
    ///Creat an empty TaskManager
    pub fn new() -> Self {
        Self {
            upper_border:0xAAAAAAAA,
            ready_queue: VecDeque::new(),
            lower_border:0xBBBBBBBB
        }
    }
    ///Add a task to `TaskManager`
    pub fn add(&mut self, task: Arc<TaskControlBlock>) {
        self.ready_queue.push_back(task);
    }
    ///Remove the first task and return it,or `None` if `TaskManager` is empty
    pub fn fetch(&mut self) -> Option<Arc<TaskControlBlock>> {
        self.ready_queue.pop_front()
    }
    
    ///Get ready_queue_pointer
    pub fn get_ready_queue_pointer(&mut self) -> &VecDeque<Arc<TaskControlBlock>> {
        &self.ready_queue
    }
}

lazy_static! {
    pub static ref TASK_MANAGER: UPSafeCell<TaskManager> =
        unsafe { UPSafeCell::new(TaskManager::new()) };
}

static mut TM_RQ:usize=0;

///Interface offered to add task
pub fn add_task(task: Arc<TaskControlBlock>) {
    TASK_MANAGER.exclusive_access().add(task);
    //println!("TASK_MANAGER in {:p}\n",&TASK_MANAGER);
    unsafe{
        TM_RQ=&TASK_MANAGER.exclusive_access().ready_queue as *const _ as usize;
    }    
}
///Interface offered to pop the first task
pub fn fetch_task() -> Option<Arc<TaskControlBlock>> {
    TASK_MANAGER.exclusive_access().fetch()
}