Skip to content

Commit 40cbc78

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 9047532 commit 40cbc78

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
@@ -862,12 +862,22 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
862862
return ERR_TASK_NOT_FOUND;
863863
}
864864

865+
bool is_current = (kcb->task_current->data == task);
866+
867+
/* Removed task from ready queue */
868+
if (task->state == TASK_RUNNING || task->state == TASK_READY)
869+
sched_migrate_task(task, priority);
870+
865871
/* Update priority and level */
866872
task->prio = priority;
867873
task->prio_level = extract_priority_level(priority);
868874
task->time_slice = get_priority_timeslice(task->prio_level);
869875

870876
CRITICAL_LEAVE();
877+
878+
if (is_current)
879+
mo_task_yield();
880+
871881
return ERR_OK;
872882
}
873883

0 commit comments

Comments
 (0)