summaryrefslogtreecommitdiffstats
path: root/cpukit/score/include/rtems/score/threadimpl.h
blob: f212e239284dacf9023b9b5b0e91f2d1049e60d0 (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
/**
 * @file
 *
 * @brief Inlined Routines from the Thread Handler
 *
 * This file contains the macro implementation of the inlined
 * routines from the Thread handler.
 */

/*
 *  COPYRIGHT (c) 1989-2008.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  Copyright (c) 2014-2015 embedded brains GmbH.
 *
 *  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_THREADIMPL_H
#define _RTEMS_SCORE_THREADIMPL_H

#include <rtems/score/thread.h>
#include <rtems/score/assert.h>
#include <rtems/score/chainimpl.h>
#include <rtems/score/interr.h>
#include <rtems/score/isr.h>
#include <rtems/score/objectimpl.h>
#include <rtems/score/resourceimpl.h>
#include <rtems/score/statesimpl.h>
#include <rtems/score/sysstate.h>
#include <rtems/score/threadqimpl.h>
#include <rtems/score/todimpl.h>
#include <rtems/score/freechain.h>
#include <rtems/config.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @addtogroup ScoreThread
 */
/**@{**/

/**
 *  The following structure contains the information necessary to manage
 *  a thread which it is  waiting for a resource.
 */
#define THREAD_STATUS_PROXY_BLOCKING 0x1111111

/**
 *  Self for the GNU Ada Run-Time
 */
SCORE_EXTERN void *rtems_ada_self;

typedef struct {
  Objects_Information Objects;

  Freechain_Control Free_thread_queue_heads;
} Thread_Information;

/**
 *  The following defines the information control block used to
 *  manage this class of objects.
 */
SCORE_EXTERN Thread_Information _Thread_Internal_information;

/**
 *  The following points to the thread whose floating point
 *  context is currently loaded.
 */
#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
SCORE_EXTERN Thread_Control *_Thread_Allocated_fp;
#endif

#define THREAD_CHAIN_NODE_TO_THREAD( node ) \
  RTEMS_CONTAINER_OF( node, Thread_Control, Wait.Node.Chain )

#define THREAD_RBTREE_NODE_TO_THREAD( node ) \
  RTEMS_CONTAINER_OF( node, Thread_Control, Wait.Node.RBTree )

#if defined(RTEMS_SMP)
#define THREAD_RESOURCE_NODE_TO_THREAD( node ) \
  RTEMS_CONTAINER_OF( node, Thread_Control, Resource_node )
#endif

void _Thread_Initialize_information(
  Thread_Information  *information,
  Objects_APIs         the_api,
  uint16_t             the_class,
  uint32_t             maximum,
  bool                 is_string,
  uint32_t             maximum_name_length
#if defined(RTEMS_MULTIPROCESSING)
  ,
  bool                 supports_global
#endif
);

/**
 *  @brief Initialize thread handler.
 *
 *  This routine performs the initialization necessary for this handler.
 */
void _Thread_Handler_initialization(void);

/**
 *  @brief Create idle thread.
 *
 *  This routine creates the idle thread.
 *
 *  @warning No thread should be created before this one.
 */
void _Thread_Create_idle(void);

/**
 *  @brief Start thread multitasking.
 *
 *  This routine initiates multitasking.  It is invoked only as
 *  part of initialization and its invocation is the last act of
 *  the non-multitasking part of the system initialization.
 */
void _Thread_Start_multitasking( void ) RTEMS_NO_RETURN;

/**
 *  @brief Allocate the requested stack space for the thread.
 *
 *  Allocate the requested stack space for the thread.
 *  Set the Start.stack field to the address of the stack.
 *
 *  @param[in] the_thread is the thread where the stack space is requested
 *  @param[in] stack_size is the stack space is requested
 *
 *  @retval actual size allocated after any adjustment
 *  @retval zero if the allocation failed
 */
size_t _Thread_Stack_Allocate(
  Thread_Control *the_thread,
  size_t          stack_size
);

/**
 *  @brief Deallocate thread stack.
 *
 *  Deallocate the Thread's stack.
 */
void _Thread_Stack_Free(
  Thread_Control *the_thread
);

/**
 *  @brief Initialize thread.
 *
 *  This routine initializes the specified the thread.  It allocates
 *  all memory associated with this thread.  It completes by adding
 *  the thread to the local object table so operations on this
 *  thread id are allowed.
 *
 *  @note If stack_area is NULL, it is allocated from the workspace.
 *
 *  @note If the stack is allocated from the workspace, then it is
 *        guaranteed to be of at least minimum size.
 */
bool _Thread_Initialize(
  Thread_Information                   *information,
  Thread_Control                       *the_thread,
  const struct Scheduler_Control       *scheduler,
  void                                 *stack_area,
  size_t                                stack_size,
  bool                                  is_fp,
  Priority_Control                      priority,
  bool                                  is_preemptible,
  Thread_CPU_budget_algorithms          budget_algorithm,
  Thread_CPU_budget_algorithm_callout   budget_callout,
  uint32_t                              isr_level,
  Objects_Name                          name
);

/**
 *  @brief Initializes thread and executes it.
 *
 *  This routine initializes the executable information for a thread
 *  and makes it ready to execute.  After this routine executes, the
 *  thread competes with all other threads for CPU time.
 *
 *  @param the_thread The thread to be started.
 *  @param entry The thread entry information.
 *  @param[in,out] cpu The processor if used to start an idle thread
 *  during system initialization.  Must be set to @c NULL to start a normal
 *  thread.
 */
bool _Thread_Start(
  Thread_Control                 *the_thread,
  const Thread_Entry_information *entry,
  Per_CPU_Control                *cpu
);

bool _Thread_Restart(
  Thread_Control                 *the_thread,
  Thread_Control                 *executing,
  const Thread_Entry_information *entry
);

void _Thread_Yield( Thread_Control *executing );

bool _Thread_Set_life_protection( bool protect );

void _Thread_Life_action_handler(
  Thread_Control  *executing,
  Thread_Action   *action,
  Per_CPU_Control *cpu,
  ISR_Level        level
);

/**
 * @brief Kills all zombie threads in the system.
 *
 * Threads change into the zombie state as the last step in the thread
 * termination sequence right before a context switch to the heir thread is
 * initiated.  Since the thread stack is still in use during this phase we have
 * to postpone the thread stack reclamation until this point.  On SMP
 * configurations we may have to busy wait for context switch completion here.
 */
void _Thread_Kill_zombies( void );

/**
 * @brief Closes the thread.
 *
 * Closes the thread object and starts the thread termination sequence.  In
 * case the executing thread is not terminated, then this function waits until
 * the terminating thread reached the zombie state.
 */
void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing );

