Skip to content

Conversation

@vicLin8712
Copy link
Collaborator

@vicLin8712 vicLin8712 commented Nov 19, 2025

O(1) scheduler: Task state transition relative APIs modifications

This PR integrates the modified sched_enqueue_task() and sched_dequeue_task() helpers introduced in #36 into the existing task state transition APIs.

This is part 2 of a 3-part stacked PR series, and depends on #36 (Part 1: infrastructure).

Summary

To support a ready-queue-based O(1) scheduler, task state transitions must explicitly maintain the ready queues by invoking the enqueue/dequeue helpers whenever a task moves into or out of the schedulable state set.

This PR updates all relevant task state transition paths to ensure that the ready queue remains consistent with the task’s state.

Detail

The task state transition of the original scheduler is shown below:
image

Only tasks in TASK_READY or TASK_RUNNING may be selected as the next runnable task.
All other task states are considered non-schedulable and may appear in arbitrary situations.

From this scheduling rule, task states naturally fall into two groups:

  • Schedulable: { TASK_RUNNING, TASK_READY }

  • Non-schedulable: { all other states }

Therefore, the enqueue/dequeue helper functions must be invoked whenever a task transitions between these two groups.
This ensures that the ready queue accurately reflects the set of tasks eligible for scheduling.

Code changes

This PR is part 2 of the stacked O(1) scheduler series.
Only the commits listed below are unique to this PR:

Verification

Linmo still operates under the original linear-search scheduler at this stage.
No scheduling behavior is changed; the ready queue is updated, but not yet used by the dispatch path introduced in later PRs.

All tests/applications in /apps build and run successfully.

Reference

#23 — Prior draft discussion
#36 — Part 1: O(1) scheduler infrastructure (base of this PR)


Summary by cubic

Updates task state transitions to keep O(1) ready queues in sync via enqueue/dequeue helpers with embedded rq_node. This is part 2 of the O(1) scheduler; no behavior change and all apps still build and run.

  • Refactors

    • Extended KCB with ready_queues[], ready_bitmap, queue_counts[], rr_cursors; added bitmap macros.
    • Added intrusive list helpers list_pushback_node() and list_remove_node(), and rq_node in tcb_t for ready-queue membership.
    • Refactored sched_enqueue_task() and sched_dequeue_task() to update per-priority queues, rr cursors, queue counts, and bitmap.
    • Integrated enqueue/dequeue into mo_task_resume(), mo_task_wakeup(), mo_task_suspend(), mo_task_delay(), mo_task_cancel(), and _sched_block().
  • Bug Fixes

    • Removed canceled/suspended/blocked tasks from ready queues to prevent accidental scheduling.
    • Advanced rr_cursors and removed nodes safely to avoid inconsistencies.

Written for commit 9ca3f90. Summary will update automatically on new commits.

@vicLin8712 vicLin8712 changed the title [2/4] O(1) scheduler: Task state transition relative APIs modifications [2/5] O(1) scheduler: Task state transition relative APIs modifications Nov 19, 2025
@vicLin8712 vicLin8712 changed the title [2/5] O(1) scheduler: Task state transition relative APIs modifications [2/3] O(1) scheduler: Task state transition relative APIs modifications Nov 19, 2025
This was referenced Nov 19, 2025
@jserv jserv changed the title [2/3] O(1) scheduler: Task state transition relative APIs modifications O(1) scheduler: Task state transition relative APIs modifications Nov 19, 2025
@jserv
Copy link
Contributor

jserv commented Nov 19, 2025

Do not include numbers in pull-request titles.

Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refine the git commits to ensure each one contains only minimal, functional changes.

@vicLin8712 vicLin8712 force-pushed the o1-sched-state-transition-APIs branch from ebee1c2 to ffd8c66 Compare November 19, 2025 11:39
@vicLin8712 vicLin8712 requested a review from jserv November 19, 2025 11:42
@jserv
Copy link
Contributor

jserv commented Nov 19, 2025

The commits below are really annoying:

  • Remove attribute((unused) from sched_dequeue_task()
  • Add ready queue dequeue path in mo_task_suspend()
  • Add ready queue dequeue path in mo_task_cancel()
  • Add sched_enqueue_task() in mo_task_resume()
  • Add ready queue enqueue path in mo_task_wakeup()
  • Add dequeuing ready queue path in mo_task_delay()
  • Add dequeuing ready queue path in _sched_block()

Read https://cbea.ms/git-commit/ carefully.

@vicLin8712 vicLin8712 force-pushed the o1-sched-state-transition-APIs branch from ffd8c66 to b00f4b3 Compare November 19, 2025 13:19
This commit extends the core scheduler data structures to support
the new O(1) scheduler design.

Adds in tcb_t:

 - rq_node: embedded list node for ready-queue membership used
   during task state transitions. This avoids redundant malloc/free
   for per-enqueue/dequeue nodes by tying the node's lifetime to
   the task control block.

Adds in kcb_t:

 - ready_bitmap: 8-bit bitmap tracking which priority levels have
   runnable tasks.
 - ready_queues[]: per-priority ready queues for O(1) task
   selection.
 - queue_counts[]: per-priority runnable task counters used for
   bookkeeping and consistency checks.
 - rr_cursors[]: round-robin cursor per priority level to support
   fair selection within the same priority.

These additions are structural only and prepare the scheduler for
O(1) ready-queue operations; they do not change behavior yet.
When a task is enqueued into or dequeued from the ready queue, the
bitmap that indicates the ready queue state should be updated.

These three marcos can be used in mo_task_dequeue() and
mo_task_enqueue() APIs to improve readability and maintain
consistency.
This commit introduces two helper functions for intrusive list
usage, where each task embeds its own list node instead of relying
on per-operation malloc/free.

The new APIs allow the scheduler to manipulate ready-queue nodes
directly:

 - list_pushback_node(): append an existing node to the end of the
   list (before the tail sentinel) without allocating memory.

 - list_remove_node(): remove a node from the list without freeing
   it, allowing the caller to control the node's lifetime.

These helpers will be used by the upcoming O(1) scheduler
enqueue/dequeue paths, which require embedded list nodes stored in
tcb_t.
This commit refactors sched_enqueue_task() and
sched_dequeue_task() to use the per-priority ready queues and the
embedded rq_node stored in tcb_t, instead of relying only on task
state inspection.

Tasks are now explicitly added to and removed from the appropriate
ready queue, and queue_counts, rr_cursors, and the ready_bitmap
are updated accordingly.
@vicLin8712 vicLin8712 force-pushed the o1-sched-state-transition-APIs branch from b00f4b3 to 9ca3f90 Compare November 19, 2025 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants