-
Notifications
You must be signed in to change notification settings - Fork 926
Expand file tree
/
Copy paththreadx_mutex_nested_priority_inheritance_test.c
More file actions
701 lines (504 loc) · 18 KB
/
threadx_mutex_nested_priority_inheritance_test.c
File metadata and controls
701 lines (504 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/* This test is designed to test multiple mutex priority inheritance situations. */
#include <stdio.h>
#include "tx_api.h"
#define DEMO_STACK_SIZE TEST_STACK_SIZE_PRINTF
/* Define the ThreadX object control blocks... */
static TX_THREAD thread_0;
#ifndef TX_DISABLE_PREEMPTION_THRESHOLD
static TX_THREAD thread_1;
static TX_THREAD thread_2;
static TX_THREAD thread_3;
static TX_THREAD thread_4;
static TX_THREAD thread_5;
static TX_THREAD thread_6;
static TX_MUTEX mutex_0;
static TX_MUTEX mutex_1;
static TX_MUTEX mutex_2;
static TX_MUTEX mutex_3;
/* Define the counters used in the demo application... */
static ULONG thread_0_counter;
static ULONG thread_1_counter;
static ULONG thread_2_counter;
static ULONG thread_3_counter;
static ULONG thread_4_counter;
static ULONG thread_5_counter;
static ULONG thread_6_counter;
#endif
/* Define thread prototypes. */
static void thread_0_entry(ULONG thread_input);
#ifndef TX_DISABLE_PREEMPTION_THRESHOLD
static void thread_1_entry(ULONG thread_input);
static void thread_2_entry(ULONG thread_input);
static void thread_3_entry(ULONG thread_input);
static void thread_4_entry(ULONG thread_input);
static void thread_5_entry(ULONG thread_input);
static void thread_6_entry(ULONG thread_input);
#endif
/* Prototype for test control return. */
void test_control_return(UINT status);
/* Define what the initial system looks like. */
#ifdef CTEST
void test_application_define(void *first_unused_memory)
#else
void threadx_mutex_nested_priority_inheritance_application_define(void *first_unused_memory)
#endif
{
CHAR *pointer;
UINT status;
pointer = (CHAR *) first_unused_memory;
/* Create thread. */
status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
pointer, DEMO_STACK_SIZE,
16, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Mutex Nested Priority Inheritance Test...................... ERROR #1\n");
test_control_return(1);
}
#ifndef TX_DISABLE_PREEMPTION_THRESHOLD
/* Create thread. */
status = tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
pointer, DEMO_STACK_SIZE,
8, 8, TX_NO_TIME_SLICE, TX_DONT_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Mutex Nested Priority Inheritance Test...................... ERROR #2\n");
test_control_return(1);
}
/* Create thread. */
status = tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
pointer, DEMO_STACK_SIZE,
4, 4, TX_NO_TIME_SLICE, TX_DONT_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Mutex Nested Priority Inheritance Test...................... ERROR #3\n");
test_control_return(1);
}
/* Create thread. */
status = tx_thread_create(&thread_3, "thread 3", thread_3_entry, 3,
pointer, DEMO_STACK_SIZE,
30, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Mutex Nested Priority Inheritance Test...................... ERROR #4\n");
test_control_return(1);
}
/* Create thread. */
status = tx_thread_create(&thread_4, "thread 4", thread_4_entry, 4,
pointer, DEMO_STACK_SIZE,
4, 4, TX_NO_TIME_SLICE, TX_DONT_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Mutex Nested Priority Inheritance Test...................... ERROR #4a\n");
test_control_return(1);
}
/* Create thread. */
status = tx_thread_create(&thread_5, "thread 5", thread_5_entry, 5,
pointer, DEMO_STACK_SIZE,
2, 2, TX_NO_TIME_SLICE, TX_DONT_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Mutex Nested Priority Inheritance Test...................... ERROR #4b\n");
test_control_return(1);
}
/* Create thread. */
status = tx_thread_create(&thread_6, "thread 6", thread_6_entry, 6,
pointer, DEMO_STACK_SIZE,
30, 30, TX_NO_TIME_SLICE, TX_DONT_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Mutex Nested Priority Inheritance Test...................... ERROR #4c\n");
test_control_return(1);
}
/* Create the mutexes. */
status = tx_mutex_create(&mutex_0, "mutex 0", TX_NO_INHERIT);
status += tx_mutex_create(&mutex_1, "mutex 1", TX_INHERIT);
status += tx_mutex_create(&mutex_2, "mutex 2", TX_INHERIT);
status += tx_mutex_create(&mutex_3, "mutex 3", TX_INHERIT);
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Mutex Nested Priority Inheritance Test...................... ERROR #5\n");
test_control_return(1);
}
#endif
}
/* Define the test threads. */
static void thread_0_entry(ULONG thread_input)
{
#ifdef TX_DISABLE_PREEMPTION_THRESHOLD
/* Preemption threshold is not enabled, skip this test. */
/* Inform user. */
printf("Running Mutex Nested Priority Inheritance Test...................... SUCCESS!\n");
test_control_return(0);
#else
UINT test_case = 0;
UINT loop_count = 0;
UINT priority;
UINT status;
/* Inform user. */
printf("Running Mutex Nested Priority Inheritance Test...................... ");
/* Get the mutex that will be owned always.... */
tx_mutex_get(&mutex_3, TX_WAIT_FOREVER);
while(loop_count++ < 50)
{
/* Increment the thread counter. */
thread_0_counter++;
tx_mutex_get(&mutex_0, TX_WAIT_FOREVER);
tx_mutex_get(&mutex_1, TX_WAIT_FOREVER);
tx_mutex_get(&mutex_2, TX_WAIT_FOREVER);
tx_thread_resume(&thread_1);
tx_thread_resume(&thread_2);
/* Suspend this thread. */
tx_thread_suspend(&thread_0);
/* Resume thread thread 3. */
tx_thread_resume(&thread_3);
/* Thread priority should be 4. */
if (thread_0.tx_thread_priority != 4)
{
/* Test error! */
printf("ERROR #6\n");
test_control_return(1);
}
/* Release the mutexes... Depending on the order they are released should dictate
the thread's returned to priority. */
if (test_case == 0)
{
tx_mutex_put(&mutex_2);
/* Priority should now be returned to priority 8. */
if (thread_0.tx_thread_priority != 8)
{
/* Test error! */
printf("ERROR #7\n");
test_control_return(2);
}
tx_mutex_put(&mutex_1);
/* Priority should now be returned to priority 15. */
if (thread_0.tx_thread_priority != 15)
{
/* Test error! */
printf("ERROR #8\n");
test_control_return(3);
}
tx_mutex_put(&mutex_0);
/* No change. */
if (thread_0.tx_thread_priority != 15)
{
/* Test error! */
printf("ERROR #9\n");
test_control_return(4);
}
}
else if (test_case == 1)
{
priority = thread_0.tx_thread_priority;
tx_mutex_put(&mutex_0);
/* Should have no change in priority since nothing was inherited for this mutex. */
if (thread_0.tx_thread_priority != priority)
{
/* Test error! */
printf("ERROR #10\n");
test_control_return(4);
}
tx_mutex_put(&mutex_1);
/* Should not do anything since mutex 2 elevated to a higher priority. */
if (thread_0.tx_thread_priority != priority)
{
/* Test error! */
printf("ERROR #11\n");
test_control_return(5);
}
tx_mutex_put(&mutex_2);
/* Should go back to priority 15. */
if (thread_0.tx_thread_priority != 15)
{
/* Test error! */
printf("ERROR #12\n");
test_control_return(6);
}
}
else if (test_case == 2)
{
tx_mutex_put(&mutex_2);
/* Should go back to priority 8. */
if (thread_0.tx_thread_priority != 8)
{
/* Test error! */
printf("ERROR #13\n");
test_control_return(7);
}
tx_mutex_put(&mutex_0);
/* Should not do anything. */
if (thread_0.tx_thread_priority != 8)
{
/* Test error! */
printf("ERROR #14\n");
test_control_return(8);
}
tx_mutex_put(&mutex_1);
/* Should go back to priority 15. */
if (thread_0.tx_thread_priority != 15)
{
/* Test error! */
printf("ERROR #15\n");
test_control_return(9);
}
}
else if (test_case == 3)
{
priority = thread_0.tx_thread_priority;
tx_mutex_put(&mutex_1);
/* Should not do anything since mutex 2 is still owned. */
if (thread_0.tx_thread_priority != priority)
{
/* Test error! */
printf("ERROR #16\n");
test_control_return(10);
}
tx_mutex_put(&mutex_0);
/* Should not do anything. */
if (thread_0.tx_thread_priority != priority)
{
/* Test error! */
printf("ERROR #17\n");
test_control_return(11);
}
tx_mutex_put(&mutex_2);
/* Should finally go back to priority 15. */
if (thread_0.tx_thread_priority != 15)
{
/* Test error! */
printf("ERROR #18\n");
test_control_return(12);
}
}
else if (test_case == 4)
{
priority = thread_0.tx_thread_priority;
tx_mutex_put(&mutex_1);
/* Should not do anything since mutex 2 is still owned. */
if (thread_0.tx_thread_priority != priority)
{
/* Test error! */
printf("ERROR #19\n");
test_control_return(13);
}
tx_mutex_put(&mutex_2);
/* Should return us back to priority 15. */
if (thread_0.tx_thread_priority != 15)
{
/* Test error! */
printf("ERROR #20\n");
test_control_return(14);
}
tx_mutex_put(&mutex_0);
/* Should not do anything. */
if (thread_0.tx_thread_priority != 15)
{
/* Test error! */
printf("ERROR #21\n");
test_control_return(15);
}
}
else if (test_case == 5)
{
priority = thread_0.tx_thread_priority;
tx_mutex_put(&mutex_0);
/* Should not do anything since mutex 2 is still owned. */
if (thread_0.tx_thread_priority != priority)
{
/* Test error! */
printf("ERROR #22\n");
test_control_return(16);
}
tx_mutex_put(&mutex_2);
/* Should return us back to priority 8. */
if (thread_0.tx_thread_priority != 8)
{
/* Test error! */
printf("ERROR #23\n");
test_control_return(17);
}
tx_mutex_put(&mutex_1);
/* Should return us back to priority 15. */
if (thread_0.tx_thread_priority != 15)
{
/* Test error! */
printf("ERROR #24\n");
test_control_return(18);
}
}
test_case++;
if (test_case > 5)
test_case = 0;
}
/* Check for thread 3 running... this should not happen! */
if (thread_3_counter != 50)
{
printf("ERROR #25\n");
test_control_return(19);
}
/* At this point, mutex 3 owned by this thread. */
/* Resume thread 6, lowest priority thread. */
status = tx_thread_resume(&thread_6);
/* Check for an error. */
if ((status != TX_SUCCESS) || (thread_0.tx_thread_priority != 15) || (thread_6_counter != 0))
{
printf("ERROR #27\n");
test_control_return(19);
}
/* Now resume thread 4. */
status = tx_thread_resume(&thread_4);
/* Check for an error. */
if ((status != TX_SUCCESS) || (thread_0.tx_thread_priority != 4) || (thread_6_counter != 0))
{
printf("ERROR #28\n");
test_control_return(19);
}
/* Now resume thread 5. */
status = tx_thread_resume(&thread_5);
/* Check for an error. */
if ((status != TX_SUCCESS) || (thread_0.tx_thread_priority != 2) || (thread_6_counter != 0))
{
printf("ERROR #29\n");
test_control_return(19);
}
/* Sleep to let thread 6 run, which is lower priority. */
tx_thread_sleep(1);
/* Now release the mutex. */
status = tx_mutex_put(&mutex_3);
/* Check for an error. */
if ((status != TX_SUCCESS) || (thread_0.tx_thread_priority != 15) || (thread_6_counter != 0) || (thread_4_counter != 1) || (thread_5_counter != 1))
{
printf("ERROR #30\n");
test_control_return(19);
}
/* Sleep to let thread 6 run and release the mutex. */
tx_thread_sleep(2);
/* If we get here the test was successful! */
printf("SUCCESS!\n");
test_control_return(0);
#endif
}
#ifndef TX_DISABLE_PREEMPTION_THRESHOLD
static void thread_1_entry(ULONG thread_input)
{
UINT old_priority;
UINT old_threshold;
while(1)
{
/* Increment the thread counter. */
thread_1_counter++;
/* Update the thread priority and thread preemption-threshold of thread 0. */
tx_thread_priority_change(&thread_0, 15, &old_priority);
tx_thread_preemption_change(&thread_0, 14, &old_threshold);
/* Get mutex. */
tx_mutex_get(&mutex_1, TX_WAIT_FOREVER);
tx_mutex_put(&mutex_1);
tx_thread_suspend(&thread_1);
/* Increment the thread counter. */
thread_1_counter++;
/* Update the thread priority and thread preemption-threshold of thread 0. */
tx_thread_priority_change(&thread_0, 15, &old_priority);
tx_thread_preemption_change(&thread_0, 8, &old_threshold);
/* Get mutex. */
tx_mutex_get(&mutex_1, TX_WAIT_FOREVER);
tx_mutex_put(&mutex_1);
tx_thread_suspend(&thread_1);
}
}
static void thread_2_entry(ULONG thread_input)
{
UINT old_priority;
UINT old_threshold;
while(1)
{
/* Increment the thread counter. */
thread_2_counter++;
/* Update the thread priority and thread preemption-threshold of thread 0. */
tx_thread_priority_change(&thread_0, 15, &old_priority);
tx_thread_preemption_change(&thread_0, 14, &old_threshold);
/* Get mutex. */
tx_mutex_get(&mutex_2, TX_WAIT_FOREVER);
tx_mutex_put(&mutex_2);
tx_thread_suspend(&thread_2);
/* Increment the thread counter. */
thread_2_counter++;
/* Update the thread priority and thread preemption-threshold of thread 0. */
tx_thread_priority_change(&thread_0, 15, &old_priority);
tx_thread_preemption_change(&thread_0, 8, &old_threshold);
/* Get mutex. */
tx_mutex_get(&mutex_2, TX_WAIT_FOREVER);
tx_mutex_put(&mutex_2);
tx_thread_suspend(&thread_2);
}
}
static void thread_3_entry(ULONG thread_input)
{
while(1)
{
/* Resume thread 0. */
tx_thread_resume(&thread_0);
/* Increment the thread counter. */
thread_3_counter++;
/* Suspend this thread. */
tx_thread_suspend(&thread_3);
}
}
static void thread_4_entry(ULONG thread_input)
{
while(1)
{
/* Get priority inherit mutex. */
tx_mutex_get(&mutex_3, TX_WAIT_FOREVER);
/* Increment the thread counter. */
thread_4_counter++;
/* Release the priority inherit mutex. */
tx_mutex_put(&mutex_3);
/* Suspend this thread. */
tx_thread_suspend(&thread_4);
}
}
static void thread_5_entry(ULONG thread_input)
{
while(1)
{
/* Get priority inherit mutex. */
tx_mutex_get(&mutex_3, TX_WAIT_FOREVER);
/* Increment the thread counter. */
thread_5_counter++;
/* Release the priority inherit mutex. */
tx_mutex_put(&mutex_3);
/* Suspend this thread. */
tx_thread_suspend(&thread_5);
}
}
static void thread_6_entry(ULONG thread_input)
{
while(1)
{
/* Get priority inherit mutex. */
tx_mutex_get(&mutex_3, TX_WAIT_FOREVER);
/* Increment the thread counter. */
thread_6_counter++;
/* Release the priority inherit mutex. */
tx_mutex_put(&mutex_3);
/* Suspend this thread. */
tx_thread_suspend(&thread_6);
}
}
#endif