Skip to content

Commit 9166c08

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 ab4bcb5 commit 9166c08

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
@@ -1015,12 +1015,22 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
10151015
return ERR_TASK_NOT_FOUND;
10161016
}
10171017

1018+
bool is_current = (kcb->task_current->data == task);
1019+
1020+
/* Removed task from ready queue */
1021+
if (task->state == TASK_RUNNING || task->state == TASK_READY)
1022+
sched_migrate_task(task, priority);
1023+
10181024
/* Update priority and level */
10191025
task->prio = priority;
10201026
task->prio_level = extract_priority_level(priority);
10211027
task->time_slice = get_priority_timeslice(task->prio_level);
10221028

10231029
CRITICAL_LEAVE();
1030+
1031+
if (is_current)
1032+
mo_task_yield();
1033+
10241034
return ERR_OK;
10251035
}
10261036

0 commit comments

Comments
 (0)