Skip to content

Commit

Permalink
fix(threads): edge-case yielding with core-affinities
Browse files Browse the repository at this point in the history
  • Loading branch information
elenaf9 committed Oct 22, 2024
1 parent c7a1a0b commit 47a299d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/riot-rs-threads/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,14 @@ fn cleanup() -> ! {
/// "Yields" to another thread with the same priority.
pub fn yield_same() {
THREADS.with_mut(|mut threads| {
let Some(prio) = threads.current().map(|t| t.prio) else {
let Some(&mut Thread {
prio,
pid: _pid,
#[cfg(feature = "core-affinity")]
core_affinity: _affinity,
..
}) = threads.current()
else {
return;
};

Expand All @@ -632,11 +639,21 @@ pub fn yield_same() {
}

// On multicore, the current thread is removed from the runqueue, and then
// re-added **at the tail** in `sched` the next time the scheduler is triggered.
// re-added **at the tail** in `sched` the next time the scheduler is invoked.
// Simply triggering the scheduler therefore implicitly advances the runqueue.
#[cfg(feature = "multicore")]
if !threads.runqueue.is_empty(prio) {
schedule();

// Check if the yielding thread can continue their execution on another
// core that currently runs a lower priority thread.
// This is only necessary when core-affinities are enabled, because only
// then it is possible that a lower prio thread runs while a higher prio
// runqueue isn't empty.
#[cfg(feature = "core-affinity")]
if _affinity == CoreAffinity::no_affinity() {
threads.schedule_if_higher_prio(_pid, prio);
}
}
})
}
Expand Down

0 comments on commit 47a299d

Please sign in to comment.