pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Threads
      • Mutexes
      • Semaphores
      • Message queues
      • Poll groups
      • Timers
      • Work queues
      • Delayed work
      • Heaps
      • Time
      • Idle states
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • CRC
      • SHA-256
      • AES
      • Files
      • Settings
      • Translations
      • Input events
      • Loader
      • Log levels
      • Profiler
      • Shell commands
      • Task watchdog
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • GPU
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Rust
      • Kernel benchmark
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Priority inversion in Rust

The priority inversion sample written in Rust, 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. This page is maintained in the pebbleos repository: docs/samples/rust/kernel/classic/priority_inversion.md.

Overview

  • Sources
  • Output