summaryrefslogtreecommitdiffstats
path: root/spec/rtems/task/req/delete.yml
blob: 105005216ed43e2e3a777eb27b3e6af0e8ca1a3b (plain) (blame)
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
copyrights:
- Copyright (C) 2021 embedded brains GmbH (http://www.embedded-brains.de)
enabled-by: true
functional-type: action
links:
- role: interface-function
  uid: ../if/delete
post-conditions:
- name: Status
  states:
  - name: Ok
    test-code: |
      T_rsc_success( ctx->status );
    text: |
      The return status of ${../if/delete:/name} shall be
      ${../../status/if/successful:/name}.
  - name: InvId
    test-code: |
      T_rsc( ctx->status, RTEMS_INVALID_ID );
    text: |
      The return status of ${../if/delete:/name} shall be
      ${../../status/if/invalid-id:/name}.
  - name: CalledFromISR
    test-code: |
      T_rsc( ctx->status, RTEMS_CALLED_FROM_ISR );
    text: |
      The return status of ${../if/delete:/name} shall be
      ${../../status/if/called-from-isr:/name}.
  - name: NoReturn
    test-code: |
      T_rsc( ctx->status, RTEMS_NOT_IMPLEMENTED );
    text: |
      The ${../if/delete:/name} call shall not return.
  test-epilogue: null
  test-prologue: null
- name: FatalError
  states:
  - name: 'Yes'
    test-code: |
      T_eq_u32( ctx->calls.fatal, 1 );
    text: |
      The fatal error with a fatal source of
      ${/score/interr/if/internal-error-core:/name} and a fatal code of
      ${/score/interr/if/bad-thread-dispatch-disable-level:/name} shall occur
      through the ${../if/delete:/name} call.
  - name: Nop
    test-code: |
      T_eq_u32( ctx->calls.fatal, 0 );
    text: |
      No fatal error shall occur through the ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: Zombie
  states:
  - name: 'Yes'
    test-code: |
      T_eq_u32( ctx->worker_state & STATES_ZOMBIE, STATES_ZOMBIE )
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter shall
      be in the zombie state after the ${../if/delete:/name} call.
  - name: 'No'
    test-code: |
      T_eq_u32( ctx->worker_state & STATES_ZOMBIE, 0 )
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter shall
      not be in the zombie state after the ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: TaskPriority
  states:
  - name: Raise
    test-code: |
      T_eq_u32( ctx->worker_priority, PRIO_ULTRA_HIGH );
    text: |
      Each priority of the calling task which is higher than the highest
      priority of the task specified by the ${../if/delete:/params[0]/name}
      parameter shall be made the highest priority of the task.
  - name: Nop
    test-code: |
      T_eq_u32( ctx->worker_priority, PRIO_NORMAL );
    text: |
      The priorities of the task specified by the
      ${../if/delete:/params[0]/name} parameter shall not be changed by the
      ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: RestartExtensions
  states:
  - name: Nop
    test-code: |
      T_eq_u32( ctx->calls_after_restart.thread_restart, 0 );
    text: |
      The thread delete user extensions shall not be invoked by the
      ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: TerminateExtensions
  states:
  - name: 'Yes'
    test-code: |
      T_eq_u32( ctx->calls_after_restart.thread_terminate, 1 );
    text: |
      The thread terminate user extensions shall be invoked by the
      ${../if/delete:/name} call.
  - name: Nop
    test-code: |
      T_eq_u32( ctx->calls_after_restart.thread_terminate, 0 );
    text: |
      The thread terminate user extensions shall not be invoked by the
      ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: Dormant
  states:
  - name: 'Yes'
    test-code: |
      T_eq_u32( ctx->worker_state & STATES_DORMANT, STATES_DORMANT )
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter shall
      be dormant after the ${../if/delete:/name} call.
  - name: 'No'
    test-code: |
      T_eq_u32( ctx->worker_state & STATES_DORMANT, 0 )
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter shall
      not be dormant after the ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: Suspended
  states:
  - name: 'Yes'
    test-code: |
      T_eq_u32( ctx->worker_state & STATES_SUSPENDED, STATES_SUSPENDED )
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter shall
      be suspended after the ${../if/delete:/name} call.
  - name: 'No'
    test-code: |
      T_eq_u32( ctx->worker_state & STATES_SUSPENDED, 0 )
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter shall
      not be suspended after the ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: Restarting
  states:
  - name: 'Yes'
    test-code: |
      T_ne_int( ctx->worker_life_state & THREAD_LIFE_RESTARTING, 0 );
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter
      shall be restarting after the ${../if/delete:/name} call.
  - name: 'No'
    test-code: |
      T_eq_int( ctx->worker_life_state & THREAD_LIFE_RESTARTING, 0 );
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter
      shall not be restarting after the ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: Terminating
  states:
  - name: 'Yes'
    test-code: |
      T_ne_int( ctx->worker_life_state & THREAD_LIFE_TERMINATING, 0 );
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter
      shall be terminating after the ${../if/delete:/name} call.
  - name: 'No'
    test-code: |
      T_eq_int( ctx->worker_life_state & THREAD_LIFE_TERMINATING, 0 );
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter
      shall not be terminating after the ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: Protected
  states:
  - name: 'Yes'
    test-code: |
      T_ne_int( ctx->worker_life_state & THREAD_LIFE_PROTECTED, 0 );
    text: |
      The thread life of the task specified by the
      ${../if/delete:/params[0]/name} parameter be protected after the
      ${../if/delete:/name} call.
  - name: 'No'
    test-code: |
      T_eq_int( ctx->worker_life_state & THREAD_LIFE_PROTECTED, 0 );
    text: |
      The thread life of the task specified by the
      ${../if/delete:/params[0]/name} parameter shall not be protected after
      the ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
- name: State
  states:
  - name: Enqueued
    test-code: |
      T_ne_u32( ctx->worker_state & STATES_BLOCKED, 0 )
      T_not_null( ctx->worker_wait_queue );
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter shall
      be enqueued on a ${/glossary/waitqueue:/term} and blocked.
  - name: Ready
    test-code: |
      T_eq_u32( ctx->worker_state & STATES_BLOCKED, 0 )
      T_null( ctx->worker_wait_queue );
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter shall
      not be enqueued on a ${/glossary/waitqueue:/term} and not blocked.
  - name: Blocked
    test-code: |
      T_ne_u32( ctx->worker_state & STATES_BLOCKED, 0 )
      T_null( ctx->worker_wait_queue );
    text: |
      The task specified by the ${../if/delete:/params[0]/name} parameter shall
      be not enqueued on a ${/glossary/waitqueue:/term} and blocked.
  test-epilogue: null
  test-prologue: null
- name: Timer
  states:
  - name: Active
    test-code: |
      T_eq_int( ctx->worker_timer_info.state, TASK_TIMER_TICKS );
    text: |
      The timer of the task specified by the ${../if/delete:/params[0]/name}
      parameter shall be active after the ${../if/delete:/name} call.
  - name: Inactive
    test-code: |
      T_eq_int( ctx->worker_timer_info.state, TASK_TIMER_INACTIVE );
    text: |
      The timer of the task specified by the ${../if/delete:/params[0]/name}
      parameter shall be inactive after the ${../if/delete:/name} call.
  test-epilogue: null
  test-prologue: null
pre-conditions:
- name: Id
  states:
  - name: Executing
    test-code: |
      ctx->id = RTEMS_SELF;
    text: |
      While the ${../if/delete:/params[0]/name} parameter is associated with
      the calling task.
  - name: Other
    test-code: |
      ctx->id = ctx->worker_id;
    text: |
      While the ${../if/delete:/params[0]/name} parameter is associated with a
      task other than the calling task.
  - name: Invalid
    test-code: |
      ctx->id = INVALID_ID;
    text: |
      While the ${../if/delete:/params[0]/name} parameter is not associated with
      a task.
  test-epilogue: null
  test-prologue: null
- name: Context
  states:
  - name: Task
    test-code: |
      ctx->interrupt = false;
    text: |
      While the ${../if/delete:/name} directive is called from within task
      context.
  - name: Interrupt
    test-code: |
      ctx->interrupt = true;
    text: |
      While the ${../if/delete:/name} directive is called from within
      interrupt context.
  test-epilogue: null
  test-prologue: null
- name: ThreadDispatch
  states:
  - name: Disabled
    test-code: |
      ctx->dispatch_disabled = true;
    text: |
      While thread dispatching is disabled for the calling task.
  - name: Enabled
    test-code: |
      ctx->dispatch_disabled = false;
    text: |
      While thread dispatching is enabled for the calling task.
  test-epilogue: null
  test-prologue: null
- name: CallerPriority
  states:
  - name: Vital
    test-code: |
      ctx->vital_deleter_priority = true;
    text: |
      While at least one priority of the calling task is higher than the
      highest priority of the task specified by the
      ${../if/delete:/params[0]/name} parameter.
  - name: Dispensable
    test-code: |
      ctx->vital_deleter_priority = false;
    text: |
      While all priorities of the calling task are lower than or equal to the
      highest priority of the task specified by the
      ${../if/delete:/params[0]/name} parameter.
  test-epilogue: null
  test-prologue: null
- name: Dormant
  states:
  - name: 'No'
    test-code: |
      ctx->dormant = false;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name} parameter
      is not dormant.
  - name: 'Yes'
    test-code: |
      ctx->dormant = true;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name} parameter
      is dormant.
  test-epilogue: null
  test-prologue: null
