Skip to content

Commit

Permalink
feat(tests): add dynamic thread priority test
Browse files Browse the repository at this point in the history
  • Loading branch information
elenaf9 committed Sep 30, 2024
1 parent 0cbe0c6 commit 5e240e6
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
"tests/gpio-interrupt-nrf",
"tests/gpio-interrupt-stm32",
"tests/i2c-controller",
"tests/threading-dynamic-prios",
"tests/threading-lock",
]

Expand Down
1 change: 1 addition & 0 deletions tests/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ subdirs:
- gpio-interrupt-nrf
- gpio-interrupt-stm32
- i2c-controller
- threading-dynamic-prios
- threading-lock
12 changes: 12 additions & 0 deletions tests/threading-dynamic-prios/Cargo.toml
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 }
5 changes: 5 additions & 0 deletions tests/threading-dynamic-prios/laze.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apps:
- name: threading-dynamic-prios
selects:
- ?release
- sw/threading
52 changes: 52 additions & 0 deletions tests/threading-dynamic-prios/src/main.rs
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.")
}

0 comments on commit 5e240e6

Please sign in to comment.