Skip to content

Commit 75b4eeb

Browse files
committed
Refactor sched_enqueue_task() for O(1) scheduler support
Previously, sched_enqueue_task() only changed task state without inserting into ready queue. As a result, the scheduler could not select enqueued task for execution. This change pushes the task into the appropriate ready queue using list_pusback(), and initializes realated attribution such as the ready bitmap and RR cursor. The ready queue for corresponging task priority will be initialized at this enqueue path and never be released afterward. With this updated API, tasks can be enqueued into the ready queue and selected by cursor-based O(1) scheduler.
1 parent 6bd43f1 commit 75b4eeb

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

kernel/task.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static const uint8_t priority_timeslices[TASK_PRIORITY_LEVELS] = {
8282
TASK_TIMESLICE_IDLE /* Priority 7: Idle */
8383
};
8484

85-
/* Mark task as ready (state-based) */
85+
/* Enqueue task into ready queue */
8686
static void sched_enqueue_task(tcb_t *task);
8787

8888
/* Utility and Validation Functions */
@@ -349,17 +349,41 @@ void _yield(void) __attribute__((weak, alias("yield")));
349349
* practical performance with strong guarantees for fairness and reliability.
350350
*/
351351

352-
/* Add task to ready state - simple state-based approach */
352+
/* Enqueue task into ready queue */
353353
static void sched_enqueue_task(tcb_t *task)
354354
{
355355
if (unlikely(!task))
356356
return;
357357

358+
uint8_t prio_level = task->prio_level;
359+
358360
/* Ensure task has appropriate time slice for its priority */
359-
task->time_slice = get_priority_timeslice(task->prio_level);
361+
task->time_slice = get_priority_timeslice(prio_level);
360362
task->state = TASK_READY;
361363

362-
/* Task selection is handled directly through the master task list */
364+
list_t **rq = &kcb->harts->ready_queues[prio_level];
365+
list_node_t **cursor = &kcb->harts->rr_cursors[prio_level];
366+
367+
if (!*rq)
368+
*rq = list_create();
369+
370+
list_node_t *rq_node = list_pushback(*rq, task);
371+
if (unlikely(!rq_node))
372+
return;
373+
374+
/* Update task count in ready queue */
375+
kcb->harts->queue_counts[prio_level]++;
376+
377+
/* Setup first rr_cursor */
378+
if (!*cursor)
379+
*cursor = rq_node;
380+
381+
/* Advance cursor when cursor same as running task */
382+
if (*cursor == kcb->task_current)
383+
*cursor = rq_node;
384+
385+
BITMAP_SET(task->prio_level);
386+
return;
363387
}
364388

365389
/* Remove task from ready queues - state-based approach for compatibility */

0 commit comments

Comments
 (0)