Skip to content

Commit

Permalink
Add num tasks in scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
hky1999 committed Sep 13, 2024
1 parent c8d25d9 commit 6ca9626
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/cfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,8 @@ impl<T> BaseScheduler for CFScheduler<T> {
false
}
}

fn num_tasks(&self) -> usize {
self.ready_queue.len()
}
}
22 changes: 20 additions & 2 deletions src/fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ impl<T> Deref for FifoTask<T> {
/// It internally uses a linked list as the ready queue.
pub struct FifoScheduler<T> {
ready_queue: List<Arc<FifoTask<T>>>,
num_tasks: usize,
}

impl<T> FifoScheduler<T> {
/// Creates a new empty [`FifoScheduler`].
pub const fn new() -> Self {
Self {
ready_queue: List::new(),
num_tasks: 0,
}
}
/// get the name of scheduler
Expand All @@ -77,18 +79,30 @@ impl<T> BaseScheduler for FifoScheduler<T> {
fn init(&mut self) {}

fn add_task(&mut self, task: Self::SchedItem) {
self.num_tasks += 1;
self.ready_queue.push_back(task);
}

fn remove_task(&mut self, task: &Self::SchedItem) -> Option<Self::SchedItem> {
unsafe { self.ready_queue.remove(task) }
let res = unsafe { self.ready_queue.remove(task) };
if res.is_some() {
// Only decrement the number of tasks if the task is removed.
self.num_tasks -= 1;
}
res
}

fn pick_next_task(&mut self) -> Option<Self::SchedItem> {
self.ready_queue.pop_front()
let res = self.ready_queue.pop_front();
if res.is_some() {
// Only decrement the number of tasks if the task is picked.
self.num_tasks -= 1;
}
res
}

fn put_prev_task(&mut self, prev: Self::SchedItem, _preempt: bool) {
self.num_tasks += 1;
self.ready_queue.push_back(prev);
}

Expand All @@ -99,4 +113,8 @@ impl<T> BaseScheduler for FifoScheduler<T> {
fn set_priority(&mut self, _task: &Self::SchedItem, _prio: isize) -> bool {
false
}

fn num_tasks(&self) -> usize {
self.num_tasks
}
}
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ pub trait BaseScheduler {
/// `current` is the current running task.
fn task_tick(&mut self, current: &Self::SchedItem) -> bool;

/// set priority for a task
/// Set priority for a task.
fn set_priority(&mut self, task: &Self::SchedItem, prio: isize) -> bool;

/// Returns the number of tasks in the scheduler.
fn num_tasks(&self) -> usize;
}
4 changes: 4 additions & 0 deletions src/round_robin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ impl<T, const S: usize> BaseScheduler for RRScheduler<T, S> {
fn set_priority(&mut self, _task: &Self::SchedItem, _prio: isize) -> bool {
false
}

fn num_tasks(&self) -> usize {
self.ready_queue.len()
}
}

0 comments on commit 6ca9626

Please sign in to comment.