os/task/
manager.rs

1//!Implementation of [`TaskManager`]
2use super::TaskControlBlock;
3use crate::sync::UPSafeCell;
4use alloc::collections::VecDeque;
5use alloc::sync::Arc;
6use lazy_static::*;
7///A array of `TaskControlBlock` that is thread-safe
8pub struct TaskManager {
9    ready_queue: VecDeque<Arc<TaskControlBlock>>,
10}
11
12/// A simple FIFO scheduler.
13impl TaskManager {
14    ///Creat an empty TaskManager
15    pub fn new() -> Self {
16        Self {
17            ready_queue: VecDeque::new(),
18        }
19    }
20    ///Add a task to `TaskManager`
21    pub fn add(&mut self, task: Arc<TaskControlBlock>) {
22        self.ready_queue.push_back(task);
23    }
24    ///Remove the first task and return it,or `None` if `TaskManager` is empty
25    pub fn fetch(&mut self) -> Option<Arc<TaskControlBlock>> {
26        self.ready_queue.pop_front()
27    }
28}
29
30lazy_static! {
31    pub static ref TASK_MANAGER: UPSafeCell<TaskManager> =
32        unsafe { UPSafeCell::new(TaskManager::new()) };
33}
34///Interface offered to add task
35pub fn add_task(task: Arc<TaskControlBlock>) {
36    TASK_MANAGER.exclusive_access().add(task);
37}
38///Interface offered to pop the first task
39pub fn fetch_task() -> Option<Arc<TaskControlBlock>> {
40    TASK_MANAGER.exclusive_access().fetch()
41}