Skip to content

Commit 0da3eb0

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 0e1a10f commit 0da3eb0

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
@@ -847,12 +847,22 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
847847
return ERR_TASK_NOT_FOUND;
848848
}
849849

850+
bool is_current = (kcb->task_current->data == task);
851+
852+
/* Removed task from ready queue */
853+
if (task->state == TASK_RUNNING || task->state == TASK_READY)
854+
sched_migrate_task(task, priority);
855+
850856
/* Update priority and level */
851857
task->prio = priority;
852858
task->prio_level = extract_priority_level(priority);
853859
task->time_slice = get_priority_timeslice(task->prio_level);
854860

855861
CRITICAL_LEAVE();
862+
863+
if (is_current)
864+
mo_task_yield();
865+
856866
return ERR_OK;
857867
}
858868

0 commit comments

Comments
 (0)