Skip to content

Commit 34de951

Browse files
committed
fix(task): in choose_next_running_task, always clear running_task if it's in the Waiting state
Any app using `Kernel::sleep` can verify this fix.
1 parent 19c2855 commit 34de951

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/constance/src/kernel/task.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,26 @@ pub(super) fn choose_next_running_task<System: Kernel>(
600600
};
601601

602602
// The priority of the next task to run
603+
//
604+
// The default value is `usize::max_value() - 1` for the following reason:
605+
// If `running_task` is in the Waiting state and there's no other task to
606+
// schedule at the moment, we want to assign `None` to `running_task`. In
607+
// this case, if the default value was the same as `prev_task_priority`,
608+
// `prev_task_priority` would be equal to `next_task_priority`, and the
609+
// `if` statement below would return too early. We make sure this does not
610+
// happen by making the default value of `next_task_priority` lower.
611+
//
612+
// `usize::max_value()` never collides with an actual task priority because
613+
// of the priority range restriction imposed by `CfgBuilder::
614+
// num_task_priority_levels`.
603615
let next_task_priority = System::state()
604616
.task_ready_bitmap
605617
.read(&*lock)
606618
.find_set()
607-
.unwrap_or(usize::max_value());
619+
.unwrap_or(usize::max_value() - 1);
608620

609-
// Return if there's no task willing to take over the current one.
621+
// Return if there's no task willing to take over the current one, and
622+
// the current one can still run.
610623
if prev_task_priority <= next_task_priority {
611624
return;
612625
}

0 commit comments

Comments
 (0)