Skip to content

Commit 6a82af0

Browse files
committed
samples: work-philosophers
This is intended to be a demonstration of implementing the dining philosopher's problem using work queues. At this point, it just serves as a test of the workqueue and async mechanism. Signed-off-by: David Brown <[email protected]>
1 parent bda59c9 commit 6a82af0

File tree

8 files changed

+394
-0
lines changed

8 files changed

+394
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(work_philosophers)
7+
8+
rust_cargo_application()

samples/work-philosophers/Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
[package]
5+
# This must be rustapp for now.
6+
name = "rustapp"
7+
version = "0.1.0"
8+
edition = "2021"
9+
description = "A sample hello world application in Rust"
10+
license = "Apache-2.0 or MIT"
11+
12+
[lib]
13+
crate-type = ["staticlib"]
14+
15+
[dependencies]
16+
zephyr = "0.1.0"
17+
18+
# Dependencies that are used by build.rs.
19+
[build-dependencies]
20+
zephyr-build = "0.1.0"
21+
22+
[profile.release]
23+
debug-assertions = true
24+
overflow-checks = true
25+
debug = true

samples/work-philosophers/Kconfig

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "Rust Dining Philosphers"
5+
6+
source "Kconfig.zephyr"
7+
8+
choice
9+
prompt "Select Synchronization implementation"
10+
default SYNC_CHANNEL
11+
12+
config SYNC_SYS_SEMAPHORE
13+
bool "Use sys::Semaphore to synchronize forks"
14+
help
15+
Use to have the dining philosophers sample use sys::Semaphore, with one per fork, to
16+
synchronize.
17+
18+
config SYNC_SYS_DYNAMIC_SEMAPHORE
19+
bool "Use a dynamic sys::Semaphore to synchronize forks"
20+
help
21+
Use to have the dining philosophers sample use sys::Semaphore, with one per fork, to
22+
synchronize. The Semaphores will be dynamically allocated.
23+
24+
config SYNC_SYS_MUTEX
25+
bool "Use sys::Semaphore to synchronize forks"
26+
help
27+
Use to have the dining philosophers sample use sys::Mutex, with one per fork, to
28+
synchronize.
29+
30+
config SYNC_CONDVAR
31+
bool "Use sync::Condvar and sync::Mutex to synchronize forks"
32+
help
33+
Use to have the dining philosophers sample use a single data structure, protected
34+
by a sync::Mutex and coordinated with a sync::Condvar, to synchronize.
35+
36+
config SYNC_CHANNEL
37+
bool "Use sync::channel to synchronize forks"
38+
help
39+
Use to have the dining philosophers sample use a worker thread, communicating via
40+
channels to synchronize.
41+
42+
config SYNC_WORKQUEUE
43+
bool "Use workqueues to simulate the philosophers"
44+
help
45+
Use workqueues to simulate the philosophers.
46+
47+
endchoice
48+
49+
if SYNC_CHANNEL
50+
config USE_BOUNDED_CHANNELS
51+
bool "Should channel sync use bounded channels?"
52+
default y
53+
help
54+
If set, the channel-based communication will use bounded channels with bounds calculated
55+
to not ever block.
56+
endif
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# This board doesn't have a serial console, so use RTT.
5+
CONFIG_UART_CONSOLE=n
6+
CONFIG_RTT_CONSOLE=y
7+
CONFIG_USE_SEGGER_RTT=y

samples/work-philosophers/build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2023 Linaro LTD
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// This crate needs access to kconfig variables. This is an example of how to do that. The
5+
// zephyr-build must be a build dependency.
6+
7+
fn main() {
8+
zephyr_build::export_bool_kconfig();
9+
}

samples/work-philosophers/prj.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) 2024 Linaro LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_RUST=y
5+
CONFIG_RUST_ALLOC=y
6+
CONFIG_MAIN_STACK_SIZE=8192
7+
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
8+
9+
CONFIG_POLL=y
10+
11+
# CONFIG_DEBUG=y
12+
13+
# CONFIG_USERSPACE=y
14+
15+
# Some debugging
16+
CONFIG_THREAD_MONITOR=y
17+
CONFIG_THREAD_ANALYZER=y
18+
CONFIG_THREAD_ANALYZER_USE_PRINTK=y
19+
CONFIG_THREAD_ANALYZER_AUTO=n
20+
# CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=15

samples/work-philosophers/sample.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
sample:
2+
description: Philosphers, in Rust
3+
name: workq philosophers rust
4+
common:
5+
harness: console
6+
harness_config:
7+
type: one_line
8+
regex:
9+
- "All threads done"
10+
tags: rust
11+
filter: CONFIG_RUST_SUPPORTED
12+
tests:
13+
sample.rust.philosopher:
14+
tags: introduction
15+
min_ram: 32

0 commit comments

Comments
 (0)