Skip to content

Commit 0d16104

Browse files
committed
Add sched_migrate_task() helper
This commit introduces a new API, sched_migrate_task(), which enables migration of a task between ready queues of different priority levels. The function safely removes the task from its current ready queue and enqueues it into the target queue, updating the corresponding RR cursor and ready bitmap to maintain scheduler consistency. This helper will be used in mo_task_priority() and other task management routines that adjust task priority dynamically. Future improvement: The current enqueue path allocates a new list node for each task insertion based on its TCB pointer. In the future, this can be optimized by directly transferring or reusing the existing list node between ready queues, eliminating the need for an additional malloc() and free() operations during priority migrations.
1 parent bcbac6b commit 0d16104

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

kernel/task.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,28 @@ static list_node_t *sched_dequeue_task(tcb_t *task)
411411
return rq_node;
412412
}
413413

414+
/* Task migration from origin to new priority ready queue */
415+
static void sched_migrate_task(tcb_t *task, int16_t priority)
416+
{
417+
if (unlikely(!task || !is_valid_priority(priority)))
418+
return;
419+
420+
if (task->prio == priority)
421+
return;
422+
423+
/* Unlink task node from origin ready queue */
424+
list_node_t *rq_node = sched_dequeue_task(task);
425+
free(rq_node);
426+
427+
/* Update new properties */
428+
task->prio = priority;
429+
task->prio_level = extract_priority_level(priority);
430+
431+
/* Enqueue into new priority ready queue*/
432+
sched_enqueue_task(task);
433+
return;
434+
}
435+
414436
/* Handle time slice expiration for current task */
415437
void sched_tick_current_task(void)
416438
{

0 commit comments

Comments
 (0)