@@ -68,22 +68,18 @@ extern "C" fn rust_main() {
68
68
printkln ! ( "Hello world from Rust on {}" , zephyr:: kconfig:: CONFIG_BOARD ) ;
69
69
printkln ! ( "Time tick: {}" , zephyr:: time:: SYS_FREQUENCY ) ;
70
70
71
- let stats = Arc :: new ( Mutex :: new_from (
72
- Stats :: default ( ) ,
73
- STAT_MUTEX . init_once ( ( ) ) . unwrap ( ) ,
74
- ) ) ;
71
+ let stats = & STAT_MUTEX ;
75
72
76
73
let syncers = get_syncer ( ) ;
77
74
78
75
printkln ! ( "Pre fork" ) ;
79
76
80
77
for ( i, syncer) in ( 0 ..NUM_PHIL ) . zip ( syncers. into_iter ( ) ) {
81
- let child_stat = stats. clone ( ) ;
82
78
let thread = PHIL_THREADS [ i]
83
79
. init_once ( PHIL_STACKS [ i] . init_once ( ( ) ) . unwrap ( ) )
84
80
. unwrap ( ) ;
85
81
thread. spawn ( move || {
86
- phil_thread ( i, syncer, child_stat ) ;
82
+ phil_thread ( i, syncer, stats ) ;
87
83
} ) ;
88
84
}
89
85
@@ -133,7 +129,7 @@ fn get_syncer() -> Vec<Arc<dyn ForkSync>> {
133
129
get_channel_syncer ( )
134
130
}
135
131
136
- fn phil_thread ( n : usize , syncer : Arc < dyn ForkSync > , stats : Arc < Mutex < Stats > > ) {
132
+ fn phil_thread ( n : usize , syncer : Arc < dyn ForkSync > , stats : & ' static Mutex < Stats > ) {
137
133
printkln ! ( "Child {} started: {:?}" , n, syncer) ;
138
134
139
135
// Determine our two forks.
@@ -191,6 +187,17 @@ struct Stats {
191
187
thinking : [ u64 ; NUM_PHIL ] ,
192
188
}
193
189
190
+ impl Stats {
191
+ /// Const constructor to allow for static initializaion.
192
+ pub const fn new ( ) -> Self {
193
+ Self {
194
+ count : [ 0 ; NUM_PHIL ] ,
195
+ eating : [ 0 ; NUM_PHIL ] ,
196
+ thinking : [ 0 ; NUM_PHIL ] ,
197
+ }
198
+ }
199
+ }
200
+
194
201
impl Stats {
195
202
fn record_eat ( & mut self , index : usize , time : Duration ) {
196
203
self . eating [ index] += time. to_millis ( ) ;
@@ -211,9 +218,9 @@ impl Stats {
211
218
}
212
219
}
213
220
221
+ static STAT_MUTEX : Mutex < Stats > = Mutex :: new ( Stats :: new ( ) ) ;
222
+
214
223
kobj_define ! {
215
224
static PHIL_THREADS : [ StaticThread ; NUM_PHIL ] ;
216
225
static PHIL_STACKS : [ ThreadStack <PHIL_STACK_SIZE >; NUM_PHIL ] ;
217
-
218
- static STAT_MUTEX : StaticMutex ;
219
226
}
0 commit comments