/**
 * @brief Clears the specified thread state.
 *
 * In case the previous state is a non-ready state and the next state is the
 * ready state, then the thread is unblocked by the scheduler.
 *
 * @param[in] the_thread The thread.
 * @param[in] state The state to clear.  It must not be zero.
 *
 * @return The previous state.
 */
States_Control _Thread_Clear_state(
  Thread_Control *the_thread,
  States_Control  state
);

/**
 * @brief Sets the specified thread state.
 *
 * In case the previous state is the ready state, then the thread is blocked by
 * the scheduler.
 *
 * @param[in] the_thread The thread.
 * @param[in] state The state to set.  It must not be zero.
 *
 * @return The previous state.
 */
States_Control _Thread_Set_state(
  Thread_Control *the_thread,
  States_Control  state
);

/**
 * @brief Clears all thread states.
 *
 * In case the previous state is a non-ready state, then the thread is
 * unblocked by the scheduler.
 *
 * @param[in] the_thread The thread.
 */
RTEMS_INLINE_ROUTINE void _Thread_Ready(
  Thread_Control *the_thread
)
{
  _Thread_Clear_state( the_thread, STATES_ALL_SET );
}

/**
 *  @brief Initializes enviroment for a thread.
 *
 *  This routine initializes the context of @a the_thread to its
 *  appropriate starting state.
 *
 *  @param[in] the_thread is the pointer to the thread control block.
 */
void _Thread_Load_environment(
  Thread_Control *the_thread
);

void _Thread_Entry_adaptor_idle( Thread_Control *executing );

void _Thread_Entry_adaptor_numeric( Thread_Control *executing );

void _Thread_Entry_adaptor_pointer( Thread_Control *executing );

/**
 *  @brief Wrapper function for all threads.
 *
 *  This routine is the wrapper function for all threads.  It is
 *  the starting point for all threads.  The user provided thread
 *  entry point is invoked by this routine.  Operations
 *  which must be performed immediately before and after the user's
 *  thread executes are found here.
 *
 *  @note On entry, it is assumed all interrupts are blocked and that this
 *  routine needs to set the initial isr level.  This may or may not
 *  actually be needed by the context switch routine and as a result
 *  interrupts may already be at there proper level.  Either way,
 *  setting the initial isr level properly here is safe.
 */
void _Thread_Handler( void );

/**
 * @brief Executes the global constructors and then restarts itself as the
 * first initialization thread.
 *
 * The first initialization thread is the first RTEMS initialization task or
 * the first POSIX initialization thread in case no RTEMS initialization tasks
 * are present.
 */
void _Thread_Global_construction(
  Thread_Control                 *executing,
  const Thread_Entry_information *entry
) RTEMS_NO_RETURN;

/**
 *  @brief Ended the delay of a thread.
 *
 *  This routine is invoked when a thread must be unblocked at the
 *  end of a time based delay (i.e. wake after or wake when).
 *  It is called by the watchdog handler.
 *
 *  @param[in] id is the thread id
 *  @param[in] ignored is not used
 */
void _Thread_Delay_ended(
  Objects_Id  id,
  void       *ignored
);

/**
 * @brief Returns true if the left thread priority is less than the right
 * thread priority in the intuitive sense of priority and false otherwise.
 */
RTEMS_INLINE_ROUTINE bool _Thread_Priority_less_than(
  Priority_Control left,
  Priority_Control right
)
{
  return left > right;
}

/**
 * @brief Returns the highest priority of the left and right thread priorities
 * in the intuitive sense of priority.
 */
RTEMS_INLINE_ROUTINE Priority_Control _Thread_Priority_highest(
  Priority_Control left,
  Priority_Control right
)
{
  return _Thread_Priority_less_than( left, right ) ? right : left;
}

/**
 * @brief Filters a thread priority change.
 *
 * Called by _Thread_Change_priority() under the protection of the thread lock.
 *
 * @param[in] the_thread The thread.
 * @param[in, out] new_priority The new priority of the thread.  The filter may
 * alter this value.
 * @param[in] arg The argument passed to _Thread_Change_priority().
 *
 * @retval true Change the current priority.
 * @retval false Otherwise.
 */
