Skip to content

Commit

Permalink
feat(examples): add dynamic thread priority example
Browse files Browse the repository at this point in the history
  • Loading branch information
elenaf9 committed Sep 13, 2024
1 parent ed48734 commit 2bbea44
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
8 changes: 8 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 examples/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ subdirs:
- random
- threading
- threading-channel
- threading-dynamic-prios
- usb-serial
10 changes: 10 additions & 0 deletions examples/threading-dynamic-prios/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "threading-dynamic-prios"
version = "0.1.0"
authors = ["Elena Frank <[email protected]>"]
edition.workspace = true
publish = false

[dependencies]
riot-rs = { path = "../../src/riot-rs", features = ["threading"] }
riot-rs-boards = { path = "../../src/riot-rs-boards" }
13 changes: 13 additions & 0 deletions examples/threading-dynamic-prios/README.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions examples/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
42 changes: 42 additions & 0 deletions examples/threading-dynamic-prios/src/main.rs
Original file line number Diff line number Diff line change
@@ -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()
);
}

0 comments on commit 2bbea44

Please sign in to comment.