Skip to content

Commit 3ac26b9

Browse files
committed
Add enqueue path to sched_enqueue_task()
Previously, `sched_enqueue_task()` only marked task state as TASK_READY to represent the task has been enqueued due to the original scheduler selects the next task based on the global list and all tasks are kept in it. After new data structure, ready_queue[], is added for keeping runnable tasks, the enqueuing task API should push the embedded linkage list node, rq_node, into the corresponding ready_queue. This change aligns with the new task selection based on the ready queue.
1 parent eedb4c4 commit 3ac26b9

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

kernel/task.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static const uint8_t priority_timeslices[TASK_PRIORITY_LEVELS] = {
7474
TASK_TIMESLICE_IDLE /* Priority 7: Idle */
7575
};
7676

77-
/* Mark task as ready (state-based) */
77+
/* Enqueue task into ready queue */
7878
static void sched_enqueue_task(tcb_t *task);
7979

8080
/* Utility and Validation Functions */
@@ -357,17 +357,36 @@ void _yield(void) __attribute__((weak, alias("yield")));
357357
* practical performance with strong guarantees for fairness and reliability.
358358
*/
359359

360-
/* Add task to ready state - simple state-based approach */
360+
/* Enqueue task into ready queue */
361361
static void sched_enqueue_task(tcb_t *task)
362362
{
363363
if (unlikely(!task))
364364
return;
365365

366+
uint8_t prio_level = task->prio_level;
367+
366368
/* Ensure task has appropriate time slice for its priority */
367-
task->time_slice = get_priority_timeslice(task->prio_level);
369+
task->time_slice = get_priority_timeslice(prio_level);
368370
task->state = TASK_READY;
369371

370-
/* Task selection is handled directly through the master task list */
372+
list_t **rq = &kcb->ready_queues[prio_level];
373+
list_node_t **cursor = &kcb->rr_cursors[prio_level];
374+
375+
if (!*rq)
376+
*rq = list_create();
377+
378+
list_pushback_node(*rq, &task->rq_node);
379+
380+
/* Setup first rr_cursor */
381+
if (!*cursor)
382+
*cursor = &task->rq_node;
383+
384+
/* Advance cursor when cursor same as running task */
385+
if (*cursor == kcb->task_current)
386+
*cursor = &task->rq_node;
387+
388+
kcb->ready_bitmap |= (1U << (task->prio_level));
389+
return;
371390
}
372391

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

0 commit comments

Comments
 (0)