@@ -26,6 +26,8 @@ static struct rt_mutex static_mutex;
26
26
static rt_mutex_t dynamic_mutex ;
27
27
#endif /* RT_USING_HEAP */
28
28
29
+ static volatile int _sync_flag ;
30
+
29
31
/* init test */
30
32
static void test_static_mutex_init (void )
31
33
{
@@ -72,11 +74,15 @@ static void static_mutex_take_entry(void *param)
72
74
{
73
75
uassert_true (RT_FALSE );
74
76
}
77
+ _sync_flag ++ ;
75
78
}
79
+
76
80
static void test_static_mutex_take (void )
77
81
{
78
82
rt_err_t result ;
79
83
84
+ _sync_flag = 0 ;
85
+
80
86
result = rt_mutex_init (& static_mutex , "static_mutex" , RT_IPC_FLAG_PRIO );
81
87
if (RT_EOK != result )
82
88
{
@@ -104,8 +110,10 @@ static void test_static_mutex_take(void)
104
110
/* startup thread take second */
105
111
rt_thread_startup (tid );
106
112
107
- /* let system schedule */
108
- rt_thread_mdelay (5 );
113
+ while (_sync_flag != 1 )
114
+ {
115
+ rt_thread_mdelay (10 );
116
+ }
109
117
110
118
result = rt_mutex_detach (& static_mutex );
111
119
if (RT_EOK != result )
@@ -128,18 +136,24 @@ static void static_mutex_release_entry(void *param)
128
136
{
129
137
uassert_true (RT_FALSE );
130
138
}
139
+ _sync_flag ++ ;
131
140
}
132
141
static void test_static_mutex_release (void )
133
142
{
134
143
rt_err_t result ;
135
144
145
+ _sync_flag = 0 ;
146
+
136
147
result = rt_mutex_init (& static_mutex , "static_mutex" , RT_IPC_FLAG_PRIO );
137
148
if (RT_EOK != result )
138
149
{
139
150
uassert_true (RT_FALSE );
140
151
return ;
141
152
}
142
153
154
+ result = rt_mutex_release (& static_mutex );
155
+ uassert_true (result < 0 );
156
+
143
157
/* take mutex */
144
158
result = rt_mutex_take (& static_mutex , RT_WAITING_FOREVER );
145
159
if (RT_EOK != result )
@@ -165,8 +179,10 @@ static void test_static_mutex_release(void)
165
179
/* startup thread and take mutex second */
166
180
rt_thread_startup (tid );
167
181
168
- /* let system schedule */
169
- rt_thread_mdelay (5 );
182
+ while (_sync_flag != 1 )
183
+ {
184
+ rt_thread_mdelay (10 );
185
+ }
170
186
171
187
result = rt_mutex_detach (& static_mutex );
172
188
if (RT_EOK != result )
@@ -188,11 +204,14 @@ static void static_mutex_trytake_entry(void *param)
188
204
{
189
205
uassert_true (RT_FALSE );
190
206
}
207
+ _sync_flag ++ ;
191
208
}
192
209
static void test_static_mutex_trytake (void )
193
210
{
194
211
rt_err_t result ;
195
212
213
+ _sync_flag = 0 ;
214
+
196
215
result = rt_mutex_init (& static_mutex , "static_mutex" , RT_IPC_FLAG_PRIO );
197
216
if (RT_EOK != result )
198
217
{
@@ -220,8 +239,10 @@ static void test_static_mutex_trytake(void)
220
239
/* startup thread and trytake mutex second */
221
240
rt_thread_startup (tid );
222
241
223
- /* let system schedule */
224
- rt_thread_mdelay (5 );
242
+ while (_sync_flag != 1 )
243
+ {
244
+ rt_thread_mdelay (10 );
245
+ }
225
246
226
247
result = rt_mutex_detach (& static_mutex );
227
248
if (RT_EOK != result )
@@ -250,6 +271,7 @@ static void static_thread1_entry(void *param)
250
271
{
251
272
uassert_true (RT_TRUE );
252
273
}
274
+ _sync_flag ++ ;
253
275
}
254
276
255
277
static void static_thread2_entry (void * param )
@@ -265,6 +287,7 @@ static void static_thread2_entry(void *param)
265
287
{
266
288
rt_mutex_release (mutex );
267
289
}
290
+ _sync_flag ++ ;
268
291
}
269
292
static void static_thread3_entry (void * param )
270
293
{
@@ -282,6 +305,7 @@ static void static_thread3_entry(void *param)
282
305
while (rt_tick_get () - tick < (RT_TICK_PER_SECOND / 2 ));
283
306
284
307
rt_mutex_release (mutex );
308
+ _sync_flag ++ ;
285
309
}
286
310
287
311
static void test_static_pri_reverse (void )
@@ -291,6 +315,8 @@ static void test_static_pri_reverse(void)
291
315
tid2 = RT_NULL ;
292
316
tid3 = RT_NULL ;
293
317
318
+ _sync_flag = 0 ;
319
+
294
320
result = rt_mutex_init (& static_mutex , "static_mutex" , RT_IPC_FLAG_PRIO );
295
321
if (RT_EOK != result )
296
322
{
@@ -328,7 +354,10 @@ static void test_static_pri_reverse(void)
328
354
if (tid3 != RT_NULL )
329
355
rt_thread_startup (tid3 );
330
356
331
- rt_thread_mdelay (1000 );
357
+ while (_sync_flag != 3 )
358
+ {
359
+ rt_thread_mdelay (10 );
360
+ }
332
361
333
362
result = rt_mutex_detach (& static_mutex );
334
363
if (RT_EOK != result )
@@ -385,11 +414,15 @@ static void dynamic_mutex_take_entry(void *param)
385
414
{
386
415
uassert_true (RT_FALSE );
387
416
}
417
+ _sync_flag ++ ;
388
418
}
419
+
389
420
static void test_dynamic_mutex_take (void )
390
421
{
391
422
rt_err_t result ;
392
423
424
+ _sync_flag = 0 ;
425
+
393
426
dynamic_mutex = rt_mutex_create ("dynamic_mutex" , RT_IPC_FLAG_PRIO );
394
427
if (RT_NULL == dynamic_mutex )
395
428
{
@@ -417,8 +450,10 @@ static void test_dynamic_mutex_take(void)
417
450
/* startup thread take second */
418
451
rt_thread_startup (tid );
419
452
420
- /* let system schedule */
421
- rt_thread_mdelay (5 );
453
+ while (_sync_flag != 1 )
454
+ {
455
+ rt_thread_mdelay (10 );
456
+ }
422
457
423
458
result = rt_mutex_delete (dynamic_mutex );
424
459
if (RT_EOK != result )
@@ -441,18 +476,23 @@ static void dynamic_mutex_release_entry(void *param)
441
476
{
442
477
uassert_true (RT_FALSE );
443
478
}
479
+ _sync_flag ++ ;
444
480
}
445
481
static void test_dynamic_mutex_release (void )
446
482
{
447
483
rt_err_t result ;
448
484
485
+ _sync_flag = 0 ;
449
486
dynamic_mutex = rt_mutex_create ("dynamic_mutex" , RT_IPC_FLAG_PRIO );
450
487
if (RT_NULL == dynamic_mutex )
451
488
{
452
489
uassert_true (RT_FALSE );
453
490
return ;
454
491
}
455
492
493
+ result = rt_mutex_release (dynamic_mutex );
494
+ uassert_true (result < 0 );
495
+
456
496
/* take mutex */
457
497
result = rt_mutex_take (dynamic_mutex , RT_WAITING_FOREVER );
458
498
if (RT_EOK != result )
@@ -478,8 +518,10 @@ static void test_dynamic_mutex_release(void)
478
518
/* startup thread and take mutex second */
479
519
rt_thread_startup (tid );
480
520
481
- /* let system schedule */
482
- rt_thread_mdelay (5 );
521
+ while (_sync_flag != 1 )
522
+ {
523
+ rt_thread_mdelay (10 );
524
+ }
483
525
484
526
result = rt_mutex_delete (dynamic_mutex );
485
527
if (RT_EOK != result )
@@ -501,11 +543,13 @@ static void dynamic_mutex_trytake_entry(void *param)
501
543
{
502
544
uassert_true (RT_FALSE );
503
545
}
546
+ _sync_flag ++ ;
504
547
}
505
548
static void test_dynamic_mutex_trytake (void )
506
549
{
507
550
rt_err_t result ;
508
551
552
+ _sync_flag = 0 ;
509
553
dynamic_mutex = rt_mutex_create ("dynamic_mutex" , RT_IPC_FLAG_PRIO );
510
554
if (RT_NULL == dynamic_mutex )
511
555
{
@@ -533,8 +577,10 @@ static void test_dynamic_mutex_trytake(void)
533
577
/* startup thread and trytake mutex second */
534
578
rt_thread_startup (tid );
535
579
536
- /* let system schedule */
537
- rt_thread_mdelay (5 );
580
+ while (_sync_flag != 1 )
581
+ {
582
+ rt_thread_mdelay (10 );
583
+ }
538
584
539
585
result = rt_mutex_delete (dynamic_mutex );
540
586
if (RT_EOK != result )
@@ -559,6 +605,7 @@ static void dynamic_thread1_entry(void *param)
559
605
{
560
606
uassert_true (RT_TRUE );
561
607
}
608
+ _sync_flag ++ ;
562
609
}
563
610
564
611
static void dynamic_thread2_entry (void * param )
@@ -574,6 +621,7 @@ static void dynamic_thread2_entry(void *param)
574
621
{
575
622
rt_mutex_release (mutex );
576
623
}
624
+ _sync_flag ++ ;
577
625
}
578
626
static void dynamic_thread3_entry (void * param )
579
627
{
@@ -591,6 +639,7 @@ static void dynamic_thread3_entry(void *param)
591
639
while (rt_tick_get () - tick < (RT_TICK_PER_SECOND / 2 ));
592
640
593
641
rt_mutex_release (mutex );
642
+ _sync_flag ++ ;
594
643
}
595
644
596
645
static void test_dynamic_pri_reverse (void )
@@ -600,6 +649,7 @@ static void test_dynamic_pri_reverse(void)
600
649
tid2 = RT_NULL ;
601
650
tid3 = RT_NULL ;
602
651
652
+ _sync_flag = 0 ;
603
653
dynamic_mutex = rt_mutex_create ("dynamic_mutex" , RT_IPC_FLAG_PRIO );
604
654
if (RT_NULL == dynamic_mutex )
605
655
{
@@ -637,7 +687,10 @@ static void test_dynamic_pri_reverse(void)
637
687
if (tid3 != RT_NULL )
638
688
rt_thread_startup (tid3 );
639
689
640
- rt_thread_mdelay (1000 );
690
+ while (_sync_flag != 3 )
691
+ {
692
+ rt_thread_mdelay (10 );
693
+ }
641
694
642
695
result = rt_mutex_delete (dynamic_mutex );
643
696
if (RT_EOK != result )
@@ -646,6 +699,66 @@ static void test_dynamic_pri_reverse(void)
646
699
uassert_true (RT_TRUE );
647
700
}
648
701
702
+ static void recursive_lock_test_entry (void * param )
703
+ {
704
+ rt_err_t result ;
705
+ rt_mutex_t mutex = (rt_mutex_t )param ;
706
+
707
+ result = rt_mutex_take (mutex , RT_WAITING_FOREVER );
708
+ uassert_true (result == RT_EOK );
709
+ uassert_true (_sync_flag == 0 );
710
+ result = rt_mutex_take (mutex , RT_WAITING_FOREVER );
711
+ uassert_true (result == RT_EOK );
712
+ _sync_flag ++ ;
713
+ }
714
+
715
+ static void test_recurse_lock (void )
716
+ {
717
+ rt_err_t result ;
718
+
719
+ _sync_flag = 0 ;
720
+ result = rt_mutex_init (& static_mutex , "static_mutex" , RT_IPC_FLAG_PRIO );
721
+ uassert_true (result == RT_EOK );
722
+
723
+ /* take mutex and not release */
724
+ result = rt_mutex_take (& static_mutex , RT_WAITING_FOREVER );
725
+ uassert_true (result == RT_EOK );
726
+
727
+ /* take mutex twice */
728
+ result = rt_mutex_take (& static_mutex , RT_WAITING_FOREVER );
729
+ uassert_true (result == RT_EOK );
730
+
731
+ rt_thread_t tid = rt_thread_create ("mutex_th" ,
732
+ recursive_lock_test_entry ,
733
+ & static_mutex ,
734
+ THREAD_STACKSIZE ,
735
+ 10 ,
736
+ 10 );
737
+ _sync_flag = -1 ;
738
+
739
+ if (tid != RT_NULL )
740
+ rt_thread_startup (tid );
741
+
742
+ result = rt_mutex_release (& static_mutex );
743
+ uassert_true (result == RT_EOK );
744
+
745
+ _sync_flag = 0 ;
746
+
747
+ result = rt_mutex_release (& static_mutex );
748
+ uassert_true (result == RT_EOK );
749
+
750
+ while (_sync_flag != 1 )
751
+ {
752
+ rt_thread_mdelay (10 );
753
+ }
754
+
755
+ result = rt_mutex_take (& static_mutex , RT_WAITING_FOREVER );
756
+ uassert_true (result == RT_EOK );
757
+
758
+ result = rt_mutex_detach (& static_mutex );
759
+ uassert_true (result == RT_EOK );
760
+ }
761
+
649
762
static rt_err_t utest_tc_init (void )
650
763
{
651
764
#ifdef RT_USING_HEAP
@@ -678,6 +791,7 @@ static void testcase(void)
678
791
UTEST_UNIT_RUN (test_dynamic_mutex_trytake );
679
792
UTEST_UNIT_RUN (test_dynamic_pri_reverse );
680
793
#endif
794
+ UTEST_UNIT_RUN (test_recurse_lock );
681
795
}
682
796
UTEST_TC_EXPORT (testcase , "testcases.kernel.mutex_tc" , utest_tc_init , utest_tc_cleanup , 1000 );
683
797
0 commit comments