- name: Suspended
  states:
  - name: 'Yes'
    test-code: |
      ctx->suspended = true;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name} parameter
      is suspended.
  - name: 'No'
    test-code: |
      ctx->suspended = false;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name} parameter
      is not suspended.
  test-epilogue: null
  test-prologue: null
- name: Restarting
  states:
  - name: 'No'
    test-code: |
      ctx->restarting = false;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name} parameter
      is not restarting.
  - name: 'Yes'
    test-code: |
      ctx->restarting = true;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name} parameter
      is restarting.
  test-epilogue: null
  test-prologue: null
- name: Terminating
  states:
  - name: 'No'
    test-code: |
      ctx->terminating = false;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name} parameter
      is not terminating.
  - name: 'Yes'
    test-code: |
      ctx->terminating = true;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name} parameter
      is terminating.
  test-epilogue: null
  test-prologue: null
- name: Protected
  states:
  - name: 'Yes'
    test-code: |
      ctx->protected = true;
    text: |
      While thread life of the task specified by the
      ${../if/delete:/params[0]/name} parameter is protected.
  - name: 'No'
    test-code: |
      ctx->protected = false;
    text: |
      While thread life of the task specified by the
      ${../if/delete:/params[0]/name} parameter is not protected.
  test-epilogue: null
  test-prologue: null
