Enable scheduler to fetch scheduling information directly from Thread

This commit is contained in:
jellllly420
2024-09-23 15:35:21 +08:00
committed by Tate, Hongliang Tian
parent 9cc63149f1
commit 5c8e369057
3 changed files with 96 additions and 88 deletions

View File

@ -2,11 +2,13 @@
use alloc::{boxed::Box, collections::VecDeque, sync::Arc, vec::Vec};
use super::{inject_scheduler, EnqueueFlags, LocalRunQueue, Scheduler, UpdateFlags};
use super::{
info::CommonSchedInfo, inject_scheduler, EnqueueFlags, LocalRunQueue, Scheduler, UpdateFlags,
};
use crate::{
cpu::{num_cpus, PinCurrentCpu},
sync::SpinLock,
task::{disable_preempt, AtomicCpuId, Task},
task::{disable_preempt, Task},
};
pub fn init() {
@ -16,12 +18,12 @@ pub fn init() {
}
/// A simple FIFO (First-In-First-Out) task scheduler.
struct FifoScheduler<T: FifoSchedInfo> {
struct FifoScheduler<T: CommonSchedInfo> {
/// A thread-safe queue to hold tasks waiting to be executed.
rq: Vec<SpinLock<FifoRunQueue<T>>>,
}
impl<T: FifoSchedInfo> FifoScheduler<T> {
impl<T: CommonSchedInfo> FifoScheduler<T> {
/// Creates a new instance of `FifoScheduler`.
fn new(nr_cpus: u32) -> Self {
let mut rq = Vec::new();
@ -37,7 +39,7 @@ impl<T: FifoSchedInfo> FifoScheduler<T> {
}
}
impl<T: FifoSchedInfo + Send + Sync> Scheduler<T> for FifoScheduler<T> {
impl<T: CommonSchedInfo + Send + Sync> Scheduler<T> for FifoScheduler<T> {
fn enqueue(&self, runnable: Arc<T>, flags: EnqueueFlags) -> Option<u32> {
let mut still_in_rq = false;
let target_cpu = {
@ -77,12 +79,12 @@ impl<T: FifoSchedInfo + Send + Sync> Scheduler<T> for FifoScheduler<T> {
}
}
struct FifoRunQueue<T: FifoSchedInfo> {
struct FifoRunQueue<T: CommonSchedInfo> {
current: Option<Arc<T>>,
queue: VecDeque<Arc<T>>,
}
impl<T: FifoSchedInfo> FifoRunQueue<T> {
impl<T: CommonSchedInfo> FifoRunQueue<T> {
pub const fn new() -> Self {
Self {
current: None,
@ -91,7 +93,7 @@ impl<T: FifoSchedInfo> FifoRunQueue<T> {
}
}
impl<T: FifoSchedInfo> LocalRunQueue<T> for FifoRunQueue<T> {
impl<T: CommonSchedInfo> LocalRunQueue<T> for FifoRunQueue<T> {
fn current(&self) -> Option<&Arc<T>> {
self.current.as_ref()
}
@ -119,13 +121,3 @@ impl Default for FifoScheduler<Task> {
Self::new(num_cpus())
}
}
impl FifoSchedInfo for Task {
fn cpu(&self) -> &AtomicCpuId {
&self.schedule_info().cpu
}
}
trait FifoSchedInfo {
fn cpu(&self) -> &AtomicCpuId;
}

View File

@ -4,6 +4,8 @@
use core::sync::atomic::{AtomicU32, Ordering};
use crate::task::Task;
/// Fields of a task that OSTD will never touch.
///
/// The type ought to be defined by the OSTD user and injected into the task.
@ -61,3 +63,15 @@ impl Default for AtomicCpuId {
Self::new(Self::NONE)
}
}
impl CommonSchedInfo for Task {
fn cpu(&self) -> &AtomicCpuId {
&self.schedule_info().cpu
}
}
/// Trait for fetching common scheduling information.
pub trait CommonSchedInfo {
/// Gets the CPU that the task is running on or lately ran on.
fn cpu(&self) -> &AtomicCpuId;
}