Skip to content

Commit fdfd6b5

Browse files
committed
Refactor task state–related operation APIs
This commit refactors all task operation APIs that are related to task state transitions to support the new scheduler. The simplified mo_enqueue_task() and mo_dequeue_task() routines are now invoked directly inside these operations. Enqueue and dequeue actions are performed only when the state transition crosses the following groups: {TASK_RUNNING, TASK_READY} ↔ {other states} The sections below describe the detailed changes for each API: - sched_wakeup_task(): Add TASK_RUNNING as part of the state-group complement, avoid running tasks enqueue again. - mo_task_cancel(): Cancel all tasks except TASK_RUNNING. If the task is in TASK_READY, mo_dequeue_task() is invoked before cancellation. - mo_task_delay(): Transition from TASK_RUNNING to TASK_BLOCKED; call mo_dequeue_task() accordingly. - mo_task_suspend(): This API can be called for both TASK_RUNNING and TASK_READY tasks. Both conditions require invoking mo_dequeue_task() before transitioning to TASK_SUSPEND. - mo_task_resume(): Transition from TASK_SUSPEND to TASK_READY; call mo_enqueue_task(). - _sched_block(): Invoked only when a TASK_RUNNING task calls mutex- related APIs; performs the TASK_RUNNING → TASK_BLOCKED transition.
1 parent 94da1c9 commit fdfd6b5

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

kernel/task.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -428,20 +428,15 @@ void sched_tick_current_task(void)
428428
}
429429
}
430430

431-
/* Task wakeup - simple state transition approach */
431+
/* Task wakeup and enqueue into ready queue */
432432
void sched_wakeup_task(tcb_t *task)
433433
{
434434
if (unlikely(!task))
435435
return;
436436

437-
/* Mark task as ready - scheduler will find it during round-robin traversal
438-
*/
439-
if (task->state != TASK_READY) {
440-
task->state = TASK_READY;
441-
/* Ensure task has time slice */
442-
if (task->time_slice == 0)
443-
task->time_slice = get_priority_timeslice(task->prio_level);
444-
}
437+
/* Enqueue task into ready queue */
438+
if (task->state != TASK_READY && task->state != TASK_RUNNING)
439+
sched_enqueue_task(task);
445440
}
446441

447442
/* Efficient Round-Robin Task Selection with O(n) Complexity
@@ -721,6 +716,10 @@ int32_t mo_task_cancel(uint16_t id)
721716
}
722717
}
723718

719+
/* Remove from ready queue */
720+
if (tcb->state == TASK_READY)
721+
sched_dequeue_task(tcb);
722+
724723
CRITICAL_LEAVE();
725724

726725
/* Free memory outside critical section */
@@ -750,7 +749,9 @@ void mo_task_delay(uint16_t ticks)
750749

751750
tcb_t *self = kcb->task_current->data;
752751

753-
/* Set delay and blocked state - scheduler will skip blocked tasks */
752+
/* Set delay and blocked state, dequeue from ready queue */
753+
sched_dequeue_task(self);
754+
754755
self->delay = ticks;
755756
self->state = TASK_BLOCKED;
756757
NOSCHED_LEAVE();
@@ -777,6 +778,11 @@ int32_t mo_task_suspend(uint16_t id)
777778
return ERR_TASK_CANT_SUSPEND;
778779
}
779780

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+
sched_dequeue_task(task);
785+
780786
task->state = TASK_SUSPENDED;
781787
bool is_current = (kcb->task_current == node);
782788

@@ -805,9 +811,8 @@ int32_t mo_task_resume(uint16_t id)
805811
CRITICAL_LEAVE();
806812
return ERR_TASK_CANT_RESUME;
807813
}
808-
809-
/* mark as ready - scheduler will find it */
810-
task->state = TASK_READY;
814+
/* Enqueue resumed task into ready queue */
815+
sched_enqueue_task(task);
811816

812817
CRITICAL_LEAVE();
813818
return ERR_OK;
@@ -921,6 +926,9 @@ void _sched_block(queue_t *wait_q)
921926

922927
tcb_t *self = kcb->task_current->data;
923928

929+
/* Remove node from ready queue */
930+
sched_dequeue_task(self);
931+
924932
if (queue_enqueue(wait_q, self) != 0)
925933
panic(ERR_SEM_OPERATION);
926934

0 commit comments

Comments
 (0)