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

Message queue in Rust

The message queue sample written in Rust, on the pebbleos crate.

MsgQueue<T, N> holds N messages of type T, any Copy type, which put() and get() copy in and out.

Run:

pbl samples samples/rust/kernel/msgq

Boards:

every board

Sources

src/lib.rs
#![no_std]

use core::time::Duration;

use pebbleos::msgq::MsgQueue;
use pebbleos::thread::{self, Priority, Thread};
use pebbleos::time::Timeout;
use pebbleos::{Errno, Result, println};

const NUM_SAMPLES: u32 = 5;

#[derive(Clone, Copy)]
struct Sample {
    seq: u32,
    value: i32,
}

// Messages are copied in and out, so the queue owns all the storage it needs.
static SAMPLES: MsgQueue<Sample, 2> = MsgQueue::new();

static PRODUCER: Thread = Thread::new();

fn produce() {
    for seq in 0..NUM_SAMPLES {
        let sample = Sample {
            seq,
            value: seq as i32 * 10,
        };

        // Blocks while the queue is full: a slow consumer slows the producer
        // down.
        if let Err(e) = SAMPLES.put(sample, Timeout::FOREVER) {
            println!("producer: put failed ({e})");
            return;
        }
    }
}

fn consume() -> Result<()> {
    let mut received = 0;

    loop {
        match SAMPLES.get(Duration::from_millis(100)) {
            Ok(sample) => {
                println!("consumer: sample {} = {}", sample.seq, sample.value);
                received += 1;
            }
            Err(Errno::EAGAIN) => break,
            Err(e) => return Err(e),
        }
    }

    println!("consumer: no sample for 100 ms, {received} received");

    if received == NUM_SAMPLES {
        Ok(())
    } else {
        Err(Errno::EIO)
    }
}

fn jump_the_queue() -> Result<()> {
    let routine = Sample { seq: 100, value: 0 };
    let urgent = Sample { seq: 999, value: 0 };

    SAMPLES.put(routine, Timeout::NO_WAIT)?;
    SAMPLES.put_front(urgent, Timeout::NO_WAIT)?;

    // The queue is full: without waiting, a third message is refused.
    let ret = SAMPLES.put(routine, Timeout::NO_WAIT);
    println!("main: put on a full queue returns {ret:?}");

    let sample = SAMPLES.get(Timeout::NO_WAIT)?;
    println!(
        "main: sample {} was put last and comes out first",
        sample.seq
    );
    SAMPLES.purge();

    Ok(())
}

#[pebbleos::main]
fn main() -> Result<()> {
    PRODUCER.spawn(c"producer", Priority::MAIN.lower(1), produce)?;

    // Let the producer run until the queue stops it.
    thread::sleep(Duration::from_millis(50));
    println!(
        "main: producer is blocked with {} of {} samples queued",
        SAMPLES.len(),
        NUM_SAMPLES
    );

    let sample = SAMPLES.peek()?;
    println!("main: sample {} is next, and still queued", sample.seq);

    consume()?;
    jump_the_queue()?;

    println!("Sample done");

    Ok(())
}

Output

main: producer is blocked with 2 of 5 samples queued
main: sample 0 is next, and still queued
consumer: sample 0 = 0
consumer: sample 1 = 10
consumer: sample 2 = 20
consumer: sample 3 = 30
consumer: sample 4 = 40
consumer: no sample for 100 ms, 5 received
main: put on a full queue returns Err\(EBUSY\)
main: sample 999 was put last and comes out first
Sample done

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

Overview

  • Sources
  • Output