summaryrefslogtreecommitdiffstats
path: root/cpukit/include/rtems/score/schedulersmpimpl.h
blob: c37f53c8c02a218559b734b003d2c7ffd61cab23 (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
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
/**
 * @file
 *
 * @ingroup RTEMSScoreSchedulerSMP
 *
 * @brief This header file provides interfaces of the
 *   @ref RTEMSScoreSchedulerSMP which are only used by the implementation.
 */

/*
 * Copyright (c) 2013, 2021 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifndef _RTEMS_SCORE_SCHEDULERSMPIMPL_H
#define _RTEMS_SCORE_SCHEDULERSMPIMPL_H

#include <rtems/score/schedulersmp.h>
#include <rtems/score/assert.h>
#include <rtems/score/chainimpl.h>
#include <rtems/score/schedulersimpleimpl.h>
#include <rtems/bspIo.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
 * @addtogroup RTEMSScoreSchedulerSMP
 *
 * The scheduler nodes can be in four states
 * - @ref SCHEDULER_SMP_NODE_BLOCKED,
 * - @ref SCHEDULER_SMP_NODE_SCHEDULED, and
 * - @ref SCHEDULER_SMP_NODE_READY.
 *
 * State transitions are triggered via basic operations
 * - _Scheduler_SMP_Enqueue(),
 * - _Scheduler_SMP_Enqueue_scheduled(), and
 * - _Scheduler_SMP_Block().
 *
 * @dot
 * digraph {
 *   node [style="filled"];
 *
 *   bs [label="BLOCKED"];
 *   ss [label="SCHEDULED", fillcolor="green"];
 *   rs [label="READY", fillcolor="red"];
 *
 *   edge [label="enqueue"];
 *   edge [fontcolor="darkgreen", color="darkgreen"];
 *
 *   bs -> ss;
 *
 *   edge [fontcolor="red", color="red"];
 *
 *   bs -> rs;
 *
 *   edge [label="enqueue other"];
 *
 *   ss -> rs;
 *
 *   edge [label="block"];
 *   edge [fontcolor="black", color="black"];
 *
 *   ss -> bs;
 *   rs -> bs;
 *
 *   edge [label="block other"];
 *   edge [fontcolor="darkgreen", color="darkgreen"];
 *
 *   rs -> ss;
 * }
 * @enddot
 *
 * During system initialization each processor of the scheduler instance starts
 * with an idle thread assigned to it.  Lets have a look at an example with two
 * idle threads I and J with priority 5.  We also have blocked threads A, B and
 * C with priorities 1, 2 and 3 respectively.  The scheduler nodes are ordered
 * with respect to the thread priority from left to right in the below
 * diagrams.  The highest priority node (lowest priority number) is the
 * leftmost node.  Since the processor assignment is independent of the thread
 * priority the processor indices may move from one state to the other.
 *
 * @dot
 * digraph {
 *   node [style="filled"];
 *   edge [dir="none"];
 *   subgraph {
 *     rank = same;
 *
 *     i [label="I (5)", fillcolor="green"];
 *     j [label="J (5)", fillcolor="green"];
 *     a [label="A (1)"];
 *     b [label="B (2)"];
 *     c [label="C (3)"];
 *     i -> j;
 *   }
 *
 *   subgraph {
 *     rank = same;
 *
 *     p0 [label="PROCESSOR 0", shape="box"];
 *     p1 [label="PROCESSOR 1", shape="box"];
 *   }
 *
 *   i -> p0;
 *   j -> p1;
 * }
 * @enddot
 *
 * Lets start A.  For this an enqueue operation is performed.
 *
 * @dot
 * digraph {
 *   node [style="filled"];
 *   edge [dir="none"];
 *
 *   subgraph {
 *     rank = same;
 *
 *     i [label="I (5)", fillcolor="green"];
 *     j [label="J (5)", fillcolor="red"];
 *     a [label="A (1)", fillcolor="green"];
 *     b [label="B (2)"];
 *     c [label="C (3)"];
 *     a -> i;
 *   }
 *
 *   subgraph {
 *     rank = same;
 *
 *     p0 [label="PROCESSOR 0", shape="box"];
 *     p1 [label="PROCESSOR 1", shape="box"];
 *   }
 *
 *   i -> p0;
 *   a -> p1;
 * }
 * @enddot
 *
 * Lets start C.
 *
 * @dot
 * digraph {
 *   node [style="filled"];
 *   edge [dir="none"];
 *
 *   subgraph {
 *     rank = same;
 *
 *     a [label="A (1)", fillcolor="green"];
 *     c [label="C (3)", fillcolor="green"];
 *     i [label="I (5)", fillcolor="red"];
 *     j [label="J (5)", fillcolor="red"];
 *     b [label="B (2)"];
 *     a -> c;
 *     i -> j;
 *   }
 *
 *   subgraph {
 *     rank = same;
 *
 *     p0 [label="PROCESSOR 0", shape="box"];
 *     p1 [label="PROCESSOR 1", shape="box"];
 *   }
 *
 *   c -> p0;
 *   a -> p1;
 * }
 * @enddot
 *
 * Lets start B.
 *
 * @dot
 * digraph {
 *   node [style="filled"];
 *   edge [dir="none"];
 *
 *   subgraph {
 *     rank = same;
 *
 *     a [label="A (1)", fillcolor="green"];
 *     b [label="B (2)", fillcolor="green"];
 *     c [label="C (3)", fillcolor="red"];
 *     i [label="I (5)", fillcolor="red"];
 *     j [label="J (5)", fillcolor="red"];
 *     a -> b;
 *     c -> i -> j;
 *   }
 *
 *   subgraph {
 *     rank = same;
 *
 *     p0 [label="PROCESSOR 0", shape="box"];
 *     p1 [label="PROCESSOR 1", shape="box"];
 *   }
 *
 *   b -> p0;
 *   a -> p1;
 * }
 * @enddot
 *
 * Lets change the priority of thread A to 4.
 *
 * @dot
 * digraph {
 *   node [style="filled"];
 *   edge [dir="none"];
 *
 *   subgraph {
 *     rank = same;
 *
 *     b [label="B (2)", fillcolor="green"];
 *     c [label="C (3)", fillcolor="green"];
 *     a [label="A (4)", fillcolor="red"];
 *     i [label="I (5)", fillcolor="red"];
 *     j [label="J (5)", fillcolor="red"];
 *     b -> c;
 *     a -> i -> j;
 *   }
 *
 *   subgraph {
 *     rank = same;
 *
 *     p0 [label="PROCESSOR 0", shape="box"];
 *     p1 [label="PROCESSOR 1", shape="box"];
 *   }
 *
 *   b -> p0;
 *   c -> p1;
 * }
 * @enddot
 *
 * Now perform a blocking operation with thread B.  Please note that thread A
 * migrated now from processor 0 to processor 1 and thread C still executes on
 * processor 1.
 *
 * @dot
 * digraph {
 *   node [style="filled"];
 *   edge [dir="none"];
 *
 *   subgraph {
 *     rank = same;
 *
 *     c [label="C (3)", fillcolor="green"];
 *     a [label="A (4)", fillcolor="green"];
 *     i [label="I (5)", fillcolor="red"];
 *     j [label="J (5)", fillcolor="red"];
 *     b [label="B (2)"];
 *     c -> a;
 *     i -> j;
 *   }
 *
 *   subgraph {
 *     rank = same;
 *
 *     p0 [label="PROCESSOR 0", shape="box"];
 *     p1 [label="PROCESSOR 1", shape="box"];
 *   }
 *
 *   a -> p0;
 *   c -> p1;
 * }
 * @enddot
 *
 * @{
 */

typedef bool ( *Scheduler_SMP_Has_ready )(
  Scheduler_Context *context
);

typedef Scheduler_Node *( *Scheduler_SMP_Get_highest_ready )(
  Scheduler_Context *context,
  Scheduler_Node    *filter
);

typedef Scheduler_Node *( *Scheduler_SMP_Get_lowest_ready )(
  Scheduler_Context *context
);

typedef Scheduler_Node *( *Scheduler_SMP_Get_lowest_scheduled )(
  Scheduler_Context *context,
  Scheduler_Node    *filter
);

typedef void ( *Scheduler_SMP_Extract )(
  Scheduler_Context *context,
  Scheduler_Node    *node_to_extract
);

typedef void ( *Scheduler_SMP_Insert )(
  Scheduler_Context *context,
  Scheduler_Node    *node_to_insert,
  Priority_Control   insert_priority
);

typedef void ( *Scheduler_SMP_Move )(
  Scheduler_Context *context,
  Scheduler_Node    *node_to_move
);

typedef bool ( *Scheduler_SMP_Ask_for_help )(
  Scheduler_Context *context,
  Thread_Control    *thread,
  Scheduler_Node    *node
);

typedef void ( *Scheduler_SMP_Update )(
  Scheduler_Context *context,
  Scheduler_Node    *node_to_update,
  Priority_Control   new_priority
);

typedef void ( *Scheduler_SMP_Set_affinity )(
  Scheduler_Context *context,
  Scheduler_Node    *node,
  void              *arg
);

typedef bool ( *Scheduler_SMP_Enqueue )(
  Scheduler_Context *context,
  Scheduler_Node    *node_to_enqueue,
  Priority_Control   priority
);

typedef void ( *Scheduler_SMP_Enqueue_scheduled )(
  Scheduler_Context *context,
  Scheduler_Node    *node_to_enqueue,
  Priority_Control   priority
);

typedef void ( *Scheduler_SMP_Allocate_processor )(
  Scheduler_Context *context,
  Scheduler_Node    *scheduled,
  Per_CPU_Control   *cpu
);

typedef void ( *Scheduler_SMP_Register_idle )(
  Scheduler_Context *context,
  Scheduler_Node    *idle,
  Per_CPU_Control   *cpu
);

/**
 * @brief Does nothing.
 *
 * @param context This parameter is unused.
 * @param idle This parameter is unused.
 * @param cpu This parameter is unused.
 */
static inline void _Scheduler_SMP_Do_nothing_register_idle(
  Scheduler_Context *context,
  Scheduler_Node    *idle,
  Per_CPU_Control   *cpu
)
{
  (void) context;
  (void) idle;
  (void) cpu;
}

/**
 * @brief Checks if @a to_insert is less or equal than the priority of the chain node.
 *
 * @param key is the priority to compare.
 *
 * @param to_insert is the chain node to insert.
 *
 * @param next is the chain node to compare the priority of.
 *
 * @retval true @a to_insert is less or equal than the priority of @a next.
 * @retval false @a to_insert is greater than the priority of @a next.
 */
static inline bool _Scheduler_SMP_Priority_less_equal(
  const void       *key,
  const Chain_Node *to_insert,
  const Chain_Node *next
)
{
  const Priority_Control   *priority_to_insert;
  const Scheduler_SMP_Node *node_next;

  (void) to_insert;
  priority_to_insert = (const Priority_Control *) key;
  node_next = (const Scheduler_SMP_Node *) next;

  return *priority_to_insert <= node_next->priority;
}

/**
 * @brief Gets the scheduler smp context.
 *
 * @param context The context to cast to Scheduler_SMP_Context *.
 *
 * @return @a context cast to Scheduler_SMP_Context *.
 */
static inline Scheduler_SMP_Context *_Scheduler_SMP_Get_self(
  Scheduler_Context *context
)
{
  return (Scheduler_SMP_Context *) context;
}

/**
 * @brief Initializes the scheduler smp context.
 *
 * @param[out] self The context to initialize.
 */
static inline void _Scheduler_SMP_Initialize(
  Scheduler_SMP_Context *self
)
{
  _Chain_Initialize_empty( &self->Scheduled );
}

/**
 * @brief Gets the scheduler smp node of the thread.
 *
 * @param thread The thread to get the smp node of.
 *
 * @return The scheduler smp node of @a thread.
 */
static inline Scheduler_SMP_Node *_Scheduler_SMP_Thread_get_node(
  Thread_Control *thread
)
{
  return (Scheduler_SMP_Node *) _Thread_Scheduler_get_home_node( thread );
}

/**
 * @brief Gets the scheduler smp node of the thread.
 *
 * @param thread The thread to get the smp node of.
 *
 * @return The scheduler smp node of @a thread.
 */
static inline Scheduler_SMP_Node *_Scheduler_SMP_Thread_get_own_node(
  Thread_Control *thread
)
{
  return (Scheduler_SMP_Node *) _Thread_Scheduler_get_home_node( thread );
}

/**
 * @brief Gets the scheduler smp node.
 *
 * @param node The node to cast to Scheduler_SMP_Node *.
 *
 * @return @a node cast to Scheduler_SMP_Node *.
 */
static inline Scheduler_SMP_Node *_Scheduler_SMP_Node_downcast(
  Scheduler_Node *node
)
{
  return (Scheduler_SMP_Node *) node;
}

/**
 * @brief Gets the state of the node.
 *
 * @param node The node to get the state of.
 *
 * @return The state of @a node.
 */
static inline Scheduler_SMP_Node_state _Scheduler_SMP_Node_state(
  const Scheduler_Node *node
)
{
  return ( (const Scheduler_SMP_Node *) node )->state;
}

/**
 * @brief Gets the priority of the node.
 *
 * @param node The node to get the priority of.
 *
 * @return The priority of @a node.
 */
static inline Priority_Control _Scheduler_SMP_Node_priority(
  const Scheduler_Node *node
)
{
  return ( (const Scheduler_SMP_Node *) node )->priority;
}

/**
 * @brief Initializes the scheduler smp node.
 *
 * @param scheduler The scheduler instance.
 * @param[out] node The node to initialize.
 * @param thread The thread of the scheduler smp node.
 * @param priority The priority to initialize @a node with.
 */
static inline void _Scheduler_SMP_Node_initialize(
  const Scheduler_Control *scheduler,
  Scheduler_SMP_Node      *node,
  Thread_Control          *thread,
  Priority_Control         priority
)
{
  _Scheduler_Node_do_initialize( scheduler, &node->Base, thread, priority );
  node->state = SCHEDULER_SMP_NODE_BLOCKED;
  node->priority = priority;
}

/**
 * @brief Updates the priority of the node to the new priority.
 *
 * @param[out] node The node to update the priority of.
 * @param new_priority The new priority for @a node.
 */
static inline void _Scheduler_SMP_Node_update_priority(
  Scheduler_SMP_Node *node,
  Priority_Control    new_priority
)
{
  node->priority = new_priority;
}

/**
 * @brief Changes the state of the node to the given state.
 *
 * @param[out] node the node to change the state of.
 * @param new_state The new state for @a node.
 */
static inline void _Scheduler_SMP_Node_change_state(
  Scheduler_Node           *node,
  Scheduler_SMP_Node_state  new_state
)
{
  Scheduler_SMP_Node *the_node;

  the_node = _Scheduler_SMP_Node_downcast( node );
  the_node->state = new_state;
}

/**
 * @brief Checks if the processor is owned by the given context.
 *
 * @param context The context to check whether @a cpu is owned by it.
 * @param cpu The cpu to check whether it is owned by @a context.
 *
 * @retval true @a cpu is owned by @a context.
 * @retval false @a cpu is not owned by @a context.
 */
static inline bool _Scheduler_SMP_Is_processor_owned_by_us(
  const Scheduler_Context *context,
  const Per_CPU_Control   *cpu
)
{
  return cpu->Scheduler.context == context;
}

/**
 * @brief Removes the thread's ask for help request from the processor.
 *
 * The caller must be the owner of the thread's scheduler lock.
 *
 * @param[in, out] thread is the thread of the ask for help request.
 *
 * @param[in, out] cpu is the processor from which the ask for help request
 *   should be removed.
 */
void _Scheduler_SMP_Remove_ask_for_help_from_processor(
  Thread_Control  *thread,
  Per_CPU_Control *cpu
);

/**
 * @brief Cancels the thread's ask for help request.
 *
 * The caller must be the owner of the thread's scheduler lock.
 *
 * @param[in, out] thread is the thread of the ask help request.
 */
static inline void _Scheduler_SMP_Cancel_ask_for_help( Thread_Control *thread )
{
  Per_CPU_Control *cpu;

  _Assert( _ISR_lock_Is_owner( &thread->Scheduler.Lock ) );
  cpu = thread->Scheduler.ask_for_help_cpu;

  if ( RTEMS_PREDICT_FALSE( cpu != NULL ) ) {
    _Scheduler_SMP_Remove_ask_for_help_from_processor( thread, cpu );
  }
}

/**
 * @brief Requests to ask for help for the thread.
 *
 * The actual ask for help operations are carried out during
 * _Thread_Do_dispatch() on the current processor.
 *
 * An alternative approach would be to carry out the requests on a processor
 * related to the thread.  This could reduce the overhead for the preempting
 * thread a bit, however, there are at least two problems with this approach.
 * Firstly, we have to figure out what is a processor related to the thread.
 * Secondly, we may need an inter-processor interrupt.
 *
 * @param[in, out] thread is the thread in need for help.
 */
static inline void _Scheduler_SMP_Request_ask_for_help( Thread_Control *thread )
{
  ISR_lock_Context lock_context;
  Per_CPU_Control *cpu_self;

  cpu_self = _Per_CPU_Get();

  _Assert( thread->Scheduler.ask_for_help_cpu == NULL );
  thread->Scheduler.ask_for_help_cpu = cpu_self;
  cpu_self->dispatch_necessary = true;

  _Per_CPU_Acquire( cpu_self, &lock_context );
  _Chain_Append_unprotected(
    &cpu_self->Threads_in_need_for_help,
    &thread->Scheduler.Help_node
  );
  _Per_CPU_Release( cpu_self, &lock_context );
}

/**
 * @brief This enumeration defines what a scheduler should do with a node which
 * could be scheduled.
 */
typedef enum {
  SCHEDULER_SMP_DO_SCHEDULE,
  SCHEDULER_SMP_DO_NOT_SCHEDULE
} Scheduler_SMP_Action;

/**
 * @brief Tries to schedule the scheduler node.
 *
 * When an SMP scheduler needs to schedule a node, it shall use this function
 * to determine what it shall do with the node.
 *
 * This function uses the state of the node and the scheduler state of the
 * owner thread to determine what shall be done.  Each scheduler maintains its
 * nodes independent of other schedulers.  This function ensures that a thread
 * is scheduled by at most one scheduler.  If a node requires an executing
 * thread due to some locking protocol and the owner thread is already
 * scheduled by another scheduler, then an idle thread will be attached to the
 * node.
 *
 * @param[in, out] node is the node which should be scheduled.
 *
 * @param get_idle_node is the get idle node handler.
 *
 * @param arg is the get idle node handler argument.
 *
 * @retval SCHEDULER_SMP_DO_SCHEDULE The node shall be scheduled.
 *
 * @retval SCHEDULER_SMP_DO_NOT_SCHEDULE The node shall be blocked.  This
 *   action is returned, if the owner thread is already scheduled by another
 *   scheduler.
 */
static inline Scheduler_SMP_Action _Scheduler_SMP_Try_to_schedule(
  Scheduler_Node          *node,
  Scheduler_Get_idle_node  get_idle_node,
  void                    *arg
)
{
  ISR_lock_Context        lock_context;
  Thread_Control         *owner;
  Thread_Scheduler_state  owner_state;
  int                     owner_sticky_level;

  owner = _Scheduler_Node_get_owner( node );
  _Assert( _Scheduler_Node_get_idle( node ) == NULL );

  _Thread_Scheduler_acquire_critical( owner, &lock_context );
  owner_state = owner->Scheduler.state;
  owner_sticky_level = node->sticky_level;

  if ( RTEMS_PREDICT_TRUE( owner_state == THREAD_SCHEDULER_READY ) ) {
    _Scheduler_SMP_Cancel_ask_for_help( owner );
    _Scheduler_Thread_change_state( owner, THREAD_SCHEDULER_SCHEDULED );
    _Thread_Scheduler_release_critical( owner, &lock_context );
    return SCHEDULER_SMP_DO_SCHEDULE;
  }

  _Thread_Scheduler_release_critical( owner, &lock_context );

  if (
    ( owner_state == THREAD_SCHEDULER_SCHEDULED && owner_sticky_level <= 1 ) ||
    owner_sticky_level == 0
  ) {
    _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_BLOCKED );

    return SCHEDULER_SMP_DO_NOT_SCHEDULE;
  }

  (void) _Scheduler_Use_idle_thread( node, get_idle_node, arg );

  return SCHEDULER_SMP_DO_SCHEDULE;
}

