Skip to content

Commit 95d9f81

Browse files
committed
Add dequeue path in sched_dequeue_task()
Previously, sched_dequeue_task() was a no-op stub, which was sufficient when the scheduler selected tasks from the global list. Since new data structure, ready_queue, is added for keeping all runnable tasks, a dequeue path is required to remove tasks from ready queue to ensure it always holds runnable tasks. This change aligns with the non-runnable task should not in the ready queue.
1 parent 3ac26b9 commit 95d9f81

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

kernel/task.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,32 @@ static void sched_enqueue_task(tcb_t *task)
389389
return;
390390
}
391391

392-
/* Remove task from ready queues - state-based approach for compatibility */
392+
/* Remove task from ready queue */
393393
void sched_dequeue_task(tcb_t *task)
394394
{
395395
if (unlikely(!task))
396396
return;
397397

398-
/* For tasks that need to be removed from ready state (suspended/cancelled),
399-
* we rely on the state change. The scheduler will skip non-ready tasks
400-
* when it encounters them during the round-robin traversal.
401-
*/
398+
uint8_t prio_level = task->prio_level;
399+
400+
/* For task that need to be removed from ready/running state, it need be
401+
* removed from corresponding ready queue. */
402+
list_t *rq = kcb->ready_queues[prio_level];
403+
list_node_t **cursor = &kcb->rr_cursors[prio_level];
404+
405+
/* Safely move cursor to next task node. */
406+
if (&task->rq_node == *cursor)
407+
*cursor = list_cnext(rq, *cursor);
408+
409+
/* Remove ready queue node */
410+
list_remove_node(rq, &task->rq_node);
411+
412+
/* Update task count in ready queue */
413+
if (rq->length == 0) {
414+
*cursor = NULL;
415+
kcb->ready_bitmap &= ~(1U << (task->prio_level));
416+
}
417+
return;
402418
}
403419

404420
/* Handle time slice expiration for current task */

0 commit comments

Comments
 (0)