refactor(threads): avoid unnecessary scheduler invocations #459
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR implements some changes and checks to avoid unnecessary scheduler invocations:
set_state
as a central pointThreadState::Running
, only trigger the scheduler if the thread has a higher priority than the currently running one (new)ThreadState::Running
into another state because this only ever happens if a currently running thread gets blockedyield_now
if the runqueue contains any other threadsIssues/PRs references
Extracted from #397.
Benchmarks
(ticks per iteration)
bench_sched_flags
is PR'd in #456nRF52840dk:
bench_sched_yield
with two threadsbench_sched_yield
with one threadbench_sched_flags
RPI-Pico
bench_sched_yield
with two threadsbench_sched_yield
with one threadbench_sched_flags
The additional checks come a some cost when the scheduler invocation is really needed, as visible in the
bench_sched_yield
benchmark.But I think this is tolerable, because the actual context switching logic (i.e.
sched
) is not affected, and in many cases we save an unnecessary scheduler call that would be much more expensive.Open Questions
See self review.
Change checklist