From 2bbea449e071a8701389dafa7fc760a25a184a57 Mon Sep 17 00:00:00 2001 From: Elena Frank Date: Wed, 31 Jul 2024 16:34:05 +0200 Subject: [PATCH] feat(examples): add dynamic thread priority example --- Cargo.lock | 8 ++++ examples/laze.yml | 1 + examples/threading-dynamic-prios/Cargo.toml | 10 +++++ examples/threading-dynamic-prios/README.md | 13 ++++++ examples/threading-dynamic-prios/laze.yml | 5 +++ examples/threading-dynamic-prios/src/main.rs | 42 ++++++++++++++++++++ 6 files changed, 79 insertions(+) create mode 100644 examples/threading-dynamic-prios/Cargo.toml create mode 100644 examples/threading-dynamic-prios/README.md create mode 100644 examples/threading-dynamic-prios/laze.yml create mode 100644 examples/threading-dynamic-prios/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index c9cc8c88f..292820c94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4439,6 +4439,14 @@ dependencies = [ "riot-rs-boards", ] +[[package]] +name = "threading-dynamic-prios" +version = "0.1.0" +dependencies = [ + "riot-rs", + "riot-rs-boards", +] + [[package]] name = "time" version = "0.3.36" diff --git a/examples/laze.yml b/examples/laze.yml index a69997482..79e5dbafe 100644 --- a/examples/laze.yml +++ b/examples/laze.yml @@ -21,4 +21,5 @@ subdirs: - random - threading - threading-channel + - threading-dynamic-prios - usb-serial diff --git a/examples/threading-dynamic-prios/Cargo.toml b/examples/threading-dynamic-prios/Cargo.toml new file mode 100644 index 000000000..1487edadf --- /dev/null +++ b/examples/threading-dynamic-prios/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "threading-dynamic-prios" +version = "0.1.0" +authors = ["Elena Frank "] +edition.workspace = true +publish = false + +[dependencies] +riot-rs = { path = "../../src/riot-rs", features = ["threading"] } +riot-rs-boards = { path = "../../src/riot-rs-boards" } diff --git a/examples/threading-dynamic-prios/README.md b/examples/threading-dynamic-prios/README.md new file mode 100644 index 000000000..a74870279 --- /dev/null +++ b/examples/threading-dynamic-prios/README.md @@ -0,0 +1,13 @@ +# threading-dynamic-prios + +## About + +This application demonstrates threading with dynamic priorities. + +## How to run + +In this folder, run + + laze build -b nrf52840dk run + +The application will start two threads that change their priorities during runtime. diff --git a/examples/threading-dynamic-prios/laze.yml b/examples/threading-dynamic-prios/laze.yml new file mode 100644 index 000000000..2bc4a6805 --- /dev/null +++ b/examples/threading-dynamic-prios/laze.yml @@ -0,0 +1,5 @@ +apps: + - name: threading-dynamic-prios + selects: + - ?release + - sw/threading diff --git a/examples/threading-dynamic-prios/src/main.rs b/examples/threading-dynamic-prios/src/main.rs new file mode 100644 index 000000000..1a58b0e62 --- /dev/null +++ b/examples/threading-dynamic-prios/src/main.rs @@ -0,0 +1,42 @@ +#![no_main] +#![no_std] +#![feature(type_alias_impl_trait)] +#![feature(used_with_arg)] + +use riot_rs::{debug::log::*, thread::ThreadId}; + +#[riot_rs::thread(autostart, priority = 3)] +fn thread0() { + let pid = riot_rs::thread::current_pid().unwrap(); + info!( + "{}: Running at prio {}.", + pid, + riot_rs::thread::get_priority(pid).unwrap() + ); + let new_thread1_prio = 5; + info!("{}: Changing Thread 1's prio to {}.", pid, new_thread1_prio); + riot_rs::thread::set_priority(ThreadId::new(1), new_thread1_prio); + info!( + "{}: Running again at prio {}.", + pid, + riot_rs::thread::get_priority(pid).unwrap() + ); +} + +#[riot_rs::thread(autostart, priority = 1)] +fn thread1() { + let pid = riot_rs::thread::current_pid().unwrap(); + info!( + "{}: Running at prio {}.", + pid, + riot_rs::thread::get_priority(pid).unwrap() + ); + let new_prio = 1; + info!("{}: Changing own prio back to {}.", pid, new_prio); + riot_rs::thread::set_priority(pid, new_prio); + info!( + "{}: Running again at prio {}.", + pid, + riot_rs::thread::get_priority(pid).unwrap() + ); +}