Skip to content

Commit baf74c0

Browse files
committed
samples: philosopher: Allow sync type as Kconfig
Add Kconfig entries to the sample that allow a selection of which synchronization algorithm to use. This demonstrates how an app can have conditional compilation, which requires a `build.rs` script to invoke the kconfig value exporter. Signed-off-by: David Brown <[email protected]>
1 parent a4f9548 commit baf74c0

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

samples/philosophers/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ crate-type = ["staticlib"]
1515
[dependencies]
1616
zephyr = "3.7.0"
1717

18+
# Dependencies that are used by build.rs.
19+
[build-dependencies]
20+
zephyr-build = "3.7.0"
21+
1822
[profile.release]
1923
debug-assertions = true
2024
overflow-checks = true

samples/philosophers/Kconfig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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_CONDVAR
11+
12+
config SYNC_SYS_MUTEX
13+
bool "Use sys::Mutex to synchronize forks"
14+
help
15+
Use to have the dining philosophers sample use sys::Mutex, with one per fork, to
16+
synchronize.
17+
18+
config SYNC_CONDVAR
19+
bool "Use sync::Condvar and sync::Mutex to synchronize forks"
20+
help
21+
Use to have the dining philosophers sample use a single data structure, protected
22+
by a sync::Mutex and coordinated with a sync::Condvar, to synchronize.
23+
endchoice

samples/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/philosophers/src/condsync.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct CondSync {
2727
}
2828

2929
impl CondSync {
30+
#[allow(dead_code)]
3031
pub fn new() -> CondSync {
3132
MUTEX.init();
3233
CONDVAR.init();

samples/philosophers/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
#![no_std]
55

6+
// Cargo tries to detect configs that have typos in them. Unfortunately, the Zephyr Kconfig system
7+
// uses a large number of Kconfigs and there is no easy way to know which ones might conceivably be
8+
// valid. This prevents a warning about each cfg that is used.
9+
#![allow(unexpected_cfgs)]
10+
611
extern crate alloc;
712

813
use alloc::boxed::Box;
@@ -16,7 +21,10 @@ use zephyr::{
1621
sync::{Arc, Mutex},
1722
};
1823

24+
// These are optional, based on Kconfig, so allow them to be unused.
25+
#[allow(unused_imports)]
1926
use crate::condsync::CondSync;
27+
#[allow(unused_imports)]
2028
use crate::sysmutex::SysMutexSync;
2129

2230
mod condsync;
@@ -86,8 +94,8 @@ extern "C" fn rust_main() {
8694
}
8795
}
8896

89-
#[allow(dead_code)]
90-
fn get_syncerb() -> Vec<Arc<dyn ForkSync>> {
97+
#[cfg(CONFIG_SYNC_SYS_MUTEX)]
98+
fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
9199
// Simple mutex version.
92100
let syncer = Box::new(SysMutexSync::new())
93101
as Box<dyn ForkSync>;
@@ -99,6 +107,7 @@ fn get_syncerb() -> Vec<Arc<dyn ForkSync>> {
99107
result
100108
}
101109

110+
#[cfg(CONFIG_SYNC_CONDVAR)]
102111
fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
103112
// Condvar version
104113
let syncer = Box::new(CondSync::new())

samples/philosophers/src/sysmutex.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct SysMutexSync {
2828
}
2929

3030
impl SysMutexSync {
31+
#[allow(dead_code)]
3132
pub fn new() -> SysMutexSync {
3233
let locks = MUTEXES.each_ref().map(|m| {
3334
m.init();

0 commit comments

Comments
 (0)