Skip to content

Commit

Permalink
fix(task): don't access scheduler from BlockedTaskQueue::wakeup_task
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Sep 14, 2023
1 parent 599e094 commit 253ca62
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
12 changes: 9 additions & 3 deletions src/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ impl PerCoreScheduler {
pub fn handle_waiting_tasks(&mut self) {
without_interrupts(|| {
crate::executor::run();
self.blocked_tasks.handle_waiting_tasks();
for task in self.blocked_tasks.handle_waiting_tasks() {
self.ready_queue.push(task);
}
});
}

Expand All @@ -305,7 +307,10 @@ impl PerCoreScheduler {
#[cfg(feature = "smp")]
pub fn custom_wakeup(&mut self, task: TaskHandle) {
if task.get_core_id() == self.core_id {
without_interrupts(|| self.blocked_tasks.custom_wakeup(task));
without_interrupts(|| {
let task = self.blocked_tasks.custom_wakeup(task);
self.ready_queue.push(task);
});
} else {
get_scheduler_input(task.get_core_id())
.lock()
Expand Down Expand Up @@ -447,7 +452,8 @@ impl PerCoreScheduler {
let mut input_locked = CoreLocal::get().scheduler_input.lock();

while let Some(task) = input_locked.wakeup_tasks.pop_front() {
self.blocked_tasks.custom_wakeup(task);
let task = self.blocked_tasks.custom_wakeup(task);
self.ready_queue.push(task);
}

while let Some(new_task) = input_locked.new_tasks.pop_front() {
Expand Down
54 changes: 26 additions & 28 deletions src/scheduler/task.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::boxed::Box;
use alloc::collections::{LinkedList, VecDeque};
use alloc::rc::Rc;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::cmp::Ordering;
use core::fmt;
Expand Down Expand Up @@ -463,31 +464,26 @@ impl BlockedTaskQueue {
}

fn wakeup_task(task: Rc<RefCell<Task>>) {
{
let mut borrowed = task.borrow_mut();
debug!(
"Waking up task {} on core {}",
borrowed.id, borrowed.core_id
);

assert!(
borrowed.core_id == core_id(),
"Try to wake up task {} on the wrong core {} != {}",
borrowed.id,
borrowed.core_id,
core_id()
);
let mut borrowed = task.borrow_mut();
debug!(
"Waking up task {} on core {}",
borrowed.id, borrowed.core_id
);

assert!(
borrowed.status == TaskStatus::Blocked,
"Trying to wake up task {} which is not blocked",
borrowed.id
);
borrowed.status = TaskStatus::Ready;
}
assert!(
borrowed.core_id == core_id(),
"Try to wake up task {} on the wrong core {} != {}",
borrowed.id,
borrowed.core_id,
core_id()
);

// Add the task to the ready queue.
core_scheduler().ready_queue.push(task);
assert!(
borrowed.status == TaskStatus::Blocked,
"Trying to wake up task {} which is not blocked",
borrowed.id
);
borrowed.status = TaskStatus::Ready;
}

#[cfg(any(feature = "tcp", feature = "udp"))]
Expand Down Expand Up @@ -574,7 +570,7 @@ impl BlockedTaskQueue {
}

/// Manually wake up a blocked task.
pub fn custom_wakeup(&mut self, task: TaskHandle) {
pub fn custom_wakeup(&mut self, task: TaskHandle) -> Rc<RefCell<Task>> {
let mut first_task = true;
let mut cursor = self.list.cursor_front_mut();

Expand Down Expand Up @@ -624,9 +620,9 @@ impl BlockedTaskQueue {
}

// Wake it up.
Self::wakeup_task(task_ref);
Self::wakeup_task(task_ref.clone());

return;
return task_ref;
}

first_task = false;
Expand All @@ -640,7 +636,7 @@ impl BlockedTaskQueue {
///
/// Should be called by the One-Shot Timer interrupt handler when the wakeup time for
/// at least one task has elapsed.
pub fn handle_waiting_tasks(&mut self) {
pub fn handle_waiting_tasks(&mut self) -> Vec<Rc<RefCell<Task>>> {
// Get the current time.
let time = arch::processor::get_timer_ticks();

Expand Down Expand Up @@ -695,8 +691,10 @@ impl BlockedTaskQueue {
.map_or_else(|| None, |node| node.wakeup_time),
);

for task in tasks {
for task in tasks.iter().cloned() {
Self::wakeup_task(task);
}

tasks
}
}

0 comments on commit 253ca62

Please sign in to comment.