---
title: "Semaphore in Rust"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/semaphore/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Semaphore in Rust

Source: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/semaphore/

The [semaphore sample](https://developer.repebble.com/pebbleos/docs/samples/kernel/semaphore.md) written in [Rust](https://developer.repebble.com/pebbleos/docs/development/build_system/rust.md), on the `pebbleos` crate.

The transfer interrupt is a `Timer` whose function gives the semaphore; errors are `Result`s, 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](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/rust/kernel/semaphore.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/semaphore.md).