/**
 * @brief Allocates a processor to the user of the scheduled node.
 *
 * Attempts to prevent migrations but does not take into account affinity.
 *
 * @param[in, out] context is the scheduler context.
 *
 * @param[in, out] scheduled is the scheduled node that gets the processor allocated.
 *
 * @param[in, out] cpu is the processor to allocate.
 */
static inline void _Scheduler_SMP_Allocate_processor_lazy(
  Scheduler_Context *context,
  Scheduler_Node    *scheduled,
  Per_CPU_Control   *cpu
)
{
  Thread_Control *scheduled_thread = _Scheduler_Node_get_user( scheduled );
  Per_CPU_Control *scheduled_cpu = _Thread_Get_CPU( scheduled_thread );
  Per_CPU_Control *cpu_self = _Per_CPU_Get();

  _Assert( _ISR_Get_level() != 0 );

  if ( cpu == scheduled_cpu ) {
    _Thread_Set_CPU( scheduled_thread, cpu );
    _Thread_Dispatch_update_heir( cpu_self, cpu, scheduled_thread );

    return;
  }

  if (
    _Thread_Is_executing_on_a_processor( scheduled_thread ) &&
    _Scheduler_SMP_Is_processor_owned_by_us( context, scheduled_cpu )
  ) {
    Thread_Control *heir = scheduled_cpu->heir;
    _Thread_Dispatch_update_heir( cpu_self, scheduled_cpu, scheduled_thread );
    _Thread_Set_CPU( heir, cpu );
    _Thread_Dispatch_update_heir( cpu_self, cpu, heir );

    return;
  }

  _Thread_Set_CPU( scheduled_thread, cpu );
  _Thread_Dispatch_update_heir( cpu_self, cpu, scheduled_thread );
}

