summaryrefslogtreecommitdiff
path: root/common_smp/inc/tx_thread.h
blob: 7dc7801bead254266860f16068ae5d03fb9a08c4 (plain)
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
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** ThreadX Component                                                     */
/**                                                                       */
/**   Thread                                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/**************************************************************************/
/*                                                                        */
/*  COMPONENT DEFINITION                                   RELEASE        */
/*                                                                        */
/*    tx_thread.h                                        PORTABLE SMP     */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This file defines the ThreadX thread control component, including   */
/*    data types and external references.  It is assumed that tx_api.h    */
/*    and tx_port.h have already been included.                           */
/*                                                                        */
/**************************************************************************/

#ifndef TX_THREAD_H
#define TX_THREAD_H


/* Add include files needed for in-line macros.  */

#include "tx_initialize.h"


/* Define thread control specific data definitions.  */

#define TX_THREAD_ID                            ((ULONG) 0x54485244)
#define TX_THREAD_MAX_BYTE_VALUES               256
#define TX_THREAD_PRIORITY_GROUP_MASK           ((ULONG) 0xFF)
#define TX_THREAD_PRIORITY_GROUP_SIZE           8
#define TX_THREAD_EXECUTE_LOG_SIZE              ((UINT) 8)
#define TX_THREAD_SMP_PROTECT_WAIT_LIST_SIZE    (TX_THREAD_SMP_MAX_CORES + 1)


/* Define the default thread stack checking. This can be overridden by
   a particular port, which is necessary if the stack growth is from
   low address to high address (the default logic is for stacks that
   grow from high address to low address.  */

