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 b68eb13
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
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()
}
}
10 changes: 10 additions & 0 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 @@ -78,17 +80,21 @@ impl<T> BaseScheduler for FifoScheduler<T> {

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

fn remove_task(&mut self, task: &Self::SchedItem) -> Option<Self::SchedItem> {
self.num_tasks -= 1;
unsafe { self.ready_queue.remove(task) }
}

fn pick_next_task(&mut self) -> Option<Self::SchedItem> {
self.num_tasks -= 1;
self.ready_queue.pop_front()
}

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 +105,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 b68eb13

Please sign in to comment.