Skip to content

Commit 168c94a

Browse files
committed
Refactor priority-change path and introduce sched_migrate_task()
This commit introduces the sched_migrate_task() helper, which handles migration of a task to the correct ready queue when its priority changes. If the task is already in a ready queue, the helper dequeues it from the old priority level, enqueues it into the new one, and updates all related bookkeeping. In addition, if a TASK_RUNNING task changes its priority, it now yields immediately. This ensures that the scheduler always executes tasks in strict priority order, preventing a running task from continuing to run at an outdated priority level.
1 parent 6935ab1 commit 168c94a

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

kernel/task.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,12 +857,22 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
857857
return ERR_TASK_NOT_FOUND;
858858
}
859859

860+
bool is_current = (kcb->task_current->data == task);
861+
862+
/* Removed task from ready queue */
863+
if (task->state == TASK_RUNNING || task->state == TASK_READY)
864+
sched_migrate_task(task, priority);
865+
860866
/* Update priority and level */
861867
task->prio = priority;
862868
task->prio_level = extract_priority_level(priority);
863869
task->time_slice = get_priority_timeslice(task->prio_level);
864870

865871
CRITICAL_LEAVE();
872+
873+
if (is_current)
874+
mo_task_yield();
875+
866876
return ERR_OK;
867877
}
868878

0 commit comments

Comments
 (0)