- name: State
  states:
  - name: Enqueued
    test-code: |
      ctx->blocked = true;
      ctx->enqueued = true;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name}
      parameter is enqueued on a ${/glossary/waitqueue:/term}.
  - name: Ready
    test-code: |
      ctx->blocked = false;
      ctx->enqueued = false;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name}
      parameter is a ${/glossary/readytask:/term} or a
      ${/glossary/scheduledtask:/term}.
  - name: Blocked
    test-code: |
      ctx->blocked = true;
      ctx->enqueued = false;
    text: |
      While the task specified by the ${../if/delete:/params[0]/name}
      parameter is blocked.
  test-epilogue: null
  test-prologue: null
- name: Timer
  states:
  - name: Inactive
    test-code: |
      ctx->timer_active = false;
    text: |
      While timer of the task specified by the ${../if/delete:/params[0]/name}
      parameter is inactive.
  - name: Active
    test-code: |
      ctx->timer_active = true;
    text: |
      While timer of the task specified by the ${../if/delete:/params[0]/name}
      parameter is active.
  test-epilogue: null
  test-prologue: null
rationale: null
references: []
requirement-type: functional
skip-reasons:
  ExecutingIsNotDormant: |
    An executing thread was started and thus is never dormant.
  ExecutingIsNotBlocked: |
    An executing thread is not blocked.
  NotBlockedHasInactiveTimer: |
    The timer of a not blocked thread is inactive.
  ThreadDispatchDisabled: |
    While ISRs or nested requests are processed, the thread dispatching is
    disabled.
test-action: |
  rtems_status_code sc;

  if ( ctx->id != INVALID_ID && !ctx->dormant ) {
    ctx->worker_is_mutex_owner = false;
    StartTask( ctx->worker_id, Worker, ctx );

    /* Let the worker catch signals and set the thread life protection state */
    Yield();

    sc = rtems_signal_send( ctx->worker_id, RTEMS_SIGNAL_0 );
    T_rsc_success( sc );

    if ( ctx->restarting ) {
      sc = rtems_task_restart( ctx->worker_id, 0 );
      T_rsc_success( sc );
    }

    if ( ctx->terminating ) {
      sc = rtems_task_restart( ctx->deleter_id, (rtems_task_argument) ctx );
      T_rsc_success( sc );
    } else {
      Yield();
    }
  }

  if ( ctx->id != RTEMS_SELF ) {
    if ( ctx->interrupt ) {
      CallWithinISR( Delete, ctx );
    } else {
      sc = rtems_task_restart( ctx->deleter_2_id, (rtems_task_argument) ctx );
      T_rsc_success( sc );
    }
  }

  Cleanup( ctx );
test-brief: null
test-cleanup: null
test-context:
- brief: |
    This member provides the scheduler operation records.
  description: null
  member: |
    T_scheduler_log_10 scheduler_log
- brief: |
    This member provides a jump context to resume a thread dispatch.
  description: null
  member: |
    jmp_buf thread_dispatch_context;
- brief: |
    This member contains the identifier of the runner scheduler.
  description: null
  member: |
    rtems_id scheduler_id
- brief: |
    This member contains the identifier of the runner task.
  description: null
  member: |
    rtems_id runner_id
- brief: |
    This member references the TCB of the runner task.
  description: null
  member: |
    rtems_tcb *runner_tcb
- brief: |
    This member contains the identifier of the mutex.
  description: null
  member: |
    rtems_id mutex_id
- brief: |
    This member provides an event set used to set up the blocking conditions of
    the task to delete.
  description: null
  member: |
    rtems_event_set events
- brief: |
    This member contains the identifier of the worker task.
  description: null
  member: |
    rtems_id worker_id
- brief: |
    This member references the TCB of the worker task.
  description: null
  member: |
    rtems_tcb *worker_tcb
- brief: |
    This member contains the worker state at the end of the
    ${../if/delete:/name} call.
  description: null
  member: |
    States_Control worker_state
- brief: |
    This member contains the worker timer info at the end of the
    ${../if/delete:/name} call.
  description: null
  member: |
    TaskTimerInfo worker_timer_info;
- brief: |
    This member contains the worker thread queue at the end of the
    ${../if/delete:/name} call.
  description: null
  member: |
    const Thread_queue_Queue *worker_wait_queue;
- brief: |
    This member contains the worker thread life state at the end of the
    ${../if/delete:/name} call.
  description: null
  member: |
    Thread_Life_state worker_life_state
- brief: |
    This member contains the worker priority at the end of the
    ${../if/delete:/name} call.
  description: null
  member: |
    rtems_task_priority worker_priority
- brief: |
    This member contains the identifier of the deleter task.
  description: null
  member: |
    rtems_id deleter_id
- brief: |
    This member references the TCB of the deleter task.
  description: null
  member: |
    rtems_tcb *deleter_tcb
- brief: |
    This member contains the identifier of the second deleter task.
  description: null
  member: |
    rtems_id deleter_2_id
- brief: |
    This member references the TCB of the second deleter task.
  description: null
  member: |
    rtems_tcb *deleter_2_tcb
- brief: |
    This member contains the identifier of the test user extensions.
  description: null
  member: |
    rtems_id extension_id
- brief: |
    This member contains extension calls.
  description: null
  member: |
    ExtensionCalls calls;
- brief: |
    This member contains extension calls after the ${../if/delete:/name} call.
  description: null
  member: |
    ExtensionCalls calls_after_restart;
- brief: |
    This member contains the delete counter.
  description: null
  member: |
    uint32_t restart_counter
- brief: |
    If this member is true, then the worker shall be dormant before the
    ${../if/delete:/name} call.
  description: null
  member: |
    bool dormant
- brief: |
    If this member is true, then the worker shall be suspended before the
    ${../if/delete:/name} call.
  description: null
  member: |
    bool suspended
- brief: |
    If this member is true, then the thread life of the worker shall be
    protected before the ${../if/delete:/name} call.
  description: null
  member: |
    bool protected
- brief: |
    If this member is true, then the worker shall be restarting before the
    ${../if/delete:/name} call.
  description: null
  member: |
    bool restarting
- brief: |
    If this member is true, then the worker shall be terminating before the
    ${../if/delete:/name} call.
  description: null
  member: |
    bool terminating
- brief: |
    If this member is true, then the ${../if/delete:/name} shall be called
    from within interrupt context.
  description: null
  member: |
    bool interrupt
- brief: |
    If this member is true, then the worker shall be blocked before the
    ${../if/delete:/name} call.
  description: null
  member: |
    bool blocked
- brief: |
    If this member is true, then the worker shall be enqueued on a
    ${/glossary/waitqueue:/term} before the ${../if/delete:/name} call.
  description: null
  member: |
    bool enqueued
- brief: |
    If this member is true, then the worker obtained a mutex.
  description: null
  member: |
    bool worker_is_mutex_owner
- brief: |
    If this member is true, then the timer of the worker shall be active before
    the ${../if/delete:/name} call.
  description: null
  member: |
    bool timer_active
- brief: |
    If this member is true, then the deleter shall have a vital priority for
    the worker.
  description: null
  member: |
    bool vital_deleter_priority
- brief: |
    If this member is true, then thread dispatching is disabled by the worker
    task before the ${../if/delete:/name} call.
  description: null
  member: |
    bool dispatch_disabled
- brief: |
    If this member is true, then it is expected to delete the worker.
  description: null
  member: |
    bool delete_worker_expected
- brief: |
    This member contains the return value of the ${../if/delete:/name}
    call.
  description: null
  member: |
    rtems_status_code status
- brief: |
    This member specifies if the ${../if/delete:/params[0]/name}
    parameter value.
  description: null
  member: |
    rtems_id id
test-context-support: null
test-description: null
test-header: null
test-includes:
- rtems.h
- rtems/test-scheduler.h
- rtems/bspIo.h
- rtems/score/io.h
- rtems/score/statesimpl.h
- rtems/score/threaddispatch.h
- rtems/score/threadimpl.h
- limits.h
- setjmp.h
test-local-includes:
- tx-support.h
test-prepare: |
  ctx->status = RTEMS_NOT_IMPLEMENTED;
  ctx->restart_counter = 0;

  ctx->delete_worker_expected = false;
  ctx->worker_id = CreateTask( "WORK", PRIO_NORMAL );
  ctx->delete_worker_expected = true;

  ctx->worker_tcb = GetThread( ctx->worker_id );
  ctx->worker_state = UINT32_MAX;
  ctx->worker_life_state = INT_MAX;
  ctx->worker_priority = UINT32_MAX;
test-setup:
  brief: null
  code: |
    rtems_status_code sc;

    ctx->runner_id = rtems_task_self();
    ctx->runner_tcb = GetThread( ctx->runner_id );
    ctx->scheduler_id = GetSelfScheduler();
    ctx->mutex_id = CreateMutexNoProtocol();
    ObtainMutex( ctx->mutex_id );

    sc = rtems_extension_create(
      rtems_build_name( 'T', 'E', 'S', 'T' ),
      &extensions,
      &ctx->extension_id
    );
    T_rsc_success( sc );

    SetFatalHandler( Fatal, ctx );
    SetTaskSwitchExtension( TaskSwitch );
    SetSelfPriority( PRIO_NORMAL );

    ctx->deleter_id = CreateTask( "DELE", PRIO_HIGH );
    ctx->deleter_tcb = GetThread( ctx->deleter_id );
    StartTask( ctx->deleter_id, Deleter, NULL );

    ctx->deleter_2_id = CreateTask( "DEL2", PRIO_ULTRA_HIGH );
    ctx->deleter_2_tcb = GetThread( ctx->deleter_2_id );
    StartTask( ctx->deleter_2_id, SecondDeleter, NULL );
  description: null
test-stop: null
test-support: |
  typedef ${.:/test-context-type} Context;

  static void CaptureWorkerState( Context *ctx )
  {
    T_scheduler_log *log;

    log = T_scheduler_record( NULL );

    if ( log != NULL ) {
      T_eq_ptr( &log->header, &ctx->scheduler_log.header );

      ctx->worker_wait_queue = ctx->worker_tcb->Wait.queue;
      ctx->worker_state = ctx->worker_tcb->current_state;
      ctx->worker_life_state = ctx->worker_tcb->Life.state;
      ctx->worker_priority =
        SCHEDULER_PRIORITY_UNMAP( _Thread_Get_priority( ctx->worker_tcb ) );
      CopyExtensionCalls( &ctx->calls, &ctx->calls_after_restart );
      GetTaskTimerInfoByThread( ctx->worker_tcb, &ctx->worker_timer_info );
    }
  }

  static void TaskSwitch( rtems_tcb *executing, rtems_tcb *heir )
  {
    (void) executing;
    (void) heir;
    CaptureWorkerState( T_fixture_context() );
  }

  static void VerifyTaskPreparation( const Context *ctx )
  {
    if ( ctx->id != INVALID_ID ) {
      States_Control state;
      Thread_Life_state life_state;

      state = STATES_READY;
      life_state = ctx->worker_tcb->Life.state;

      if ( ctx->suspended ) {
        state |= STATES_SUSPENDED;
      }

      if ( ctx->dormant ) {
        T_eq_int( life_state, 0 );
        state |= STATES_DORMANT;
      } else {
        T_eq( ctx->protected, ( life_state & THREAD_LIFE_PROTECTED ) != 0 );
        T_eq( ctx->restarting, ( life_state & THREAD_LIFE_RESTARTING ) != 0 );
        T_eq( ctx->terminating, ( life_state & THREAD_LIFE_TERMINATING ) != 0 );

        if ( ctx->blocked ) {
          if ( ctx->enqueued ) {
            state |= STATES_WAITING_FOR_MUTEX;
          } else {
            state |= STATES_WAITING_FOR_EVENT;
          }
        }
      }

      T_eq_u32( ctx->worker_tcb->current_state, state );
    }
  }

  static void Fatal(
    rtems_fatal_source source,
    rtems_fatal_code   code,
    void              *arg
  )
  {
    Context *ctx;

    T_eq_int( source, INTERNAL_ERROR_CORE );
    T_eq_ulong( code, INTERNAL_ERROR_BAD_THREAD_DISPATCH_DISABLE_LEVEL );

    ctx = arg;
    ++ctx->calls.fatal;
    T_assert_eq_int( ctx->calls.fatal, 1 );
    longjmp( ctx->thread_dispatch_context, 1 );
  }

  static void ResumeThreadDispatch(
    rtems_fatal_source source,
    rtems_fatal_code   code,
    void              *arg
  )
  {
    Context *ctx;

    T_eq_int( source, INTERNAL_ERROR_CORE );
    T_eq_ulong( code, INTERNAL_ERROR_BAD_THREAD_DISPATCH_DISABLE_LEVEL );

    ctx = arg;
    SetFatalHandler( Fatal, ctx );
    longjmp( ctx->thread_dispatch_context, 1 );
  }

  static void Delete( void *arg )
  {
    Context         *ctx;
    T_scheduler_log *log;

    ctx = arg;

    if ( ctx->suspended && ctx->id != INVALID_ID ) {
      if ( ctx->id != RTEMS_SELF || ctx->interrupt ) {
        SuspendTask( ctx->worker_id );
      } else {
        Per_CPU_Control *cpu_self;

        /*
         * Where the system was built with SMP support enabled, a suspended
         * executing thread during the ${../if/delete:/name} call can happen
         * if the thread was suspended by another processor and the
         * inter-processor interrupt did not yet arrive.  Where the system was
         * built with SMP support disabled, this state cannot happen with the
         * current implementation.  However, we still specify and validate this
         * behaviour unconditionally since there exist alternative
         * implementations which would lead to such a state if the executing
         * thread is suspended by an ISR.
         */
        cpu_self = _Thread_Dispatch_disable();
        SuspendSelf();
        cpu_self->dispatch_necessary = false;
        _Thread_Dispatch_enable( cpu_self );
      }
    }

    if ( ctx->dispatch_disabled ) {
      _Thread_Dispatch_disable();
    }

    VerifyTaskPreparation( ctx );
    ClearExtensionCalls( &ctx->calls );

    log = T_scheduler_record_10( &ctx->scheduler_log );
    T_null( log );

    if ( setjmp( ctx->thread_dispatch_context ) == 0 ) {
      ctx->status = rtems_task_delete( ctx->id );
    } else {
      _Thread_Dispatch_unnest( _Per_CPU_Get() );
    }

    CaptureWorkerState( ctx );

    if ( ctx->dispatch_disabled ) {
      _Thread_Dispatch_enable( _Per_CPU_Get() );
    }
  }

  static void Block( Context *ctx )
  {
    rtems_interval ticks;

    if ( ctx->timer_active ) {
      ticks = UINT32_MAX;
    } else {
      ticks = RTEMS_NO_TIMEOUT;
    }

    if ( ctx->enqueued ) {
      ObtainMutexTimed( ctx->mutex_id, ticks );
      ctx->worker_is_mutex_owner = true;
    } else {
      /*
       * Do not use a stack variable for the event set, since we may jump out
       * of the directive call.
       */
      (void) rtems_event_receive(
        RTEMS_ALL_EVENTS,
        RTEMS_EVENT_ANY | RTEMS_WAIT,
        ticks,
        &ctx->events
      );
    }
  }

  static void BlockDone( Context *ctx )
  {
    if ( ctx->enqueued ) {
      ReleaseMutex( ctx->mutex_id );
    }
  }

  static void Signal( rtems_signal_set signals )
  {
    Context *ctx;

    (void) signals;
    ctx = T_fixture_context();

    if ( ctx->id == RTEMS_SELF ) {
      SetPriority( ctx->runner_id, PRIO_LOW );

      if ( ctx->interrupt ) {
        if ( ctx->blocked ) {
          Per_CPU_Control *cpu_self;

          SetFatalHandler( ResumeThreadDispatch, ctx );
          cpu_self = _Thread_Dispatch_disable();

          if ( setjmp( ctx->thread_dispatch_context ) == 0 ) {
            Block( ctx );
          } else {
            _Thread_Dispatch_unnest( cpu_self );
          }

          CallWithinISR( Delete, ctx );

          _Thread_Dispatch_direct( cpu_self );
          BlockDone( ctx );
        } else {
          CallWithinISR( Delete, ctx );
        }
      } else {
        Delete( ctx );
      }
    } else {
      if ( ctx->blocked ) {
        Block( ctx );
        BlockDone( ctx );
      } else {
        SetPriority( ctx->runner_id, PRIO_HIGH );
      }
    }

    if ( ctx->protected ) {
      _Thread_Set_life_protection( 0 );
    }
  }

  static void Deleter( rtems_task_argument arg )
  {
    Context *ctx;

    ctx = (Context *) arg;

    if ( ctx != NULL ) {
      /* We have to prevent the priority boost in the task delete below */
      SetPriority( ctx->runner_id, PRIO_LOW );
      SetSelfPriorityNoYield( PRIO_NORMAL );

      DeleteTask( ctx->worker_id );
    }

    SuspendSelf();
  }

  static void SecondDeleter( rtems_task_argument arg )
  {
    Context *ctx;

    ctx = (Context *) arg;

    if ( ctx != NULL ) {
      if ( !ctx->vital_deleter_priority ) {
        SetPriority( ctx->runner_id, PRIO_LOW );
        SetSelfPriorityNoYield( PRIO_NORMAL );
      }

      Delete( ctx );
    }

    SuspendSelf();
  }

  static void Worker( rtems_task_argument arg )
  {
    Context *ctx;

    ctx = T_fixture_context();

    if ( arg != 0 ) {
      rtems_status_code sc;

      sc = rtems_signal_catch( Signal, RTEMS_NO_ASR );
      T_rsc_success( sc );

      if ( ctx->protected ) {
        _Thread_Set_life_protection( THREAD_LIFE_PROTECTED );
      }

      Yield();
    }

    if ( IsMutexOwner( ctx->mutex_id ) ) {
      ReleaseMutex( ctx->mutex_id );
    }

    rtems_task_exit();
  }

  static void ThreadDelete( rtems_tcb *executing, rtems_tcb *deleted )
  {
    Context *ctx;

    ctx = T_fixture_context();
    ++ctx->calls.thread_delete;

    T_eq_u32( executing->Object.id, ctx->runner_id );

    if ( ctx->delete_worker_expected ) {
      T_eq_u32( deleted->Object.id, ctx->worker_id );
    }
  }

  static void ThreadRestart( rtems_tcb *executing, rtems_tcb *restarted )
  {
    Context *ctx;

    ctx = T_fixture_context();
    ++ctx->calls.thread_restart;
  }

  static void ThreadTerminate( rtems_tcb *executing )
  {
    Context *ctx;

    ctx = T_fixture_context();
    ++ctx->calls.thread_terminate;

    T_eq_u32( executing->Object.id, ctx->worker_id );

    if ( IsMutexOwner( ctx->mutex_id ) ) {
      ReleaseMutex( ctx->mutex_id );
    }
  }

  static void Cleanup( Context *ctx )
  {
    SetSelfPriority( PRIO_VERY_LOW );

    if ( ( ctx->id == RTEMS_SELF || ctx->interrupt ) && ctx->suspended ) {
      ResumeTask( ctx->worker_id );
    }

    if ( ctx->protected && ctx->blocked ) {
      if ( ctx->enqueued ) {
        ReleaseMutex( ctx->mutex_id );
        ObtainMutex( ctx->mutex_id );
      } else {
        SendEvents( ctx->worker_id, RTEMS_EVENT_0 );
      }
    }

    if (
      ctx->id == INVALID_ID ||
      ( ctx->calls.thread_terminate == 0 && 
      !( ctx->dormant && ctx->status != RTEMS_CALLED_FROM_ISR ) )
    ) {
      DeleteTask( ctx->worker_id );
    }

    SetSelfPriority( PRIO_NORMAL );
  }

  static const rtems_extensions_table extensions = {
    .thread_delete = ThreadDelete,
    .thread_restart = ThreadRestart,
    .thread_terminate = ThreadTerminate
  };
