@@ -15,6 +15,7 @@ use core::pin::Pin;
15
15
use alloc:: collections:: vec_deque:: VecDeque ;
16
16
use alloc:: vec;
17
17
use alloc:: vec:: Vec ;
18
+ use static_cell:: StaticCell ;
18
19
use zephyr:: sync:: SpinMutex ;
19
20
use zephyr:: time:: NoWait ;
20
21
use zephyr:: work:: futures:: work_size;
@@ -47,6 +48,9 @@ const WORK_STACK_SIZE: usize = 2048;
47
48
const TOTAL_ITERS : usize = 1_000 ;
48
49
// const TOTAL_ITERS: usize = 10_000;
49
50
51
+ /// A heapless::Vec, with a maximum size of the number of threads chosen above.
52
+ type HeaplessVec < T > = heapless:: Vec < T , NUM_THREADS > ;
53
+
50
54
#[ no_mangle]
51
55
extern "C" fn rust_main ( ) {
52
56
let tester = ThreadTests :: new ( NUM_THREADS ) ;
@@ -101,17 +105,17 @@ extern "C" fn rust_main() {
101
105
/// low priority task is providing the data.
102
106
struct ThreadTests {
103
107
/// Each test thread gets a semaphore, to use as appropriate for that test.
104
- sems : Vec < Arc < Semaphore > > ,
108
+ sems : HeaplessVec < & ' static Semaphore > ,
105
109
106
110
/// This semaphore is used to ping-ping back to another thread.
107
- back_sems : Vec < Arc < Semaphore > > ,
111
+ back_sems : HeaplessVec < & ' static Semaphore > ,
108
112
109
113
/// Each test also has a message queue, for testing, that it has sender and receiver for.
110
- chans : Vec < ChanPair < u32 > > ,
114
+ chans : HeaplessVec < ChanPair < u32 > > ,
111
115
112
116
/// In addition, each test has a channel it owns the receiver for that listens for commands
113
117
/// about what test to run.
114
- commands : Vec < Sender < Command > > ,
118
+ commands : HeaplessVec < Sender < Command > > ,
115
119
116
120
/// Low and high also take commands.
117
121
low_command : Sender < Command > ,
@@ -145,10 +149,10 @@ impl ThreadTests {
145
149
let _ = Arc :: into_raw ( workq. clone ( ) ) ;
146
150
147
151
let mut result = Self {
148
- sems : Vec :: new ( ) ,
149
- back_sems : Vec :: new ( ) ,
150
- chans : Vec :: new ( ) ,
151
- commands : Vec :: new ( ) ,
152
+ sems : HeaplessVec :: new ( ) ,
153
+ back_sems : HeaplessVec :: new ( ) ,
154
+ chans : HeaplessVec :: new ( ) ,
155
+ commands : HeaplessVec :: new ( ) ,
152
156
results : ChanPair :: new_unbounded ( ) ,
153
157
low_command : low_send,
154
158
high_command : high_send,
@@ -157,18 +161,21 @@ impl ThreadTests {
157
161
158
162
let mut thread_commands = Vec :: new ( ) ;
159
163
160
- for _ in 0 ..count {
161
- let sem = Arc :: new ( Semaphore :: new ( 0 , u32:: MAX ) ) ;
162
- result. sems . push ( sem. clone ( ) ) ;
164
+ static SEMS : [ StaticCell < Semaphore > ; NUM_THREADS ] = [ const { StaticCell :: new ( ) } ; NUM_THREADS ] ;
165
+ static BACK_SEMS : [ StaticCell < Semaphore > ; NUM_THREADS ] = [ const { StaticCell :: new ( ) } ; NUM_THREADS ] ;
163
166
164
- let sem = Arc :: new ( Semaphore :: new ( 0 , u32:: MAX ) ) ;
165
- result. back_sems . push ( sem) ;
167
+ for i in 0 ..count {
168
+ let sem = SEMS [ i] . init ( Semaphore :: new ( 0 , u32:: MAX ) ) ;
169
+ result. sems . push ( sem) . unwrap ( ) ;
170
+
171
+ let sem = BACK_SEMS [ i] . init ( Semaphore :: new ( 0 , u32:: MAX ) ) ;
172
+ result. back_sems . push ( sem) . unwrap ( ) ;
166
173
167
174
let chans = ChanPair :: new_bounded ( 1 ) ;
168
- result. chans . push ( chans. clone ( ) ) ;
175
+ result. chans . push ( chans. clone ( ) ) . unwrap ( ) ;
169
176
170
177
let ( csend, crecv) = bounded ( 1 ) ;
171
- result. commands . push ( csend) ;
178
+ result. commands . push ( csend) . unwrap ( ) ;
172
179
thread_commands. push ( crecv) ;
173
180
}
174
181
@@ -213,8 +220,8 @@ impl ThreadTests {
213
220
work_size( Self :: ping_pong_worker_async(
214
221
result. clone( ) ,
215
222
0 ,
216
- result. sems[ 0 ] . clone ( ) ,
217
- result. back_sems[ 0 ] . clone ( ) ,
223
+ result. sems[ 0 ] ,
224
+ result. back_sems[ 0 ] ,
218
225
6
219
226
) )
220
227
) ;
@@ -348,7 +355,7 @@ impl ThreadTests {
348
355
// ourselves.
349
356
Command :: SimpleSemAsync ( count) => {
350
357
spawn (
351
- Self :: simple_sem_async ( this. clone ( ) , id, this. sems [ id] . clone ( ) , count) ,
358
+ Self :: simple_sem_async ( this. clone ( ) , id, this. sems [ id] , count) ,
352
359
& this. workq ,
353
360
c"worker" ,
354
361
) ;
@@ -360,7 +367,7 @@ impl ThreadTests {
360
367
Self :: simple_sem_yield_async (
361
368
this. clone ( ) ,
362
369
id,
363
- this. sems [ id] . clone ( ) ,
370
+ this. sems [ id] ,
364
371
count,
365
372
) ,
366
373
& this. workq ,
@@ -371,7 +378,7 @@ impl ThreadTests {
371
378
372
379
Command :: SemWaitAsync ( count) => {
373
380
spawn (
374
- Self :: sem_take_async ( this. clone ( ) , id, this. sems [ id] . clone ( ) , count) ,
381
+ Self :: sem_take_async ( this. clone ( ) , id, this. sems [ id] , count) ,
375
382
& this. workq ,
376
383
c"worker" ,
377
384
) ;
@@ -380,7 +387,7 @@ impl ThreadTests {
380
387
381
388
Command :: SemWaitSameAsync ( count) => {
382
389
spawn (
383
- Self :: sem_take_async ( this. clone ( ) , id, this. sems [ id] . clone ( ) , count) ,
390
+ Self :: sem_take_async ( this. clone ( ) , id, this. sems [ id] , count) ,
384
391
& this. workq ,
385
392
c"worker" ,
386
393
) ;
@@ -399,8 +406,8 @@ impl ThreadTests {
399
406
Self :: ping_pong_worker_async (
400
407
this. clone ( ) ,
401
408
id,
402
- this. sems [ id] . clone ( ) ,
403
- this. back_sems [ id] . clone ( ) ,
409
+ this. sems [ id] ,
410
+ this. back_sems [ id] ,
404
411
count,
405
412
) ,
406
413
& this. workq ,
@@ -424,8 +431,8 @@ impl ThreadTests {
424
431
Self :: ping_pong_worker_async (
425
432
this. clone ( ) ,
426
433
th,
427
- this. sems [ 0 ] . clone ( ) ,
428
- this. back_sems [ 0 ] . clone ( ) ,
434
+ this. sems [ 0 ] ,
435
+ this. back_sems [ 0 ] ,
429
436
count,
430
437
) ,
431
438
& this. workq ,
@@ -464,7 +471,7 @@ impl ThreadTests {
464
471
}
465
472
}
466
473
467
- async fn simple_sem_async ( this : Arc < Self > , id : usize , sem : Arc < Semaphore > , count : usize ) {
474
+ async fn simple_sem_async ( this : Arc < Self > , id : usize , sem : & ' static Semaphore , count : usize ) {
468
475
for _ in 0 ..count {
469
476
sem. give ( ) ;
470
477
sem. take_async ( NoWait ) . await . unwrap ( ) ;
@@ -477,7 +484,7 @@ impl ThreadTests {
477
484
. unwrap ( ) ;
478
485
}
479
486
480
- async fn simple_sem_yield_async ( this : Arc < Self > , id : usize , sem : Arc < Semaphore > , count : usize ) {
487
+ async fn simple_sem_yield_async ( this : Arc < Self > , id : usize , sem : & ' static Semaphore , count : usize ) {
481
488
for _ in 0 ..count {
482
489
sem. give ( ) ;
483
490
sem. take_async ( NoWait ) . await . unwrap ( ) ;
@@ -509,7 +516,7 @@ impl ThreadTests {
509
516
}
510
517
}
511
518
512
- async fn sem_take_async ( this : Arc < Self > , id : usize , sem : Arc < Semaphore > , count : usize ) {
519
+ async fn sem_take_async ( this : Arc < Self > , id : usize , sem : & ' static Semaphore , count : usize ) {
513
520
for _ in 0 ..count {
514
521
// Enable this to verify that we are actually blocking.
515
522
if false {
@@ -551,8 +558,8 @@ impl ThreadTests {
551
558
async fn ping_pong_worker_async (
552
559
this : Arc < Self > ,
553
560
id : usize ,
554
- sem : Arc < Semaphore > ,
555
- back_sem : Arc < Semaphore > ,
561
+ sem : & ' static Semaphore ,
562
+ back_sem : & ' static Semaphore ,
556
563
count : usize ,
557
564
) {
558
565
for i in 0 ..count {
@@ -615,7 +622,7 @@ impl ThreadTests {
615
622
// No reply.
616
623
}
617
624
618
- async fn sem_giver_async ( this : Arc < Self > , sems : Vec < Arc < Semaphore > > , count : usize ) {
625
+ async fn sem_giver_async ( this : Arc < Self > , sems : HeaplessVec < & ' static Semaphore > , count : usize ) {
619
626
for _ in 0 ..count {
620
627
for sem in & sems {
621
628
sem. give ( ) ;
@@ -699,7 +706,7 @@ impl ThreadTests {
699
706
}
700
707
}
701
708
702
- #[ derive( Clone ) ]
709
+ #[ derive( Clone , Debug ) ]
703
710
struct ChanPair < T > {
704
711
sender : Sender < T > ,
705
712
receiver : Receiver < T > ,
0 commit comments