Skip to main content

strat9_kernel/process/sched_classes/
idle.rs

1// SPDX-License-Identifier: MPL-2.0
2
3use super::{CurrentRuntime, SchedClassRq};
4use crate::process::task::Task;
5use alloc::sync::Arc;
6
7pub struct IdleClassRq {
8    idle_task: Option<Arc<Task>>,
9}
10
11impl IdleClassRq {
12    /// Creates a new instance.
13    pub fn new() -> Self {
14        Self { idle_task: None }
15    }
16}
17
18impl SchedClassRq for IdleClassRq {
19    /// Performs the enqueue operation.
20    fn enqueue(&mut self, task: Arc<Task>) {
21        if let super::SchedPolicy::Idle = task.sched_policy() {
22            self.idle_task = Some(task);
23        }
24    }
25
26    /// Performs the len operation.
27    fn len(&self) -> usize {
28        if self.idle_task.is_some() {
29            1
30        } else {
31            0
32        }
33    }
34
35    /// Performs the pick next operation.
36    fn pick_next(&mut self) -> Option<Arc<Task>> {
37        self.idle_task.clone()
38    }
39
40    /// Updates current.
41    fn update_current(&mut self, _rt: &CurrentRuntime, _task: &Task, is_yield: bool) -> bool {
42        is_yield
43    }
44
45    /// Performs the remove operation.
46    fn remove(&mut self, task_id: crate::process::TaskId) -> bool {
47        if let Some(task) = &self.idle_task {
48            if task.id == task_id {
49                self.idle_task = None;
50                return true;
51            }
52        }
53        false
54    }
55}