Skip to content
Snippets Groups Projects
Commit 7445ef1f authored by michael's avatar michael
Browse files

Use Iterator::position, extract task removal to a separate method, and...

Use Iterator::position, extract task removal to a separate method, and correctly set priority bitmask to reflect empty lists.
parent 02ccc140
No related branches found
No related tags found
No related merge requests found
...@@ -272,6 +272,30 @@ impl PriorityTaskQueue { ...@@ -272,6 +272,30 @@ impl PriorityTaskQueue {
task task
} }
/// Remove the task at index from the queue and return that task,
/// or None if the index is out of range or the list is empty.
fn remove_from_queue(
&mut self,
task_index: usize,
queue_index: usize,
) -> Option<Rc<RefCell<Task>>> {
//assert!(prio < NO_PRIORITIES, "Priority {} is too high", prio);
let queue = &mut self.queues[queue_index];
if task_index <= queue.len() {
// Calling remove is unstable: https://github.com/rust-lang/rust/issues/69210
let mut split_list = queue.split_off(task_index);
let element = split_list.pop_front();
queue.append(&mut split_list);
if queue.is_empty() {
self.prio_bitmap &= !(1 << queue_index as u64);
}
element
} else {
None
}
}
/// Pop the task with the highest priority from the queue /// Pop the task with the highest priority from the queue
pub fn pop(&mut self) -> Option<Rc<RefCell<Task>>> { pub fn pop(&mut self) -> Option<Rc<RefCell<Task>>> {
if let Some(i) = msb(self.prio_bitmap) { if let Some(i) = msb(self.prio_bitmap) {
...@@ -304,20 +328,15 @@ impl PriorityTaskQueue { ...@@ -304,20 +328,15 @@ impl PriorityTaskQueue {
/// Change priority of specific task /// Change priority of specific task
pub fn set_priority(&mut self, handle: TaskHandle, prio: Priority) -> Result<(), ()> { pub fn set_priority(&mut self, handle: TaskHandle, prio: Priority) -> Result<(), ()> {
let i = handle.get_priority().into() as usize; let old_priority = handle.get_priority().into() as usize;
if let Some(index) = self.queues[old_priority]
for (index, current_task) in self.queues[i].iter().enumerate() { .iter()
// Move the task from its old list to the new list. .position(|current_task| current_task.borrow().id == handle.id)
if handle.id == current_task.borrow().id { {
// Calling remove is unstable: https://github.com/rust-lang/rust/issues/69210 let Some(task) = self.remove_from_queue(index, old_priority) else { return Err(()) };
let mut split_list = self.queues[i].split_off(index); task.borrow_mut().prio = prio;
let task = split_list.pop_front().ok_or(())?; self.push(task);
self.queues[i].append(&mut split_list); return Ok(());
task.borrow_mut().prio = prio;
self.push(task);
return Ok(());
}
} }
Err(()) Err(())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment