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

Threads in Rust

The threads sample written in Rust, on the pebbleos crate.

Each Thread is a static that owns its stack and runs a closure; spawn() fails while the previous one still runs, and join() waits for it to end.

Run:

pbl samples samples/rust/kernel/threads

Boards:

every board

Sources

src/lib.rs
#![no_std]

use core::sync::atomic::{AtomicU32, Ordering};
use core::time::Duration;

use pebbleos::thread::{self, Priority, Thread};
use pebbleos::{Result, println};

const ROUNDS: u32 = 3;

// main() runs at Priority::MAIN: one thread above it, two below.
const URGENT_PRIO: Priority = Priority::MAIN.raise(1);
const PEER_PRIO: Priority = Priority::MAIN.lower(1);

static URGENT: Thread = Thread::new();
static PING: Thread = Thread::new();
static PONG: Thread = Thread::new();

fn peer(rounds: &AtomicU32) {
    let me = thread::current();

    while rounds.load(Ordering::Relaxed) < ROUNDS {
        println!("{}: round {}", me.name(), rounds.load(Ordering::Relaxed));
        rounds.fetch_add(1, Ordering::Relaxed);
        // Threads of equal priority also take turns on every tick; yielding
        // does it now.
        thread::yield_now();
    }
}

#[pebbleos::main]
fn main() -> Result<()> {
    static PING_ROUNDS: AtomicU32 = AtomicU32::new(0);
    static PONG_ROUNDS: AtomicU32 = AtomicU32::new(0);

    println!("main: creating a thread of higher priority");
    URGENT.spawn(c"urgent", URGENT_PRIO, || {
        println!("urgent: preempted main, which is still inside spawn()");
    })?;
    println!(
        "main: urgent has {}",
        if URGENT.is_finished() {
            "ended"
        } else {
            "not ended"
        }
    );

    PING.spawn(c"ping", PEER_PRIO, || peer(&PING_ROUNDS))?;
    PONG.spawn(c"pong", PEER_PRIO, || peer(&PONG_ROUNDS))?;
    println!("main: created two threads of lower priority, which wait for main to block");

    PING.join(Duration::from_secs(1))?;
    PONG.join(Duration::from_secs(1))?;

    println!(
        "main: ping ran {} rounds, pong ran {} rounds",
        PING_ROUNDS.load(Ordering::Relaxed),
        PONG_ROUNDS.load(Ordering::Relaxed)
    );
    println!("Sample done");

    Ok(())
}

Output

main: creating a thread of higher priority
urgent: preempted main, which is still inside spawn\(\)
main: urgent has ended
main: created two threads of lower priority, which wait for main to block
main: ping ran 3 rounds, pong ran 3 rounds
Sample done

Generated from PebbleOS 7753e801d. This page is maintained in the pebbleos repository: docs/samples/rust/kernel/threads.md.

Overview

  • Sources
  • Output