#ifndef TX_THREAD_STACK_CHECK
#define TX_THREAD_STACK_CHECK(thread_ptr)                                                                                       \
    {                                                                                                                           \
    TX_INTERRUPT_SAVE_AREA                                                                                                      \
        TX_DISABLE                                                                                                              \
        if (((thread_ptr)) && ((thread_ptr) -> tx_thread_id == TX_THREAD_ID))                                                   \
        {                                                                                                                       \
            if (((ULONG *) (thread_ptr) -> tx_thread_stack_ptr) < ((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr))      \
            {                                                                                                                   \
                (thread_ptr) -> tx_thread_stack_highest_ptr =  (thread_ptr) -> tx_thread_stack_ptr;                             \
            }                                                                                                                   \
            if ((*((ULONG *) (thread_ptr) -> tx_thread_stack_start) != TX_STACK_FILL) ||                                        \
                (*((ULONG *) (((UCHAR *) (thread_ptr) -> tx_thread_stack_end) + 1)) != TX_STACK_FILL) ||                        \
                (((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) < ((ULONG *) (thread_ptr) -> tx_thread_stack_start)))  \
            {                                                                                                                   \
                TX_RESTORE                                                                                                      \
                _tx_thread_stack_error_handler((thread_ptr));                                                                   \
                TX_DISABLE                                                                                                      \
            }                                                                                                                   \
            if (*(((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) - 1) != TX_STACK_FILL)                                \
            {                                                                                                                   \
                TX_RESTORE                                                                                                      \
                _tx_thread_stack_analyze((thread_ptr));                                                                         \
                TX_DISABLE                                                                                                      \
            }                                                                                                                   \
        }                                                                                                                       \
        TX_RESTORE                                                                                                              \
    }
#endif


/* Define default post thread delete macro to whitespace, if it hasn't been defined previously (typically in tx_port.h).  */

#ifndef TX_THREAD_DELETE_PORT_COMPLETION
#define TX_THREAD_DELETE_PORT_COMPLETION(t)
#endif


/* Define default post thread reset macro to whitespace, if it hasn't been defined previously (typically in tx_port.h).  */

#ifndef TX_THREAD_RESET_PORT_COMPLETION
#define TX_THREAD_RESET_PORT_COMPLETION(t)
#endif


/* Define the thread create internal extension macro to whitespace, if it hasn't been defined previously (typically in tx_port.h).  */

#ifndef TX_THREAD_CREATE_INTERNAL_EXTENSION
#define TX_THREAD_CREATE_INTERNAL_EXTENSION(t)
#endif


/* Define internal thread control function prototypes.  */

VOID        _tx_thread_initialize(VOID);
VOID        _tx_thread_schedule(VOID);
VOID        _tx_thread_shell_entry(VOID);
VOID        _tx_thread_stack_analyze(TX_THREAD *thread_ptr);
VOID        _tx_thread_stack_build(TX_THREAD *thread_ptr, VOID (*function_ptr)(VOID));
VOID        _tx_thread_stack_error(TX_THREAD *thread_ptr);
VOID        _tx_thread_stack_error_handler(TX_THREAD *thread_ptr);
VOID        _tx_thread_system_preempt_check(VOID);
VOID        _tx_thread_system_resume(TX_THREAD *thread_ptr);
VOID        _tx_thread_system_ni_resume(TX_THREAD *thread_ptr);
VOID        _tx_thread_system_return(VOID);
VOID        _tx_thread_system_suspend(TX_THREAD *thread_ptr);
VOID        _tx_thread_system_ni_suspend(TX_THREAD *thread_ptr, ULONG timeout);
VOID        _tx_thread_time_slice(VOID);
VOID        _tx_thread_timeout(ULONG timeout_input);


/* Define all internal SMP prototypes.  */

void        _tx_thread_smp_current_state_set(ULONG new_state);
UINT        _tx_thread_smp_find_next_priority(UINT priority);
void        _tx_thread_smp_high_level_initialize(void);
void        _tx_thread_smp_rebalance_execute_list(UINT core_index);


/* Define all internal ThreadX SMP low-level assembly routines.   */

VOID        _tx_thread_smp_core_wait(void);
void        _tx_thread_smp_initialize_wait(void);
void        _tx_thread_smp_low_level_initialize(UINT number_of_cores);
void        _tx_thread_smp_core_preempt(UINT core);


/* Thread control component external data declarations follow.  */

#define THREAD_DECLARE extern


/* Define the pointer that contains the system stack pointer.  This is
   utilized when control returns from a thread to the system to reset the
   current stack.  This is setup in the low-level initialization function. */

THREAD_DECLARE  VOID *                      _tx_thread_system_stack_ptr[TX_THREAD_SMP_MAX_CORES];


/* Define the current thread pointer.  This variable points to the currently
   executing thread.  If this variable is NULL, no thread is executing.  */

THREAD_DECLARE  TX_THREAD *                 _tx_thread_current_ptr[TX_THREAD_SMP_MAX_CORES];


/* Define the variable that holds the next thread to execute.  It is important
   to remember that this is not necessarily equal to the current thread
   pointer.  */

THREAD_DECLARE  TX_THREAD *                 _tx_thread_execute_ptr[TX_THREAD_SMP_MAX_CORES];


/* Define the ThreadX SMP scheduling and mapping data structures.  */

THREAD_DECLARE  TX_THREAD *                 _tx_thread_smp_schedule_list[TX_THREAD_SMP_MAX_CORES];
THREAD_DECLARE  ULONG                       _tx_thread_smp_reschedule_pending;
THREAD_DECLARE  TX_THREAD_SMP_PROTECT       _tx_thread_smp_protection;
THREAD_DECLARE  volatile ULONG              _tx_thread_smp_release_cores_flag;
THREAD_DECLARE  ULONG                       _tx_thread_smp_system_error;
THREAD_DECLARE  ULONG                       _tx_thread_smp_inter_core_interrupts[TX_THREAD_SMP_MAX_CORES];

THREAD_DECLARE  ULONG                       _tx_thread_smp_protect_wait_list_size;
THREAD_DECLARE  ULONG                       _tx_thread_smp_protect_wait_list[TX_THREAD_SMP_PROTECT_WAIT_LIST_SIZE];
THREAD_DECLARE  ULONG                       _tx_thread_smp_protect_wait_counts[TX_THREAD_SMP_MAX_CORES];
THREAD_DECLARE  ULONG                       _tx_thread_smp_protect_wait_list_lock_protect_in_force;
THREAD_DECLARE  ULONG                       _tx_thread_smp_protect_wait_list_tail;
THREAD_DECLARE  ULONG                       _tx_thread_smp_protect_wait_list_head;


/* Define logic for conditional dynamic maximum number of cores.  */

#ifdef TX_THREAD_SMP_DYNAMIC_CORE_MAX

THREAD_DECLARE  ULONG                       _tx_thread_smp_max_cores;
THREAD_DECLARE  ULONG                       _tx_thread_smp_detected_cores;

#endif



/* Define the head pointer of the created thread list.  */

THREAD_DECLARE  TX_THREAD *                 _tx_thread_created_ptr;


/* Define the variable that holds the number of created threads. */

THREAD_DECLARE  ULONG                       _tx_thread_created_count;


/* Define the current state variable.  When this value is 0, a thread
   is executing or the system is idle.  Other values indicate that
   interrupt or initialization processing is active.  This variable is
   initialized to TX_INITIALIZE_IN_PROGRESS to indicate initialization is
   active.  */

THREAD_DECLARE  volatile ULONG              _tx_thread_system_state[TX_THREAD_SMP_MAX_CORES];


/* Determine if we need to remap system state to a function call.  */

#ifndef TX_THREAD_SMP_SOURCE_CODE


/* Yes, remap system state to a function call so we can get the system state for the current core.  */

#define _tx_thread_system_state             _tx_thread_smp_current_state_get()


/* Yes, remap get current thread to a function call so we can get the current thread for the current core.  */

#define _tx_thread_current_ptr              _tx_thread_smp_current_thread_get()

#endif


/* Define the 32-bit priority bit-maps. There is one priority bit map for each
   32 priority levels supported. If only 32 priorities are supported there is
   only one bit map. Each bit within a priority bit map represents that one
   or more threads at the associated thread priority are ready.  */

THREAD_DECLARE  ULONG                       _tx_thread_priority_maps[TX_MAX_PRIORITIES/32];


/* Define the priority map active bit map that specifies which of the previously
   defined priority maps have something set. This is only necessary if more than
   32 priorities are supported.  */

#if TX_MAX_PRIORITIES > 32
THREAD_DECLARE  ULONG                       _tx_thread_priority_map_active;
#endif


#ifndef TX_DISABLE_PREEMPTION_THRESHOLD

/* Define the 32-bit preempt priority bit maps.  There is one preempt bit map
   for each 32 priority levels supported. If only 32 priorities are supported
   there is only one bit map. Each set set bit corresponds to a preempted priority
   level that had preemption-threshold active to protect against preemption of a
   range of relatively higher priority threads.  */

THREAD_DECLARE  ULONG                       _tx_thread_preempted_maps[TX_MAX_PRIORITIES/32];


/* Define the preempt map active bit map that specifies which of the previously
   defined preempt maps have something set. This is only necessary if more than
   32 priorities are supported.  */

#if TX_MAX_PRIORITIES > 32
THREAD_DECLARE  ULONG                       _tx_thread_preempted_map_active;
#endif


/* Define the array that contains the thread at each priority level that was scheduled with
   preemption-threshold enabled. This will be useful when returning from a nested
   preemption-threshold condition.  */

THREAD_DECLARE  TX_THREAD                   *_tx_thread_preemption_threshold_list[TX_MAX_PRIORITIES];


#endif


/* Define the last thread scheduled with preemption-threshold. When preemption-threshold is
   disabled, a thread with preemption-threshold set disables all other threads from running.
   Effectively, its preemption-threshold is 0.  */

THREAD_DECLARE  TX_THREAD                   *_tx_thread_preemption__threshold_scheduled;


/* Define the array of thread pointers.  Each entry represents the threads that
   are ready at that priority group.  For example, index 10 in this array
   represents the first thread ready at priority 10.  If this entry is NULL,
   no threads are ready at that priority.  */

THREAD_DECLARE  TX_THREAD *                 _tx_thread_priority_list[TX_MAX_PRIORITIES];


/* Define the global preempt disable variable.  If this is non-zero, preemption is
   disabled.  It is used internally by ThreadX to prevent preemption of a thread in
   the middle of a service that is resuming or suspending another thread.  */

THREAD_DECLARE  volatile UINT               _tx_thread_preempt_disable;


/* Define the global function pointer for mutex cleanup on thread completion or
   termination. This pointer is setup during mutex initialization.  */

THREAD_DECLARE  VOID            (*_tx_thread_mutex_release)(TX_THREAD *thread_ptr);


/* Define the global build options variable.  This contains a bit map representing
   how the ThreadX library was built. The following are the bit field definitions:

                    Bit(s)                   Meaning

                    31                  Reserved
                    30                  TX_NOT_INTERRUPTABLE defined
                    29-24               Priority groups 1  -> 32 priorities
                                                        2  -> 64 priorities
                                                        3  -> 96 priorities

                                                        ...

                                                        32 -> 1024 priorities
                    23                  TX_TIMER_PROCESS_IN_ISR defined
                    22                  TX_REACTIVATE_INLINE defined
                    21                  TX_DISABLE_STACK_FILLING defined
                    20                  TX_ENABLE_STACK_CHECKING defined
                    19                  TX_DISABLE_PREEMPTION_THRESHOLD defined
                    18                  TX_DISABLE_REDUNDANT_CLEARING defined
                    17                  TX_DISABLE_NOTIFY_CALLBACKS defined
                    16                  TX_BLOCK_POOL_ENABLE_PERFORMANCE_INFO defined
                    15                  TX_BYTE_POOL_ENABLE_PERFORMANCE_INFO defined
                    14                  TX_EVENT_FLAGS_ENABLE_PERFORMANCE_INFO defined
                    13                  TX_MUTEX_ENABLE_PERFORMANCE_INFO defined
                    12                  TX_QUEUE_ENABLE_PERFORMANCE_INFO defined
                    11                  TX_SEMAPHORE_ENABLE_PERFORMANCE_INFO defined
                    10                  TX_THREAD_ENABLE_PERFORMANCE_INFO defined
                    9                   TX_TIMER_ENABLE_PERFORMANCE_INFO defined
                    8                   TX_ENABLE_EVENT_TRACE | TX_ENABLE_EVENT_LOGGING defined
                    7                   Reserved
                    6                   Reserved
                    5                   Reserved
                    4                   Reserved
                    3                   Reserved
                    2                   Reserved
                    1                   64-bit FPU Enabled
                    0                   Reserved  */

THREAD_DECLARE  ULONG                       _tx_build_options;


#ifdef TX_ENABLE_STACK_CHECKING

/* Define the global function pointer for stack error handling. If a stack error is
   detected and the application has registered a stack error handler, it will be
   called via this function pointer.  */

THREAD_DECLARE  VOID                        (*_tx_thread_application_stack_error_handler)(TX_THREAD *thread_ptr);

#endif

#ifdef TX_THREAD_ENABLE_PERFORMANCE_INFO

/* Define the total number of thread resumptions. Each time a thread enters the
   ready state this variable is incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_resume_count;


/* Define the total number of thread suspensions. Each time a thread enters a
   suspended state this variable is incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_suspend_count;


/* Define the total number of solicited thread preemptions. Each time a thread is
   preempted by directly calling a ThreadX service, this variable is incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_solicited_preemption_count;


/* Define the total number of interrupt thread preemptions. Each time a thread is
   preempted as a result of an ISR calling a ThreadX service, this variable is
   incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_interrupt_preemption_count;


/* Define the total number of priority inversions. Each time a thread is blocked by
   a mutex owned by a lower-priority thread, this variable is incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_priority_inversion_count;


/* Define the total number of time-slices.  Each time a time-slice operation is
   actually performed (another thread is setup for running) this variable is
   incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_time_slice_count;


/* Define the total number of thread relinquish operations.  Each time a thread
   relinquish operation is actually performed (another thread is setup for running)
   this variable is incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_relinquish_count;


/* Define the total number of thread timeouts. Each time a thread has a
   timeout this variable is incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_timeout_count;


/* Define the total number of thread wait aborts. Each time a thread's suspension
   is lifted by the tx_thread_wait_abort call this variable is incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_wait_abort_count;


/* Define the total number of idle system thread returns. Each time a thread returns to
   an idle system (no other thread is ready to run) this variable is incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_idle_return_count;


/* Define the total number of non-idle system thread returns. Each time a thread returns to
   a non-idle system (another thread is ready to run) this variable is incremented.  */

THREAD_DECLARE  ULONG                       _tx_thread_performance_non_idle_return_count;

#endif


/* Define macros and helper functions.  */

/* Define the MOD32 bit set macro that is used to set/clear a priority bit within a specific
   priority group.  */

#if TX_MAX_PRIORITIES > 32
#define MAP_INDEX                               (map_index)
#ifndef TX_MOD32_BIT_SET
#define TX_MOD32_BIT_SET(a,b)                   (b) = (((ULONG) 1) << ((a)%((UINT) 32)));
#endif
#else
#define MAP_INDEX                               (0)
#ifndef TX_MOD32_BIT_SET
#define TX_MOD32_BIT_SET(a,b)                   (b) = (((ULONG) 1) << ((a)));
#endif
#endif


/* Define the DIV32 bit set macro that is used to set/clear a priority group bit and is
   only necessary when using priorities greater than 32.  */

#if TX_MAX_PRIORITIES > 32
#ifndef TX_DIV32_BIT_SET
#define TX_DIV32_BIT_SET(a,b)                   (b) = (((ULONG) 1) << ((a)/((UINT) 32)));
#endif
#endif


/* Define state change macro that can be used by run-mode debug agents to keep track of thread
   state changes. By default, it is mapped to white space.  */

#ifndef TX_THREAD_STATE_CHANGE
#define TX_THREAD_STATE_CHANGE(a, b)
#endif


/* Define the macro to set the current thread pointer. This is particularly useful in SMP
   versions of ThreadX to add additional processing.  The default implementation is to simply
   access the global current thread pointer directly.  */

#ifndef TX_THREAD_SET_CURRENT
#define TX_THREAD_SET_CURRENT(a)            TX_MEMSET(&_tx_thread_current_ptr[0], (a), sizeof(_tx_thread_current_ptr));
#endif


/* Define the get system state macro. By default, it is mapped to white space.  */

#ifndef TX_THREAD_GET_SYSTEM_STATE
#define TX_THREAD_GET_SYSTEM_STATE()        _tx_thread_smp_current_state_get()
#endif


/* Define the check for whether or not to call the _tx_thread_system_return function.  A non-zero value
   indicates that _tx_thread_system_return should not be called.  */

#ifndef TX_THREAD_SYSTEM_RETURN_CHECK
#define TX_THREAD_SYSTEM_RETURN_CHECK(c)    (c) = (ULONG) _tx_thread_preempt_disable; (c) = (c) | TX_THREAD_GET_SYSTEM_STATE();
#endif


/* Define the timeout setup macro used in _tx_thread_create.  */

#ifndef TX_THREAD_CREATE_TIMEOUT_SETUP
#define TX_THREAD_CREATE_TIMEOUT_SETUP(t)    (t) -> tx_thread_timer.tx_timer_internal_timeout_function =  &(_tx_thread_timeout);                \
                                             (t) -> tx_thread_timer.tx_timer_internal_timeout_param =     TX_POINTER_TO_ULONG_CONVERT((t));
#endif


/* Define the thread timeout pointer setup macro used in _tx_thread_timeout.  */

#ifndef TX_THREAD_TIMEOUT_POINTER_SETUP
#define TX_THREAD_TIMEOUT_POINTER_SETUP(t)   (t) =  TX_ULONG_TO_THREAD_POINTER_CONVERT(timeout_input);
#endif


#ifdef TX_THREAD_SMP_SOURCE_CODE


/* Determine if the in-line capability has been disabled.  */

#ifndef TX_DISABLE_INLINE


/* Define the inline option, which is compiler specific. If not defined, it will be resolved as
   "inline".  */

#ifndef INLINE_DECLARE
#define INLINE_DECLARE  inline
#endif


/* Define the lowest bit set macro. Note, that this may be overridden
   by a port specific definition if there is supporting assembly language
   instructions in the architecture.  */

#ifndef TX_LOWEST_SET_BIT_CALCULATE

static INLINE_DECLARE UINT  _tx_thread_lowest_set_bit_calculate(ULONG map)
{
UINT    bit_set;

    if ((map & ((ULONG) 0x1)) != ((ULONG) 0))
    {
        bit_set = ((UINT) 0);
    }
    else
    {
        map =  map & (ULONG) ((~map) + ((ULONG) 1));
        if (map < ((ULONG) 0x100))
        {
            bit_set = ((UINT) 1);
        }
        else if (map < ((ULONG) 0x10000))
        {
            bit_set =  ((UINT) 9);
            map =  map >> ((UINT) 8);
        }
        else if (map < ((ULONG) 0x01000000))
        {
            bit_set = ((UINT) 17);
            map = map >> ((UINT) 16);
        }
        else
        {
            bit_set = ((UINT) 25);
            map = map >> ((UINT) 24);
        }
        if (map >= ((ULONG) 0x10))
        {
            map = map >> ((UINT) 4);
            bit_set = bit_set + ((UINT) 4);
        }
        if (map >= ((ULONG) 0x4))
        {
            map = map >> ((UINT) 2);
            bit_set = bit_set + ((UINT) 2);
        }
        bit_set = bit_set - (UINT) (map & (ULONG) 0x1);
    }

    return(bit_set);
}


#define TX_LOWEST_SET_BIT_CALCULATE(m, b)   (b) =  _tx_thread_lowest_set_bit_calculate((m));

#endif


/* Define the next priority macro. Note, that this may be overridden
   by a port specific definition.  */

#ifndef TX_NEXT_PRIORITY_FIND
#if TX_MAX_PRIORITIES > 32
static INLINE_DECLARE UINT _tx_thread_smp_next_priority_find(UINT priority)
{
ULONG           map_index;
ULONG           local_priority_map_active;
ULONG           local_priority_map;
ULONG           priority_bit;
ULONG           first_bit_set;
ULONG           found_priority;

    found_priority =  ((UINT) TX_MAX_PRIORITIES);
    if (priority < ((UINT) TX_MAX_PRIORITIES))
    {
        map_index =  priority/((UINT) 32);
        local_priority_map =  _tx_thread_priority_maps[map_index];
        priority_bit =        (((ULONG) 1) << (priority % ((UINT) 32)));
        local_priority_map =  local_priority_map & ~(priority_bit - ((UINT)1));
        if (local_priority_map != ((ULONG) 0))
        {
            TX_LOWEST_SET_BIT_CALCULATE(local_priority_map, first_bit_set)
            found_priority =  (map_index * ((UINT) 32)) + first_bit_set;
        }
        else
        {
            /* Move to next map index.  */
            map_index++;
            if (map_index < (((UINT) TX_MAX_PRIORITIES)/((UINT) 32)))
            {
                priority_bit =               (((ULONG) 1) << (map_index));
                local_priority_map_active =  _tx_thread_priority_map_active & ~(priority_bit - ((UINT) 1));
                if (local_priority_map_active != ((ULONG) 0))
                {
                    TX_LOWEST_SET_BIT_CALCULATE(local_priority_map_active, map_index)
                    local_priority_map =  _tx_thread_priority_maps[map_index];
                    TX_LOWEST_SET_BIT_CALCULATE(local_priority_map, first_bit_set)
                    found_priority =  (map_index * ((UINT) 32)) + first_bit_set;
                }
            }
        }
    }
    return(found_priority);
}
#else

static INLINE_DECLARE UINT _tx_thread_smp_next_priority_find(UINT priority)
{
UINT            first_bit_set;
ULONG           local_priority_map;
UINT            next_priority;

    local_priority_map =  _tx_thread_priority_maps[0];
    local_priority_map =  local_priority_map >> priority;
    next_priority =  priority;
    if (local_priority_map == ((ULONG) 0))
    {
        next_priority =  ((UINT) TX_MAX_PRIORITIES);
    }
    else
    {
        if (next_priority >= ((UINT) TX_MAX_PRIORITIES))
        {
            next_priority =  ((UINT) TX_MAX_PRIORITIES);
        }
        else
        {
            TX_LOWEST_SET_BIT_CALCULATE(local_priority_map, first_bit_set)
            next_priority =  priority + first_bit_set;
        }
    }

    return(next_priority);
}
#endif
#endif

static INLINE_DECLARE void  _tx_thread_smp_schedule_list_clear(void)
{
#if TX_THREAD_SMP_MAX_CORES > 6
UINT    i;
#endif


    /* Clear the schedule list.  */
    _tx_thread_smp_schedule_list[0] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 1
    _tx_thread_smp_schedule_list[1] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 2
    _tx_thread_smp_schedule_list[2] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 3
    _tx_thread_smp_schedule_list[3] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 4
    _tx_thread_smp_schedule_list[4] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 5
    _tx_thread_smp_schedule_list[5] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 6

    /* Loop to clear the remainder of the schedule list.  */
    i =  ((UINT) 6);

#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX

    while (i < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else

    while (i < _tx_thread_smp_max_cores)
#endif
    {
        /* Clear entry in schedule list.  */
        _tx_thread_smp_schedule_list[i] =  TX_NULL;

        /* Move to next index.  */
        i++;
    }
#endif
#endif
#endif
#endif
#endif
#endif
}

static INLINE_DECLARE VOID  _tx_thread_smp_execute_list_clear(void)
{
#if TX_THREAD_SMP_MAX_CORES > 6
UINT    j;
#endif

    /* Clear the execute list.  */
    _tx_thread_execute_ptr[0] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 1
    _tx_thread_execute_ptr[1] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 2
    _tx_thread_execute_ptr[2] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 3
    _tx_thread_execute_ptr[3] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 4
    _tx_thread_execute_ptr[4] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 5
    _tx_thread_execute_ptr[5] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 6

    /* Loop to clear the remainder of the execute list.  */
    j =  ((UINT) 6);

#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX

    while (j < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else

    while (j < _tx_thread_smp_max_cores)
#endif
    {

        /* Clear entry in execute list.  */
        _tx_thread_execute_ptr[j] =  TX_NULL;

        /* Move to next index.  */
        j++;
    }
#endif
#endif
#endif
#endif
#endif
#endif
}


static INLINE_DECLARE VOID  _tx_thread_smp_schedule_list_setup(void)
{
#if TX_THREAD_SMP_MAX_CORES > 6
UINT    j;
#endif

    _tx_thread_smp_schedule_list[0] =  _tx_thread_execute_ptr[0];
#if TX_THREAD_SMP_MAX_CORES > 1
    _tx_thread_smp_schedule_list[1] =  _tx_thread_execute_ptr[1];
#if TX_THREAD_SMP_MAX_CORES > 2
    _tx_thread_smp_schedule_list[2] =  _tx_thread_execute_ptr[2];
#if TX_THREAD_SMP_MAX_CORES > 3
    _tx_thread_smp_schedule_list[3] =  _tx_thread_execute_ptr[3];
#if TX_THREAD_SMP_MAX_CORES > 4
    _tx_thread_smp_schedule_list[4] =  _tx_thread_execute_ptr[4];
#if TX_THREAD_SMP_MAX_CORES > 5
    _tx_thread_smp_schedule_list[5] =  _tx_thread_execute_ptr[5];
#if TX_THREAD_SMP_MAX_CORES > 6

    /* Loop to setup the remainder of the schedule list.  */
    j =  ((UINT) 6);

#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX
    while (j < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else

    while (j < _tx_thread_smp_max_cores)
#endif
    {

        /* Setup entry in schedule list.  */
        _tx_thread_smp_schedule_list[j] =  _tx_thread_execute_ptr[j];

        /* Move to next index.  */
        j++;
    }
#endif
#endif
#endif
#endif
#endif
#endif
}


#ifdef TX_THREAD_SMP_INTER_CORE_INTERRUPT
static INLINE_DECLARE VOID  _tx_thread_smp_core_interrupt(TX_THREAD *thread_ptr, UINT current_core, UINT target_core)
{

TX_THREAD   *current_thread;


    /* Make sure this is a different core, since there is no need to interrupt the current core for
       a scheduling change.  */
    if (current_core != target_core)
    {

        /* Yes, a different core is present.  */

        /* Pickup the currently executing thread.  */
        current_thread =  _tx_thread_current_ptr[target_core];

        /* Determine if they are the same.  */
        if ((current_thread != TX_NULL) && (thread_ptr != current_thread))
        {

            /* Not the same and not NULL... determine if the core is running at thread level.  */
            if (_tx_thread_system_state[target_core] < TX_INITIALIZE_IN_PROGRESS)
            {

                /* Preempt the mapped thread.  */
                _tx_thread_smp_core_preempt(target_core);
            }
        }
    }
}
#else

/* Define to whitespace.  */
#define _tx_thread_smp_core_interrupt(a,b,c)

#endif


#ifdef TX_THREAD_SMP_WAKEUP_LOGIC
static INLINE_DECLARE VOID  _tx_thread_smp_core_wakeup(UINT current_core, UINT target_core)
{

    /* Determine if the core specified is not the current core - no need to wakeup the
       current core.  */
    if (target_core != current_core)
    {

        /* Wakeup based on application's macro.  */
        TX_THREAD_SMP_WAKEUP(target_core);
    }
}
#else

/* Define to whitespace.  */
#define _tx_thread_smp_core_wakeup(a,b)

#endif


static INLINE_DECLARE VOID  _tx_thread_smp_execute_list_setup(UINT core_index)
{

TX_THREAD   *schedule_thread;
UINT        i;


    /* Loop to copy the schedule list into the execution list.  */
    i =  ((UINT) 0);
#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX

    while (i < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else

    while (i < _tx_thread_smp_max_cores)
#endif
    {

        /* Pickup the thread to schedule.  */
        schedule_thread =  _tx_thread_smp_schedule_list[i];

        /* Copy the schedule list into the execution list.  */
        _tx_thread_execute_ptr[i] =  schedule_thread;

        /* If necessary, interrupt the core with the new thread to schedule.  */
        _tx_thread_smp_core_interrupt(schedule_thread, core_index, i);

#ifdef TX_THREAD_SMP_WAKEUP_LOGIC

        /* Does this need to be waked up?  */
        if ((i != core_index) && (schedule_thread != TX_NULL))
        {

            /* Wakeup based on application's macro.  */
            TX_THREAD_SMP_WAKEUP(i);
        }
#endif
        /* Move to next index.  */
        i++;
    }
}


static INLINE_DECLARE ULONG  _tx_thread_smp_available_cores_get(void)
{

#if TX_THREAD_SMP_MAX_CORES > 6
UINT    j;
#endif
ULONG   available_cores;

    available_cores =  ((ULONG) 0);
    if (_tx_thread_execute_ptr[0] == TX_NULL)
    {
        available_cores =  ((ULONG) 1);
    }
#if TX_THREAD_SMP_MAX_CORES > 1
    if (_tx_thread_execute_ptr[1] == TX_NULL)
    {
        available_cores =  available_cores | ((ULONG) 2);
    }
#if TX_THREAD_SMP_MAX_CORES > 2
    if (_tx_thread_execute_ptr[2] == TX_NULL)
    {
        available_cores =  available_cores | ((ULONG) 4);
    }
#if TX_THREAD_SMP_MAX_CORES > 3
    if (_tx_thread_execute_ptr[3] == TX_NULL)
    {
        available_cores =  available_cores | ((ULONG) 8);
    }
#if TX_THREAD_SMP_MAX_CORES > 4
    if (_tx_thread_execute_ptr[4] == TX_NULL)
    {
        available_cores =  available_cores | ((ULONG) 0x10);
    }
#if TX_THREAD_SMP_MAX_CORES > 5
    if (_tx_thread_execute_ptr[5] == TX_NULL)
    {
        available_cores =  available_cores | ((ULONG) 0x20);
    }
#if TX_THREAD_SMP_MAX_CORES > 6

    /* Loop to setup the remainder of the schedule list.  */
    j =  ((UINT) 6);

#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX
    while (j < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else

    while (j < _tx_thread_smp_max_cores)
#endif
    {

        /* Determine if this core is available.  */
        if (_tx_thread_execute_ptr[j] == TX_NULL)
        {
            available_cores =  available_cores | (((ULONG) 1) << j);
        }

        /* Move to next core.  */
        j++;
    }
#endif
#endif
#endif
#endif
#endif
#endif
    return(available_cores);
}


static INLINE_DECLARE ULONG  _tx_thread_smp_possible_cores_get(void)
{

#if TX_THREAD_SMP_MAX_CORES > 6
UINT    j;
#endif
ULONG       possible_cores;
TX_THREAD   *thread_ptr;

    possible_cores =  ((ULONG) 0);
    thread_ptr =  _tx_thread_execute_ptr[0];
    if (thread_ptr != TX_NULL)
    {
        possible_cores =  thread_ptr -> tx_thread_smp_cores_allowed;
    }
#if TX_THREAD_SMP_MAX_CORES > 1
    thread_ptr =  _tx_thread_execute_ptr[1];
    if (thread_ptr != TX_NULL)
    {
        possible_cores =  possible_cores | thread_ptr -> tx_thread_smp_cores_allowed;
    }
#if TX_THREAD_SMP_MAX_CORES > 2
    thread_ptr =  _tx_thread_execute_ptr[2];
    if (thread_ptr != TX_NULL)
    {
        possible_cores =  possible_cores | thread_ptr -> tx_thread_smp_cores_allowed;
    }
#if TX_THREAD_SMP_MAX_CORES > 3
    thread_ptr =  _tx_thread_execute_ptr[3];
    if (thread_ptr != TX_NULL)
    {
        possible_cores =  possible_cores | thread_ptr -> tx_thread_smp_cores_allowed;
    }
#if TX_THREAD_SMP_MAX_CORES > 4
    thread_ptr =  _tx_thread_execute_ptr[4];
    if (thread_ptr != TX_NULL)
    {
        possible_cores =  possible_cores | thread_ptr -> tx_thread_smp_cores_allowed;
    }
#if TX_THREAD_SMP_MAX_CORES > 5
    thread_ptr =  _tx_thread_execute_ptr[5];
    if (thread_ptr != TX_NULL)
    {
        possible_cores =  possible_cores | thread_ptr -> tx_thread_smp_cores_allowed;
    }
#if TX_THREAD_SMP_MAX_CORES > 6

    /* Loop to setup the remainder of the schedule list.  */
    j =  ((UINT) 6);

#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX
    while (j < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else

    while (j < _tx_thread_smp_max_cores)
#endif
    {

        /* Determine if this core is available.  */
        thread_ptr =  _tx_thread_execute_ptr[j];
        if (thread_ptr != TX_NULL)
        {
            possible_cores =  possible_cores | thread_ptr -> tx_thread_smp_cores_allowed;
        }

        /* Move to next core.  */
        j++;
    }
#endif
#endif
#endif
#endif
#endif
#endif
    return(possible_cores);
}


static INLINE_DECLARE UINT  _tx_thread_smp_lowest_priority_get(void)
{

#if TX_THREAD_SMP_MAX_CORES > 6
UINT    j;
#endif
TX_THREAD   *thread_ptr;
UINT        lowest_priority;

    lowest_priority =  ((UINT) 0);
    thread_ptr =  _tx_thread_execute_ptr[0];
    if (thread_ptr != TX_NULL)
    {
        if (thread_ptr -> tx_thread_priority > lowest_priority)
        {
            lowest_priority =  thread_ptr -> tx_thread_priority;
        }
    }
#if TX_THREAD_SMP_MAX_CORES > 1
    thread_ptr =  _tx_thread_execute_ptr[1];
    if (thread_ptr != TX_NULL)
    {
        if (thread_ptr -> tx_thread_priority > lowest_priority)
        {
            lowest_priority =  thread_ptr -> tx_thread_priority;
        }
    }
#if TX_THREAD_SMP_MAX_CORES > 2
    thread_ptr =  _tx_thread_execute_ptr[2];
    if (thread_ptr != TX_NULL)
    {
        if (thread_ptr -> tx_thread_priority > lowest_priority)
        {
            lowest_priority =  thread_ptr -> tx_thread_priority;
        }
    }
#if TX_THREAD_SMP_MAX_CORES > 3
    thread_ptr =  _tx_thread_execute_ptr[3];
    if (thread_ptr != TX_NULL)
    {
        if (thread_ptr -> tx_thread_priority > lowest_priority)
        {
            lowest_priority =  thread_ptr -> tx_thread_priority;
        }
    }
#if TX_THREAD_SMP_MAX_CORES > 4
    thread_ptr =  _tx_thread_execute_ptr[4];
    if (thread_ptr != TX_NULL)
    {
        if (thread_ptr -> tx_thread_priority > lowest_priority)
        {
            lowest_priority =  thread_ptr -> tx_thread_priority;
        }
    }
#if TX_THREAD_SMP_MAX_CORES > 5
    thread_ptr =  _tx_thread_execute_ptr[5];
    if (thread_ptr != TX_NULL)
    {
        if (thread_ptr -> tx_thread_priority > lowest_priority)
        {
            lowest_priority =  thread_ptr -> tx_thread_priority;
        }
    }
#if TX_THREAD_SMP_MAX_CORES > 6

    /* Loop to setup the remainder of the schedule list.  */
    j =  ((UINT) 6);

#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX
    while (j < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else

    while (j < _tx_thread_smp_max_cores)
#endif
    {

        /* Determine if this core has a thread scheduled.  */
        thread_ptr =  _tx_thread_execute_ptr[j];
        if (thread_ptr != TX_NULL)
        {

            /* Is this the new lowest priority?  */
            if (thread_ptr -> tx_thread_priority > lowest_priority)
            {
                lowest_priority =  thread_ptr -> tx_thread_priority;
            }
        }

        /* Move to next core.  */
        j++;
    }
#endif
#endif
#endif
#endif
#endif
#endif
    return(lowest_priority);
}


static INLINE_DECLARE UINT  _tx_thread_smp_remap_solution_find(TX_THREAD *schedule_thread, ULONG available_cores, ULONG thread_possible_cores, ULONG test_possible_cores)
{

UINT            core;
UINT            previous_core;
ULONG           test_cores;
ULONG           last_thread_cores;
UINT            queue_first, queue_last;
UINT            core_queue[TX_THREAD_SMP_MAX_CORES-1];
TX_THREAD       *thread_ptr;
TX_THREAD       *last_thread;
TX_THREAD       *thread_remap_list[TX_THREAD_SMP_MAX_CORES];


    /* Clear the last thread cores in the search.  */
    last_thread_cores =  ((ULONG) 0);

    /* Set the last thread pointer to NULL.  */
    last_thread =  TX_NULL;

    /* Setup the core queue indices.  */
    queue_first =  ((UINT) 0);
    queue_last =   ((UINT) 0);

    /* Build a list of possible cores for this thread to execute on, starting
       with the previously mapped core.  */
    core =  schedule_thread -> tx_thread_smp_core_mapped;
    if ((thread_possible_cores & (((ULONG) 1) << core)) != ((ULONG) 0))
    {

        /* Remember this potential mapping.  */
        thread_remap_list[core] =   schedule_thread;
        core_queue[queue_last] =    core;

        /* Move to next slot.  */
        queue_last++;

        /* Clear this core.  */
        thread_possible_cores =  thread_possible_cores & ~(((ULONG) 1) << core);
    }

    /* Loop to add additional possible cores.  */
    while (thread_possible_cores != ((ULONG) 0))
    {

        /* Determine the first possible core.  */
        test_cores =  thread_possible_cores;
        TX_LOWEST_SET_BIT_CALCULATE(test_cores, core)

        /* Clear this core.  */
        thread_possible_cores =  thread_possible_cores & ~(((ULONG) 1) << core);

        /* Remember this potential mapping.  */
        thread_remap_list[core] =  schedule_thread;
        core_queue[queue_last] =   core;

        /* Move to next slot.  */
        queue_last++;
    }

    /* Loop to evaluate the potential thread mappings, against what is already mapped.  */
    do
    {

        /* Pickup the next entry.  */
        core = core_queue[queue_first];

        /* Move to next slot.  */
        queue_first++;

        /* Retrieve the thread from the current mapping.  */
        thread_ptr =  _tx_thread_smp_schedule_list[core];

        /* Determine if there is a thread currently mapped to this core.  */
        if (thread_ptr != TX_NULL)
        {

            /* Determine the cores available for this thread.  */
            thread_possible_cores =  thread_ptr -> tx_thread_smp_cores_allowed;
            thread_possible_cores =  test_possible_cores & thread_possible_cores;

            /* Are there any possible cores for this thread?  */
            if (thread_possible_cores != ((ULONG) 0))
            {

                /* Determine if there are cores available for this thread.  */
                if ((thread_possible_cores & available_cores) != ((ULONG) 0))
                {

                    /* Yes, remember the final thread and cores that are valid for this thread.  */
                    last_thread_cores =  thread_possible_cores & available_cores;
                    last_thread =        thread_ptr;

                    /* We are done - get out of the loop!  */
                    break;
                }
                else
                {

                    /* Remove cores that will be added to the list.  */
                    test_possible_cores =  test_possible_cores & ~(thread_possible_cores);

                    /* Loop to add this thread to the potential mapping list.  */
                    do
                    {

                        /* Calculate the core.  */
                        test_cores =  thread_possible_cores;
                        TX_LOWEST_SET_BIT_CALCULATE(test_cores, core)

                        /* Clear this core.  */
                        thread_possible_cores =  thread_possible_cores & ~(((ULONG) 1) << core);

                        /* Remember this thread for remapping.  */
                        thread_remap_list[core] =  thread_ptr;

                        /* Remember this core.  */
                        core_queue[queue_last] =  core;

                        /* Move to next slot.  */
                        queue_last++;

                    } while (thread_possible_cores != ((ULONG) 0));
                }
            }
        }
    } while (queue_first != queue_last);

    /* Was a remapping solution found?  */
    if (last_thread != TX_NULL)
    {

        /* Pickup the core of the last thread to remap.  */
        core =  last_thread -> tx_thread_smp_core_mapped;

        /* Pickup the thread from the remapping list.  */
        thread_ptr =  thread_remap_list[core];

        /* Loop until we arrive at the thread we have been trying to map.  */
        while (thread_ptr != schedule_thread)
        {

            /* Move this thread in the schedule list.  */
            _tx_thread_smp_schedule_list[core] =  thread_ptr;

            /* Remember the previous core.  */
            previous_core =  core;

            /* Pickup the core of thread to remap.  */
            core =  thread_ptr -> tx_thread_smp_core_mapped;

            /* Save the new core mapping for this thread.  */
            thread_ptr -> tx_thread_smp_core_mapped =  previous_core;

            /* Move the next thread.  */
            thread_ptr =  thread_remap_list[core];
        }

        /* Save the remaining thread in the updated schedule list.  */
        _tx_thread_smp_schedule_list[core] =  thread_ptr;

        /* Update this thread's core mapping.  */
        thread_ptr -> tx_thread_smp_core_mapped =  core;

        /* Finally, setup the last thread in the remapping solution.  */
        test_cores =  last_thread_cores;
        TX_LOWEST_SET_BIT_CALCULATE(test_cores, core)

        /* Setup the last thread.  */
        _tx_thread_smp_schedule_list[core] =     last_thread;

        /* Remember the core mapping for this thread.  */
        last_thread -> tx_thread_smp_core_mapped =  core;
    }
    else
    {

        /* Set core to the maximum value in order to signal a remapping solution was not found.  */
        core =  ((UINT) TX_THREAD_SMP_MAX_CORES);
    }

    /* Return core to the caller.  */
    return(core);
}


static INLINE_DECLARE ULONG  _tx_thread_smp_preemptable_threads_get(UINT priority, TX_THREAD *possible_preemption_list[TX_THREAD_SMP_MAX_CORES])
{

UINT        i, j, k;
TX_THREAD   *thread_ptr;
TX_THREAD   *next_thread;
TX_THREAD   *search_thread;
TX_THREAD   *list_head;
ULONG       possible_cores =  ((ULONG) 0);


    /* Clear the possible preemption list.  */
    possible_preemption_list[0] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 1
    possible_preemption_list[1] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 2
    possible_preemption_list[2] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 3
    possible_preemption_list[3] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 4
    possible_preemption_list[4] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 5
    possible_preemption_list[5] =  TX_NULL;
#if TX_THREAD_SMP_MAX_CORES > 6

    /* Loop to clear the remainder of the possible preemption list.  */
    j =  ((UINT) 6);

#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX

    while (j < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else

    while (j < _tx_thread_smp_max_cores)
#endif
    {

        /* Clear entry in possible preemption list.  */
        possible_preemption_list[j] =  TX_NULL;

        /* Move to next core.  */
        j++;
    }
#endif
#endif
#endif
#endif
#endif
#endif

    /* Loop to build a list of threads of less priority.  */
    i =  ((UINT) 0);
    j =  ((UINT) 0);
#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX
    while (i < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else

    while (i < _tx_thread_smp_max_cores)
#endif
    {

        /* Pickup the currently mapped thread.  */
        thread_ptr =  _tx_thread_execute_ptr[i];

        /* Is there a thread scheduled for this core?  */
        if (thread_ptr != TX_NULL)
        {

            /* Update the possible cores bit map.  */
            possible_cores =  possible_cores | thread_ptr -> tx_thread_smp_cores_allowed;

            /* Can this thread be preempted?  */
            if (priority < thread_ptr -> tx_thread_priority)
            {

                /* Thread that can be added to the preemption possible list.  */

                /* Yes, this scheduled thread is lower priority, so add it to the preemption possible list.  */
                possible_preemption_list[j] =  thread_ptr;

                /* Move to next entry in preemption possible list.  */
                j++;
            }
        }

        /* Move to next core.  */
        i++;
    }

    /* Check to see if there are more than 2 threads that can be preempted.  */
    if (j > ((UINT) 1))
    {

        /* Yes, loop through the preemption possible list and sort by priority.  */
        i =  ((UINT) 0);
        do
        {

            /* Pickup preemptable thread.  */
            thread_ptr =  possible_preemption_list[i];

            /* Initialize the search index.  */
            k =  i + ((UINT) 1);

            /* Loop to get the lowest priority thread at the front of the list.  */
            while (k < j)
            {

                /* Pickup the next thread to evaluate.  */
                next_thread =  possible_preemption_list[k];

                /* Is this thread lower priority?  */
                if (next_thread -> tx_thread_priority > thread_ptr -> tx_thread_priority)
                {

                    /* Yes, swap the threads.  */
                    possible_preemption_list[i] =  next_thread;
                    possible_preemption_list[k] =  thread_ptr;
                    thread_ptr =  next_thread;
                }
                else
                {

                    /* Compare the thread priorities.  */
                    if (next_thread -> tx_thread_priority == thread_ptr -> tx_thread_priority)
                    {

                        /* Equal priority threads...  see which is in the ready list first.  */
                        search_thread =   thread_ptr -> tx_thread_ready_next;

                        /* Pickup the list head.  */
                        list_head =  _tx_thread_priority_list[thread_ptr -> tx_thread_priority];

                        /* Now loop to see if the next thread is after the current thread preemption.  */
                        while (search_thread != list_head)
                        {

                            /* Have we found the next thread?  */
                            if (search_thread == next_thread)
                            {

                                /* Yes, swap the threads.  */
                                possible_preemption_list[i] =  next_thread;
                                possible_preemption_list[k] =  thread_ptr;
                                thread_ptr =  next_thread;
                                break;
                            }

                            /* Move to the next thread.  */
                            search_thread =  search_thread -> tx_thread_ready_next;
                        }
                    }

                    /* Move to examine the next possible preemptable thread.  */
                    k++;
                }
            }

            /* We have found the lowest priority thread to preempt, now find the next lowest.  */
            i++;
        }
        while (i < (j-((UINT) 1)));
    }

    /* Return the possible cores.  */
    return(possible_cores);
}

static INLINE_DECLARE VOID  _tx_thread_smp_simple_priority_change(TX_THREAD *thread_ptr, UINT new_priority)
{

UINT            priority;
ULONG           priority_bit;
TX_THREAD       *head_ptr;
TX_THREAD       *tail_ptr;
#if TX_MAX_PRIORITIES > 32
UINT            map_index;
#endif

    /* Pickup the priority.  */
    priority =  thread_ptr -> tx_thread_priority;

    /* Determine if there are other threads at this priority that are
       ready.  */
    if (thread_ptr -> tx_thread_ready_next != thread_ptr)
    {

        /* Yes, there are other threads at this priority ready.  */

        /* Just remove this thread from the priority list.  */
        (thread_ptr -> tx_thread_ready_next) -> tx_thread_ready_previous =    thread_ptr -> tx_thread_ready_previous;
        (thread_ptr -> tx_thread_ready_previous) -> tx_thread_ready_next =    thread_ptr -> tx_thread_ready_next;

        /* Determine if this is the head of the priority list.  */
        if (_tx_thread_priority_list[priority] == thread_ptr)
        {

            /* Update the head pointer of this priority list.  */
            _tx_thread_priority_list[priority] =  thread_ptr -> tx_thread_ready_next;
        }
    }
    else
    {

        /* This is the only thread at this priority ready to run.  Set the head
           pointer to NULL.  */
        _tx_thread_priority_list[priority] =    TX_NULL;

#if TX_MAX_PRIORITIES > 32

        /* Calculate the index into the bit map array.  */
        map_index =  priority/((UINT) 32);
#endif

        /* Clear this priority bit in the ready priority bit map.  */
        TX_MOD32_BIT_SET(priority, priority_bit)
        _tx_thread_priority_maps[MAP_INDEX] =  _tx_thread_priority_maps[MAP_INDEX] & (~(priority_bit));

#if TX_MAX_PRIORITIES > 32

        /* Determine if there are any other bits set in this priority map.  */
        if (_tx_thread_priority_maps[MAP_INDEX] == ((ULONG) 0))
        {

            /* No, clear the active bit to signify this priority map has nothing set.  */
            TX_DIV32_BIT_SET(priority, priority_bit)
            _tx_thread_priority_map_active =  _tx_thread_priority_map_active & (~(priority_bit));
        }
#endif
    }

    /* Determine if the actual thread priority should be setup, which is the
       case if the new priority is higher than the priority inheritance.  */
    if (new_priority < thread_ptr -> tx_thread_inherit_priority)
    {

        /* Change thread priority to the new user's priority.  */
        thread_ptr -> tx_thread_priority =           new_priority;
        thread_ptr -> tx_thread_preempt_threshold =  new_priority;
    }
    else
    {

        /* Change thread priority to the priority inheritance.  */
        thread_ptr -> tx_thread_priority =           thread_ptr -> tx_thread_inherit_priority;
        thread_ptr -> tx_thread_preempt_threshold =  thread_ptr -> tx_thread_inherit_priority;
    }

    /* Now, place the thread at the new priority level.  */

    /* Determine if there are other threads at this priority that are
       ready.  */
    head_ptr =  _tx_thread_priority_list[new_priority];
    if (head_ptr != TX_NULL)
    {

        /* Yes, there are other threads at this priority already ready.  */

        /* Just add this thread to the priority list.  */
        tail_ptr =                                 head_ptr -> tx_thread_ready_previous;
        tail_ptr -> tx_thread_ready_next =         thread_ptr;
        head_ptr -> tx_thread_ready_previous =     thread_ptr;
        thread_ptr -> tx_thread_ready_previous =   tail_ptr;
        thread_ptr -> tx_thread_ready_next =       head_ptr;
    }
    else
    {

        /* First thread at this priority ready.  Add to the front of the list.  */
        _tx_thread_priority_list[new_priority] =   thread_ptr;
        thread_ptr -> tx_thread_ready_next =       thread_ptr;
        thread_ptr -> tx_thread_ready_previous =   thread_ptr;

#if TX_MAX_PRIORITIES > 32

        /* Calculate the index into the bit map array.  */
        map_index =  new_priority/((UINT) 32);

        /* Set the active bit to remember that the priority map has something set.  */
        TX_DIV32_BIT_SET(new_priority, priority_bit)
        _tx_thread_priority_map_active =  _tx_thread_priority_map_active | priority_bit;
#endif

        /* Or in the thread's priority bit.  */
        TX_MOD32_BIT_SET(new_priority, priority_bit)
        _tx_thread_priority_maps[MAP_INDEX] =  _tx_thread_priority_maps[MAP_INDEX] | priority_bit;
    }
}
#else

/* In-line was disabled.  All of the above helper fuctions must be defined as actual functions.  */

UINT   _tx_thread_lowest_set_bit_calculate(ULONG map);
#define TX_LOWEST_SET_BIT_CALCULATE(m, b)   (b) =  _tx_thread_lowest_set_bit_calculate((m));

UINT   _tx_thread_smp_next_priority_find(UINT priority);
VOID   _tx_thread_smp_schedule_list_clear(void);
VOID   _tx_thread_smp_execute_list_clear(void);
VOID   _tx_thread_smp_schedule_list_setup(void);

#ifdef TX_THREAD_SMP_INTER_CORE_INTERRUPT
VOID   _tx_thread_smp_core_interrupt(TX_THREAD *thread_ptr, UINT current_core, UINT target_core);
#else
/* Define to whitespace.  */
#define _tx_thread_smp_core_interrupt(a,b,c)
#endif

#ifdef TX_THREAD_SMP_WAKEUP_LOGIC
VOID   _tx_thread_smp_core_wakeup(UINT current_core, UINT target_core);
#else
/* Define to whitespace.  */
#define _tx_thread_smp_core_wakeup(a,b)
#endif

VOID   _tx_thread_smp_execute_list_setup(UINT core_index);
ULONG  _tx_thread_smp_available_cores_get(void);
ULONG  _tx_thread_smp_possible_cores_get(void);
UINT   _tx_thread_smp_lowest_priority_get(void);
UINT   _tx_thread_smp_remap_solution_find(TX_THREAD *schedule_thread, ULONG available_cores, ULONG thread_possible_cores, ULONG test_possible_cores);
ULONG  _tx_thread_smp_preemptable_threads_get(UINT priority, TX_THREAD *possible_preemption_list[TX_THREAD_SMP_MAX_CORES]);
VOID   _tx_thread_smp_simple_priority_change(TX_THREAD *thread_ptr, UINT new_priority);

#endif


#endif

#endif