Skip to content

Commit 9bc9235

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 8e7c1b3 commit 9bc9235

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
@@ -996,12 +996,30 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
996996
return ERR_TASK_NOT_FOUND;
997997
}
998998

999+
bool is_current = (kcb->task_current->data == task);
1000+
1001+
/* Removed task from ready queue */
1002+
if (task->state == TASK_RUNNING || task->state == TASK_READY) {
1003+
sched_dequeue_task(task);
1004+
1005+
/* Update new properties */
1006+
task->prio = priority;
1007+
task->prio_level = extract_priority_level(priority);
1008+
1009+
/* Enqueue task node into new priority ready queue*/
1010+
sched_enqueue_task(task);
1011+
}
1012+
9991013
/* Update priority and level */
10001014
task->prio = priority;
10011015
task->prio_level = extract_priority_level(priority);
10021016
task->time_slice = get_priority_timeslice(task->prio_level);
10031017

10041018
CRITICAL_LEAVE();
1019+
1020+
if (is_current)
1021+
mo_task_yield();
1022+
10051023
return ERR_OK;
10061024
}
10071025

0 commit comments

Comments
 (0)