Skip to content

Commit c9a07e7

Browse files
committed
Use mo_task_migration() in mo_task_priority()
This change refactors the priority update process in mo_task_priority() to include early-return checks and proper task migration handling. - Early-return conditions: * Prevent modification of the idle task. * Disallow assigning TASK_PRIO_IDLE to non-idle tasks. The idle task is created by idle_task_init() during system startup and must retain its fixed priority. - Task migration: If the priority-changed task resides in a ready queue (TASK_READY or TASK_RUNNING), sched_migrate_task() is called to move it to the queue corresponding to the new priority. - Running task behavior: When the current running task changes its own priority, it yields the CPU so the scheduler can dispatch the next highest-priority task.
1 parent 142920e commit c9a07e7

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
@@ -856,12 +856,22 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
856856
return ERR_TASK_NOT_FOUND;
857857
}
858858

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

864870
CRITICAL_LEAVE();
871+
872+
if (is_current)
873+
mo_task_yield();
874+
865875
return ERR_OK;
866876
}
867877

0 commit comments

Comments
 (0)