/**
 * @brief Allocates exactly the processor to the user of the scheduled node.
 *
 * This method is slightly different from
 * _Scheduler_SMP_Allocate_processor_lazy() in that it does what it is asked to
 * do.  _Scheduler_SMP_Allocate_processor_lazy() attempts to prevent migrations
 * but does not take into account affinity.
 *
 * @param[in, out] context is the scheduler context.
 *
 * @param[in, out] scheduled is the scheduled node that gets the processor allocated.
 *
 * @param[in, out] cpu is the processor to allocate.
 */
static inline void _Scheduler_SMP_Allocate_processor_exact(
  Scheduler_Context *context,
  Scheduler_Node    *scheduled,
  Per_CPU_Control   *cpu
)
{
  Thread_Control *scheduled_thread = _Scheduler_Node_get_user( scheduled );
  Per_CPU_Control *cpu_self = _Per_CPU_Get();

  (void) context;

  _Thread_Set_CPU( scheduled_thread, cpu );
  _Thread_Dispatch_update_heir( cpu_self, cpu, scheduled_thread );
}

/**
 * @brief Allocates the processor to the user of the scheduled node using the
 *   given allocation handler.
 *
 * @param[in, out] context is the scheduler context.
 *
 * @param[in, out] scheduled is the scheduled node that gets the processor allocated.
 *
 * @param[in, out] cpu is the processor to allocate.
 *
 * @param allocate_processor is the handler which should allocate the processor.
 */
static inline void _Scheduler_SMP_Allocate_processor(
  Scheduler_Context                *context,
  Scheduler_Node                   *scheduled,
  Per_CPU_Control                  *cpu,
  Scheduler_SMP_Allocate_processor  allocate_processor
)
{
  _Scheduler_SMP_Node_change_state( scheduled, SCHEDULER_SMP_NODE_SCHEDULED );
  ( *allocate_processor )( context, scheduled, cpu );
}

/**
 * @brief Preempts the victim's thread and allocates a processor for the user
 *   of the scheduled node.
 *
 * @param[in, out] context is the scheduler context.
 *
 * @param scheduled[in, out] is the node of the user thread that is about to
 *   get a processor allocated.
 *
 * @param[in, out] victim is the victim node of the thread to preempt.
 *
 * @param[in, out] victim_idle is the idle thread used by the victim node or NULL.
 *
 * @param allocate_processor The function for allocation of a processor for the new thread.
 */
static inline void _Scheduler_SMP_Preempt(
  Scheduler_Context                *context,
  Scheduler_Node                   *scheduled,
  Scheduler_Node                   *victim,
  Thread_Control                   *victim_idle,
  Scheduler_SMP_Allocate_processor  allocate_processor
)
{
  Thread_Control   *victim_owner;
  ISR_lock_Context  lock_context;
  Per_CPU_Control  *cpu;

  _Scheduler_SMP_Node_change_state( victim, SCHEDULER_SMP_NODE_READY );

  victim_owner = _Scheduler_Node_get_owner( victim );
  _Thread_Scheduler_acquire_critical( victim_owner, &lock_context );

  if ( RTEMS_PREDICT_TRUE( victim_idle == NULL ) ) {
    if ( victim_owner->Scheduler.state == THREAD_SCHEDULER_SCHEDULED ) {
      _Scheduler_Thread_change_state( victim_owner, THREAD_SCHEDULER_READY );

      if ( victim_owner->Scheduler.helping_nodes > 0 ) {
        _Scheduler_SMP_Request_ask_for_help( victim_owner );
      }
    }

    cpu = _Thread_Get_CPU( victim_owner );
  } else {
    cpu = _Thread_Get_CPU( victim_idle );
  }

  _Thread_Scheduler_release_critical( victim_owner, &lock_context );

  _Scheduler_SMP_Allocate_processor(
    context,
    scheduled,
    cpu,
    allocate_processor
  );
}

