summaryrefslogtreecommitdiffstats
path: root/spec/rtems/scheduler/req/remove-processor.yml
blob: 58993b53442099cfd90d17206dc53c7dedd6c825 (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
SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
copyrights:
- Copyright (C) 2021 embedded brains GmbH & Co. KG
enabled-by: true
functional-type: action
links:
- role: interface-function
  uid: ../if/remove-processor
post-conditions:
- name: Status
  states:
  - name: Ok
    test-code: |
      T_rsc_success( ctx->status );
    text: |
      The return status of ${../if/remove-processor:/name} shall be
      ${../../status/if/successful:/name}.
  - name: InvId
    test-code: |
      T_rsc( ctx->status, RTEMS_INVALID_ID );
    text: |
      The return status of ${../if/remove-processor:/name} shall be
      ${../../status/if/invalid-id:/name}.
  - name: InvNum
    test-code: |
      T_rsc( ctx->status, RTEMS_INVALID_NUMBER );
    text: |
      The return status of ${../if/remove-processor:/name} shall be
      ${../../status/if/invalid-number:/name}.
  - name: InUse
    test-code: |
      T_rsc( ctx->status, RTEMS_RESOURCE_IN_USE );
    text: |
      The return status of ${../if/remove-processor:/name} shall be
      ${../../status/if/resource-in-use:/name}.
  test-epilogue: null
  test-prologue: null
- name: Removed
  states:
  - name: 'Yes'
    test-code: |
      if ( ctx->home && ctx->helping ) {
        /*
         * For these test scenarios we use scheduler A in which the runner
         * remains scheduled.  So, an ask for help request is issued, when the
         * processor allocated to a task which uses the scheduler as a helping
         * scheduler is removed.
         */
        T_eq_u32( ctx->id, SCHEDULER_A_ID );
        T_eq_sz( ctx->scheduler_log.header.recorded, 3 );
        T_eq_int(
          ctx->scheduler_log.events[ 0 ].operation,
          T_SCHEDULER_REMOVE_PROCESSOR
        );
        T_eq_int(
          ctx->scheduler_log.events[ 1 ].operation,
          T_SCHEDULER_ASK_FOR_HELP
        );
        T_eq_int(
          ctx->scheduler_log.events[ 2 ].operation,
          T_SCHEDULER_ASK_FOR_HELP
        );
      } else {
        T_eq_sz( ctx->scheduler_log.header.recorded, 1 );
        T_eq_int(
          ctx->scheduler_log.events[ 0 ].operation,
          T_SCHEDULER_REMOVE_PROCESSOR
        );
      }
    text: |
      The processor specified by the ${../if/remove-processor:/params[1]/name}
      parameter shall be removed from the scheduler specified by the
      ${../if/remove-processor:/params[0]/name} by the
      ${../if/remove-processor:/name} call.
  - name: Nop
    test-code: |
      T_eq_sz( ctx->scheduler_log.header.recorded, 0 );
    text: |
      No processor shall be removed from a scheduler by the
      ${../if/remove-processor:/name} call.
  test-epilogue: null
  test-prologue: null
pre-conditions:
- name: Id
  states:
  - name: Invalid
    test-code: |
      ctx->id = INVALID_ID;
    text: |
      While the ${../if/remove-processor:/params[0]/name} parameter is not
      associated with a scheduler.
  - name: Scheduler
    test-code: |
      ctx->id = SCHEDULER_A_ID;
    text: |
      While the ${../if/remove-processor:/params[0]/name} parameter is
      associated with a scheduler.
  test-epilogue: null
  test-prologue: null
- name: CPUIndex
  states:
  - name: Valid
    test-code: |
      ctx->cpu_index = 0;
    text: |
      While the ${../if/remove-processor:/params[1]/name} parameter is less than
      the configured processor maximum.
  - name: Invalid
    test-code: |
      ctx->cpu_index = rtems_configuration_get_maximum_processors();
    text: |
      While the ${../if/remove-processor:/params[1]/name} parameter is greater
      than or equal to the configured processor maximum.
  test-epilogue: null
  test-prologue: null
- name: Owned
  states:
  - name: 'Yes'
    test-code: |
      ctx->owned = true;
    text: |
      While the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter is owned by the
      scheduler specified by the ${../if/remove-processor:/params[0]/name}
      parameter.
  - name: 'No'
    test-code: |
      ctx->owned = false;
    text: |
      While the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter is not owned by the
      scheduler specified by the ${../if/remove-processor:/params[0]/name}
      parameter.
  test-epilogue: null
  test-prologue: null
- name: Last
  states:
  - name: 'Yes'
    test-code: |
      ctx->last = true;
    text: |
      While the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter is the last processor
      owned by the scheduler specified by the
      ${../if/remove-processor:/params[0]/name} parameter.
  - name: 'No'
    test-code: |
      ctx->last = false;
    text: |
      While the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter is not the last
      processor owned by the scheduler specified by the
      ${../if/remove-processor:/params[0]/name} parameter.
  test-epilogue: null
  test-prologue: null
- name: Home
  states:
  - name: 'Yes'
    test-code: |
      ctx->home = true;
    text: |
      While at least one non-idle task exists which uses the scheduler
      specified by the ${../if/remove-processor:/params[0]/name} parameter as
      its ${/glossary/scheduler-home:/term}.
  - name: 'No'
    test-code: |
      ctx->home = false;
    text: |
      While no non-idle task exists which uses the scheduler specified by the
      ${../if/remove-processor:/params[0]/name} parameter as its
      ${/glossary/scheduler-home:/term}.
  test-epilogue: null
  test-prologue: null
- name: RequiredByAffinity
  states:
  - name: 'Yes'
    test-code: |
      ctx->required_by_affinity = true;
    text: |
      While at least one non-idle task which uses the scheduler specified by
      the ${../if/remove-processor:/params[0]/name} parameter as its
      ${/glossary/scheduler-home:/term} exists those processor affinity set
      requires the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter.
  - name: 'No'
    test-code: |
      ctx->required_by_affinity = false;
    text: |
      While no non-idle task which uses the scheduler specified by the
      ${../if/remove-processor:/params[0]/name} parameter as its
      ${/glossary/scheduler-home:/term} exists those processor affinity set
      requires the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter.
  test-epilogue: null
  test-prologue: null
- name: UsedBy
  states:
  - name: Idle
    test-code: |
      ctx->idle = true;
      ctx->task = false;
      ctx->helping = false;
    text: |
      While the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter is used by an idle
      task.
  - name: Task
    test-code: |
      ctx->idle = false;
      ctx->task = true;
      ctx->helping = false;
    text: |
      While the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter is used by a task
      task which uses the scheduler specified by the
      ${../if/remove-processor:/params[0]/name} parameter as its
      ${/glossary/scheduler-home:/term}.
  - name: TaskIdle
    test-code: |
      ctx->idle = true;
      ctx->task = true;
      ctx->helping = false;
    text: |
      While the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter is used by an idle
      task on behalf of a task task which uses the scheduler specified by the
      ${../if/remove-processor:/params[0]/name} parameter as its
      ${/glossary/scheduler-home:/term}.
  - name: Helping
    test-code: |
      if ( !ctx->last && rtems_scheduler_get_processor_maximum() < 3 ) {
        ${.:skip}
      } else {
        ctx->idle = false;
        ctx->task = false;
        ctx->helping = true;
      }
    text: |
      While the processor specified by the
      ${../if/remove-processor:/params[1]/name} parameter is used by a task
      task which uses the scheduler specified by the
      ${../if/remove-processor:/params[0]/name} parameter as a
      ${/glossary/scheduler-helping:/term}.
  test-epilogue: null
  test-prologue: null
rationale: null
references: []
requirement-type: functional
skip-reasons:
  OnlyOneCPU: |
    Where the system is build with SMP support disabled, the system has exactly
    one processor and there is no processor available to remove from a
    scheduler.  In addition, the scheduler helping protocol is not available.
  NoHomeNoTaskUser: |
    The processor can only be used by a task if a task uses the scheduler as
    its home scheduler.
  LastIsRequired: |
    The last processor is required by a task which uses the scheduler as its
    home scheduler.
test-action: |
  if (
    ctx->id == INVALID_ID ||
    ctx->cpu_index == rtems_configuration_get_maximum_processors() ||
    ( ctx->owned && ctx->last && ctx->home && ctx->required_by_affinity &&
      ( ctx->task || ctx->idle ) )
  ) {
    DoRemoveProcessor( ctx );
  } else {
  #if defined(RTEMS_SMP)
    if ( ctx->owned && !ctx->home && ctx->helping ) {
      RemoveWithHelpingOnly( ctx );
    } else {
      if ( ctx->owned ) {
        rtems_id worker_a;
        rtems_id worker_b;

        worker_a = ctx->worker_id[ WORKER_A ];
        worker_b = ctx->worker_id[ WORKER_B ];

        ctx->cpu_index = 1;

        if ( ctx->last ) {
          ctx->id = SCHEDULER_B_ID;
        } else {
          RemoveProcessor( SCHEDULER_B_ID, 1 );
          AddProcessor( SCHEDULER_A_ID, 1 );
        }

        if ( ctx->home ) {
          SetScheduler( worker_a, ctx->id, PRIO_LOW );

          if ( ctx->required_by_affinity ) {
            SetAffinityOne( worker_a, 1 );
          } else {
            SetAffinityAll( worker_a );
          }
        }

        if ( ctx->idle ) {
          if ( ctx->task ) {
            SendAndSync( ctx, WORKER_A, EVENT_STICKY_OBTAIN );
            SuspendTask( worker_a );
          }
        } else if ( ctx->task ) {
          MakeBusy( ctx, WORKER_A );
        } else if ( ctx->helping ) {
          T_true( ctx->home );

          if ( ctx->last ) {
            SendEvents( worker_b, EVENT_OBTAIN );
            SetPriority( worker_b, PRIO_LOW );
          } else {
            SetScheduler( worker_b, SCHEDULER_C_ID, PRIO_LOW );
            SendAndSync( ctx, WORKER_B, EVENT_OBTAIN );
            MakeBusyAndSync( ctx, WORKER_C );
          }

          SendAndSync( ctx, WORKER_A, EVENT_OBTAIN );
          MakeBusy( ctx, WORKER_B );
          WaitForBusy( ctx, WORKER_B );
        }

        DoRemoveProcessor( ctx );

        if ( ctx->idle ) {
          if ( ctx->task ) {
            ResumeTask( worker_a );
            SendAndSync( ctx, WORKER_A, EVENT_STICKY_RELEASE );
          }
        } else if ( ctx->task ) {
          StopBusyAndWait( ctx, WORKER_A );
        } else if ( ctx->helping ) {
          StopBusy( ctx, WORKER_B );

          if ( ctx->last ) {
            SetPriority( worker_b, PRIO_HIGH );
            SendEvents( worker_b, EVENT_RELEASE );
          } else {
            StopBusyAndWait( ctx, WORKER_C );
            SendAndSync( ctx, WORKER_B, EVENT_RELEASE );
            SetScheduler( worker_b, SCHEDULER_A_ID, PRIO_HIGH );
          }

          WaitForExecutionStop( worker_b );
          SendAndSync( ctx, WORKER_A, EVENT_RELEASE );
        }

        SetAffinityAll( worker_a );
        SetScheduler( worker_a, SCHEDULER_A_ID, PRIO_HIGH );

        if ( !ctx->last ) {
          RemoveProcessor( SCHEDULER_A_ID, 1 );
          AddProcessor( SCHEDULER_B_ID, 1 );
        }
      } else {
        ctx->id = SCHEDULER_B_ID;
        DoRemoveProcessor( ctx );
      }
    }
  #else
    T_unreachable();
  #endif
  }
test-brief: null
test-cleanup: null
test-context:
- brief: |
    This member contains the runner identifier.
  description: null
  member: |
    rtems_id runner_id
- brief: |
    This member contains the worker identifiers.
  description: null
  member: |
    rtems_id worker_id[ WORKER_COUNT ]
- brief: |
    This member contains the mutex identifier.
  description: null
  member: |
    rtems_id mutex_id
- brief: |
    This member contains the sticky mutex identifier.
  description: null
  member: |
    rtems_id sticky_id
- brief: |
    This member contains the worker busy status.
  description: null
  member: |
    volatile bool busy[ WORKER_COUNT ];
- brief: |
    This member contains the worker busy status.
  description: null
  member: |
    volatile uint32_t busy_counter[ WORKER_COUNT ];
- brief: |
    This member contains the barrier to synchronize the runner and the workers.
  description: null
  member: |
    SMP_barrier_Control barrier
- brief: |
    This member contains the call within ISR request.
  description: null
  member: |
    CallWithinISRRequest request;
- brief: |
    This member provides the context to wrap thread queue operations.
  description: null
  member: |
    WrapThreadQueueContext wrap_tq_ctx
- brief: |
    If this member is true, then the processor to remove shall be owned by the
    scheduler.
  description: null
  member: |
    bool owned
- brief: |
    If this member is true, then the processor to remove shall be the last
    processor of the scheduler.
  description: null
  member: |
    bool last
- brief: |
    If this member is true, then at least one non-idle task shall use the
    scheduler as its home scheduler.
  description: null
  member: |
    bool home
- brief: |
    If this member is true, then at least one non-idle task shall required the
    processor to remove due to its affinity set.
  description: null
  member: |
    bool required_by_affinity
- brief: |
    If this member is true, then the processor to remove shall be used by an
    idle task.
  description: null
  member: |
    bool idle
- brief: |
    If this member is true, then the processor to remove shall be used by a
    task or on behalf of a task which uses the scheduler as its home scheduler.
  description: null
  member: |
    bool task
- brief: |
    If this member is true, then the processor to remove shall be used by a
    task which uses the scheduler as a helping scheduler.
  description: null
  member: |
    bool helping
- brief: |
    This member provides the scheduler operation records.
  description: null
  member: |
    T_scheduler_log_4 scheduler_log;
- brief: |
    This member contains the return value of the
    ${../if/remove-processor:/name} call.
  description: null
  member: |
    rtems_status_code status
- brief: |
    This member specifies the ${../if/remove-processor:/params[0]/name}
    parameter value.
  description: null
  member: |
    rtems_id id
- brief: |
    This member specifies the ${../if/remove-processor:/params[1]/name}
    parameter value.
  description: null
  member: |
    uint32_t cpu_index
test-context-support: |
  typedef enum {
    WORKER_A,
    WORKER_B,
    WORKER_C,
    WORKER_COUNT
  } WorkerIndex;
test-description: null
test-header: null
test-includes:
- rtems.h
- rtems/test-scheduler.h
- rtems/score/percpu.h
- rtems/score/smpbarrier.h
test-local-includes:
- ts-config.h
- tx-support.h
test-prepare: |
  ctx->status = RTEMS_NOT_IMPLEMENTED;
test-setup:
  brief: null
  code: |
    #if defined(RTEMS_SMP)
    rtems_status_code   sc;
    rtems_task_priority priority;

    ctx->runner_id = rtems_task_self();
    ctx->mutex_id = CreateMutex();

    sc = rtems_semaphore_create(
      rtems_build_name( 'S', 'T', 'K', 'Y' ),
      1,
      RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY |
        RTEMS_MULTIPROCESSOR_RESOURCE_SHARING,
      PRIO_NORMAL,
      &ctx->sticky_id
    );
    T_rsc_success( sc );

    sc = rtems_semaphore_set_priority(
      ctx->sticky_id,
      SCHEDULER_B_ID,
      PRIO_NORMAL,
      &priority
    );
    T_rsc_success( sc );

    if ( rtems_scheduler_get_processor_maximum() >= 3 ) {
      sc = rtems_semaphore_set_priority(
        ctx->sticky_id,
        SCHEDULER_C_ID,
        PRIO_LOW,
        &priority
      );
      T_rsc_success( sc );

      ctx->worker_id[ WORKER_C ] = CreateTask( "WRKC", PRIO_NORMAL );
      SetScheduler( ctx->worker_id[ WORKER_C ], SCHEDULER_C_ID, PRIO_NORMAL );
      StartTask( ctx->worker_id[ WORKER_C ], WorkerC, ctx );

      if ( rtems_scheduler_get_processor_maximum() >= 4 ) {
        RemoveProcessor( SCHEDULER_C_ID, 3 );
      }
    }

    SetSelfPriority( PRIO_NORMAL );
    SetSelfAffinityOne( 0 );

    ctx->worker_id[ WORKER_A ] = CreateTask( "WRKA", PRIO_HIGH );
    StartTask( ctx->worker_id[ WORKER_A ], WorkerA, ctx );

    ctx->worker_id[ WORKER_B ] = CreateTask( "WRKB", PRIO_HIGH );
    StartTask( ctx->worker_id[ WORKER_B ], WorkerB, ctx );

    WrapThreadQueueInitialize( &ctx->wrap_tq_ctx, RequestISR, ctx );
    #endif
  description: null
test-stop: null
test-support: |
  typedef ${.:/test-context-type} Context;

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

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

    ctx->status = rtems_scheduler_remove_processor( ctx->id, ctx->cpu_index );

    log = T_scheduler_record( NULL );
    T_eq_ptr( &log->header, &ctx->scheduler_log.header );

    if ( ctx->status == RTEMS_SUCCESSFUL ) {
      AddProcessor( ctx->id, ctx->cpu_index );
    }
  }

  #if defined(RTEMS_SMP)

  #define EVENT_SYNC_RUNNER RTEMS_EVENT_0

  #define EVENT_OBTAIN RTEMS_EVENT_1

  #define EVENT_RELEASE RTEMS_EVENT_2

  #define EVENT_STICKY_OBTAIN RTEMS_EVENT_3

  #define EVENT_STICKY_RELEASE RTEMS_EVENT_4

  #define EVENT_RESTART RTEMS_EVENT_5

  #define EVENT_BUSY RTEMS_EVENT_6

  #define EVENT_SYNC_RUNNER_LATE RTEMS_EVENT_7

  static void Barriers( void *arg )
  {
    Context          *ctx;
    SMP_barrier_State barrier_state;

    ctx = arg;
    _SMP_barrier_State_initialize( &barrier_state );

    /* A */
    _SMP_barrier_Wait( &ctx->barrier, &barrier_state, 2 );

    /* B */
    _SMP_barrier_Wait( &ctx->barrier, &barrier_state, 2 );
  }

  static void RequestISR( void *arg )
  {
    Context *ctx;

    ctx = arg;
    ctx->request.handler = Barriers;
    ctx->request.arg = ctx;
    CallWithinISRSubmit( &ctx->request );
  }

  static void SendAndSync(
    Context        *ctx,
    WorkerIndex     worker,
    rtems_event_set event
  )
  {
    SendEvents( ctx->worker_id[ worker ], EVENT_SYNC_RUNNER | event );
    ReceiveAllEvents( EVENT_SYNC_RUNNER );
    WaitForExecutionStop( ctx->worker_id[ worker ] );
  }

  static void MakeBusy( Context *ctx, WorkerIndex worker )
  {
    ctx->busy_counter[ worker ] = 0;
    ctx->busy[ worker ] = true;
    SendEvents( ctx->worker_id[ worker ], EVENT_BUSY );
  }

  static void MakeBusyAndSync( Context *ctx, WorkerIndex worker )
  {
    ctx->busy_counter[ worker ] = 0;
    ctx->busy[ worker ] = true;
    SendEvents( ctx->worker_id[ worker ], EVENT_SYNC_RUNNER | EVENT_BUSY );
    ReceiveAllEvents( EVENT_SYNC_RUNNER );
  }

  static void StopBusy( Context *ctx, WorkerIndex worker )
  {
    ctx->busy[ worker ] = false;
  }

  static void StopBusyAndWait( Context *ctx, WorkerIndex worker )
  {
    StopBusy( ctx, worker );
    WaitForExecutionStop( ctx->worker_id[ worker ] );
  }

  static void WaitForBusy( Context *ctx, WorkerIndex worker )
  {
    while ( ctx->busy_counter[ worker ] == 0 ) {
      /* Wait */
    }
  }

  static void RemoveWithHelpingOnly( Context *ctx )
  {
    SMP_barrier_State barrier_state;

    /*
     * Use the mutex and the worker to construct the removal of the last
     * processor of a scheduler while a thread is scheduled.
     */

    _SMP_barrier_Control_initialize( &ctx->barrier );
    _SMP_barrier_State_initialize( &barrier_state );

    SetScheduler( ctx->worker_id[ WORKER_B ], SCHEDULER_B_ID, PRIO_NORMAL );

    /* Let worker B help worker A */
    SendEvents( ctx->worker_id[ WORKER_A ], EVENT_OBTAIN );
    SendAndSync( ctx, WORKER_B, EVENT_OBTAIN );

    /*
     * Restart the worker B to withdraw the help offer and wait on barriers.
     * Move worker B to scheduler A.  Remove the processor while worker A is
     * scheduled.
     */

    SendEvents( ctx->worker_id[ WORKER_A ], EVENT_RESTART );

    /* A */
    _SMP_barrier_Wait( &ctx->barrier, &barrier_state, 2 );

    SetScheduler( ctx->worker_id[ WORKER_B ], SCHEDULER_A_ID, PRIO_HIGH );

    ctx->id = SCHEDULER_B_ID;
    ctx->cpu_index = 1;
    DoRemoveProcessor( ctx );

    /* B */
    _SMP_barrier_Wait( &ctx->barrier, &barrier_state, 2 );

    /* Clean up all used resources */
    SetSelfPriority( PRIO_NORMAL );
    SendEvents( ctx->worker_id[ WORKER_A ], EVENT_RELEASE );
    T_busy(100000);
  }

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

    ctx = (Context *) arg;

    while ( true ) {
      rtems_event_set events;

      events = ReceiveAnyEvents();

      if ( ( events & EVENT_SYNC_RUNNER ) != 0 ) {
        SendEvents( ctx->runner_id, EVENT_SYNC_RUNNER );
      }

      if ( ( events & EVENT_OBTAIN ) != 0 ) {
        ObtainMutex( ctx->mutex_id );
      }

      if ( ( events & EVENT_RELEASE ) != 0 ) {
        ReleaseMutex( ctx->mutex_id );
      }

      if ( ( events & EVENT_STICKY_OBTAIN ) != 0 ) {
        ObtainMutex( ctx->sticky_id );
      }

      if ( ( events & EVENT_STICKY_RELEASE ) != 0 ) {
        ReleaseMutex( ctx->sticky_id );
      }

      if ( ( events & EVENT_RESTART ) != 0 ) {
        rtems_status_code sc;

        T_eq_u32( rtems_scheduler_get_processor(), 0 );
        SetPriority( ctx->runner_id, PRIO_VERY_HIGH );
        T_eq_u32( rtems_scheduler_get_processor(), 1 );

        if ( !ctx->last ) {
          SetScheduler( ctx->worker_id[ WORKER_C ], SCHEDULER_A_ID, PRIO_LOW );
          RemoveProcessor( SCHEDULER_C_ID, 2 );
          AddProcessor( SCHEDULER_B_ID, 2 );
        }

        WrapThreadQueueExtract(
          &ctx->wrap_tq_ctx,
          GetThread( ctx->worker_id[ WORKER_B ] )
        );

        sc = rtems_task_restart(
          ctx->worker_id[ WORKER_B ],
          (rtems_task_argument) ctx
          );
        T_rsc_success( sc );

        T_eq_u32( rtems_scheduler_get_processor(), 0 );

        if ( !ctx->last ) {
          RemoveProcessor( SCHEDULER_B_ID, 2 );
          AddProcessor( SCHEDULER_C_ID, 2 );
          SetScheduler( ctx->worker_id[ WORKER_C ], SCHEDULER_C_ID, PRIO_NORMAL );
        }
      }

      if ( ( events & EVENT_BUSY ) != 0 ) {
        while ( ctx->busy[ worker ] ) {
          ++ctx->busy_counter[ worker ];
        }
      }

      if ( ( events & EVENT_SYNC_RUNNER_LATE ) != 0 ) {
        SendEvents( ctx->runner_id, EVENT_SYNC_RUNNER );
      }
    }
  }

  static void WorkerA( rtems_task_argument arg )
  {
    Worker( arg, WORKER_A );
  }

  static void WorkerB( rtems_task_argument arg )
  {
    Worker( arg, WORKER_B );
  }

  static void WorkerC( rtems_task_argument arg )
  {
    Worker( arg, WORKER_C );
  }
  #endif