typedef bool ( *Thread_Change_priority_filter )(
  Thread_Control   *the_thread,
  Priority_Control *new_priority,
  void             *arg
);

/**
 * @brief Changes the priority of a thread if allowed by the filter function.
 *
 * It changes current priority of the thread to the new priority in case the
 * filter function returns true.  In this case the scheduler is notified of the
 * priority change as well.
 *
 * @param[in] the_thread The thread.
 * @param[in] new_priority The new priority of the thread.
 * @param[in] arg The argument for the filter function.
 * @param[in] filter The filter function to determine if a priority change is
 * allowed and optionally perform other actions under the protection of the
 * thread lock simultaneously with the update of the current priority.
 * @param[in] prepend_it In case this is true, then the thread is prepended to
 * its priority group in its scheduler instance, otherwise it is appended.
 */
void _Thread_Change_priority(
  Thread_Control                *the_thread,
  Priority_Control               new_priority,
  void                          *arg,
  Thread_Change_priority_filter  filter,
  bool                           prepend_it
);

/**
 * @brief Raises the priority of a thread.
 *
 * It changes the current priority of the thread to the new priority if the new
 * priority is higher than the current priority.  In this case the thread is
 * appended to its new priority group in its scheduler instance.
 *
 * @param[in] the_thread The thread.
 * @param[in] new_priority The new priority of the thread.
 *
 * @see _Thread_Change_priority().
 */
void _Thread_Raise_priority(
  Thread_Control   *the_thread,
  Priority_Control  new_priority
);

/**
 * @brief Inherit the priority of a thread.
 *
 * It changes the current priority of the inheritor thread to the current priority
 * of the ancestor thread if it is higher than the current priority of the inheritor
 * thread.  In this case the inheritor thread is appended to its new priority group
 * in its scheduler instance.
 *
 * On SMP configurations, the priority is changed to PRIORITY_PSEUDO_ISR in
 * case the own schedulers of the inheritor and ancestor thread differ (priority
 * boosting).
 *
 * @param[in] inheritor The thread to inherit the priority.
 * @param[in] ancestor The thread to bequeath its priority to the inheritor
 *   thread.
 */
#if defined(RTEMS_SMP)
void _Thread_Inherit_priority(
  Thread_Control *inheritor,
  Thread_Control *ancestor
);
#else
RTEMS_INLINE_ROUTINE void _Thread_Inherit_priority(
  Thread_Control *inheritor,
  Thread_Control *ancestor
)
{
  _Thread_Raise_priority( inheritor, ancestor->current_priority );
}
#endif

/**
 * @brief Sets the current to the real priority of a thread.
 *
 * Sets the priority restore hint to false.
 */
void _Thread_Restore_priority( Thread_Control *the_thread );

/**
 * @brief Sets the priority of a thread.
 *
 * It sets the real priority of the thread.  In addition it changes the current
 * priority of the thread if the new priority is higher than the current
 * priority or the thread owns no resources.
 *
 * @param[in] the_thread The thread.
 * @param[in] new_priority The new priority of the thread.
 * @param[out] old_priority The old real priority of the thread.  This pointer
 * must not be @c NULL.
 * @param[in] prepend_it In case this is true, then the thread is prepended to
 * its priority group in its scheduler instance, otherwise it is appended.
 *
 * @see _Thread_Change_priority().
 */
void _Thread_Set_priority(
  Thread_Control   *the_thread,
  Priority_Control  new_priority,
  Priority_Control *old_priority,
  bool              prepend_it
);

/**
 *  @brief Maps thread Id to a TCB pointer.
 *
 *  This function maps thread IDs to thread control
 *  blocks.  If ID corresponds to a local thread, then it
 *  returns the_thread control pointer which maps to ID
 *  and @a location is set to OBJECTS_LOCAL.  If the thread ID is
 *  global and resides on a remote node, then location is set
 *  to OBJECTS_REMOTE, and the_thread is undefined.
 *  Otherwise, location is set to OBJECTS_ERROR and
 *  the_thread is undefined.
 *
 *  @param[in] id is the id of the thread.
 *  @param[in] location is the location of the block.
 *
 *  @note  The performance of many RTEMS services depends upon
 *         the quick execution of the "good object" path in this
 *         routine.  If there is a possibility of saving a few
 *         cycles off the execution time, this routine is worth
 *         further optimization attention.
 */
Thread_Control *_Thread_Get (
  Objects_Id         id,
  Objects_Locations *location
);

/**
 * @brief Gets a thread by its identifier.
 *
 * @see _Objects_Get_isr_disable().
 */
Thread_Control *_Thread_Get_interrupt_disable(
  Objects_Id         id,
  Objects_Locations *location,
  ISR_lock_Context  *lock_context
);

RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Get_CPU(
  const Thread_Control *thread
)
{
#if defined(RTEMS_SMP)
  return thread->Scheduler.cpu;
#else
  (void) thread;

  return _Per_CPU_Get();
#endif
}

RTEMS_INLINE_ROUTINE void _Thread_Set_CPU(
  Thread_Control *thread,
  Per_CPU_Control *cpu
)
{
#if defined(RTEMS_SMP)
  thread->Scheduler.cpu = cpu;
#else
  (void) thread;
  (void) cpu;
#endif
}

