Skip to content

fix(task): mark a task taken by preemption as running - #817

Merged
ytakano merged 5 commits into
tier4:mainfrom
ytakano:fix/review-followups-814
Dec 1, 2026
Merged

ytakano merged 5 commits into
tier4:mainfrom
ytakano:fix/review-followups-814

Conversation

@ytakano

@ytakano ytakano commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR fixes the three points found in the code review of the change for #814, and fixes the output directory of make docs.

This PR is stacked on the PR for #814 (branch refactor/remove-execution-ensured). Please merge that PR first.

1. Mark a task taken by preemption as running

A task that do_preemption() starts was not marked as running, unlike a task taken by Scheduler::get_next().

  • On a new thread, the task ran with the state Runnable.
  • When the task resumed on its own thread, it ran with the state Preempted.

Task::wake() handles Running, Runnable and Preempted in the same way, so no wrong behavior was found. The fix makes the state consistent with Scheduler::get_next().

  • awkernel_async_lib/src/task.rs, get_next_task(): set State::Running for a task taken from preempt::get_next_task(). A task in Terminated or Panicked is not changed.
  • awkernel_async_lib/src/task/preempt.rs, do_preemption(): set State::Running for a preempted task that resumes on its own thread.

2. Reword a comment in run_main()

The comment on RUNNING[cpu_id].store(task.id, ...) said that the store is needed for schedulers other than PrioritizedFIFO. Every path of get_next_task() now stores the task ID, so the comment says that the store is redundant and remains as a safeguard. The store itself is not removed.

3. Sync the Scheduler trait in the mdbook

mdbook/src/internal/scheduler.md did not have get_next_if() and queued_cpu_mask(). The trait in the page now matches awkernel_async_lib/src/scheduler.rs.

4. Build the mdbook into awkernel/docs

mdbook v0.5 resolves the relative path of -d from the current directory, so make docs wrote the book outside the repository. The Makefile now passes $(CURDIR)/docs.

docs/ is regenerated with mdbook v0.5.4.

Related links

How was this PR tested?

  • scripts/ci.sh passes on my machine.

scripts/ci.sh was not run. The following were run instead.

  • make clippy: no warnings or errors on all targets.

  • cargo +nightly-2026-11-09 test_awkernel_async_lib passes.

  • QEMU x86_64 (16 CPUs, release build), no panic:

    • test_multi_prioritized_scheduler: all 30 tasks started and finished.
    • test_prioritized_rr: in about 57 seconds, each of the 24 high-priority tasks ran 503 to 517 times. This is the same as before the fix.
  • The state of running tasks was measured on QEMU x86_64 (16 CPUs, release build) with test_prioritized_rr for 60 seconds. Temporary code counted the state at two points. The code is not included in this PR.

    Point State Before (3150007d) After (this PR)
    In run_main(), just before the poll Running 110 10,441
    Runnable 11,692 0
    Just after a thread resumes from preemption Running 11,677 170,379
    Preempted 160,165 0

    Before the fix, most tasks ran with the state Runnable or Preempted. After the fix, all tasks ran with the state Running. "Before" is the commit for Remove the execution_ensured parameter of Scheduler::get_next() and `get_next_task() #814, which has the same code as main on this path. main itself was not measured.

  • make docs writes the book to awkernel/docs, and docs/internal/scheduler.html has get_next_if and queued_cpu_mask.

Notes for reviewers

  • The commits are separated. fix(task): mark a task taken by preemption as running has the code and mdbook source changes. fix(docs): build mdbook into awkernel/docs and regenerate it has the Makefile change and the generated files (69 files).
  • The diff of docs/ is large because mdbook v0.5.4 adds a hash to the file names of CSS, JavaScript and fonts. The only change in the content is docs/internal/scheduler.html.
  • The SPIN model specification/awkernel_async_lib/src/task/preemptive_spin/preemptive.pml is not updated. The model does not set the state to Running on the preemption path, so it now differs from the code in point 1. The model will be updated and verified in a separate PR.

ytakano and others added 3 commits November 25, 2026 18:32
…xt() (tier4#814)

After tier4#809, `get_next_task()` is always called with
`execution_ensured == true`, so the parameter and the `false` branches
of the schedulers are dead code.

Remove the parameter from `Scheduler::get_next()`,
`scheduler::get_next_task()`, and `task::get_next_task()`. The dequeued
task is now unconditionally marked as running. The behavior does not
change.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A task that `do_preemption()` starts was not marked as running, unlike
a task taken by `Scheduler::get_next()`. It ran with the state
`Runnable` on a new thread, or `Preempted` when it resumed on its own
thread.

- `task::get_next_task()`: set `State::Running` for a task taken from
  `preempt::get_next_task()`.
- `do_preemption()`: set `State::Running` for a preempted task that
  resumes on its own thread.
- `run_main()`: reword the comment on the redundant `RUNNING` store.
- mdbook: sync the `Scheduler` trait with the source.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
mdbook v0.5 resolves the relative path of `-d` from the current
directory, so `make docs` wrote the book outside the repository.
Pass an absolute path instead, and regenerate `docs/` with
mdbook v0.5.4.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ytakano
ytakano marked this pull request as ready for review November 25, 2026 09:49
Comment thread awkernel_async_lib/src/task.rs Resolved
Comment thread awkernel_async_lib/src/task/preempt.rs Resolved
Comment thread awkernel_async_lib/src/task.rs Resolved

@romancardenas romancardenas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! @atsushi421 found a few issues that should be considered. Probably, the one about moving the task state change in preemptions to the update_last_executed() critical section at preempt.rs:197-201.

Comment thread awkernel_async_lib/src/task.rs Resolved
Address review comments on tier4#817.

- Mark `next` as `Running` (and clear `need_preemption` if it was
  `Preempted`) in the same critical section as `update_last_executed()`,
  before its ID is stored in `RUNNING`. Other CPUs no longer see a running
  task in `Runnable` or `Preempted`, and the separate markings in
  `get_next_task()` and the preempt-context branch are removed.
- On `create_thread` failure, revert `next` to `Runnable` and restore
  `RUNNING[cpu_id]` to the current task. Previously the stale ID made the
  `assert_eq!` in `run_main` panic after the current task's poll.
- Replace the redundant `RUNNING` store in `run_main` with
  `debug_assert_eq!` so a missing `set_current_task()` is detected
  instead of hidden.
- Reuse the local `cpu_id` in `ClusteredScheduler::get_next()`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@romancardenas romancardenas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@ytakano
ytakano merged commit 4fb7b21 into tier4:main Dec 1, 2026
1 check passed
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.

3 participants