test-target: testsuites/validation/tc-scheduler-remove-processor.c
test-teardown:
  brief: null
  code: |
    #if defined(RTEMS_SMP)
    DeleteTask( ctx->worker_id[ WORKER_A ] );
    DeleteTask( ctx->worker_id[ WORKER_B ] );
    DeleteTask( ctx->worker_id[ WORKER_C ] );
    DeleteMutex( ctx->mutex_id );
    DeleteMutex( ctx->sticky_id );
    WrapThreadQueueDestroy( &ctx->wrap_tq_ctx );

    if ( rtems_scheduler_get_processor_maximum() >= 4 ) {
      AddProcessor( SCHEDULER_C_ID, 3 );
    }

    RestoreRunnerPriority();
    SetSelfAffinityAll();
    #endif
  description: null
text: ${.:text-template}
transition-map:
- enabled-by: true
  post-conditions:
    Status: InvId
    Removed: Nop
  pre-conditions:
    Id:
    - Invalid
    CPUIndex: all
    Owned: N/A
    Last: N/A
    Home: N/A
    RequiredByAffinity: N/A
    UsedBy: N/A
- enabled-by: true
  post-conditions:
    Status: InvNum
    Removed: Nop
  pre-conditions:
    Id:
    - Scheduler
    CPUIndex:
    - Invalid
    Owned: N/A
    Last: N/A
    Home: N/A
    RequiredByAffinity: N/A
    UsedBy: N/A
