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

Semaphore in Rust

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

The transfer interrupt is a Timer whose function gives the semaphore; errors are Results, printed as Rust prints them.

Run:

pbl samples samples/rust/kernel/semaphore

Boards:

every board

Sources

src/lib.rs
#![no_std]

use core::time::Duration;

use pebbleos::sync::{Mutex, Semaphore};
use pebbleos::thread::{self, Priority, Thread};
use pebbleos::time::Timeout;
use pebbleos::timer::Timer;
use pebbleos::{Errno, Result, println};

const NUM_WORKERS: usize = 4;
const NUM_CHANNELS: u32 = 2;

// Binary: an interrupt tells a thread that what it waits for has happened.
static TRANSFER_DONE: Semaphore = Semaphore::new(0, 1);

// Counting: as many tokens as there are channels to share.
static CHANNELS: Semaphore = Semaphore::new(NUM_CHANNELS, NUM_CHANNELS);

struct Stats {
    busy: u32,
    max_busy: u32,
}

static STATS: Mutex<Stats> = Mutex::new(Stats {
    busy: 0,
    max_busy: 0,
});

static WORKERS: [Thread; NUM_WORKERS] = [const { Thread::new() }; NUM_WORKERS];

// Runs in the tick interrupt, standing for the one of a peripheral.
static TRANSFER_TIMER: Timer = Timer::new(|_| TRANSFER_DONE.give(), ());

fn transfer(duration: Duration) -> Result<()> {
    TRANSFER_TIMER.start(duration);

    if TRANSFER_DONE.take(Duration::from_millis(100)).is_err() {
        TRANSFER_TIMER.stop();
        return Err(Errno::ETIMEDOUT);
    }

    Ok(())
}

fn worker() {
    let _ = CHANNELS.take(Timeout::FOREVER);

    {
        let mut stats = STATS.lock();
        stats.busy += 1;
        stats.max_busy = stats.max_busy.max(stats.busy);
    }

    thread::sleep(Duration::from_millis(20));

    STATS.lock().busy -= 1;

    CHANNELS.give();
}

#[pebbleos::main]
fn main() -> Result<()> {
    let ret = transfer(Duration::from_millis(10));
    println!("main: 10 ms transfer returns {ret:?}");

    let ret = transfer(Duration::from_secs(5));
    println!("main: 5 s transfer returns {ret:?}");

    for worker_thread in &WORKERS {
        worker_thread.spawn(c"worker", Priority::MAIN.lower(1), worker)?;
    }
    for worker_thread in &WORKERS {
        worker_thread.join(Duration::from_secs(1))?;
    }

    println!(
        "main: {NUM_WORKERS} workers shared {NUM_CHANNELS} channels, at most {} busy at once",
        STATS.lock().max_busy
    );
    println!("main: {} channels free again", CHANNELS.count());

    println!("Sample done");

    Ok(())
}

Output

main: 10 ms transfer returns Ok\(\(\)\)
main: 5 s transfer returns Err\(ETIMEDOUT\)
main: 4 workers shared 2 channels, at most 2 busy at once
main: 2 channels free again
Sample done

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

Overview

  • Sources
  • Output