File tree Expand file tree Collapse file tree 4 files changed +80
-3
lines changed Expand file tree Collapse file tree 4 files changed +80
-3
lines changed Original file line number Diff line number Diff line change @@ -7,12 +7,18 @@ source "Kconfig.zephyr"
7
7
8
8
choice
9
9
prompt "Select Synchronization implementation"
10
- default SYNC_SYS_SEMAPHORE
10
+ default SYNC_SYS_MUTEX
11
11
12
12
config SYNC_SYS_SEMAPHORE
13
13
bool "Use sys::Semaphore to synchronize forks"
14
14
help
15
- Use to have the dining philosophers sample use sys::Semaphore, with one per form,
16
- to synchronize.
15
+ Use to have the dining philosophers sample use sys::Mutex, with one per fork, to
16
+ synchronize.
17
+
18
+ config SYNC_SYS_MUTEX
19
+ bool "Use sys::Semaphore to synchronize forks"
20
+ help
21
+ Use to have the dining philosophers sample use sys::Mutex, with one per fork, to
22
+ synchronize.
17
23
18
24
endchoice
Original file line number Diff line number Diff line change 20
20
min_ram : 32
21
21
extra_configs :
22
22
- CONFIG_SYNC_SYS_SEMAPHORE=y
23
+ sample.rust.philosopher.sysmutex :
24
+ tags : introduction
25
+ min_ram : 32
26
+ extra_configs :
27
+ - CONFIG_SYNC_SYS_MUTEX=y
Original file line number Diff line number Diff line change @@ -23,8 +23,11 @@ use zephyr::{
23
23
24
24
// These are optional, based on Kconfig, so allow them to be unused.
25
25
#[ allow( unused_imports) ]
26
+ use crate :: sysmutex:: SysMutexSync ;
27
+ #[ allow( unused_imports) ]
26
28
use crate :: semsync:: semaphore_sync;
27
29
30
+ mod sysmutex;
28
31
mod semsync;
29
32
30
33
/// How many philosophers. There will be the same number of forks.
@@ -82,6 +85,18 @@ fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
82
85
semaphore_sync ( )
83
86
}
84
87
88
+ #[ cfg( CONFIG_SYNC_SYS_MUTEX ) ]
89
+ fn get_syncer ( ) -> Vec < Arc < dyn ForkSync > > {
90
+ let syncer = Box :: new ( SysMutexSync :: new ( ) )
91
+ as Box < dyn ForkSync > ;
92
+ let syncer: Arc < dyn ForkSync > = Arc :: from ( syncer) ;
93
+ let mut result = Vec :: new ( ) ;
94
+ for _ in 0 ..NUM_PHIL {
95
+ result. push ( syncer. clone ( ) ) ;
96
+ }
97
+ result
98
+ }
99
+
85
100
fn phil_thread ( n : usize , syncer : Arc < dyn ForkSync > ) {
86
101
printkln ! ( "Child {} started: {:?}" , n, syncer) ;
87
102
Original file line number Diff line number Diff line change
1
+ // Copyright (c) 2024 Linaro LTD
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ //! # sys::Mutex implementation of ForkSync
5
+ //!
6
+ //! This is a simple implementation of the Fork synchronizer that uses underlying Zephyr `k_mutex`
7
+ //! wrapped in `sys::Mutex`. The ForkSync semantics map simply to these.
8
+
9
+ use crate :: {
10
+ ForkSync ,
11
+ NUM_PHIL ,
12
+ } ;
13
+ use zephyr:: kobj_define;
14
+ use zephyr:: sys:: sync:: Mutex ;
15
+ use zephyr:: time:: Forever ;
16
+
17
+ type SysMutexes = [ Mutex ; NUM_PHIL ] ;
18
+
19
+ /// A simple implementation of ForkSync based on underlying Zephyr sys::Mutex, which uses explicit
20
+ /// lock and release semantics.
21
+
22
+ #[ derive( Debug ) ]
23
+ pub struct SysMutexSync {
24
+ locks : SysMutexes ,
25
+ }
26
+
27
+ impl SysMutexSync {
28
+ #[ allow( dead_code) ]
29
+ pub fn new ( ) -> SysMutexSync {
30
+ let locks = MUTEXES . each_ref ( ) . map ( |m| {
31
+ m. init_once ( ( ) ) . unwrap ( )
32
+ } ) ;
33
+ SysMutexSync { locks }
34
+ }
35
+ }
36
+
37
+ impl ForkSync for SysMutexSync {
38
+ fn take ( & self , index : usize ) {
39
+ self . locks [ index] . lock ( Forever ) . unwrap ( ) ;
40
+ }
41
+
42
+ fn release ( & self , index : usize ) {
43
+ unsafe {
44
+ self . locks [ index] . unlock ( ) . unwrap ( ) ;
45
+ }
46
+ }
47
+ }
48
+
49
+ kobj_define ! {
50
+ static MUTEXES : [ StaticMutex ; NUM_PHIL ] ;
51
+ }
You can’t perform that action at this time.
0 commit comments