/**
 * @brief Returns the lowest member of the scheduled nodes.
 *
 * @param context The scheduler context instance.
 * @param filter This parameter is unused.
 *
 * @return The lowest scheduled node.
 */
static inline Scheduler_Node *_Scheduler_SMP_Get_lowest_scheduled(
  Scheduler_Context *context,
  Scheduler_Node    *filter
)
{
  Scheduler_SMP_Context *self;
  Scheduler_Node        *lowest_scheduled;

  (void) filter;

  self = _Scheduler_SMP_Get_self( context );

  _Assert( !_Chain_Is_empty( &self->Scheduled ) );
  lowest_scheduled = (Scheduler_Node *) _Chain_Last( &self->Scheduled );

  _Assert(
    _Chain_Next( &lowest_scheduled->Node.Chain ) ==
      _Chain_Tail( &self->Scheduled )
  );

  return lowest_scheduled;
}

/**
 * @brief Tries to schedule the given node.
 *
 * Schedules the node, or blocks if that is necessary.
 *
 * @param context The scheduler context instance.
 * @param[in, out] node The node to insert into the scheduled nodes.
 * @param priority The priority of @a node.
 * @param[in, out] lowest_scheduled The lowest member of the scheduled nodes.
 * @param insert_scheduled Function to insert a node into the set of
 *   scheduled nodes.
 * @param move_from_scheduled_to_ready Function to move a node from the set
 *   of scheduled nodes to the set of ready nodes.
 * @param allocate_processor Function to allocate a processor to a node
 *   based on the rules of the scheduler.
 */
static inline void _Scheduler_SMP_Enqueue_to_scheduled(
  Scheduler_Context                *context,
  Scheduler_Node                   *node,
  Priority_Control                  priority,
  Scheduler_Node                   *lowest_scheduled,
  Scheduler_SMP_Insert              insert_scheduled,
  Scheduler_SMP_Move                move_from_scheduled_to_ready,
  Scheduler_SMP_Move                move_from_ready_to_scheduled,
  Scheduler_SMP_Allocate_processor  allocate_processor,
  Scheduler_Get_idle_node           get_idle_node,
  Scheduler_Release_idle_node       release_idle_node
)
{
  Thread_Control      *lowest_scheduled_idle;
  Scheduler_SMP_Action action;

  lowest_scheduled_idle = _Scheduler_Release_idle_thread_if_necessary(
    lowest_scheduled,
    release_idle_node,
    context
  );

  ( *move_from_scheduled_to_ready )( context, lowest_scheduled );

  action = _Scheduler_SMP_Try_to_schedule( node, get_idle_node, context );

  if ( RTEMS_PREDICT_TRUE( action == SCHEDULER_SMP_DO_SCHEDULE ) ) {
    _Scheduler_SMP_Preempt(
      context,
      node,
      lowest_scheduled,
      lowest_scheduled_idle,
      allocate_processor
    );

    ( *insert_scheduled )( context, node, priority );
  } else {
    _Assert( action == SCHEDULER_SMP_DO_NOT_SCHEDULE );

    if ( lowest_scheduled_idle != NULL ) {
      (void) _Scheduler_Use_idle_thread( lowest_scheduled, get_idle_node, context );
    }

    ( *move_from_ready_to_scheduled )( context, lowest_scheduled );
  }
}

/**
 * @brief Enqueues a node according to the specified order function.
 *
 * The node must not be in the scheduled state.
 *
 * @param context The scheduler instance context.
 * @param[in, out] node The node to enqueue.
 * @param priority The node insert priority.
 * @param order The order function.
 * @param insert_ready Function to insert a node into the set of ready
 *   nodes.
 * @param insert_scheduled Function to insert a node into the set of
 *   scheduled nodes.
 * @param move_from_scheduled_to_ready Function to move a node from the set
 *   of scheduled nodes to the set of ready nodes.
 * @param get_lowest_scheduled Function to select the node from the
 *   scheduled nodes to replace.  It may not be possible to find one, in this
 *   case a pointer must be returned so that the order functions returns false
 *   if this pointer is passed as the second argument to the order function.
 * @param allocate_processor Function to allocate a processor to a node
 *   based on the rules of the scheduler.
 */
static inline bool _Scheduler_SMP_Enqueue(
  Scheduler_Context                  *context,
  Scheduler_Node                     *node,
  Priority_Control                    insert_priority,
  Chain_Node_order                    order,
  Scheduler_SMP_Insert                insert_ready,
  Scheduler_SMP_Insert                insert_scheduled,
  Scheduler_SMP_Move                  move_from_scheduled_to_ready,
  Scheduler_SMP_Move                  move_from_ready_to_scheduled,
  Scheduler_SMP_Get_lowest_scheduled  get_lowest_scheduled,
  Scheduler_SMP_Allocate_processor    allocate_processor,
  Scheduler_Get_idle_node             get_idle_node,
  Scheduler_Release_idle_node         release_idle_node
)
{
  bool            needs_help;
  Scheduler_Node *lowest_scheduled;

  lowest_scheduled = ( *get_lowest_scheduled )( context, node );

  if (
    ( *order )(
      &insert_priority,
      &node->Node.Chain,
      &lowest_scheduled->Node.Chain
    )
  ) {
    _Scheduler_SMP_Enqueue_to_scheduled(
      context,
      node,
      insert_priority,
      lowest_scheduled,
      insert_scheduled,
      move_from_scheduled_to_ready,
      move_from_ready_to_scheduled,
      allocate_processor,
      get_idle_node,
      release_idle_node
    );
    needs_help = false;
  } else {
    _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_READY );
    ( *insert_ready )( context, node, insert_priority );
    needs_help = true;
  }

  return needs_help;
}

/**
 * @brief Enqueues a scheduled node according to the specified order
 * function.
 *
 * @param context The scheduler instance context.
 * @param[in, out] node The node to enqueue.
 * @param order The order function.
 * @param extract_from_ready Function to extract a node from the set of
 *   ready nodes.
 * @param get_highest_ready Function to get the highest ready node.
 * @param insert_ready Function to insert a node into the set of ready
 *   nodes.
 * @param insert_scheduled Function to insert a node into the set of
 *   scheduled nodes.
 * @param move_from_ready_to_scheduled Function to move a node from the set
 *   of ready nodes to the set of scheduled nodes.
 * @param allocate_processor Function to allocate a processor to a node
 *   based on the rules of the scheduler.
 */
static inline void _Scheduler_SMP_Enqueue_scheduled(
  Scheduler_Context                *context,
  Scheduler_Node                   *const node,
  Priority_Control                  insert_priority,
  Chain_Node_order                  order,
  Scheduler_SMP_Extract             extract_from_ready,
  Scheduler_SMP_Get_highest_ready   get_highest_ready,
  Scheduler_SMP_Insert              insert_ready,
  Scheduler_SMP_Insert              insert_scheduled,
  Scheduler_SMP_Move                move_from_ready_to_scheduled,
  Scheduler_SMP_Allocate_processor  allocate_processor,
  Scheduler_Get_idle_node           get_idle_node,
  Scheduler_Release_idle_node       release_idle_node
)
{
  Thread_Control *node_idle;

  node_idle = _Scheduler_Release_idle_thread_if_necessary(
    node,
    release_idle_node,
    context
  );

  while ( true ) {
    Scheduler_Node       *highest_ready;
    Scheduler_SMP_Action  action;

    highest_ready = ( *get_highest_ready )( context, node );

    /*
     * The node has been extracted from the scheduled chain.  We have to place
     * it now on the scheduled or ready set.
     */
    if (
      node->sticky_level > 0 && ( *order )(
        &insert_priority,
        &node->Node.Chain,
        &highest_ready->Node.Chain
      )
    ) {
      if ( node_idle != NULL ) {
        Thread_Control   *owner;
        ISR_lock_Context  lock_context;

        owner = _Scheduler_Node_get_owner( node );
        _Thread_Scheduler_acquire_critical( owner, &lock_context );

        if ( owner->Scheduler.state == THREAD_SCHEDULER_READY ) {
          Per_CPU_Control *cpu;

          _Scheduler_SMP_Cancel_ask_for_help( owner );
          _Scheduler_Thread_change_state( owner, THREAD_SCHEDULER_SCHEDULED );
          cpu = _Thread_Get_CPU( node_idle );
          _Thread_Set_CPU( owner, cpu );
          _Thread_Scheduler_release_critical( owner, &lock_context );
          _Thread_Dispatch_update_heir( _Per_CPU_Get(), cpu, owner );
        } else {
          Thread_Control *new_idle;

          _Thread_Scheduler_release_critical( owner, &lock_context );
          new_idle = _Scheduler_Use_idle_thread( node, get_idle_node, context );
          _Assert_Unused_variable_equals( new_idle, node_idle );
        }
      }

      ( *insert_scheduled )( context, node, insert_priority );

      return;
    }

    action = _Scheduler_SMP_Try_to_schedule(
      highest_ready,
      get_idle_node,
      context
    );

    if ( RTEMS_PREDICT_TRUE( action == SCHEDULER_SMP_DO_SCHEDULE ) ) {
      _Scheduler_SMP_Preempt(
        context,
        highest_ready,
        node,
        node_idle,
        allocate_processor
      );

      ( *move_from_ready_to_scheduled )( context, highest_ready );
      ( *insert_ready )( context, node, insert_priority );
      return;
    }

    _Assert( action == SCHEDULER_SMP_DO_NOT_SCHEDULE );
    ( *extract_from_ready )( context, highest_ready );
  }
}

