Skip to content

Commit 4355ce8

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 fbfdfde commit 4355ce8

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
@@ -454,20 +454,15 @@ void sched_tick_current_task(void)
454454
}
455455
}
456456

457-
/* Task wakeup - simple state transition approach */
457+
/* Task wakeup and enqueue into ready queue */
458458
void sched_wakeup_task(tcb_t *task)
459459
{
460460
if (unlikely(!task))
461461
return;
462462

463-
/* Mark task as ready - scheduler will find it during round-robin traversal
464-
*/
465-
if (task->state != TASK_READY) {
466-
task->state = TASK_READY;
467-
/* Ensure task has time slice */
468-
if (task->time_slice == 0)
469-
task->time_slice = get_priority_timeslice(task->prio_level);
470-
}
463+
/* Enqueue task into ready queue */
464+
if (task->state != TASK_READY && task->state != TASK_RUNNING)
465+
sched_enqueue_task(task);
471466
}
472467

473468
/* Efficient Round-Robin Task Selection with O(n) Complexity
@@ -879,6 +874,10 @@ int32_t mo_task_cancel(uint16_t id)
879874
}
880875
}
881876

877+
/* Remove from ready queue */
878+
if (tcb->state == TASK_READY)
879+
sched_dequeue_task(tcb);
880+
882881
CRITICAL_LEAVE();
883882

884883
/* Free memory outside critical section */
@@ -908,7 +907,9 @@ void mo_task_delay(uint16_t ticks)
908907

909908
tcb_t *self = kcb->task_current->data;
910909

911-
/* Set delay and blocked state - scheduler will skip blocked tasks */
910+
/* Set delay and blocked state, dequeue from ready queue */
911+
sched_dequeue_task(self);
912+
912913
self->delay = ticks;
913914
self->state = TASK_BLOCKED;
914915
NOSCHED_LEAVE();
@@ -935,6 +936,11 @@ int32_t mo_task_suspend(uint16_t id)
935936
return ERR_TASK_CANT_SUSPEND;
936937
}
937938

939+
/* Remove task node from ready queue if task is in ready queue
940+
* (TASK_RUNNING/TASK_READY).*/
941+
if (task->state == TASK_READY || task->state == TASK_RUNNING)
942+
sched_dequeue_task(task);
943+
938944
task->state = TASK_SUSPENDED;
939945
bool is_current = (kcb->task_current == node);
940946

@@ -963,9 +969,8 @@ int32_t mo_task_resume(uint16_t id)
963969
CRITICAL_LEAVE();
964970
return ERR_TASK_CANT_RESUME;
965971
}
966-
967-
/* mark as ready - scheduler will find it */
968-
task->state = TASK_READY;
972+
/* Enqueue resumed task into ready queue */
973+
sched_enqueue_task(task);
969974

970975
CRITICAL_LEAVE();
971976
return ERR_OK;
@@ -1087,6 +1092,9 @@ void _sched_block(queue_t *wait_q)
10871092

10881093
tcb_t *self = kcb->task_current->data;
10891094

1095+
/* Remove node from ready queue */
1096+
sched_dequeue_task(self);
1097+
10901098
if (queue_enqueue(wait_q, self) != 0)
10911099
panic(ERR_SEM_OPERATION);
10921100

0 commit comments

Comments
 (0)