- enabled-by: true
  post-conditions:
    Status: InvNum
    Removed: Nop
  pre-conditions:
    Id:
    - Scheduler
    CPUIndex:
    - Valid
    Owned:
    - 'No'
    Last: N/A
    Home: N/A
    RequiredByAffinity: N/A
    UsedBy: N/A
- enabled-by: true
  post-conditions:
    Status:
    - if:
        pre-conditions:
          Last: 'Yes'
          UsedBy: Helping
      then: InUse
    - else: Ok
    Removed:
    - if:
        post-conditions:
          Status: Ok
      then: 'Yes'
    - else: Nop
  pre-conditions:
    Id:
    - Scheduler
    CPUIndex:
    - Valid
    Owned:
    - 'Yes'
    Last: all
    Home:
    - 'No'
    RequiredByAffinity: N/A
    UsedBy: all
- enabled-by: true
  post-conditions:
    Status:
    - if:
        pre-conditions:
          RequiredByAffinity: 'Yes'
      then: InUse
    - if:
        pre-conditions:
          Last: 'Yes'
          UsedBy: Helping
      then: InUse
    - else: Ok
    Removed:
    - if:
        post-conditions:
          Status: Ok
      then: 'Yes'
    - else: Nop
  pre-conditions:
    Id:
    - Scheduler
    CPUIndex:
    - Valid
    Owned:
    - 'Yes'
    Last: all
    Home:
    - 'Yes'
    RequiredByAffinity: all
    UsedBy: all