/**
 * @brief Extracts a scheduled node from the scheduled nodes.
 *
 * @param context This parameter is unused.
 * @param node The node to extract from the chain it belongs to.
 */
static inline void _Scheduler_SMP_Extract_from_scheduled(
  Scheduler_Context *context,
  Scheduler_Node    *node
)
{
  (void) context;
  _Chain_Extract_unprotected( &node->Node.Chain );
}

/**
 * @brief Schedules the highest ready node.
 *
 * @param context The scheduler context instance.
 * @param victim The node of the thread that is repressed by the newly scheduled thread.
 * @param cpu is the processor to allocate.
 * @param extract_from_scheduled Function to extract a node from the set of
 *      scheduled nodes.
 * @param extract_from_ready Function to extract a node from the set of
 *      ready nodes.
 * @param get_highest_ready Function to get the highest ready node.
 * @param move_from_ready_to_scheduled Function to move a node from the set
 *      of ready nodes to the set of scheduled nodes.
 * @param allocate_processor Function to allocate a processor to a node
 *      based on the rules of the scheduler.
 */
static inline void _Scheduler_SMP_Schedule_highest_ready(
  Scheduler_Context                *context,
  Scheduler_Node                   *victim,
  Per_CPU_Control                  *cpu,
  Scheduler_SMP_Extract             extract_from_scheduled,
  Scheduler_SMP_Extract             extract_from_ready,
  Scheduler_SMP_Get_highest_ready   get_highest_ready,
  Scheduler_SMP_Move                move_from_ready_to_scheduled,
  Scheduler_SMP_Allocate_processor  allocate_processor,
  Scheduler_Get_idle_node           get_idle_node
)
{
  Scheduler_SMP_Action action;

  _Scheduler_SMP_Node_change_state( victim, SCHEDULER_SMP_NODE_BLOCKED );
  ( *extract_from_scheduled )( context, victim );

  while ( true ) {
    Scheduler_Node *highest_ready = ( *get_highest_ready )( context, victim );

    action = _Scheduler_SMP_Try_to_schedule(
      highest_ready,
      get_idle_node,
      context
    );

    if ( RTEMS_PREDICT_TRUE( action == SCHEDULER_SMP_DO_SCHEDULE ) ) {
      _Scheduler_SMP_Allocate_processor(
        context,
        highest_ready,
        cpu,
        allocate_processor
      );

      ( *move_from_ready_to_scheduled )( context, highest_ready );
      return;
    }

    _Assert( action == SCHEDULER_SMP_DO_NOT_SCHEDULE );
    ( *extract_from_ready )( context, highest_ready );
  }
}

/**
 * @brief Schedules the highest ready node and preempts a currently executing one.
 *
 * @param context The scheduler context instance.
 * @param victim The node of the thread that is repressed by the newly scheduled thread.
 * @param extract_from_ready Function to extract a node from the set of
 *      ready nodes.
 * @param get_highest_ready Function to get the highest ready node.
 * @param move_from_ready_to_scheduled Function to move a node from the set
 *      of ready nodes to the set of scheduled nodes.
 * @param allocate_processor Function to allocate a processor to a node
 *      based on the rules of the scheduler.
 */
static inline void _Scheduler_SMP_Preempt_and_schedule_highest_ready(
  Scheduler_Context                *context,
  Scheduler_Node                   *victim,
  Scheduler_SMP_Extract             extract_from_ready,
  Scheduler_SMP_Get_highest_ready   get_highest_ready,
  Scheduler_SMP_Move                move_from_ready_to_scheduled,
  Scheduler_SMP_Allocate_processor  allocate_processor,
  Scheduler_Get_idle_node           get_idle_node,
  Scheduler_Release_idle_node       release_idle_node
)
{
  Thread_Control      *victim_idle;
  Scheduler_SMP_Action action;

  _Scheduler_SMP_Node_change_state( victim, SCHEDULER_SMP_NODE_READY );
  victim_idle = _Scheduler_Release_idle_thread_if_necessary(
    victim,
    release_idle_node,
    context
  );

  while ( true ) {
    Scheduler_Node *highest_ready = ( *get_highest_ready )( context, victim );

    action = _Scheduler_SMP_Try_to_schedule(
      highest_ready,
      get_idle_node,
      context
    );

    if ( RTEMS_PREDICT_TRUE( action == SCHEDULER_SMP_DO_SCHEDULE ) ) {
      _Scheduler_SMP_Preempt(
        context,
        highest_ready,
        victim,
        victim_idle,
        allocate_processor
      );

      ( *move_from_ready_to_scheduled )( context, highest_ready );
      return;
    }

    _Assert( action == SCHEDULER_SMP_DO_NOT_SCHEDULE );
    ( *extract_from_ready )( context, highest_ready );
  }
}

/**
 * @brief Blocks the thread.
 *
 * @param context The scheduler instance context.
 * @param[in, out] thread The thread of the scheduling operation.
 * @param[in, out] node The scheduler node of the thread to block.
 * @param extract_from_scheduled Function to extract a node from the set of
 *      scheduled nodes.
 * @param extract_from_ready Function to extract a node from the set of
 *      ready nodes.
 * @param get_highest_ready Function to get the highest ready node.
 * @param move_from_ready_to_scheduled Function to move a node from the set
 *      of ready nodes to the set of scheduled nodes.
 * @param allocate_processor Function to allocate a processor to a node
 *      based on the rules of the scheduler.
 */
static inline void _Scheduler_SMP_Block(
  Scheduler_Context                *context,
  Thread_Control                   *thread,
  Scheduler_Node                   *node,
  Scheduler_SMP_Extract             extract_from_scheduled,
  Scheduler_SMP_Extract             extract_from_ready,
  Scheduler_SMP_Get_highest_ready   get_highest_ready,
  Scheduler_SMP_Move                move_from_ready_to_scheduled,
  Scheduler_SMP_Allocate_processor  allocate_processor,
  Scheduler_Get_idle_node           get_idle_node
)
{
  int                       sticky_level;
  ISR_lock_Context          lock_context;
  Scheduler_SMP_Node_state  node_state;
  Per_CPU_Control          *cpu;

  sticky_level = node->sticky_level;
  --sticky_level;
  node->sticky_level = sticky_level;
  _Assert( sticky_level >= 0 );

  _Thread_Scheduler_acquire_critical( thread, &lock_context );
  _Scheduler_SMP_Cancel_ask_for_help( thread );
  cpu = _Thread_Get_CPU( thread );
  _Scheduler_Thread_change_state( thread, THREAD_SCHEDULER_BLOCKED );
  _Thread_Scheduler_release_critical( thread, &lock_context );

  node_state = _Scheduler_SMP_Node_state( node );

  if ( RTEMS_PREDICT_FALSE( sticky_level > 0 ) ) {
    if (
      node_state == SCHEDULER_SMP_NODE_SCHEDULED &&
      _Scheduler_Node_get_idle( node ) == NULL
    ) {
      Thread_Control *idle;

      idle = _Scheduler_Use_idle_thread( node, get_idle_node, context );
      _Thread_Set_CPU( idle, cpu );
      _Thread_Dispatch_update_heir( _Per_CPU_Get(), cpu, idle );
    }

    return;
  }

  _Assert( _Scheduler_Node_get_user( node ) == thread );
  _Assert( _Scheduler_Node_get_idle( node ) == NULL );

  if ( node_state == SCHEDULER_SMP_NODE_SCHEDULED ) {
    _Scheduler_SMP_Schedule_highest_ready(
      context,
      node,
      cpu,
      extract_from_scheduled,
      extract_from_ready,
      get_highest_ready,
      move_from_ready_to_scheduled,
      allocate_processor,
      get_idle_node
    );
  } else if ( node_state == SCHEDULER_SMP_NODE_READY ) {
    _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_BLOCKED );
    ( *extract_from_ready )( context, node );
  }
}

