From 6c4d4af9fdac94556f5d315265c746c33517cc81 Mon Sep 17 00:00:00 2001 From: Elena Frank Date: Tue, 30 Apr 2024 12:13:42 +0200 Subject: [PATCH] threads: only schedule if allocation changed --- src/riot-rs-threads/src/lib.rs | 41 +++++++++++++++---------- src/riot-rs-threads/src/thread_flags.rs | 29 ++++++++++++----- src/riot-rs-threads/src/threadlist.rs | 11 ++++--- 3 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/riot-rs-threads/src/lib.rs b/src/riot-rs-threads/src/lib.rs index 8f16cf176..feb02580d 100644 --- a/src/riot-rs-threads/src/lib.rs +++ b/src/riot-rs-threads/src/lib.rs @@ -154,17 +154,21 @@ impl Threads { /// # Panics /// /// Panics if `pid` is >= [`THREADS_NUMOF`]. - pub(crate) fn set_state(&mut self, pid: ThreadId, state: ThreadState) -> ThreadState { + pub(crate) fn set_state( + &mut self, + pid: ThreadId, + state: ThreadState, + ) -> (ThreadState, Option) { let thread = &mut self.threads[pid as usize]; let old_state = thread.state; thread.state = state; - if old_state != ThreadState::Running && state == ThreadState::Running { - self.runqueue.add(thread.pid, thread.prio); - } else if old_state == ThreadState::Running && state != ThreadState::Running { - self.runqueue.del(thread.pid, thread.prio); - } - - old_state + let core = match (old_state, state) { + (old, new) if old == new => None, + (_, ThreadState::Running) => self.runqueue.add(thread.pid, thread.prio), + (ThreadState::Running, _) => self.runqueue.del(thread.pid, thread.prio), + _ => None, + }; + (old_state, core) } /// Returns the state of a thread. @@ -291,11 +295,11 @@ pub fn is_valid_pid(thread_id: ThreadId) -> bool { fn cleanup() -> ! { THREADS.with_mut(|mut threads| { let thread_id = threads.current_pid().unwrap(); - threads.set_state(thread_id, ThreadState::Invalid); + if let Some(_core_id) = threads.set_state(thread_id, ThreadState::Invalid).1 { + schedule(); + } }); - schedule(); - unreachable!(); } @@ -305,8 +309,9 @@ pub fn yield_same() { let thread = threads.current().unwrap(); let pid = thread.pid; let prio = thread.prio; - threads.runqueue.advance_from(pid, prio); - schedule(); + if let Some(_core_id) = threads.runqueue.advance_from(pid, prio) { + schedule(); + } }) } @@ -314,8 +319,9 @@ pub fn yield_same() { pub fn sleep() { THREADS.with_mut(|mut threads| { let pid = threads.current_pid().unwrap(); - threads.set_state(pid, ThreadState::Paused); - schedule(); + if let Some(_core_id) = threads.set_state(pid, ThreadState::Paused).1 { + schedule(); + } }); } @@ -326,8 +332,9 @@ pub fn wakeup(thread_id: ThreadId) -> bool { THREADS.with_mut(|mut threads| { if let Some(state) = threads.get_state(thread_id) { if state == ThreadState::Paused { - threads.set_state(thread_id, ThreadState::Running); - schedule(); + if let Some(_core_id) = threads.set_state(thread_id, ThreadState::Running).1 { + schedule(); + } true } else { false diff --git a/src/riot-rs-threads/src/thread_flags.rs b/src/riot-rs-threads/src/thread_flags.rs index fc653ed95..ff71d8e3f 100644 --- a/src/riot-rs-threads/src/thread_flags.rs +++ b/src/riot-rs-threads/src/thread_flags.rs @@ -105,8 +105,9 @@ impl Threads { }, _ => false, } { - self.set_state(thread_id, ThreadState::Running); - crate::schedule(); + if let Some(_core_id) = self.set_state(thread_id, ThreadState::Running).1 { + crate::schedule(); + } } } @@ -117,8 +118,12 @@ impl Threads { Some(mask) } else { let thread_id = thread.pid; - self.set_state(thread_id, ThreadState::FlagBlocked(WaitMode::All(mask))); - crate::schedule(); + if let Some(_core_id) = self + .set_state(thread_id, ThreadState::FlagBlocked(WaitMode::All(mask))) + .1 + { + crate::schedule(); + } None } } @@ -131,8 +136,12 @@ impl Threads { Some(res) } else { let thread_id = thread.pid; - self.set_state(thread_id, ThreadState::FlagBlocked(WaitMode::Any(mask))); - crate::schedule(); + if let Some(_core_id) = self + .set_state(thread_id, ThreadState::FlagBlocked(WaitMode::Any(mask))) + .1 + { + crate::schedule(); + } None } } @@ -147,8 +156,12 @@ impl Threads { Some(res) } else { let thread_id = thread.pid; - self.set_state(thread_id, ThreadState::FlagBlocked(WaitMode::Any(mask))); - crate::schedule(); + if let Some(_core_id) = self + .set_state(thread_id, ThreadState::FlagBlocked(WaitMode::Any(mask))) + .1 + { + crate::schedule(); + } None } } diff --git a/src/riot-rs-threads/src/threadlist.rs b/src/riot-rs-threads/src/threadlist.rs index 8a9589840..13aaa4483 100644 --- a/src/riot-rs-threads/src/threadlist.rs +++ b/src/riot-rs-threads/src/threadlist.rs @@ -21,8 +21,9 @@ impl ThreadList { let thread_id = threads.current_pid().unwrap(); threads.thread_blocklist[thread_id as usize] = self.head; self.head = Some(thread_id); - threads.set_state(thread_id, state); - crate::schedule(); + if let Some(_core_id) = threads.set_state(thread_id, state).1 { + crate::schedule(); + } }); } @@ -36,8 +37,10 @@ impl ThreadList { if let Some(head) = self.head { let old_state = THREADS.with_mut_cs(cs, |mut threads| { self.head = threads.thread_blocklist[head as usize].take(); - let old_state = threads.set_state(head, ThreadState::Running); - crate::schedule(); + let (old_state, core_id) = threads.set_state(head, ThreadState::Running); + if let Some(_core_id) = core_id { + crate::schedule(); + } old_state }); Some((head, old_state))