Skip to content

Commit

Permalink
refactor(threads): move sched-independent code to Threads
Browse files Browse the repository at this point in the history
  • Loading branch information
elenaf9 committed Sep 10, 2024
1 parent 0c6b5f7 commit 4b6ae5d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
1 change: 1 addition & 0 deletions examples/threading-multicore/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ apps:
- name: threading-multicore
selects:
- ?release
- executor-thread
41 changes: 4 additions & 37 deletions src/riot-rs-threads/src/arch/cortex_m.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -161,30 +161,6 @@ unsafe extern "C" fn PendSV() {
};
}

fn get_next_pid(threads: &mut crate::Threads) -> Option<ThreadId> {
#[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.
Expand All @@ -201,22 +177,13 @@ fn get_next_pid(threads: &mut crate::Threads) -> Option<ThreadId> {
#[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")]
Expand Down
40 changes: 40 additions & 0 deletions src/riot-rs-threads/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ThreadId> {
#[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<RunqueueId>) {
#[cfg(feature = "core-affinity")]
Expand Down

0 comments on commit 4b6ae5d

Please sign in to comment.