Skip to content

Commit 0353b5c

Browse files
committed
Refactor priority-change path in mo_task_priority()
Previously, mo_task_priority() only updated the task’s time slice and priority level. With the new scheduler design, tasks are kept in per-priority ready queues, so mo_task_priority() must also handle migrating tasks between these queues. This commit adds dequeue/enqueue logic for tasks in TASK_RUNNING or TASK_READY state, as such tasks must reside in a ready queue and a priority change implies ready-queue migration. The priority fields are still updated as part of the migration path: sched_dequeue_task() relies on the current priority, while the enqueue operation needs the new priority. Therefore, the priority update is performed between the dequeue and enqueue steps. If the priority change happens while the task is running, it must yield immediately to preserve the scheduler’s strict task-ordering policy.
1 parent 2be737a commit 0353b5c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

kernel/task.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,12 +982,30 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
982982
return ERR_TASK_NOT_FOUND;
983983
}
984984

985+
bool is_current = (kcb->task_current->data == task);
986+
987+
/* Removed task from ready queue */
988+
if (task->state == TASK_RUNNING || task->state == TASK_READY) {
989+
sched_dequeue_task(task);
990+
991+
/* Update new properties */
992+
task->prio = priority;
993+
task->prio_level = extract_priority_level(priority);
994+
995+
/* Enqueue task node into new priority ready queue*/
996+
sched_enqueue_task(task);
997+
}
998+
985999
/* Update priority and level */
9861000
task->prio = priority;
9871001
task->prio_level = extract_priority_level(priority);
9881002
task->time_slice = get_priority_timeslice(task->prio_level);
9891003

9901004
CRITICAL_LEAVE();
1005+
1006+
if (is_current)
1007+
mo_task_yield();
1008+
9911009
return ERR_OK;
9921010
}
9931011

0 commit comments

Comments
 (0)