/**
 * This function returns true if the_thread is the currently executing
 * thread, and false otherwise.
 */

RTEMS_INLINE_ROUTINE bool _Thread_Is_executing (
  const Thread_Control *the_thread
)
{
  return ( the_thread == _Thread_Executing );
}

#if defined(RTEMS_SMP)
/**
 * @brief Returns @a true in case the thread executes currently on some
 * processor in the system, otherwise @a false.
 *
 * Do not confuse this with _Thread_Is_executing() which checks only the
 * current processor.
 */
RTEMS_INLINE_ROUTINE bool _Thread_Is_executing_on_a_processor(
  const Thread_Control *the_thread
)
{
  return _CPU_Context_Get_is_executing( &the_thread->Registers );
}
#endif

/**
 * @brief Returns @a true and sets time_of_context_switch to the
 * time of the last context switch when the thread is currently executing
 * in the system, otherwise @a false.
 */
RTEMS_INLINE_ROUTINE bool _Thread_Get_time_of_last_context_switch(
  Thread_Control    *the_thread,
  Timestamp_Control *time_of_context_switch
)
{
  bool retval = false;

  _Thread_Disable_dispatch();
  #ifndef RTEMS_SMP
    if ( _Thread_Executing->Object.id == the_thread->Object.id ) {
      *time_of_context_switch = _Thread_Time_of_last_context_switch;
      retval = true;
    }
  #else
    if ( _Thread_Is_executing_on_a_processor( the_thread ) ) {
      *time_of_context_switch =
        _Thread_Get_CPU( the_thread )->time_of_last_context_switch;
      retval = true;
    }
  #endif
  _Thread_Enable_dispatch();
  return retval;
}


/**
 * This function returns true if the_thread is the heir
 * thread, and false otherwise.
 */

RTEMS_INLINE_ROUTINE bool _Thread_Is_heir (
  const Thread_Control *the_thread
)
{
  return ( the_thread == _Thread_Heir );
}

/**
 * This routine clears any blocking state for the_thread.  It performs
 * any necessary scheduling operations including the selection of
 * a new heir thread.
 */

RTEMS_INLINE_ROUTINE void _Thread_Unblock (
  Thread_Control *the_thread
)
{
  _Thread_Clear_state( the_thread, STATES_BLOCKED );
}

/**
 * This routine resets the current context of the calling thread
 * to that of its initial state.
 */

RTEMS_INLINE_ROUTINE void _Thread_Restart_self( Thread_Control *executing )
{
#if defined(RTEMS_SMP)
  ISR_Level level;

  _Giant_Release( _Per_CPU_Get() );

  _ISR_Disable_without_giant( level );
  ( void ) level;
#endif

#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
  if ( executing->fp_context != NULL )
    _Context_Restore_fp( &executing->fp_context );
#endif

  _CPU_Context_Restart_self( &executing->Registers );
}

/**
 * This function returns true if the floating point context of
 * the_thread is currently loaded in the floating point unit, and
 * false otherwise.
 */

#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
RTEMS_INLINE_ROUTINE bool _Thread_Is_allocated_fp (
  const Thread_Control *the_thread
)
{
  return ( the_thread == _Thread_Allocated_fp );
}
#endif

/*
 *  If the CPU has hardware floating point, then we must address saving
 *  and restoring it as part of the context switch.
 *
 *  The second conditional compilation section selects the algorithm used
 *  to context switch between floating point tasks.  The deferred algorithm
 *  can be significantly better in a system with few floating point tasks
 *  because it reduces the total number of save and restore FP context
 *  operations.  However, this algorithm can not be used on all CPUs due
 *  to unpredictable use of FP registers by some compilers for integer
 *  operations.
 */

RTEMS_INLINE_ROUTINE void _Thread_Save_fp( Thread_Control *executing )
{
#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
#if ( CPU_USE_DEFERRED_FP_SWITCH != TRUE )
  if ( executing->fp_context != NULL )
    _Context_Save_fp( &executing->fp_context );
#endif
#endif
}

RTEMS_INLINE_ROUTINE void _Thread_Restore_fp( Thread_Control *executing )
{
#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
#if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE )
  if ( (executing->fp_context != NULL) &&
       !_Thread_Is_allocated_fp( executing ) ) {
    if ( _Thread_Allocated_fp != NULL )
      _Context_Save_fp( &_Thread_Allocated_fp->fp_context );
    _Context_Restore_fp( &executing->fp_context );
    _Thread_Allocated_fp = executing;
  }
#else
  if ( executing->fp_context != NULL )
    _Context_Restore_fp( &executing->fp_context );
#endif
#endif
}

/**
 * This routine is invoked when the currently loaded floating
 * point context is now longer associated with an active thread.
 */

#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
RTEMS_INLINE_ROUTINE void _Thread_Deallocate_fp( void )
{
  _Thread_Allocated_fp = NULL;
}
#endif

/**
 * This function returns true if dispatching is disabled, and false
 * otherwise.
 */

RTEMS_INLINE_ROUTINE bool _Thread_Is_context_switch_necessary( void )
{
  return ( _Thread_Dispatch_necessary );
}

/**
 * This function returns true if the_thread is NULL and false otherwise.
 */

RTEMS_INLINE_ROUTINE bool _Thread_Is_null (
  const Thread_Control *the_thread
)
{
  return ( the_thread == NULL );
}

