Skip to content

Commit b2ebd49

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 f0290e5 commit b2ebd49

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
@@ -80,7 +80,7 @@ static const uint8_t priority_timeslices[TASK_PRIORITY_LEVELS] = {
8080
TASK_TIMESLICE_IDLE /* Priority 7: Idle */
8181
};
8282

83-
/* Mark task as ready (state-based) */
83+
/* Enqueue task into ready queue */
8484
static void sched_enqueue_task(tcb_t *task);
8585

8686
/* Utility and Validation Functions */
@@ -347,17 +347,39 @@ void _yield(void) __attribute__((weak, alias("yield")));
347347
* practical performance with strong guarantees for fairness and reliability.
348348
*/
349349

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

356+
uint8_t prio_level = task->prio_level;
357+
356358
/* Ensure task has appropriate time slice for its priority */
357-
task->time_slice = get_priority_timeslice(task->prio_level);
359+
task->time_slice = get_priority_timeslice(prio_level);
358360
task->state = TASK_READY;
359361

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

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

0 commit comments

Comments
 (0)