Skip to content

Commit

Permalink
threads: only schedule if allocation changed
Browse files Browse the repository at this point in the history
  • Loading branch information
elenaf9 committed Apr 30, 2024
1 parent ddfca2d commit 6c4d4af
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
41 changes: 24 additions & 17 deletions src/riot-rs-threads/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoreId>) {
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.
Expand Down Expand Up @@ -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!();
}

Expand All @@ -305,17 +309,19 @@ 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();
}
})
}

/// Suspends/ pauses the current thread's execution.
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();
}
});
}

Expand All @@ -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
Expand Down
29 changes: 21 additions & 8 deletions src/riot-rs-threads/src/thread_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/riot-rs-threads/src/threadlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
}

Expand All @@ -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))
Expand Down

0 comments on commit 6c4d4af

Please sign in to comment.