test-target: testsuites/validation/tc-task-delete.c
test-teardown:
  brief: null
  code: |
    rtems_status_code sc;

    sc = rtems_extension_delete( ctx->extension_id );
    T_rsc_success( sc );

    SetFatalHandler( NULL, NULL );
    SetTaskSwitchExtension( NULL );
    DeleteTask( ctx->deleter_id );
    DeleteTask( ctx->deleter_2_id );
    ReleaseMutex( ctx->mutex_id );
    DeleteMutex( ctx->mutex_id );
    RestoreRunnerASR();
    RestoreRunnerPriority();
  description: null
text: ${.:text-template}
transition-map:
- enabled-by: true
  post-conditions:
    Status:
    - if:
        pre-conditions:
          Context: Interrupt
      then: CalledFromISR
    - else: NoReturn
    FatalError:
    - if:
        pre-conditions:
          Context: Task
          ThreadDispatch: Disabled
      then: 'Yes'
    - else: Nop
    Dormant: 'No'
    Suspended:
    - specified-by: Suspended
    Zombie:
    - if:
        pre-conditions:
          Context: Interrupt
      then: 'No'
    - if:
        pre-conditions:
          ThreadDispatch: Disabled
      then: 'No'
    - if:
        pre-conditions:
          Suspended: 'Yes'
      then: 'No'
    - else: 'Yes'
    TaskPriority: Nop
    State:
    - specified-by: State
    Timer:
    - if:
        pre-conditions:
          Context: Interrupt
      then-specified-by: Timer
    - else: Inactive
    Restarting:
    - specified-by: Restarting
    Terminating:
    - if:
        pre-conditions:
          Context: Interrupt
      then-specified-by: Terminating
    - else: 'Yes'
    Protected:
    - if:
        pre-conditions:
          Suspended: 'No'
          Context: Task
          ThreadDispatch: Enabled
      then: 'Yes'
    - specified-by: Protected
    RestartExtensions: Nop
    TerminateExtensions:
    - if:
        pre-conditions:
          Suspended: 'No'
          Context: Task
          ThreadDispatch: Enabled
      then: 'Yes'
    - else: Nop
  pre-conditions:
    Id:
    - Executing
    Dormant:
    - 'No'
    Suspended: all
    Restarting: all
    Terminating: all
    Protected: all
    Context: all
    State: all
    Timer: all
    CallerPriority: N/A
    ThreadDispatch: all
