-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(threads): dynamic thread priorities (#370)
- Loading branch information
Showing
9 changed files
with
227 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ subdirs: | |
- gpio-interrupt-nrf | ||
- gpio-interrupt-stm32 | ||
- i2c-controller | ||
- threading-dynamic-prios | ||
- threading-lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "threading-dynamic-prios" | ||
version = "0.1.0" | ||
authors = ["Elena Frank <elena.frank@proton.me>"] | ||
edition.workspace = true | ||
license.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
riot-rs = { path = "../../src/riot-rs", features = ["threading"] } | ||
riot-rs-boards = { path = "../../src/riot-rs-boards" } | ||
portable-atomic = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apps: | ||
- name: threading-dynamic-prios | ||
selects: | ||
- ?release | ||
- sw/threading |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#![no_main] | ||
#![no_std] | ||
#![feature(type_alias_impl_trait)] | ||
#![feature(used_with_arg)] | ||
|
||
use portable_atomic::{AtomicUsize, Ordering}; | ||
|
||
use riot_rs::thread::{RunqueueId, ThreadId}; | ||
|
||
static RUN_ORDER: AtomicUsize = AtomicUsize::new(0); | ||
|
||
static TEMP_THREAD1_PRIO: RunqueueId = RunqueueId::new(5); | ||
|
||
#[riot_rs::thread(autostart, priority = 2)] | ||
fn thread0() { | ||
let pid = riot_rs::thread::current_pid().unwrap(); | ||
assert_eq!(riot_rs::thread::get_priority(pid), Some(RunqueueId::new(2))); | ||
|
||
assert_eq!(RUN_ORDER.fetch_add(1, Ordering::AcqRel), 0); | ||
|
||
let thread1_pid = ThreadId::new(1); | ||
assert_eq!( | ||
riot_rs::thread::get_priority(thread1_pid), | ||
Some(RunqueueId::new(1)) | ||
); | ||
riot_rs::thread::set_priority(thread1_pid, TEMP_THREAD1_PRIO); | ||
|
||
// thread1 runs now. | ||
|
||
assert_eq!(RUN_ORDER.fetch_add(1, Ordering::AcqRel), 2); | ||
riot_rs::debug::log::info!("Test passed!"); | ||
loop {} | ||
} | ||
|
||
#[riot_rs::thread(autostart, priority = 1)] | ||
fn thread1() { | ||
// Thread can only run after thread0 increased its prio. | ||
assert_eq!(RUN_ORDER.fetch_add(1, Ordering::AcqRel), 1); | ||
// Prio is the temp increased prio. | ||
let pid = riot_rs::thread::current_pid().unwrap(); | ||
assert_eq!(riot_rs::thread::get_priority(pid), Some(TEMP_THREAD1_PRIO)); | ||
// Other thread prios didn't change. | ||
assert_eq!( | ||
riot_rs::thread::get_priority(ThreadId::new(0)), | ||
Some(RunqueueId::new(2)) | ||
); | ||
|
||
// Reset priority. | ||
riot_rs::thread::set_priority(pid, RunqueueId::new(1)); | ||
|
||
unreachable!("Core should be blocked by higher prio thread.") | ||
} |