Skip to content

Commit ddd1205

Browse files
committed
Refactor enqueuing logic for the new scheduler
This commit refactors mo_enqueue_task() to support the data structures for the new scheduler. The RR cursor must be advanced if it same as the running task, which only happens in one task in ready queue. RR cursor will point to new enqueued task for scheduler consistency.
1 parent 3a1f682 commit ddd1205

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

kernel/task.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static const uint8_t priority_timeslices[TASK_PRIORITY_LEVELS] = {
8686
TASK_TIMESLICE_IDLE /* Priority 7: Idle */
8787
};
8888

89-
/* Mark task as ready (state-based) */
89+
/* Enqueue task into ready queue */
9090
static void sched_enqueue_task(tcb_t *task);
9191

9292
/* Utility and Validation Functions */
@@ -369,17 +369,39 @@ void _yield(void) __attribute__((weak, alias("yield")));
369369
* practical performance with strong guarantees for fairness and reliability.
370370
*/
371371

372-
/* Add task to ready state - simple state-based approach */
372+
/* Enqueue task into ready queue */
373373
static void sched_enqueue_task(tcb_t *task)
374374
{
375375
if (unlikely(!task))
376376
return;
377377

378+
uint8_t prio_level = task->prio_level;
379+
378380
/* Ensure task has appropriate time slice for its priority */
379-
task->time_slice = get_priority_timeslice(task->prio_level);
381+
task->time_slice = get_priority_timeslice(prio_level);
380382
task->state = TASK_READY;
381383

382-
/* Task selection is handled directly through the master task list */
384+
list_t **rq = &kcb->ready_queues[prio_level];
385+
list_node_t **cursor = &kcb->rr_cursors[prio_level];
386+
387+
if (!*rq)
388+
*rq = list_create();
389+
390+
list_pushback_node(*rq, &task->rq_node);
391+
392+
/* Update task count in ready queue */
393+
kcb->queue_counts[prio_level]++;
394+
395+
/* Setup first rr_cursor */
396+
if (!*cursor)
397+
*cursor = &task->rq_node;
398+
399+
/* Advance cursor when cursor same as running task */
400+
if (*cursor == kcb->task_current)
401+
*cursor = &task->rq_node;
402+
403+
bitmap_set(task->prio_level);
404+
return;
383405
}
384406

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

0 commit comments

Comments
 (0)