/**
 * @brief Unblocks the thread.
 *
 * @param context The scheduler instance context.
 * @param[in, out] thread The thread of the scheduling operation.
 * @param[in, out] node The scheduler node of the thread to block.
 * @param update Function to update the node's priority to the new value.
 * @param enqueue Function to insert a node with a priority in the ready queue
 *      of a context.
 */
static inline void _Scheduler_SMP_Unblock(
  Scheduler_Context          *context,
  Thread_Control             *thread,
  Scheduler_Node             *node,
  Scheduler_SMP_Update        update,
  Scheduler_SMP_Enqueue       enqueue,
  Scheduler_Release_idle_node release_idle_node
)
{
  Scheduler_SMP_Node_state  node_state;
  Priority_Control          priority;

  _Assert( _Chain_Is_node_off_chain( &thread->Scheduler.Help_node ) );

  ++node->sticky_level;
  _Assert( node->sticky_level > 0 );

  node_state = _Scheduler_SMP_Node_state( node );

  if ( RTEMS_PREDICT_FALSE( node_state == SCHEDULER_SMP_NODE_SCHEDULED ) ) {
    _Scheduler_Thread_change_state( thread, THREAD_SCHEDULER_SCHEDULED );
    _Scheduler_Discard_idle_thread(
      thread,
      node,
      release_idle_node,
      context
    );

    return;
  }

  _Scheduler_Thread_change_state( thread, THREAD_SCHEDULER_READY );

  priority = _Scheduler_Node_get_priority( node );
  priority = SCHEDULER_PRIORITY_PURIFY( priority );

  if ( priority != _Scheduler_SMP_Node_priority( node ) ) {
    ( *update )( context, node, priority );
  }

  if ( node_state == SCHEDULER_SMP_NODE_BLOCKED ) {
    Priority_Control insert_priority;
    bool             needs_help;

    insert_priority = SCHEDULER_PRIORITY_APPEND( priority );
    needs_help = ( *enqueue )( context, node, insert_priority );

    if ( needs_help && thread->Scheduler.helping_nodes > 0 ) {
      _Scheduler_SMP_Request_ask_for_help( thread );
    }
  } else {
    _Assert( node_state == SCHEDULER_SMP_NODE_READY );
    _Assert( node->sticky_level > 0 );
    _Assert( node->idle == NULL );
    _Scheduler_SMP_Request_ask_for_help( thread );
  }
}

/**
 * @brief Updates the priority of the node and the position in the queues it
 * is in.
 *
 * This function firstly updates the priority of the node and then extracts
 * and reinserts it into the queue the node is part of using the given
 * functions.
 *
 * @param context The scheduler instance context.
 * @param thread The thread for the operation.
 * @param[in, out] node The node to update the priority of.
 * @param extract_from_scheduled Function to extract a node from the set of
 *      scheduled nodes.
 * @param extract_from_ready Function to extract a node from the ready
 *      queue of the scheduler context.
 * @param update Function to update the priority of a node in the scheduler
 *      context.
 * @param enqueue Function to enqueue a node with a given priority.
 * @param enqueue_scheduled Function to enqueue a scheduled node.
 * @param ask_for_help Function to perform a help request.
 */
static inline void _Scheduler_SMP_Update_priority(
  Scheduler_Context              *context,
  Thread_Control                 *thread,
  Scheduler_Node                 *node,
  Scheduler_SMP_Extract           extract_from_scheduled,
  Scheduler_SMP_Extract           extract_from_ready,
  Scheduler_SMP_Update            update,
  Scheduler_SMP_Enqueue           enqueue,
  Scheduler_SMP_Enqueue_scheduled enqueue_scheduled,
  Scheduler_SMP_Ask_for_help      ask_for_help
)
{
  Priority_Control         priority;
  Priority_Control         insert_priority;
  Scheduler_SMP_Node_state node_state;

  insert_priority = _Scheduler_Node_get_priority( node );
  priority = SCHEDULER_PRIORITY_PURIFY( insert_priority );

  if ( priority == _Scheduler_SMP_Node_priority( node ) ) {
    if ( _Thread_Is_ready( thread ) ) {
      ( *ask_for_help )( context, thread, node );
    }

    return;
  }

  node_state = _Scheduler_SMP_Node_state( node );

  if ( node_state == SCHEDULER_SMP_NODE_SCHEDULED ) {
    ( *extract_from_scheduled )( context, node );
    ( *update )( context, node, priority );
    ( *enqueue_scheduled )( context, node, insert_priority );
  } else if ( node_state == SCHEDULER_SMP_NODE_READY ) {
    ( *extract_from_ready )( context, node );
    ( *update )( context, node, priority );
    ( *enqueue )( context, node, insert_priority );
  } else {
    ( *update )( context, node, priority );

    if ( _Thread_Is_ready( thread ) ) {
      ( *ask_for_help )( context, thread, node );
    }
  }
}

/**
 * @brief Performs a yield and asks for help if necessary.
 *
 * @param context The scheduler instance context.
 * @param thread The thread for the operation.
 * @param node The node of the thread that yields.
 * @param extract_from_scheduled Function to extract a node from the set of
 *      scheduled nodes.
 * @param extract_from_ready Function to extract a node from the ready
 *      queue of the scheduler context.
 * @param enqueue Function to enqueue a node with a given priority.
 * @param enqueue_scheduled Function to enqueue a scheduled node.
 */
static inline void _Scheduler_SMP_Yield(
  Scheduler_Context              *context,
  Thread_Control                 *thread,
  Scheduler_Node                 *node,
  Scheduler_SMP_Extract           extract_from_scheduled,
  Scheduler_SMP_Extract           extract_from_ready,
  Scheduler_SMP_Enqueue           enqueue,
  Scheduler_SMP_Enqueue_scheduled enqueue_scheduled
)
{
  Scheduler_SMP_Node_state node_state;
  Priority_Control         insert_priority;

  node_state = _Scheduler_SMP_Node_state( node );
  insert_priority = _Scheduler_SMP_Node_priority( node );
  insert_priority = SCHEDULER_PRIORITY_APPEND( insert_priority );

  if ( node_state == SCHEDULER_SMP_NODE_SCHEDULED ) {
    ( *extract_from_scheduled )( context, node );
    ( *enqueue_scheduled )( context, node, insert_priority );
  } else if ( node_state == SCHEDULER_SMP_NODE_READY ) {
    ( *extract_from_ready )( context, node );
    (void) ( *enqueue )( context, node, insert_priority );
  }
}

/**
 * @brief Inserts the node with the given priority into the scheduled nodes.
 *
 * @param context The scheduler instance context.
 * @param node_to_insert The scheduled node to insert.
 * @param priority_to_insert The priority with which to insert the node.
 */
static inline void _Scheduler_SMP_Insert_scheduled(
  Scheduler_Context *context,
  Scheduler_Node    *node_to_insert,
  Priority_Control   priority_to_insert
)
{
  Scheduler_SMP_Context *self;

  self = _Scheduler_SMP_Get_self( context );

  _Chain_Insert_ordered_unprotected(
    &self->Scheduled,
    &node_to_insert->Node.Chain,
    &priority_to_insert,
    _Scheduler_SMP_Priority_less_equal
  );
}

/**
 * @brief Asks for help.
 *
 * @param context The scheduler instance context.
 * @param thread The thread that asks for help.
 * @param[in, out] node The node of the thread that performs the ask for help
 *      operation.
 * @param order The order function.
 * @param insert_ready Function to insert a node into the set of ready
 *      nodes.
 * @param insert_scheduled Function to insert a node into the set of
 *      scheduled nodes.
 * @param move_from_scheduled_to_ready Function to move a node from the set
 *      of scheduled nodes to the set of ready nodes.
 * @param get_lowest_scheduled Function to select the node from the
 *      scheduled nodes to replace.
 * @param allocate_processor Function to allocate a processor to a node
 *      based on the rules of the scheduler.
 *
 * @retval true The ask for help operation was successful.
 * @retval false The ask for help operation was not successful.
 */