- enabled-by: true
  post-conditions:
    Status:
    - if:
        pre-conditions:
          Context: Interrupt
      then: CalledFromISR
    - if:
        pre-conditions:
          ThreadDispatch: Disabled
      then: NoReturn
    - else: Ok
    FatalError:
    - if:
        pre-conditions:
          Context: Task
          ThreadDispatch: Disabled
      then: 'Yes'
    - else: Nop
    Dormant: 'No'
    Suspended:
    - if:
        pre-conditions:
          Context: Interrupt
      then-specified-by: Suspended
    - else: 'No'
    Zombie: 'No'
    TaskPriority:
    - if:
        pre-conditions:
          Context: Task
          CallerPriority: Vital
      then: Raise
    - else: Nop
    State:
    - if:
        pre-conditions:
          Context: Interrupt
      then-specified-by: State
    - if:
        pre-conditions:
          Protected: 'Yes'
      then-specified-by: State
    - else: Ready
    Timer:
    - if:
        pre-conditions:
          Context: Interrupt
      then-specified-by: Timer
    - if:
        pre-conditions:
          Protected: 'Yes'
      then-specified-by: Timer
    - else: Inactive
    Restarting:
    - specified-by: Restarting
    Terminating:
    - if:
        pre-conditions:
          Context: Task
      then: 'Yes'
    - specified-by: Terminating
    Protected:
    - specified-by: Protected
    RestartExtensions: Nop
    TerminateExtensions:
    - else: Nop
  pre-conditions:
    Id:
    - Other
    Dormant:
    - 'No'
    Suspended: all
    Restarting: all
    Terminating: all
    Protected: all
    Context: all
    State: all
    Timer: all
    CallerPriority: all
    ThreadDispatch: all
