From 4b6ae5dc1048fa55e1c88c3d62b10ae94594ab16 Mon Sep 17 00:00:00 2001 From: Elena Frank Date: Sat, 7 Sep 2024 13:31:57 +0200 Subject: [PATCH] refactor(threads): move `sched`-independent code to `Threads` --- examples/threading-multicore/laze.yml | 1 + src/riot-rs-threads/src/arch/cortex_m.rs | 41 +++--------------------- src/riot-rs-threads/src/lib.rs | 40 +++++++++++++++++++++++ 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/examples/threading-multicore/laze.yml b/examples/threading-multicore/laze.yml index d647c3323..1c02609e1 100644 --- a/examples/threading-multicore/laze.yml +++ b/examples/threading-multicore/laze.yml @@ -2,3 +2,4 @@ apps: - name: threading-multicore selects: - ?release + - executor-thread diff --git a/src/riot-rs-threads/src/arch/cortex_m.rs b/src/riot-rs-threads/src/arch/cortex_m.rs index 02642c7f9..035af797b 100644 --- a/src/riot-rs-threads/src/arch/cortex_m.rs +++ b/src/riot-rs-threads/src/arch/cortex_m.rs @@ -3,7 +3,7 @@ use core::arch::asm; use core::ptr::write_volatile; use cortex_m::peripheral::{scb::SystemHandler, SCB}; -use crate::{cleanup, ThreadId, THREADS}; +use crate::{cleanup, THREADS}; #[cfg(not(any(armv6m, armv7m, armv8m)))] compile_error!("no supported ARM variant selected"); @@ -161,30 +161,6 @@ unsafe extern "C" fn PendSV() { }; } -fn get_next_pid(threads: &mut crate::Threads) -> Option { - #[cfg(not(feature = "multicore"))] - { - return threads.runqueue.get_next(); - } - #[cfg(feature = "multicore")] - #[cfg(not(feature = "core-affinity"))] - { - return threads.runqueue.pop_next(); - } - #[cfg(feature = "core-affinity")] - { - let (mut next, prio) = threads.runqueue.peek_next()?; - if !threads.is_affine_to_curr_core(next) { - let iter = threads.runqueue.iter_from(next, prio); - next = iter - .filter(|pid| threads.is_affine_to_curr_core(*pid)) - .next()? - } - threads.runqueue.del(next); - return Some(next); - }; -} - /// Schedule the next thread. /// /// It selects the next thread that should run from the runqueue. @@ -201,22 +177,13 @@ fn get_next_pid(threads: &mut crate::Threads) -> Option { #[no_mangle] unsafe fn sched() -> u128 { #[cfg(feature = "multicore")] - critical_section::with(|cs| { - let threads = unsafe { &mut *THREADS.as_ptr(cs) }; - let Some(thread) = threads.current() else { - return; - }; - if thread.state == crate::ThreadState::Running { - let prio = thread.prio; - let pid = thread.pid; - threads.runqueue.add(pid, prio); - } - }); + THREADS.with_mut(|mut threads| threads.add_current_thread_to_rq()); + loop { if let Some(res) = critical_section::with(|cs| { let threads = unsafe { &mut *THREADS.as_ptr(cs) }; - let next_pid = match get_next_pid(threads) { + let next_pid = match threads.get_next_pid() { Some(pid) => pid, None => { #[cfg(feature = "multicore")] diff --git a/src/riot-rs-threads/src/lib.rs b/src/riot-rs-threads/src/lib.rs index 1f605b31c..637ca61e6 100644 --- a/src/riot-rs-threads/src/lib.rs +++ b/src/riot-rs-threads/src/lib.rs @@ -263,6 +263,46 @@ impl Threads { } } + #[cfg(feature = "multicore")] + fn add_current_thread_to_rq(&mut self) { + let Some(thread) = self.current() else { + return; + }; + if thread.state == crate::ThreadState::Running { + let prio = thread.prio; + let pid = thread.pid; + self.runqueue.add(pid, prio); + } + } + + #[allow( + dead_code, + reason = "used in context-specific scheduler implementation" + )] + fn get_next_pid(&mut self) -> Option { + #[cfg(not(feature = "multicore"))] + { + return self.runqueue.get_next(); + } + #[cfg(feature = "multicore")] + #[cfg(not(feature = "core-affinity"))] + { + return self.runqueue.pop_next(); + } + #[cfg(feature = "core-affinity")] + { + let (mut next, prio) = self.runqueue.peek_next()?; + if !self.is_affine_to_curr_core(next) { + let iter = self.runqueue.iter_from(next, prio); + next = iter + .filter(|pid| self.is_affine_to_curr_core(*pid)) + .next()? + } + self.runqueue.del(next); + return Some(next); + }; + } + #[cfg(feature = "multicore")] fn lowest_running_prio(&self, _pid: ThreadId) -> (CoreId, Option) { #[cfg(feature = "core-affinity")]