/**
 * @brief Is proxy blocking.
 *
 * status which indicates that a proxy is blocking, and false otherwise.
 */
RTEMS_INLINE_ROUTINE bool _Thread_Is_proxy_blocking (
  uint32_t   code
)
{
  return (code == THREAD_STATUS_PROXY_BLOCKING);
}

RTEMS_INLINE_ROUTINE uint32_t _Thread_Get_maximum_internal_threads(void)
{
  /* Idle threads */
  uint32_t maximum_internal_threads =
    rtems_configuration_get_maximum_processors();

  /* MPCI thread */
#if defined(RTEMS_MULTIPROCESSING)
  if ( _System_state_Is_multiprocessing ) {
    ++maximum_internal_threads;
  }
#endif

  return maximum_internal_threads;
}

RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Internal_allocate( void )
{
  return (Thread_Control *)
    _Objects_Allocate_unprotected( &_Thread_Internal_information.Objects );
}

/**
 * @brief Gets the heir of the processor and makes it executing.
 *
 * Must be called with interrupts disabled.  The thread dispatch necessary
 * indicator is cleared as a side-effect.
 *
 * @return The heir thread.
 *
 * @see _Thread_Dispatch(), _Thread_Start_multitasking() and
 * _Thread_Dispatch_update_heir().
 */
RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Get_heir_and_make_it_executing(
  Per_CPU_Control *cpu_self
)
{
  Thread_Control *heir;

  heir = cpu_self->heir;
  cpu_self->dispatch_necessary = false;
  cpu_self->executing = heir;

  return heir;
}

#if defined( RTEMS_SMP )
RTEMS_INLINE_ROUTINE void _Thread_Dispatch_update_heir(
  Per_CPU_Control *cpu_self,
  Per_CPU_Control *cpu_for_heir,
  Thread_Control  *heir
)
{
  cpu_for_heir->heir = heir;

  if ( cpu_for_heir == cpu_self ) {
    cpu_self->dispatch_necessary = true;
  } else {
    _Per_CPU_Send_interrupt( cpu_for_heir );
  }
}
#endif

RTEMS_INLINE_ROUTINE void _Thread_Update_cpu_time_used(
  Thread_Control *executing,
  Timestamp_Control *time_of_last_context_switch
)
{
  Timestamp_Control uptime;
  Timestamp_Control ran;

  _TOD_Get_uptime( &uptime );
  _Timestamp_Subtract(
    time_of_last_context_switch,
    &uptime,
    &ran
  );
  *time_of_last_context_switch = uptime;
  _Timestamp_Add_to( &executing->cpu_time_used, &ran );
}

RTEMS_INLINE_ROUTINE void _Thread_Action_control_initialize(
  Thread_Action_control *action_control
)
{
  _Chain_Initialize_empty( &action_control->Chain );
}

RTEMS_INLINE_ROUTINE void _Thread_Action_initialize(
  Thread_Action *action
)
{
  _Chain_Set_off_chain( &action->Node );
}

RTEMS_INLINE_ROUTINE Per_CPU_Control *
  _Thread_Action_ISR_disable_and_acquire_for_executing( ISR_Level *level )
{
  Per_CPU_Control *cpu;

  _ISR_Disable_without_giant( *level );
  cpu = _Per_CPU_Get();
  _Per_CPU_Acquire( cpu );

  return cpu;
}

RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Action_ISR_disable_and_acquire(
  Thread_Control *thread,
  ISR_Level      *level
)
{
  Per_CPU_Control *cpu;

  _ISR_Disable_without_giant( *level );
  cpu = _Thread_Get_CPU( thread );
  _Per_CPU_Acquire( cpu );

  return cpu;
}

RTEMS_INLINE_ROUTINE void _Thread_Action_release_and_ISR_enable(
  Per_CPU_Control *cpu,
  ISR_Level level
)
{
  _Per_CPU_Release_and_ISR_enable( cpu, level );
}

RTEMS_INLINE_ROUTINE void _Thread_Add_post_switch_action(
  Thread_Control        *thread,
  Thread_Action         *action,
  Thread_Action_handler  handler
)
{
  Per_CPU_Control *cpu_of_thread;
  ISR_Level        level;

  cpu_of_thread = _Thread_Action_ISR_disable_and_acquire( thread, &level );

  action->handler = handler;

#if defined(RTEMS_SMP)
  if ( _Per_CPU_Get() == cpu_of_thread ) {
    cpu_of_thread->dispatch_necessary = true;
  } else {
    _Per_CPU_Send_interrupt( cpu_of_thread );
  }
#else
  cpu_of_thread->dispatch_necessary = true;
#endif

  _Chain_Append_if_is_off_chain_unprotected(
    &thread->Post_switch_actions.Chain,
    &action->Node
  );

  _Thread_Action_release_and_ISR_enable( cpu_of_thread, level );
}

RTEMS_INLINE_ROUTINE bool _Thread_Is_life_restarting(
  Thread_Life_state life_state
)
{
  return ( life_state & THREAD_LIFE_RESTARTING ) != 0;
}

RTEMS_INLINE_ROUTINE bool _Thread_Is_life_terminating(
  Thread_Life_state life_state
)
{
  return ( life_state & THREAD_LIFE_TERMINATING ) != 0;
}

RTEMS_INLINE_ROUTINE bool _Thread_Is_life_protected(
  Thread_Life_state life_state
)
{
  return ( life_state & THREAD_LIFE_PROTECTED ) != 0;
}

