Skip to content

Commit 3cff3c8

Browse files
committed
samples: philosophers: Add implementation for channels
Add a synchronizer for forks based on sending messages over channels to a worker thread. Signed-off-by: David Brown <[email protected]>
1 parent c8bc8d8 commit 3cff3c8

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

samples/philosophers/Kconfig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ source "Kconfig.zephyr"
77

88
choice
99
prompt "Select Synchronization implementation"
10-
default SYNC_CONDVAR
10+
default SYNC_CHANNEL
1111

1212
config SYNC_SYS_SEMAPHORE
1313
bool "Use sys::Semaphore to synchronize forks"
@@ -27,4 +27,10 @@ choice
2727
Use to have the dining philosophers sample use a single data structure, protected
2828
by a sync::Mutex and coordinated with a sync::Condvar, to synchronize.
2929

30+
config SYNC_CHANNEL
31+
bool "Use sync::channel to synchronize forks"
32+
help
33+
Use to have the dining philosophers sample use a worker thread, communicating via
34+
channels to synchronize.
35+
3036
endchoice

samples/philosophers/sample.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ tests:
3030
min_ram: 32
3131
extra_configs:
3232
- CONFIG_SYNC_CONDVAR=y
33+
sample.rust.philosopher.channel:
34+
tags: introduction
35+
min_ram: 32
36+
extra_configs:
37+
- CONFIG_SYNC_CHANNEL=y

samples/philosophers/src/channel.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use zephyr::{
1616
kobj_define,
1717
sync::Arc,
1818
};
19-
use zephyr::object::KobjInit;
2019

2120
use crate::{NUM_PHIL, ForkSync};
2221

@@ -113,23 +112,21 @@ impl ChannelSync {
113112
/// Generate a syncer out of a ChannelSync.
114113
#[allow(dead_code)]
115114
pub fn get_channel_syncer() -> Vec<Arc<dyn ForkSync>> {
116-
COMMAND_QUEUE.init();
117-
let command_queue = COMMAND_QUEUE.get();
115+
let command_queue = COMMAND_QUEUE.init_once(()).unwrap();
118116
let (cq_send, cq_recv) = channel::unbounded_from(command_queue);
119117
let reply_queues = REPLY_QUEUES.each_ref().map(|m| {
120-
m.init();
121-
channel::unbounded_from(m.get())
118+
channel::unbounded_from(m.init_once(()).unwrap())
122119
});
123120
let syncer = reply_queues.into_iter().map(|rqueue| {
124121
let item = Box::new(ChannelSync::new(cq_send.clone(), rqueue))
125122
as Box<dyn ForkSync>;
126123
Arc::from(item)
127124
});
128125

129-
let channel_thread = CHANNEL_THREAD.spawn(CHANNEL_STACK.token(), move || {
126+
let channel_child = CHANNEL_THREAD.init_once(CHANNEL_STACK.init_once(()).unwrap()).unwrap();
127+
channel_child.spawn(move || {
130128
channel_thread(cq_recv);
131129
});
132-
channel_thread.start();
133130

134131
syncer.collect()
135132
}

samples/philosophers/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ use crate::condsync::CondSync;
2727
#[allow(unused_imports)]
2828
use crate::sysmutex::SysMutexSync;
2929
#[allow(unused_imports)]
30+
use crate::channel::get_channel_syncer;
31+
#[allow(unused_imports)]
3032
use crate::semsync::semaphore_sync;
3133

34+
mod channel;
3235
mod condsync;
3336
mod sysmutex;
3437
mod semsync;
@@ -113,6 +116,11 @@ fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
113116
result
114117
}
115118

119+
#[cfg(CONFIG_SYNC_CHANNEL)]
120+
fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
121+
get_channel_syncer()
122+
}
123+
116124
fn phil_thread(n: usize, syncer: Arc<dyn ForkSync>) {
117125
printkln!("Child {} started: {:?}", n, syncer);
118126

zephyr/src/object.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,19 @@ macro_rules! _kobj_rule {
309309
($v:vis, $name:ident, [ThreadStack<{$size:expr}>; $asize:expr]) => {
310310
$crate::_kobj_stack!($v, $name, $size, $asize);
311311
};
312+
313+
// Queues.
314+
($v:vis, $name: ident, StaticQueue) => {
315+
#[link_section = concat!("._k_queue.static.", stringify!($name), ".", file!(), line!())]
316+
$v static $name: $crate::sys::queue::StaticQueue =
317+
unsafe { ::core::mem::zeroed() };
318+
};
319+
320+
($v:vis, $name: ident, [StaticQueue; $size:expr]) => {
321+
#[link_section = concat!("._k_queue.static.", stringify!($name), ".", file!(), line!())]
322+
$v static $name: [$crate::sys::queue::StaticQueue; $size] =
323+
unsafe { ::core::mem::zeroed() };
324+
};
312325
}
313326

314327
#[doc(hidden)]

0 commit comments

Comments
 (0)