- enabled-by: true
  post-conditions:
    Status: InvId
    FatalError: Nop
    Dormant: N/A
    Suspended: N/A
    Zombie: N/A
    TaskPriority: N/A
    State: N/A
    Timer: N/A
    Restarting: N/A
    Terminating: N/A
    Protected: N/A
    RestartExtensions: Nop
    TerminateExtensions: Nop
  pre-conditions:
    Id:
    - Invalid
    Dormant: N/A
    Suspended: N/A
    Restarting: N/A
    Terminating: N/A
    Protected: N/A
    Context: all
    State: N/A
    Timer: N/A
    CallerPriority: N/A
    ThreadDispatch: all
- enabled-by: true
  post-conditions:
    Status:
    - if:
        pre-conditions:
          Context: Interrupt
      then: CalledFromISR
    - else: Ok
    FatalError: Nop
    Dormant: 'Yes'
    Suspended:
    - specified-by: Suspended
    Zombie:
    - if:
        pre-conditions:
          Context: Interrupt
      then: 'No'
    - else: 'Yes'
    TaskPriority: Nop
    State: Ready
    Timer: Inactive
    Restarting: 'No'
    Terminating:
    - if:
        pre-conditions:
          Context: Interrupt
      then: 'No'
    - else: 'Yes'
    Protected: 'No'
    RestartExtensions: Nop
    TerminateExtensions: Nop
  pre-conditions:
    Id:
    - Other
    Dormant:
    - 'Yes'
    Suspended: all
    Restarting: N/A
    Terminating: N/A
    Protected: N/A
    Context: all
    State: N/A
    Timer: N/A
    CallerPriority: all
    ThreadDispatch: all
- enabled-by: true
  post-conditions: ExecutingIsNotDormant
  pre-conditions:
    Id:
    - Executing
    Dormant:
    - 'Yes'
    Suspended: all
    Restarting: all
    Terminating: all
    Protected: all
    Context: all
    State: all
    Timer: all
    CallerPriority: all
    ThreadDispatch: all
- enabled-by: true
  post-conditions: ExecutingIsNotBlocked
  pre-conditions:
    Id:
    - Executing
    Dormant:
    - 'No'
    Suspended: all
    Restarting: all
    Terminating: all
    Protected: all
    Context:
    - Task
    State:
    - Blocked
    - Enqueued
    Timer: all
    CallerPriority: all
    ThreadDispatch: all
- enabled-by: true
  post-conditions: NotBlockedHasInactiveTimer
  pre-conditions:
    Id:
    - Executing
    - Other
    Dormant:
    - 'No'
    Suspended: all
    Restarting: all
    Terminating: all
    Protected: all
    Context: all
    State:
    - Ready
    Timer:
    - Active
    CallerPriority: all
    ThreadDispatch: all
- enabled-by: true
  post-conditions: ThreadDispatchDisabled
  pre-conditions:
    Id: all
    Dormant: all
    Suspended: all
    Restarting: all
    Terminating: all
    Protected: all
    Context:
    - Interrupt
    State: all
    Enqueued: all
    Timer: all
    CallerPriority: all
    ThreadDispatch:
    - Enabled
type: requirement