Skip to content

Commit 2326717

Browse files
committed
samples: philosophers: Test bounded channels as well
Bounded channels only allocate on creation, and are able to block upon send until there is a message slot available. Signed-off-by: David Brown <[email protected]>
1 parent 753cde2 commit 2326717

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

samples/philosophers/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ choice
4040
channels to synchronize.
4141

4242
endchoice
43+
44+
if SYNC_CHANNEL
45+
config USE_BOUNDED_CHANNELS
46+
bool "Should channel sync use dounded channels?"
47+
default y
48+
help
49+
If set, the channel-based communication will use bounded channels with bounds calculated
50+
to not ever block.
51+
endif

samples/philosophers/sample.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ tests:
3232
min_ram: 32
3333
extra_configs:
3434
- CONFIG_SYNC_CONDVAR=y
35-
sample.rust.philosopher.channel:
35+
sample.rust.philosopher.channel_bounded:
3636
tags: introduction
3737
min_ram: 32
3838
extra_configs:
3939
- CONFIG_SYNC_CHANNEL=y
40+
- CONFIG_USE_BOUNDED_CHANNELS=y
41+
sample.rust.philosopher.channel_unbounded:
42+
tags: introduction
43+
min_ram: 32
44+
extra_configs:
45+
- CONFIG_SYNC_CHANNEL=y
46+
- CONFIG_USE_BOUNDED_CHANNELS=n

samples/philosophers/src/channel.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,22 @@ impl ChannelSync {
112112
/// Generate a syncer out of a ChannelSync.
113113
#[allow(dead_code)]
114114
pub fn get_channel_syncer() -> Vec<Arc<dyn ForkSync>> {
115-
let (cq_send, cq_recv) = channel::unbounded();
116-
let reply_queues = [(); NUM_PHIL].each_ref().map(|()| {
117-
channel::unbounded()
118-
});
115+
let (cq_send, cq_recv);
116+
let reply_queues;
117+
118+
if cfg!(CONFIG_USE_BOUNDED_CHANNELS) {
119+
// Use only one message, so that send will block, to ensure that works.
120+
(cq_send, cq_recv) = channel::bounded(1);
121+
reply_queues = [(); NUM_PHIL].each_ref().map(|()| {
122+
channel::bounded(1)
123+
});
124+
} else {
125+
(cq_send, cq_recv) = channel::unbounded();
126+
reply_queues = [(); NUM_PHIL].each_ref().map(|()| {
127+
channel::unbounded()
128+
});
129+
}
130+
119131
let syncer = reply_queues.into_iter().map(|rqueue| {
120132
let item = Box::new(ChannelSync::new(cq_send.clone(), rqueue))
121133
as Box<dyn ForkSync>;

0 commit comments

Comments
 (0)