RTEMS
thread.h
Go to the documentation of this file.
1 
12 /*
13  * COPYRIGHT (c) 1989-2014.
14  * On-Line Applications Research Corporation (OAR).
15  *
16  * Copyright (c) 2014, 2016 embedded brains GmbH.
17  *
18  * The license and distribution terms for this file may be
19  * found in the file LICENSE in this distribution or at
20  * http://www.rtems.org/license/LICENSE.
21  */
22 
23 #ifndef _RTEMS_SCORE_THREAD_H
24 #define _RTEMS_SCORE_THREAD_H
25 
26 #include <rtems/score/atomic.h>
27 #include <rtems/score/context.h>
28 #if defined(RTEMS_MULTIPROCESSING)
29 #include <rtems/score/mppkt.h>
30 #endif
31 #include <rtems/score/freechain.h>
32 #include <rtems/score/isrlock.h>
33 #include <rtems/score/objectdata.h>
34 #include <rtems/score/priority.h>
36 #include <rtems/score/stack.h>
37 #include <rtems/score/states.h>
38 #include <rtems/score/threadq.h>
39 #include <rtems/score/timestamp.h>
40 #include <rtems/score/watchdog.h>
41 
42 #if defined(RTEMS_SMP)
44 #endif
45 
46 struct rtems_user_env_t;
47 
48 struct _pthread_cleanup_context;
49 
50 struct Per_CPU_Control;
51 
52 struct _Scheduler_Control;
53 
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
80 #define RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE
81 
82 /*
83  * With the addition of the Constant Block Scheduler (CBS),
84  * this feature is needed even when POSIX is disabled.
85  */
86 #define RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT
87 
88 #if defined(RTEMS_DEBUG)
89 #define RTEMS_SCORE_THREAD_ENABLE_RESOURCE_COUNT
90 #endif
91 
102 
106 typedef struct {
107  void *( *entry )( uintptr_t argument );
109 
113 typedef struct {
114  void ( *entry )( Thread_Entry_numeric_type argument );
115  Thread_Entry_numeric_type argument;
117 
122 typedef struct {
123  void *( *entry )( void *argument );
124  void *argument;
126 
130 typedef struct {
138  void ( *adaptor )( Thread_Control *executing );
139 
144  union {
145  Thread_Entry_idle Idle;
146  Thread_Entry_numeric Numeric;
147  Thread_Entry_pointer Pointer;
148  } Kinds;
150 
158 typedef enum {
159  THREAD_CPU_BUDGET_ALGORITHM_NONE,
160  THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE,
161  #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
162  THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE,
163  #endif
164  #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
165  THREAD_CPU_BUDGET_ALGORITHM_CALLOUT
166  #endif
168 
173 
178 typedef struct {
181  /*-------------- initial execution modes ----------------- */
193  uint32_t isr_level;
199  void ( *stack_free )( void * );
202  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
203 
204  Context_Control_fp *fp_context;
205  #endif
206 
207  void *tls_area;
209 
210 #if defined(RTEMS_SMP)
211 
214 typedef enum {
221 
230 
238 #endif
239 
243 typedef struct {
244 #if defined(RTEMS_SMP)
245 
249 
254 
259 
264 
269 
280 
295 
303 
310 
317 
339 
343  Processor_mask Affinity;
344 #endif
345 
353 
364 typedef union {
365  void *mutable_object;
366  const void *immutable_object;
368 
383 typedef unsigned int Thread_Wait_flags;
384 
391 typedef struct {
392 #if defined(RTEMS_MULTIPROCESSING)
393  /*
394  * @brief This field is the identifier of the remote object this thread is
395  * waiting upon.
396  */
397  Objects_Id remote_id;
398 #endif
399 
400  uint32_t count;
407  uint32_t option;
413  uint32_t return_code;
414 
419 #if defined(RTEMS_SMP)
420  Atomic_Uint flags;
421 #else
422  Thread_Wait_flags flags;
423 #endif
424 
425 #if defined(RTEMS_SMP)
426 
443  struct {
448 
454 
465  } Lock;
466 
472 #endif
473 
483 
492 
493  Thread_queue_Heads *spare_heads;
495 
499 typedef struct {
500  ISR_LOCK_MEMBER( Lock )
501  Watchdog_Header *header;
502  Watchdog_Control Watchdog;
504 
512 typedef struct {
515 
520 
523 
528 
529 #if defined(RTEMS_SCORE_THREAD_ENABLE_RESOURCE_COUNT)
530 
531  uint32_t resource_count;
532 #endif
533 
538 
543 #if defined(RTEMS_MULTIPROCESSING)
544 
545  MP_packet_Prefix *receive_packet;
546  /****************** end of common block ********************/
547 
551  Thread_queue_MP_callout thread_queue_callout;
552 
556  RBTree_Node Active;
557 
562  Scheduler_Node Scheduler_node;
563 
571  Thread_queue_Heads Thread_queue_heads[ RTEMS_ZERO_LENGTH_ARRAY ];
572 #endif
574 
582 typedef enum {
587 } Thread_APIs;
588 
590 #define THREAD_API_FIRST THREAD_API_RTEMS
591 
593 #define THREAD_API_LAST THREAD_API_POSIX
594 
595 typedef struct Thread_Action Thread_Action;
596 
613 typedef void ( *Thread_Action_handler )(
614  Thread_Control *the_thread,
615  Thread_Action *action,
616  ISR_lock_Context *lock_context
617 );
618 
634  Chain_Node Node;
635  Thread_Action_handler handler;
636 };
637 
641 typedef struct {
645  RBTree_Control Key_value_pairs;
646 
650  ISR_LOCK_MEMBER( Lock )
652 
658 typedef struct {
659  Chain_Control Chain;
661 
672 typedef enum {
673  THREAD_LIFE_PROTECTED = 0x1,
674  THREAD_LIFE_RESTARTING = 0x2,
675  THREAD_LIFE_TERMINATING = 0x4,
676  THREAD_LIFE_CHANGE_DEFERRED = 0x8,
677  THREAD_LIFE_DETACHED = 0x10
679 
683 typedef struct {
689 
694 
699 
708  void *exit_value;
710 
711 typedef struct {
712  uint32_t flags;
713  void * control;
715 
728 
747 
750 
755 
756 #if defined(RTEMS_SCORE_THREAD_ENABLE_RESOURCE_COUNT)
757 
758  uint32_t resource_count;
759 #endif
760 
765 
770 #if defined(RTEMS_MULTIPROCESSING)
771 
772  MP_packet_Prefix *receive_packet;
773 #endif
774  /*================= end of common block =================*/
775 
776 #if defined(RTEMS_SMP) && defined(RTEMS_PROFILING)
777 
785  SMP_lock_Stats Potpourri_stats;
786 #endif
787 
789  bool is_idle;
790 #if defined(RTEMS_MULTIPROCESSING)
791 
792  bool is_global;
793 #endif
794 
797  bool is_fp;
798 
804 
809  uint32_t cpu_time_budget;
821 
826 
827  Thread_Action_control Post_switch_actions;
828 
831 #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
832 
835  Context_Control_fp *fp_context;
836 #endif
837 
838  struct _reent *libc_reent;
841 
846 
853 
854  Thread_Capture_control Capture;
855 
859  struct rtems_user_env_t *user_environment;
860 
864  struct _pthread_cleanup_context *last_cleanup_context;
865 
870 
877 };
878 
879 typedef void (*rtems_per_thread_routine)( Thread_Control * );
880 
887  rtems_per_thread_routine routine
889 
893 typedef struct {
900 
907 
922 
930 extern const size_t _Thread_Control_add_on_count;
931 
937 extern const size_t _Thread_Initial_thread_count;
938 
946 #define THREAD_DEFAULT_MAXIMUM_NAME_SIZE 16
947 
954 extern const size_t _Thread_Maximum_name_size;
955 
964 extern const size_t _Thread_Maximum_TLS_size;
965 
973 
980 #if defined(RTEMS_SMP)
982 #else
984 #endif
985 
991 #if defined(RTEMS_SMP)
992 extern const size_t _Thread_queue_Heads_size;
993 #else
994 #define _Thread_queue_Heads_size sizeof(Thread_queue_Heads)
995 #endif
996 
1000 typedef struct {
1005 
1009  union {
1016 
1023  } Thread_queue_heads;
1025 
1030 
1031 #define THREAD_INFORMATION_DEFINE_ZERO( name, api, cls ) \
1032 Thread_Information name##_Information = { \
1033  { \
1034  _Objects_Build_id( api, cls, 1, 0 ), \
1035  NULL, \
1036  _Objects_Allocate_none, \
1037  NULL, \
1038  0, \
1039  0, \
1040  0, \
1041  0, \
1042  CHAIN_INITIALIZER_EMPTY( name##_Information.Objects.Inactive ), \
1043  NULL, \
1044  NULL, \
1045  NULL \
1046  OBJECTS_INFORMATION_MP( name##_Information.Objects, NULL ), \
1047  }, { \
1048  NULL \
1049  } \
1050 }
1051 
1059 
1060 #define THREAD_INFORMATION_DEFINE( name, api, cls, max ) \
1061 static Objects_Control * \
1062 name##_Local_table[ _Objects_Maximum_per_allocation( max ) ]; \
1063 static Thread_Configured_control \
1064 name##_Objects[ _Objects_Maximum_per_allocation( max ) ]; \
1065 static Thread_queue_Configured_heads \
1066 name##_Heads[ _Objects_Maximum_per_allocation( max ) ]; \
1067 Thread_Information name##_Information = { \
1068  { \
1069  _Objects_Build_id( api, cls, 1, _Objects_Maximum_per_allocation( max ) ), \
1070  name##_Local_table, \
1071  _Objects_Is_unlimited( max ) ? \
1072  _Thread_Allocate_unlimited : _Objects_Allocate_static, \
1073  _Objects_Is_unlimited( max ) ? \
1074  _Objects_Free_unlimited : _Objects_Free_static, \
1075  0, \
1076  _Objects_Is_unlimited( max ) ? _Objects_Maximum_per_allocation( max ) : 0, \
1077  sizeof( Thread_Configured_control ), \
1078  OBJECTS_NO_STRING_NAME, \
1079  CHAIN_INITIALIZER_EMPTY( name##_Information.Objects.Inactive ), \
1080  NULL, \
1081  NULL, \
1082  &name##_Objects[ 0 ].Control.Object \
1083  OBJECTS_INFORMATION_MP( name##_Information.Objects, NULL ) \
1084  }, { \
1085  &name##_Heads[ 0 ] \
1086  } \
1087 }
1088 
1094 extern char _Thread_Idle_stacks[];
1095 
1096 #if defined(RTEMS_MULTIPROCESSING)
1097 
1103 typedef struct Thread_Configured_proxy_control Thread_Configured_proxy_control;
1104 
1110 extern Thread_Configured_proxy_control * const _Thread_MP_Proxies;
1111 #endif
1112 
1115 #ifdef __cplusplus
1116 }
1117 #endif
1118 
1119 #endif
1120 /* end of include file */
Thread_queue_Gate Tranquilizer
Tranquilizer gate used by _Thread_Wait_tranquilize().
Definition: thread.h:464
Objects_Control * _Thread_Allocate_unlimited(Objects_Information *information)
Return an inactive thread object or NULL.
const struct _Scheduler_Control * home_scheduler
The home scheduler of this thread.
Definition: thread.h:258
Timestamp_Control cpu_time_used
Definition: thread.h:820
Thread_Life_state state
The current thread life state.
Definition: thread.h:693
const Thread_queue_Operations * operations
The current thread queue operations.
Definition: thread.h:491
int64_t Timestamp_Control
Definition: timestamp.h:57
Thread_CPU_budget_algorithm_callout budget_callout
Definition: thread.h:816
Thread_Wait_information Wait
Definition: thread.h:540
Information About Each Thread&#39;s Context.
uint64_t Priority_Control
The thread priority control.
Definition: priority.h:70
Data for thread entry with one pointer argument and a pointer return value.
Definition: thread.h:122
bool is_preemptible
Definition: thread.h:795
Atomic_Uint flags
This field contains several flags used to control the wait class and state of a thread in case fine-g...
Definition: thread.h:420
Thread_Life_state
Thread life states.
Definition: thread.h:672
Thread_Wait_information Wait
Definition: thread.h:767
The priority node to build up a priority aggregation.
Definition: priority.h:98
Data for idle thread entry.
Definition: thread.h:106
Thread entry information.
Definition: thread.h:130
SPARC basic context.
Definition: cpu.h:318
Union type to hold a pointer to an immutable or a mutable object.
Definition: thread.h:364
uint32_t pending_life_change_requests
The count of pending life change requests.
Definition: thread.h:698
void rtems_iterate_over_all_threads(rtems_per_thread_routine routine) RTEMS_DEPRECATED
Deprecated, use rtems_task_iterate() instead.
struct Per_CPU_Control * cpu
The processor assigned by the current scheduler.
Definition: thread.h:268
Thread_Start_information Start
Definition: thread.h:825
Thread_queue_Control Join_queue
Definition: thread.h:519
The control block used to manage each watchdog timer.
Definition: watchdog.h:90
const size_t _Thread_Maximum_TLS_size
If this constant is greater than zero, then it defines the maximum thread-local storage size...
Per-thread information for POSIX Keys.
Definition: thread.h:641
uint32_t return_code
Definition: thread.h:413
size_t destination_offset
Offset of the pointer field in Thread_Control referencing an application configuration dependent memo...
Definition: thread.h:899
Helpers for Manipulating Timestamps.
Thread Execution State Information.
Thread_Entry_information Entry
Definition: thread.h:180
Thread_Keys_information Keys
The POSIX Keys information.
Definition: thread.h:845
This thread is scheduled with respect to the scheduler.
Definition: thread.h:229
Chain iterator for dynamic user extensions.
Definition: userextimpl.h:46
This thread is ready with respect to the scheduler.
Definition: thread.h:236
CPU_Uint32ptr Thread_Entry_numeric_type
Type of the numeric argument of a thread entry function with at least one numeric argument...
Definition: thread.h:101
Thread_Scheduler_control Scheduler
Scheduler related control.
Definition: thread.h:764
Information required to manage a thread timer.
Definition: thread.h:499
ISR_lock_Control Default
Thread wait default lock.
Definition: thread.h:447
Thread_queue_Queue * queue
The current thread queue.
Definition: thread.h:482
struct _pthread_cleanup_context * last_cleanup_context
LIFO list of POSIX cleanup contexts.
Definition: thread.h:864
Thread_Timer_information Timer
Definition: thread.h:542
Constants and Structures Associated with Watchdog Timers.
Objects_Information Objects
The object information.
Definition: thread.h:1004
Red-black tree node.
Definition: rbtree.h:55
Constants and Structures Needed to Declare a Thread Queue.
ISR lock control.
Definition: isrlock.h:56
Scheduler_Node * requests
List of pending scheduler node requests.
Definition: thread.h:316
struct User_extensions_Iterator * last_user_extensions_iterator
LIFO list of user extensions iterators.
Definition: thread.h:869
Information required to manage a thread while it is blocked.
Definition: thread.h:391
uint32_t States_Control
Definition: states.h:46
const size_t _Thread_Control_add_on_count
Thread control add-on count.
const Thread_Control_add_on _Thread_Control_add_ons[]
Thread control add-ons.
Thread queue operations.
Definition: threadq.h:517
Object Handler Data Structures.
struct _reent * libc_reent
Definition: thread.h:838
struct rtems_user_env_t * user_environment
Pointer to an optional thread-specific POSIX user environment.
Definition: thread.h:859
size_t helping_nodes
Count of nodes scheduler nodes minus one.
Definition: thread.h:309
bool was_created_with_inherited_scheduler
True, if the thread was created with an inherited scheduler (PTHREAD_INHERIT_SCHED), and false otherwise.
Definition: thread.h:803
Processor_mask Affinity
The thread processor affinity set.
Definition: thread.h:343
Thread queue heads.
Definition: threadq.h:360
Thread_APIs
Definition: thread.h:582
size_t source_offset
Offset relative to the thread control block begin to an application configuration dependent memory ar...
Definition: thread.h:905
Thread_CPU_budget_algorithms budget_algorithm
Definition: thread.h:814
This thread is blocked with respect to the scheduler.
Definition: thread.h:220
#define RTEMS_ZERO_LENGTH_ARRAY
This is a constant to declare zero-length arrays.
Definition: basedefs.h:945
#define THREAD_API_LAST
Definition: thread.h:593
unsigned int Thread_Wait_flags
This type is able to contain several flags used to control the wait class and state of a thread...
Definition: thread.h:383
int pin_level
The thread pinning to current processor level.
Definition: thread.h:338
#define ISR_LOCK_MEMBER(_designator)
Defines an ISR lock member.
Definition: isrlock.h:87
Control block to manage thread actions.
Definition: thread.h:658
Thread_Scheduler_state
The thread state with respect to the scheduler.
Definition: thread.h:214
Per CPU Core Structure.
Definition: percpu.h:347
Thread control add-on.
Definition: thread.h:893
The thread object information.
Definition: thread.h:1000
Thread action.
Definition: thread.h:633
Objects_Control Object
Definition: thread.h:727
Specification for the Packet Handler.
uintptr_t CPU_Uint32ptr
Definition: cpu.h:1141
States_Control current_state
Definition: thread.h:749
RBTree_Control Key_value_pairs
Key value pairs registered for this thread.
Definition: thread.h:645
Priority Handler API.
The information structure used to manage each API class of objects.
Definition: objectdata.h:176
Chain_Node Help_node
Node for the Per_CPU_Control::Threads_in_need_for_help chain.
Definition: thread.h:302
Atomic Operations API.
SPARC basic context.
Definition: cpu.h:478
Thread_Scheduler_state state
The current scheduler state of this thread.
Definition: thread.h:253
Thread_CPU_budget_algorithms
Definition: thread.h:158
void * exit_value
The thread exit value.
Definition: thread.h:708
const struct _Scheduler_Control * pinned_scheduler
The pinned scheduler of this thread.
Definition: thread.h:263
struct Thread_queue_Configured_heads Thread_queue_Configured_heads
The configured thread queue heads.
Definition: thread.h:981
Priority_Control initial_priority
Definition: thread.h:195
Thread_CPU_budget_algorithm_callout budget_callout
Definition: thread.h:191
Freechain_Control Free
The free thread queue heads.
Definition: thread.h:1022
void * API_Extensions[THREAD_API_LAST+1]
Definition: thread.h:840
Chain_Control Scheduler_nodes
Scheduler nodes immediately available to the schedulers for this thread.
Definition: thread.h:294
#define RTEMS_DEPRECATED
Instructs the compiler in a declaration to issue a warning whenever a variable, function, or type using this declaration will be used.
Definition: basedefs.h:193
Stack_Control Initial_stack
Definition: thread.h:201
Thread_queue_Configured_heads * initial
Contains the initial set of thread queue heads.
Definition: thread.h:1015
States_Control current_state
Definition: thread.h:522
Data for thread entry with one numeric argument and no return value.
Definition: thread.h:113
The watchdog header to manage scheduled watchdogs.
Definition: watchdog.h:71
Thread scheduler control.
Definition: thread.h:243
Thread_queue_Link Link
Thread queue link provided for use by the thread wait lock owner to build a thread queue path...
Definition: thread.h:471
Freechain Handler API.
void(* Thread_CPU_budget_algorithm_callout)(Thread_Control *)
Definition: thread.h:172
const size_t _Thread_queue_Heads_size
Size of the thread queue heads of a particular application.
Priority_Node Real_priority
The base priority of this thread in its home scheduler instance.
Definition: thread.h:527
Thread_queue_Control Join_queue
Thread queue for thread join operations and multi-purpose lock.
Definition: thread.h:746
Scheduler control.
Definition: scheduler.h:264
Scheduler node for per-thread data.
Definition: schedulernode.h:79
void * extensions[RTEMS_ZERO_LENGTH_ARRAY]
Variable length array of user extension pointers.
Definition: thread.h:876
The thread queue gate is an SMP synchronization means.
Definition: threadq.h:118
Chain_Control Pending_requests
The pending thread wait lock acquire or tranquilize requests in case the thread is enqueued on a thre...
Definition: thread.h:453
Thread_Wait_information_Object_argument_type return_argument_second
Definition: thread.h:405
Processor Mask API.
uint32_t Objects_Id
Definition: object.h:80
Objects_Control Object
Definition: thread.h:514
Handles Scheduler Nodes.
Thread_CPU_budget_algorithms budget_algorithm
Definition: thread.h:187
Chain_Control Wait_nodes
Scheduler nodes immediately available to the thread by its home scheduler and due to thread queue own...
Definition: thread.h:279
Scheduler_Node * nodes
The scheduler nodes of this thread.
Definition: thread.h:351
Thread life control.
Definition: thread.h:683
Local ISR lock context for acquire and release pairs.
Definition: isrlock.h:65
Thread_Action Action
Thread life action used to react upon thread restart and delete requests.
Definition: thread.h:688
ISR_lock_Control Lock
Lock to protect the scheduler node change requests.
Definition: thread.h:248
Context_Control Registers
Definition: thread.h:830
Thread_Information _Thread_Information
The internal thread objects information.
Thread_Life_control Life
Thread life-cycle control.
Definition: thread.h:852
Priority_Node Real_priority
The base priority of this thread in its home scheduler instance.
Definition: thread.h:754
void(* Thread_Action_handler)(Thread_Control *the_thread, Thread_Action *action, ISR_lock_Context *lock_context)
Thread action handler.
Definition: thread.h:613
ISR Locks.
Information About the Thread Stack Handler.
char _Thread_Idle_stacks[]
The idle thread stacks.
Thread_Scheduler_control Scheduler
Scheduler related control.
Definition: thread.h:537
struct Thread_Configured_control Thread_Configured_control
The configured thread control block.
Definition: thread.h:972
uint32_t cpu_time_budget
Definition: thread.h:809
const size_t _Thread_Initial_thread_count
Count of configured threads.
Thread_Timer_information Timer
Definition: thread.h:769
The freechain control.
Definition: freechain.h:42
const size_t _Thread_Maximum_name_size
Maximum size of a thread name in characters (including the terminating &#39;\0&#39; character).
bool is_idle
Definition: thread.h:789