Skip to content

Commit bde4e35

Browse files
committed
Add ready queue dequeue path in mo_task_suspend()
Previously, mo_task_suspend() only changed the task state to TASK_SUSPENDED without removing the task from the ready queue. As a result, suspended tasks could still be selected by the scheduler, leading to incorrect task switching and inconsistent queue states. This change adds a dequeue operation to remove the corresponding task node from its ready queue before marking it as suspended. Additionally, the condition to detect the currently running task has been updated: the scheduler now compares the TCB pointer (kcb->task_current->data == task) instead of the list node (kcb->task_current == node), since kcb->task_current now stores a ready queue node rather than a global task list node. If the suspended task is currently running, the CPU will yield after the task is suspended to allow the scheduler to select the next runnable task. This ensures that suspended tasks are no longer visible to the scheduler until they are resumed.
1 parent 2957225 commit bde4e35

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

kernel/task.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ static void sched_enqueue_task(tcb_t *task)
387387
}
388388

389389
/* Remove task from ready queue; return removed ready queue node */
390-
static __attribute__((unused)) list_node_t *sched_dequeue_task(tcb_t *task)
390+
static list_node_t *sched_dequeue_task(tcb_t *task)
391391
{
392392
if (unlikely(!task))
393393
return NULL;
@@ -778,8 +778,15 @@ int32_t mo_task_suspend(uint16_t id)
778778
return ERR_TASK_CANT_SUSPEND;
779779
}
780780

781+
/* Remove task node from ready queue if task is in ready queue
782+
* (TASK_RUNNING/TASK_READY).*/
783+
if (task->state == TASK_READY || task->state == TASK_RUNNING) {
784+
list_node_t *rq_node = sched_dequeue_task(task);
785+
free(rq_node);
786+
}
787+
781788
task->state = TASK_SUSPENDED;
782-
bool is_current = (kcb->task_current == node);
789+
bool is_current = (kcb->task_current->data == task);
783790

784791
CRITICAL_LEAVE();
785792

0 commit comments

Comments
 (0)