---
title: "Priority inversion in Rust"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/classic/priority_inversion/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Priority inversion in Rust

Source: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/classic/priority_inversion/

The [priority inversion sample](https://developer.repebble.com/pebbleos/docs/samples/kernel/classic/priority_inversion.md) written in [Rust](https://developer.repebble.com/pebbleos/docs/development/build_system/rust.md), on the `pebbleos` crate.

`ThreadRef::priority()` is the priority a thread runs at, inherited ones included, and `base_priority()` the one it was given.

Run:

`pbl samples samples/rust/kernel/classic/priority_inversion`

Boards:

every board but native\_emery, native\_flint, native\_gabbro

## Sources

src/lib.rs

```
#![no\_std]use core::sync::atomic::{AtomicBool, Ordering};use core::time::Duration;use pebbleos::sync::Mutex;use pebbleos::thread::{self, Priority, Thread, ThreadRef};use pebbleos::time::Instant;use pebbleos::{Result, println};// main() is the high priority thread.const MEDIUM\_PRIO: Priority = Priority::MAIN.lower(1);const LOW\_PRIO: Priority = Priority::MAIN.lower(2);static BUS: Mutex\<()\> = Mutex::new(());static HIGH\_WAITS: AtomicBool = AtomicBool::new(false);static MEDIUM\_DONE: AtomicBool = AtomicBool::new(false);static LOW: Thread = Thread::new();static MEDIUM: Thread = Thread::new();fn use\_cpu(d: Duration) {let start = Instant::now();while start.elapsed() \< d {core::hint::spin\_loop();}}fn low(high: ThreadRef) {let me = thread::current();let bus = BUS.lock();// A long piece of work, which high comes in the middle of.while !HIGH\_WAITS.load(Ordering::Relaxed) {core::hint::spin\_loop();}use\_cpu(Duration::from\_millis(10));// While high waits for the lock, its holder runs at high's priority.println!("low: ends its work at the priority of high: {}",yes\_no(me.priority() == high.priority()));drop(bus);println!("low: back to its own priority: {}",yes\_no(me.priority() == me.base\_priority()));}// Needs no lock, only the CPU, and for longer than high can wait.fn medium() {use\_cpu(Duration::from\_millis(100));MEDIUM\_DONE.store(true, Ordering::Relaxed);println!("medium: done");}#[pebbleos::main]fn main() -\> Result\<()\> {let high = thread::current();LOW.spawn(c"low", LOW\_PRIO, move || low(high))?;// Lets low take the lock.thread::sleep(Duration::from\_millis(5));// Ready to run, and above low: without inheritance it would run first,// for 100 ms.MEDIUM.spawn(c"medium", MEDIUM\_PRIO, medium)?;HIGH\_WAITS.store(true, Ordering::Relaxed);let bus = BUS.lock();println!("high: got the lock {} medium had the CPU",if MEDIUM\_DONE.load(Ordering::Relaxed) {"after"} else {"before"});drop(bus);LOW.join(Duration::from\_secs(1))?;println!("Sample done");Ok(())}fn yes\_no(b: bool) -\> &'static str {if b { "yes" } else { "no" }}
```

## Output

```
low: ends its work at the priority of high: yes
high: got the lock before medium had the CPU
medium: done
low: back to its own priority: yes
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/rust/kernel/classic/priority\_inversion.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/classic/priority_inversion.md).