- enabled-by: true
  post-conditions: NoHomeNoTaskUser
  pre-conditions:
    Id:
    - Scheduler
    CPUIndex:
    - Valid
    Owned:
    - 'Yes'
    Last: all
    Home:
    - 'No'
    RequiredByAffinity: N/A
    UsedBy:
    - Task
    - TaskIdle
- enabled-by: true
  post-conditions: LastIsRequired
  pre-conditions:
    Id:
    - Scheduler
    CPUIndex:
    - Valid
    Owned:
    - 'Yes'
    Last:
    - 'Yes'
    Home:
    - 'Yes'
    RequiredByAffinity:
    - 'No'
    UsedBy: all
- enabled-by:
    not: RTEMS_SMP
  post-conditions: OnlyOneCPU
  pre-conditions:
    Id: all
    CPUIndex: all
    Owned:
    - 'No'
    Last: all
    Home: all
    RequiredByAffinity: all
    UsedBy: all
- enabled-by:
    not: RTEMS_SMP
  post-conditions: OnlyOneCPU
  pre-conditions:
    Id: all
    CPUIndex: all
    Owned: all
    Last:
    - 'No'
    Home: all
    RequiredByAffinity: all
    UsedBy: all
- enabled-by:
    not: RTEMS_SMP
  post-conditions: OnlyOneCPU
  pre-conditions:
    Id: all
    CPUIndex: all
    Owned: all
    Last: all
    Home:
    - 'No'
    RequiredByAffinity: all
    UsedBy: all
- enabled-by:
    not: RTEMS_SMP
  post-conditions: OnlyOneCPU
  pre-conditions:
    Id: all
    CPUIndex: all
    Owned: all
    Last: all
    Home:
    - 'Yes'
    RequiredByAffinity:
    - 'No'
    UsedBy: all
- enabled-by:
    not: RTEMS_SMP
  post-conditions: OnlyOneCPU
  pre-conditions:
    Id: all
    CPUIndex: all
    Owned: all
    Last: all
    Home: all
    RequiredByAffinity: all
    UsedBy:
    - TaskIdle
    - Helping
type: requirement