RTEMS_INLINE_ROUTINE bool _Thread_Is_life_changing(
  Thread_Life_state life_state
)
{
  return ( life_state & THREAD_LIFE_RESTARTING_TERMINATING ) != 0;
}

/**
 * @brief Returns true if the thread owns resources, and false otherwise.
 *
 * Resources are accounted with the Thread_Control::resource_count resource
 * counter.  This counter is used by semaphore objects for example.
 *
 * In addition to the resource counter there is a resource dependency tree
 * available on SMP configurations.  In case this tree is non-empty, then the
 * thread owns resources.
 *
 * @param[in] the_thread The thread.
 */
RTEMS_INLINE_ROUTINE bool _Thread_Owns_resources(
  const Thread_Control *the_thread
)
{
  bool owns_resources = the_thread->resource_count != 0;

#if defined(RTEMS_SMP)
  owns_resources = owns_resources
    || _Resource_Node_owns_resources( &the_thread->Resource_node );
#endif

  return owns_resources;
}

/**
 * @brief Acquires the default thread lock inside a critical section
 * (interrupts disabled).
 *
 * @param[in] the_thread The thread.
 * @param[in] lock_context The lock context used for the corresponding lock
 * release.
 *
 * @see _Thread_Lock_release_default().
 */
RTEMS_INLINE_ROUTINE void _Thread_Lock_acquire_default_critical(
  Thread_Control   *the_thread,
  ISR_lock_Context *lock_context
)
{
  _Assert( _ISR_Get_level() != 0 );
#if defined(RTEMS_SMP)
  _SMP_ticket_lock_Acquire(
    &the_thread->Lock.Default,
    &_Thread_Executing->Lock.Stats,
    &lock_context->Lock_context.Stats_context
  );
#else
  (void) the_thread;
  (void) lock_context;
#endif
}

/**
 * @brief Acquires the default thread lock and returns the executing thread.
 *
 * @param[in] lock_context The lock context used for the corresponding lock
 * release.
 *
 * @return The executing thread.
 *
 * @see _Thread_Lock_release_default().
 */
RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Lock_acquire_default_for_executing(
  ISR_lock_Context *lock_context
)
{
  Thread_Control *executing;

  _ISR_lock_ISR_disable( lock_context );
  executing = _Thread_Executing;
  _Thread_Lock_acquire_default_critical( executing, lock_context );

  return executing;
}

/**
 * @brief Acquires the default thread lock.
 *
 * @param[in] the_thread The thread.
 * @param[in] lock_context The lock context used for the corresponding lock
 * release.
 *
 * @see _Thread_Lock_release_default().
 */
RTEMS_INLINE_ROUTINE void _Thread_Lock_acquire_default(
  Thread_Control   *the_thread,
  ISR_lock_Context *lock_context
)
{
  _ISR_lock_ISR_disable( lock_context );
  _Thread_Lock_acquire_default_critical( the_thread, lock_context );
}

/**
 * @brief Releases the thread lock inside a critical section (interrupts
 * disabled).
 *
 * The previous interrupt status is not restored.
 *
 * @param[in] lock The lock.
 * @param[in] lock_context The lock context used for the corresponding lock
 * acquire.
 */
RTEMS_INLINE_ROUTINE void _Thread_Lock_release_critical(
  void             *lock,
  ISR_lock_Context *lock_context
)
{
#if defined(RTEMS_SMP)
  _SMP_ticket_lock_Release(
    lock,
    &lock_context->Lock_context.Stats_context
  );
#else
  (void) lock;
  (void) lock_context;
#endif
}

/**
 * @brief Releases the thread lock.
 *
 * @param[in] lock The lock returned by _Thread_Lock_acquire().
 * @param[in] lock_context The lock context used for _Thread_Lock_acquire().
 */
RTEMS_INLINE_ROUTINE void _Thread_Lock_release(
  void             *lock,
  ISR_lock_Context *lock_context
)
{
  _Thread_Lock_release_critical( lock, lock_context );
  _ISR_lock_ISR_enable( lock_context );
}

/**
 * @brief Releases the default thread lock inside a critical section
 * (interrupts disabled).
 *
 * The previous interrupt status is not restored.
 *
 * @param[in] the_thread The thread.
 * @param[in] lock_context The lock context used for the corresponding lock
 * acquire.
 */
RTEMS_INLINE_ROUTINE void _Thread_Lock_release_default_critical(
  Thread_Control   *the_thread,
  ISR_lock_Context *lock_context
)
{
  _Thread_Lock_release_critical(
#if defined(RTEMS_SMP)
    &the_thread->Lock.Default,
#else
    NULL,
#endif
    lock_context
  );
}

/**
 * @brief Releases the default thread lock.
 *
 * @param[in] the_thread The thread.
 * @param[in] lock_context The lock context used for the corresponding lock
 * acquire.
 */
RTEMS_INLINE_ROUTINE void _Thread_Lock_release_default(
  Thread_Control   *the_thread,
  ISR_lock_Context *lock_context
)
{
  _Thread_Lock_release_default_critical( the_thread, lock_context );
  _ISR_lock_ISR_enable( lock_context );
}

/**
 * @brief Acquires the thread lock.
 *
 * @param[in] the_thread The thread.
 * @param[in] lock_context The lock context for _Thread_Lock_release().
 *
 * @return The lock required by _Thread_Lock_release().
 */
