Skip to content

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

Open
ytakano wants to merge 3 commits into
tier4:mainfrom
ytakano:fix/review-followups-814
Open

ytakano wants to merge 3 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
let mut info = next.info.lock(&mut node);

if !matches!(info.state, State::Terminated | State::Panicked) {
info.state = State::Running;

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.

RUNNING[cpu_id] points at next from preempt.rs:203. But next becomes Running only here or at preempt.rs:227, so other CPUs can briefly see a running task in Runnable or Preempted. Consider setting Running (with the Terminated | Panicked guard from this block) in the update_last_executed() critical section at preempt.rs:197-201 instead. That removes this block, the branch at preempt.rs:227 and one next.info lock, but the create_thread failure path then must restore Runnable.

// `next` resumes on its own thread without passing through
// `Scheduler::get_next()`, so mark it as running here.
if next_thread.is_some() {
task_info.state = super::State::Running;

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.

Unlike Scheduler::get_next(), this branch does not set need_preemption = false, although a task here is always Preempted. Nothing reads the flag today, so behavior does not change. Add task_info.need_preemption = false; here, or remove the write-only field in a follow-up.


// This is unnecessary if the task is scheduled by PrioritizedFIFO. This remains for other schedulers.
// `get_next_task()` has already stored the task ID.
// This store is redundant and remains as a safeguard.

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.

Because every path of get_next_task() already stores the ID, this store matters only when a future get_next() forgets set_current_task(). In that case it hides the bug from assert_eq!(running_id, task.id) below, so it does not act as a safeguard. Remove it, or replace it with debug_assert_eq!(RUNNING[cpu_id].load(Ordering::Relaxed), task.id);.

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.

I would say debug_assert_eq! is better, so it does not cause unconditional panics on release mode.

@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.

set_current_task(awkernel_lib::cpu::cpu_id(), task.id);
}
task_info.state = State::Running;
set_current_task(awkernel_lib::cpu::cpu_id(), task.id);

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.

Would it make sense to reuse cpu_id?

Suggested change
set_current_task(awkernel_lib::cpu::cpu_id(), task.id);
set_current_task(cpu_id, task.id);

set_current_task(awkernel_lib::cpu::cpu_id(), task.id);
}
task_info.state = State::Running;
set_current_task(awkernel_lib::cpu::cpu_id(), task.id);

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.

Would it make sense to reuse cpu_id?

Suggested change
set_current_task(awkernel_lib::cpu::cpu_id(), task.id);
set_current_task(cpu_id, task.id);

set_current_task(awkernel_lib::cpu::cpu_id(), task.task.id);
}
task_info.state = State::Running;
set_current_task(awkernel_lib::cpu::cpu_id(), task.task.id);

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.

Would it make sense to reuse cpu_id?

Suggested change
set_current_task(awkernel_lib::cpu::cpu_id(), task.task.id);
set_current_task(cpu_id, task.id);

// `invoke_preemption_tick` rotate the task out before
// it runs.
task_info.update_last_executed();
set_current_task(awkernel_lib::cpu::cpu_id(), rr_task.task.id);

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.

Would it make sense to reuse cpu_id?

Suggested change
set_current_task(awkernel_lib::cpu::cpu_id(), rr_task.task.id);
set_current_task(cpu_id, task.id);


// This is unnecessary if the task is scheduled by PrioritizedFIFO. This remains for other schedulers.
// `get_next_task()` has already stored the task ID.
// This store is redundant and remains as a safeguard.

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.

I would say debug_assert_eq! is better, so it does not cause unconditional panics on release mode.

This branch has not been deployed

No deployments
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