static inline bool _Scheduler_SMP_Ask_for_help(
  Scheduler_Context                  *context,
  Thread_Control                     *thread,
  Scheduler_Node                     *node,
  Chain_Node_order                    order,
  Scheduler_SMP_Insert                insert_ready,
  Scheduler_SMP_Insert                insert_scheduled,
  Scheduler_SMP_Move                  move_from_scheduled_to_ready,
  Scheduler_SMP_Get_lowest_scheduled  get_lowest_scheduled,
  Scheduler_SMP_Allocate_processor    allocate_processor,
  Scheduler_Release_idle_node         release_idle_node
)
{
  Scheduler_Node   *lowest_scheduled;
  ISR_lock_Context  lock_context;
  bool              success;

  if ( thread->Scheduler.pinned_scheduler != NULL ) {
    /*
     * Pinned threads are not allowed to ask for help.  Return success to break
     * the loop in _Thread_Ask_for_help() early.
     */
    return true;
  }

  lowest_scheduled = ( *get_lowest_scheduled )( context, node );

  _Thread_Scheduler_acquire_critical( thread, &lock_context );

  if ( thread->Scheduler.state == THREAD_SCHEDULER_READY ) {
    Scheduler_SMP_Node_state node_state;

    node_state = _Scheduler_SMP_Node_state( node );

    if ( node_state == SCHEDULER_SMP_NODE_BLOCKED ) {
      Priority_Control insert_priority;

      insert_priority = _Scheduler_SMP_Node_priority( node );

      if (
        ( *order )(
          &insert_priority,
          &node->Node.Chain,
          &lowest_scheduled->Node.Chain
        )
      ) {
        Thread_Control *lowest_scheduled_idle;

        _Scheduler_SMP_Cancel_ask_for_help( thread );
        _Scheduler_Thread_change_state( thread, THREAD_SCHEDULER_SCHEDULED );
        _Thread_Scheduler_release_critical( thread, &lock_context );

        lowest_scheduled_idle = _Scheduler_Release_idle_thread_if_necessary(
          lowest_scheduled,
          release_idle_node,
          context
        );

        _Scheduler_SMP_Preempt(
          context,
          node,
          lowest_scheduled,
          lowest_scheduled_idle,
          allocate_processor
        );

        ( *move_from_scheduled_to_ready )( context, lowest_scheduled );
        ( *insert_scheduled )( context, node, insert_priority );

        success = true;
      } else {
        _Thread_Scheduler_release_critical( thread, &lock_context );

        _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_READY );
        ( *insert_ready )( context, node, insert_priority );
        success = false;
      }
    } else if ( node_state == SCHEDULER_SMP_NODE_SCHEDULED ) {
      _Scheduler_SMP_Cancel_ask_for_help( thread );
      _Scheduler_Thread_change_state( thread, THREAD_SCHEDULER_SCHEDULED );
      _Thread_Scheduler_release_critical( thread, &lock_context );
      _Scheduler_Discard_idle_thread(
        thread,
        node,
        release_idle_node,
        context
      );
      success = true;
    } else {
      _Thread_Scheduler_release_critical( thread, &lock_context );
      success = false;
    }
  } else {
    _Thread_Scheduler_release_critical( thread, &lock_context );
    success = false;
  }

  return success;
}

/**
 * @brief Reconsiders help request.
 *
 * @param context The scheduler context instance.
 * @param thread The thread to reconsider the help request of.
 * @param[in, out] node The scheduler node of @a thread.
 * @param extract_from_ready Function to extract a node from the ready queue
 *      of the scheduler context.
 */
static inline void _Scheduler_SMP_Reconsider_help_request(
  Scheduler_Context     *context,
  Thread_Control        *thread,
  Scheduler_Node        *node,
  Scheduler_SMP_Extract  extract_from_ready
)
{
  ISR_lock_Context lock_context;

  _Thread_Scheduler_acquire_critical( thread, &lock_context );

  if (
    thread->Scheduler.state == THREAD_SCHEDULER_SCHEDULED
      && _Scheduler_SMP_Node_state( node ) == SCHEDULER_SMP_NODE_READY
      && node->sticky_level == 1
  ) {
    _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_BLOCKED );
    ( *extract_from_ready )( context, node );
  }

  _Thread_Scheduler_release_critical( thread, &lock_context );
}

/**
 * @brief Withdraws the node.
 *
 * @param context The scheduler context instance.
 * @param[in, out] thread The thread to change to @a next_state.
 * @param[in, out] node The node to withdraw.
 * @param next_state The new state for @a thread.
 * @param extract_from_scheduled Function to extract a node from the set of
 *      scheduled nodes.
 * @param extract_from_ready Function to extract a node from the ready queue
 *      of the scheduler context.
 * @param get_highest_ready Function to get the highest ready node.
 * @param move_from_ready_to_scheduled Function to move a node from the set
 *      of ready nodes to the set of scheduled nodes.
 * @param allocate_processor Function to allocate a processor to a node
 *      based on the rules of the scheduler.
 */
static inline void _Scheduler_SMP_Withdraw_node(
  Scheduler_Context                *context,
  Thread_Control                   *thread,
  Scheduler_Node                   *node,
  Thread_Scheduler_state            next_state,
  Scheduler_SMP_Extract             extract_from_scheduled,
  Scheduler_SMP_Extract             extract_from_ready,
  Scheduler_SMP_Get_highest_ready   get_highest_ready,
  Scheduler_SMP_Move                move_from_ready_to_scheduled,
  Scheduler_SMP_Allocate_processor  allocate_processor,
  Scheduler_Get_idle_node           get_idle_node
)
{
  ISR_lock_Context         lock_context;
  Scheduler_SMP_Node_state node_state;

  _Thread_Scheduler_acquire_critical( thread, &lock_context );

  node_state = _Scheduler_SMP_Node_state( node );

  if ( node_state == SCHEDULER_SMP_NODE_SCHEDULED ) {
    Per_CPU_Control *cpu;

    _Assert( thread == _Scheduler_Node_get_user( node ) );
    cpu = _Thread_Get_CPU( thread );
    _Scheduler_Thread_change_state( thread, next_state );
    _Thread_Scheduler_release_critical( thread, &lock_context );

    _Assert( _Scheduler_Node_get_user( node ) == thread );
    _Assert( _Scheduler_Node_get_idle( node ) == NULL );

    _Scheduler_SMP_Schedule_highest_ready(
      context,
      node,
      cpu,
      extract_from_scheduled,
      extract_from_ready,
      get_highest_ready,
      move_from_ready_to_scheduled,
      allocate_processor,
      get_idle_node
    );
  } else if ( node_state == SCHEDULER_SMP_NODE_READY ) {
    _Thread_Scheduler_release_critical( thread, &lock_context );
    _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_BLOCKED );
    ( *extract_from_ready )( context, node );
  } else {
    _Assert( node_state == SCHEDULER_SMP_NODE_BLOCKED );
    _Thread_Scheduler_release_critical( thread, &lock_context );
  }
}

/**
 * @brief Makes the node sticky.
 *
 * @param scheduler is the scheduler of the node.
 *
 * @param[in, out] the_thread is the thread owning the node.
 *
 * @param[in, out] node is the scheduler node to make sticky.
 */
static inline void _Scheduler_SMP_Make_sticky(
  const Scheduler_Control *scheduler,
  Thread_Control          *the_thread,
  Scheduler_Node          *node,
  Scheduler_SMP_Update     update,
  Scheduler_SMP_Enqueue    enqueue
)
{
  Scheduler_SMP_Node_state node_state;

  node_state = _Scheduler_SMP_Node_state( node );

  if ( node_state == SCHEDULER_SMP_NODE_BLOCKED ) {
    Scheduler_Context *context;
    Priority_Control   insert_priority;
    Priority_Control   priority;

    context = _Scheduler_Get_context( scheduler );
    priority = _Scheduler_Node_get_priority( node );
    priority = SCHEDULER_PRIORITY_PURIFY( priority );

    if ( priority != _Scheduler_SMP_Node_priority( node ) ) {
      ( *update )( context, node, priority );
    }

    insert_priority = SCHEDULER_PRIORITY_APPEND( priority );
    (void) ( *enqueue )( context, node, insert_priority );
  }
}

/**
 * @brief Cleans the sticky property from the node.
 *
 * @param scheduler is the scheduler of the node.
 *
 * @param[in, out] the_thread is the thread owning the node.
 *
 * @param[in, out] node is the scheduler node to clean the sticky property.
 */
static inline void _Scheduler_SMP_Clean_sticky(
  const Scheduler_Control          *scheduler,
  Thread_Control                   *the_thread,
  Scheduler_Node                   *node,
  Scheduler_SMP_Extract             extract_from_scheduled,
  Scheduler_SMP_Extract             extract_from_ready,
  Scheduler_SMP_Get_highest_ready   get_highest_ready,
  Scheduler_SMP_Move                move_from_ready_to_scheduled,
  Scheduler_SMP_Allocate_processor  allocate_processor,
  Scheduler_Get_idle_node           get_idle_node,
  Scheduler_Release_idle_node       release_idle_node
)
{
  Scheduler_SMP_Node_state node_state;

  node_state = _Scheduler_SMP_Node_state( node );

  if ( node_state == SCHEDULER_SMP_NODE_SCHEDULED ) {
    Thread_Control *idle;

    idle = _Scheduler_Node_get_idle( node );

    if ( idle != NULL ) {
      Scheduler_Context *context;

      context = _Scheduler_Get_context( scheduler );

      _Scheduler_Release_idle_thread( node, idle, release_idle_node, context );
      _Scheduler_SMP_Schedule_highest_ready(
        context,
        node,
        _Thread_Get_CPU( idle ),
        extract_from_scheduled,
        extract_from_ready,
        get_highest_ready,
        move_from_ready_to_scheduled,
        allocate_processor,
        get_idle_node
      );
    }
  }
}