RTEMS_INLINE_ROUTINE void *_Thread_Lock_acquire(
  Thread_Control   *the_thread,
  ISR_lock_Context *lock_context
)
{
#if defined(RTEMS_SMP)
  SMP_ticket_lock_Control *lock;

  while ( true ) {
    unsigned int first_generation;
    unsigned int second_generation;

    _ISR_lock_ISR_disable( lock_context );

    /*
     * Ensure that we read our first lock generation before we obtain our
     * current lock.  See _Thread_Lock_set_unprotected().
     */
    first_generation = _Atomic_Load_uint(
      &the_thread->Lock.generation,
      ATOMIC_ORDER_ACQUIRE
    );

    lock = the_thread->Lock.current;
    _SMP_ticket_lock_Acquire(
      lock,
      &_Thread_Executing->Lock.Stats,
      &lock_context->Lock_context.Stats_context
    );

    /*
     * The C11 memory model doesn't guarantee that we read the latest
     * generation here.  For this a read-modify-write operation would be
     * necessary.  We read at least the new generation set up by the owner of
     * our current thread lock, and so on.
     */
    second_generation = _Atomic_Load_uint(
      &the_thread->Lock.generation,
      ATOMIC_ORDER_ACQUIRE
    );

    if ( first_generation == second_generation ) {
      return lock;
    }

    _Thread_Lock_release( lock, lock_context );
  }
#else
  _ISR_Disable( lock_context->isr_level );

  return NULL;
#endif
}

#if defined(RTEMS_SMP)
/*
 * Internal function, use _Thread_Lock_set() or _Thread_Lock_restore_default()
 * instead.
 */
RTEMS_INLINE_ROUTINE void _Thread_Lock_set_unprotected(
  Thread_Control          *the_thread,
  SMP_ticket_lock_Control *new_lock
)
{
  the_thread->Lock.current = new_lock;

  /*
   * The generation release corresponds to the generation acquire in
   * _Thread_Lock_acquire() and ensures that the new lock and other fields are
   * visible to the next thread lock owner.  Otherwise someone would be able to
   * read an up to date generation number and an old lock.  See
   * _Thread_Wait_set_queue() and _Thread_Wait_restore_default_operations().
   *
   * Since we set a new lock right before, this increment is not protected by a
   * lock and thus must be an atomic operation.
   */
  _Atomic_Fetch_add_uint(
    &the_thread->Lock.generation,
    1,
    ATOMIC_ORDER_RELEASE
  );
}
#endif

/**
 * @brief Sets a new thread lock.
 *
 * The caller must not be the owner of the default thread lock.  The caller
 * must be the owner of the new lock.
 *
 * @param[in] the_thread The thread.
 * @param[in] new_lock The new thread lock.
 */
#if defined(RTEMS_SMP)
RTEMS_INLINE_ROUTINE void _Thread_Lock_set(
  Thread_Control          *the_thread,
  SMP_ticket_lock_Control *new_lock
)
{
  ISR_lock_Context lock_context;

  _Thread_Lock_acquire_default_critical( the_thread, &lock_context );
  _Assert( the_thread->Lock.current == &the_thread->Lock.Default );
  _Thread_Lock_set_unprotected( the_thread, new_lock );
  _Thread_Lock_release_default_critical( the_thread, &lock_context );
}
#else
#define _Thread_Lock_set( the_thread, new_lock ) \
  do { } while ( 0 )
#endif

/**
 * @brief Restores the default thread lock.
 *
 * The caller must be the owner of the current thread lock.
 *
 * @param[in] the_thread The thread.
 */
#if defined(RTEMS_SMP)
RTEMS_INLINE_ROUTINE void _Thread_Lock_restore_default(
  Thread_Control *the_thread
)
{
  _Thread_Lock_set_unprotected( the_thread, &the_thread->Lock.Default );
}
#else
#define _Thread_Lock_restore_default( the_thread ) \
  do { } while ( 0 )
#endif

/**
 * @brief The initial thread wait flags value set by _Thread_Initialize().
 */
#define THREAD_WAIT_FLAGS_INITIAL 0x0U

/**
 * @brief Mask to get the thread wait state flags.
 */
#define THREAD_WAIT_STATE_MASK 0xffU

/**
 * @brief Indicates that the thread begins with the blocking operation.
 *
 * A blocking operation consists of an optional watchdog initialization and the
 * setting of the appropriate thread blocking state with the corresponding
 * scheduler block operation.
 */
#define THREAD_WAIT_STATE_INTEND_TO_BLOCK 0x1U

/**
 * @brief Indicates that the thread completed the blocking operation.
 */
#define THREAD_WAIT_STATE_BLOCKED 0x2U

/**
 * @brief Indicates that a condition to end the thread wait occurred.
 *
 * This could be a timeout, a signal, an event or a resource availability.
 */
#define THREAD_WAIT_STATE_READY_AGAIN 0x4U

/**
 * @brief Mask to get the thread wait class flags.
 */
#define THREAD_WAIT_CLASS_MASK 0xff00U

/**
 * @brief Indicates that the thread waits for an event.
 */
#define THREAD_WAIT_CLASS_EVENT 0x100U

/**
 * @brief Indicates that the thread waits for a system event.
 */
#define THREAD_WAIT_CLASS_SYSTEM_EVENT 0x200U

