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

Dining philosophers in Rust

The dining philosophers sample written in Rust, on the pebbleos crate.

The forks are Mutex<()>, and a philosopher puts one down by dropping its guard.

Run:

pbl samples samples/rust/kernel/classic/dining_philosophers

Boards:

every board

Sources

src/lib.rs
#![no_std]

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

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

const NUM_PHILOSOPHERS: usize = 5;
const MEALS: u32 = 3;

struct Philosopher {
    thread: Thread,
    meals: AtomicU32,
    stuck: AtomicBool,
}

static FORKS: [Mutex<()>; NUM_PHILOSOPHERS] = [const { Mutex::new(()) }; NUM_PHILOSOPHERS];
static PHILOSOPHERS: [Philosopher; NUM_PHILOSOPHERS] = [const {
    Philosopher {
        thread: Thread::new(),
        meals: AtomicU32::new(0),
        stuck: AtomicBool::new(false),
    }
}; NUM_PHILOSOPHERS];
static HOLDING_ONE: AtomicU32 = AtomicU32::new(0);

// Stuck philosophers keep their fork until main has counted them.
static GIVE_UP: Semaphore = Semaphore::new(0, NUM_PHILOSOPHERS as u32);

fn dine(seat: usize, ordered: bool) {
    let me = &PHILOSOPHERS[seat];
    let mut first = seat;
    let mut second = (seat + 1) % NUM_PHILOSOPHERS;

    // The fix: everyone takes the lower numbered fork first, so no cycle of
    // waiters can form.
    if ordered && first > second {
        core::mem::swap(&mut first, &mut second);
    }

    while me.meals.load(Ordering::Relaxed) < MEALS {
        let first_fork = FORKS[first].lock();
        // A deadlock needs bad luck with the timing, which is staged here:
        // nobody goes on until every philosopher holds a fork. With the fix
        // they cannot all hold one.
        HOLDING_ONE.fetch_add(1, Ordering::Relaxed);
        loop {
            thread::yield_now();
            if ordered || HOLDING_ONE.load(Ordering::Relaxed) as usize >= NUM_PHILOSOPHERS {
                break;
            }
        }

        // The time limit stands in for the deadlock, which would otherwise
        // end the sample here.
        let Ok(second_fork) = FORKS[second].lock_timeout(Duration::from_millis(50)) else {
            me.stuck.store(true, Ordering::Relaxed);
            let _ = GIVE_UP.take(Timeout::FOREVER);
            return;
        };

        me.meals.fetch_add(1, Ordering::Relaxed);
        thread::sleep(Duration::from_millis(2));

        HOLDING_ONE.fetch_sub(1, Ordering::Relaxed);
        drop(second_fork);
        drop(first_fork);
    }
}

fn dinner(ordered: bool) -> Result<()> {
    HOLDING_ONE.store(0, Ordering::Relaxed);

    for (seat, p) in PHILOSOPHERS.iter().enumerate() {
        p.meals.store(0, Ordering::Relaxed);
        p.stuck.store(false, Ordering::Relaxed);
        p.thread
            .spawn(c"philosopher", Priority::MAIN.lower(1), move || {
                dine(seat, ordered)
            })?;
    }

    // Long enough for every meal, or for every time limit to pass.
    thread::sleep(Duration::from_millis(200));

    let mut meals = 0;
    let mut stuck = 0;
    for p in &PHILOSOPHERS {
        meals += p.meals.load(Ordering::Relaxed);
        stuck += p.stuck.load(Ordering::Relaxed) as u32;
        GIVE_UP.give();
    }
    for p in &PHILOSOPHERS {
        p.thread.join(Duration::from_secs(1))?;
    }
    GIVE_UP.reset();

    println!(
        "dinner: {}: {meals} meals eaten, {stuck} of {NUM_PHILOSOPHERS} philosophers stuck with one fork",
        if ordered {
            "lower fork first"
        } else {
            "left fork first"
        }
    );

    Ok(())
}

#[pebbleos::main]
fn main() -> Result<()> {
    dinner(false)?;
    dinner(true)?;

    println!("Sample done");

    Ok(())
}

Output

dinner: left fork first: 0 meals eaten, 5 of 5 philosophers stuck with one fork
dinner: lower fork first: 15 meals eaten, 0 of 5 philosophers stuck with one fork
Sample done

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

Overview

  • Sources
  • Output