/**
 * @brief Starts the idle thread on the given processor.
 *
 * @param context The scheduler context instance.
 * @param[in, out] idle The idle thread to schedule.
 * @param cpu The processor for the idle thread.
 * @param register_idle Function to register the idle thread for a cpu.
 */
static inline void _Scheduler_SMP_Do_start_idle(
  Scheduler_Context           *context,
  Thread_Control              *idle,
  Per_CPU_Control             *cpu,
  Scheduler_SMP_Register_idle  register_idle
)
{
  Scheduler_SMP_Context *self;
  Scheduler_SMP_Node    *node;

  self = _Scheduler_SMP_Get_self( context );
  node = _Scheduler_SMP_Thread_get_node( idle );

  _Scheduler_Thread_change_state( idle, THREAD_SCHEDULER_SCHEDULED );
  node->state = SCHEDULER_SMP_NODE_SCHEDULED;

  _Thread_Set_CPU( idle, cpu );
  ( *register_idle )( context, &node->Base, cpu );
  _Chain_Append_unprotected( &self->Scheduled, &node->Base.Node.Chain );
}

/**
 * @brief Adds the idle thread to the processor.
 *
 * @param context The scheduler context instance.
 * @param[in, out] idle The idle thread to add to the processor.
 * @param has_ready Function that checks if a given context has ready threads.
 * @param enqueue_scheduled Function to enqueue a scheduled node.
 * @param register_idle Function to register the idle thread for a cpu.
 */
static inline void _Scheduler_SMP_Add_processor(
  Scheduler_Context              *context,
  Thread_Control                 *idle,
  Scheduler_SMP_Has_ready         has_ready,
  Scheduler_SMP_Enqueue_scheduled enqueue_scheduled,
  Scheduler_SMP_Register_idle     register_idle
)
{
  Scheduler_SMP_Context *self;
  Scheduler_Node        *node;

  self = _Scheduler_SMP_Get_self( context );
  idle->Scheduler.state = THREAD_SCHEDULER_SCHEDULED;
  node = _Thread_Scheduler_get_home_node( idle );
  _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_SCHEDULED );
  ( *register_idle )( context, node, _Thread_Get_CPU( idle ) );

  if ( ( *has_ready )( &self->Base ) ) {
    Priority_Control insert_priority;

    insert_priority = _Scheduler_SMP_Node_priority( node );
    insert_priority = SCHEDULER_PRIORITY_APPEND( insert_priority );
    ( *enqueue_scheduled )( &self->Base, node, insert_priority );
  } else {
    _Chain_Append_unprotected( &self->Scheduled, &node->Node.Chain );
  }
}

/**
 * @brief Removes an idle thread from the processor.
 *
 * @param context The scheduler context instance.
 * @param cpu The processor to remove from.
 * @param extract_from_scheduled Function to extract a node from the set of
 *      scheduled nodes.
 * @param extract_from_ready Function to extract a node from the ready queue
 *      of the scheduler context.
 * @param enqueue Function to enqueue a node with a given priority.
 *
 * @return The idle thread of @a cpu.
 */
static inline Thread_Control *_Scheduler_SMP_Remove_processor(
  Scheduler_Context          *context,
  Per_CPU_Control            *cpu,
  Scheduler_SMP_Extract       extract_from_scheduled,
  Scheduler_SMP_Extract       extract_from_ready,
  Scheduler_SMP_Enqueue       enqueue,
  Scheduler_Get_idle_node     get_idle_node,
  Scheduler_Release_idle_node release_idle_node
)
{
  Scheduler_SMP_Context *self;
  Chain_Node            *chain_node;
  Scheduler_Node        *victim_node;
  Thread_Control        *victim_user;
  Thread_Control        *victim_owner;
  Thread_Control        *idle;

  self = _Scheduler_SMP_Get_self( context );
  chain_node = _Chain_First( &self->Scheduled );

  do {
    _Assert( chain_node != _Chain_Immutable_tail( &self->Scheduled ) );
    victim_node = (Scheduler_Node *) chain_node;
    victim_user = _Scheduler_Node_get_user( victim_node );
    chain_node = _Chain_Next( chain_node );
  } while ( _Thread_Get_CPU( victim_user ) != cpu );

  ( *extract_from_scheduled )( &self->Base, victim_node );
  victim_owner = _Scheduler_Node_get_owner( victim_node );

  if ( !victim_owner->is_idle ) {
    Thread_Control  *victim_idle;
    Scheduler_Node  *idle_node;
    Priority_Control insert_priority;

    victim_idle = _Scheduler_Release_idle_thread_if_necessary(
      victim_node,
      release_idle_node,
      &self->Base
    );
    idle_node = ( *get_idle_node )( &self->Base );
    idle = _Scheduler_Node_get_owner( idle_node );
    _Scheduler_SMP_Preempt(
      &self->Base,
      idle_node,
      victim_node,
      victim_idle,
      _Scheduler_SMP_Allocate_processor_exact
    );

    _Assert( !_Chain_Is_empty( &self->Scheduled ) );
    insert_priority = _Scheduler_SMP_Node_priority( victim_node );
    insert_priority = SCHEDULER_PRIORITY_APPEND( insert_priority );
    ( *enqueue )( &self->Base, victim_node, insert_priority );
  } else {
    _Assert( victim_owner == victim_user );
    _Assert( _Scheduler_Node_get_idle( victim_node ) == NULL );
    idle = victim_owner;
  }

  return idle;
}

/**
 * @brief Sets the affinity of the node.
 *
 * Also performs a reinsert into the queue the node is currently in.
 *
 * @param context The scheduler context instance.
 * @param thread The thread for the operation.
 * @param[in, out] node The node to set the affinity of.
 * @param arg The affinity for @a node.
 * @param set_affinity Function to set the affinity of a node.
 * @param extract_from_scheduled Function to extract a node from the set of
 *      scheduled nodes.
 * @param extract_from_ready Function to extract a node from the ready queue
 *      of the scheduler context.
 * @param get_highest_ready Function to get the highest ready node.
 * @param move_from_ready_to_scheduled Function to move a node from the set
 *      of ready nodes to the set of scheduled nodes.
 * @param enqueue Function to enqueue a node with a given priority.
 * @param allocate_processor Function to allocate a processor to a node
 *      based on the rules of the scheduler.
 */
static inline void _Scheduler_SMP_Set_affinity(
  Scheduler_Context               *context,
  Thread_Control                  *thread,
  Scheduler_Node                  *node,
  void                            *arg,
  Scheduler_SMP_Set_affinity       set_affinity,
  Scheduler_SMP_Extract            extract_from_scheduled,
  Scheduler_SMP_Extract            extract_from_ready,
  Scheduler_SMP_Get_highest_ready  get_highest_ready,
  Scheduler_SMP_Move               move_from_ready_to_scheduled,
  Scheduler_SMP_Enqueue            enqueue,
  Scheduler_SMP_Allocate_processor allocate_processor,
  Scheduler_Get_idle_node          get_idle_node,
  Scheduler_Release_idle_node      release_idle_node
)
{
  Scheduler_SMP_Node_state node_state;
  Priority_Control         insert_priority;

  node_state = _Scheduler_SMP_Node_state( node );
  insert_priority = _Scheduler_SMP_Node_priority( node );
  insert_priority = SCHEDULER_PRIORITY_APPEND( insert_priority );

  if ( node_state == SCHEDULER_SMP_NODE_SCHEDULED ) {
    ( *extract_from_scheduled )( context, node );
    _Scheduler_SMP_Preempt_and_schedule_highest_ready(
      context,
      node,
      extract_from_ready,
      get_highest_ready,
      move_from_ready_to_scheduled,
      allocate_processor,
      get_idle_node,
      release_idle_node
    );
    ( *set_affinity )( context, node, arg );
    ( *enqueue )( context, node, insert_priority );
  } else if ( node_state == SCHEDULER_SMP_NODE_READY ) {
    ( *extract_from_ready )( context, node );
    ( *set_affinity )( context, node, arg );
    ( *enqueue )( context, node, insert_priority );
  } else {
    _Assert( node_state == SCHEDULER_SMP_NODE_BLOCKED );
    ( *set_affinity )( context, node, arg );
  }
}

/** @} */

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _RTEMS_SCORE_SCHEDULERSMPIMPL_H */