File tree 4 files changed +72
-2
lines changed
4 files changed +72
-2
lines changed Original file line number Diff line number Diff line change @@ -7,14 +7,20 @@ source "Kconfig.zephyr"
7
7
8
8
choice
9
9
prompt "Select Synchronization implementation"
10
- default SYNC_CHANNEL
10
+ default SYNC_SYS_DYNAMIC_SEMAPHORE
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::Mutex , with one per fork, to
15
+ Use to have the dining philosophers sample use sys::Semaphore , with one per fork, to
16
16
synchronize.
17
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
+
18
24
config SYNC_SYS_MUTEX
19
25
bool "Use sys::Semaphore to synchronize forks"
20
26
help
Original file line number Diff line number Diff line change @@ -17,6 +17,11 @@ tests:
17
17
min_ram : 32
18
18
extra_configs :
19
19
- CONFIG_SYNC_SYS_SEMAPHORE=y
20
+ sample.rust.philosopher.dynsemaphore :
21
+ tags : introduction
22
+ min_ram : 32
23
+ extra_configs :
24
+ - CONFIG_SYNC_SYS_DYNAMIC_SEMAPHORE=y
20
25
sample.rust.philosopher.sysmutex :
21
26
tags : introduction
22
27
min_ram : 32
Original file line number Diff line number Diff line change
1
+ // Copyright (c) 2023 Linaro LTD
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ //! Semaphore based sync.
5
+ //!
6
+ //! This is the simplest type of sync, which uses a single semaphore per fork.
7
+
8
+ extern crate alloc;
9
+
10
+ use alloc:: vec:: Vec ;
11
+ use alloc:: boxed:: Box ;
12
+
13
+ use zephyr:: {
14
+ sync:: Arc , sys:: sync:: Semaphore , time:: Forever
15
+ } ;
16
+
17
+ use crate :: { ForkSync , NUM_PHIL } ;
18
+
19
+ #[ derive( Debug ) ]
20
+ pub struct SemSync {
21
+ /// The forks for this philosopher. This is a big excessive, as we really don't need all of
22
+ /// them, but the ForSync code uses the index here.
23
+ forks : [ Arc < Semaphore > ; NUM_PHIL ] ,
24
+ }
25
+
26
+ impl ForkSync for SemSync {
27
+ fn take ( & self , index : usize ) {
28
+ self . forks [ index] . take ( Forever ) . unwrap ( ) ;
29
+ }
30
+
31
+ fn release ( & self , index : usize ) {
32
+ self . forks [ index] . give ( ) ;
33
+ }
34
+ }
35
+
36
+ #[ allow( dead_code) ]
37
+ pub fn dyn_semaphore_sync ( ) -> Vec < Arc < dyn ForkSync > > {
38
+ let forks = [ ( ) ; NUM_PHIL ] . each_ref ( ) . map ( |( ) | {
39
+ Arc :: new ( Semaphore :: new ( 1 , 1 ) . unwrap ( ) )
40
+ } ) ;
41
+
42
+ let syncers = ( 0 ..NUM_PHIL ) . map ( |_| {
43
+ let syncer = SemSync {
44
+ forks : forks. clone ( ) ,
45
+ } ;
46
+ let item = Box :: new ( syncer) as Box < dyn ForkSync > ;
47
+ Arc :: from ( item)
48
+ } ) . collect ( ) ;
49
+
50
+ syncers
51
+ }
Original file line number Diff line number Diff line change @@ -30,9 +30,12 @@ use crate::sysmutex::SysMutexSync;
30
30
use crate :: channel:: get_channel_syncer;
31
31
#[ allow( unused_imports) ]
32
32
use crate :: semsync:: semaphore_sync;
33
+ #[ allow( unused_imports) ]
34
+ use crate :: dynsemsync:: dyn_semaphore_sync;
33
35
34
36
mod channel;
35
37
mod condsync;
38
+ mod dynsemsync;
36
39
mod sysmutex;
37
40
mod semsync;
38
41
@@ -95,6 +98,11 @@ fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
95
98
semaphore_sync ( )
96
99
}
97
100
101
+ #[ cfg( CONFIG_SYNC_SYS_DYNAMIC_SEMAPHORE ) ]
102
+ fn get_syncer ( ) -> Vec < Arc < dyn ForkSync > > {
103
+ dyn_semaphore_sync ( )
104
+ }
105
+
98
106
#[ cfg( CONFIG_SYNC_SYS_MUTEX ) ]
99
107
fn get_syncer ( ) -> Vec < Arc < dyn ForkSync > > {
100
108
let syncer = Box :: new ( SysMutexSync :: new ( ) )
You can’t perform that action at this time.
0 commit comments