/**
 * @brief Indicates that the thread waits for a object.
 */
#define THREAD_WAIT_CLASS_OBJECT 0x400U

RTEMS_INLINE_ROUTINE void _Thread_Wait_flags_set(
  Thread_Control    *the_thread,
  Thread_Wait_flags  flags
)
{
#if defined(RTEMS_SMP)
  _Atomic_Store_uint( &the_thread->Wait.flags, flags, ATOMIC_ORDER_RELAXED );
#else
  the_thread->Wait.flags = flags;
#endif
}

RTEMS_INLINE_ROUTINE Thread_Wait_flags _Thread_Wait_flags_get(
  const Thread_Control *the_thread
)
{
#if defined(RTEMS_SMP)
  return _Atomic_Load_uint( &the_thread->Wait.flags, ATOMIC_ORDER_RELAXED );
#else
  return the_thread->Wait.flags;
#endif
}

/**
 * @brief Tries to change the thread wait flags inside a critical section
 * (interrupts disabled).
 *
 * In case the wait flags are equal to the expected wait flags, then the wait
 * flags are set to the desired wait flags.
 *
 * @param[in] the_thread The thread.
 * @param[in] expected_flags The expected wait flags.
 * @param[in] desired_flags The desired wait flags.
 *
 * @retval true The wait flags were equal to the expected wait flags.
 * @retval false Otherwise.
 */
RTEMS_INLINE_ROUTINE bool _Thread_Wait_flags_try_change_critical(
  Thread_Control    *the_thread,
  Thread_Wait_flags  expected_flags,
  Thread_Wait_flags  desired_flags
)
{
#if defined(RTEMS_SMP)
  return _Atomic_Compare_exchange_uint(
    &the_thread->Wait.flags,
    &expected_flags,
    desired_flags,
    ATOMIC_ORDER_RELAXED,
    ATOMIC_ORDER_RELAXED
  );
#else
  bool success = the_thread->Wait.flags == expected_flags;

  if ( success ) {
    the_thread->Wait.flags = desired_flags;
  }

  return success;
#endif
}

/**
 * @brief Tries to change the thread wait flags.
 *
 * @see _Thread_Wait_flags_try_change_critical().
 */
RTEMS_INLINE_ROUTINE bool _Thread_Wait_flags_try_change(
  Thread_Control    *the_thread,
  Thread_Wait_flags  expected_flags,
  Thread_Wait_flags  desired_flags
)
{
  bool success;
#if !defined(RTEMS_SMP)
  ISR_Level level;

  _ISR_Disable_without_giant( level );
#endif

  success = _Thread_Wait_flags_try_change_critical(
    the_thread,
    expected_flags,
    desired_flags
  );

#if !defined(RTEMS_SMP)
  _ISR_Enable_without_giant( level );
#endif

  return success;
}

/**
 * @brief Sets the thread queue.
 *
 * The caller must be the owner of the thread lock.
 *
 * @param[in] the_thread The thread.
 * @param[in] new_queue The new queue.
 *
 * @see _Thread_Lock_set().
 */
RTEMS_INLINE_ROUTINE void _Thread_Wait_set_queue(
  Thread_Control     *the_thread,
  Thread_queue_Queue *new_queue
)
{
  the_thread->Wait.queue = new_queue;
}

/**
 * @brief Sets the thread queue operations.
 *
 * The caller must be the owner of the thread lock.
 *
 * @param[in] the_thread The thread.
 * @param[in] new_operations The new queue operations.
 *
 * @see _Thread_Lock_set() and _Thread_Wait_restore_default_operations().
 */
RTEMS_INLINE_ROUTINE void _Thread_Wait_set_operations(
  Thread_Control                *the_thread,
  const Thread_queue_Operations *new_operations
)
{
  the_thread->Wait.operations = new_operations;
}

/**
 * @brief Restores the default thread queue operations.
 *
 * The caller must be the owner of the thread lock.
 *
 * @param[in] the_thread The thread.
 *
 * @see _Thread_Wait_set_operations().
 */
RTEMS_INLINE_ROUTINE void _Thread_Wait_restore_default_operations(
  Thread_Control *the_thread
)
{
  the_thread->Wait.operations = &_Thread_queue_Operations_default;
}

/**
 * @brief Sets the thread wait timeout code.
 *
 * @param[in] the_thread The thread.
 * @param[in] timeout_code The new thread wait timeout code.
 */
RTEMS_INLINE_ROUTINE void _Thread_Wait_set_timeout_code(
  Thread_Control *the_thread,
  uint32_t        timeout_code
)
{
  the_thread->Wait.timeout_code = timeout_code;
}

/**
 * @brief General purpose thread wait timeout.
 *
 * @param[in] id Unused.
 * @param[in] arg The thread.
 */
void _Thread_Timeout( Objects_Id id, void *arg );

RTEMS_INLINE_ROUTINE void _Thread_Debug_set_real_processor(
  Thread_Control  *the_thread,
  Per_CPU_Control *cpu
)
{
#if defined(RTEMS_SMP) && defined(RTEMS_DEBUG)
  the_thread->Scheduler.debug_real_cpu = cpu;
#else
  (void) the_thread;
  (void) cpu;
#endif
}

/** @}*/

#ifdef __cplusplus
}
#endif

#if defined(RTEMS_MULTIPROCESSING)
#include <rtems/score/threadmp.h>
#endif

#endif
/* end of include file */