RTEMS  5.0.0
thread.h
Go to the documentation of this file.
1 
10 /*
11  * COPYRIGHT (c) 1989-2014.
12  * On-Line Applications Research Corporation (OAR).
13  *
14  * Copyright (c) 2014, 2016 embedded brains GmbH.
15  *
16  * The license and distribution terms for this file may be
17  * found in the file LICENSE in this distribution or at
18  * http://www.rtems.org/license/LICENSE.
19  */
20 
21 #ifndef _RTEMS_SCORE_THREAD_H
22 #define _RTEMS_SCORE_THREAD_H
23 
24 #include <rtems/score/atomic.h>
25 #include <rtems/score/context.h>
26 #if defined(RTEMS_MULTIPROCESSING)
27 #include <rtems/score/mppkt.h>
28 #endif
29 #include <rtems/score/freechain.h>
30 #include <rtems/score/isrlock.h>
31 #include <rtems/score/objectdata.h>
32 #include <rtems/score/priority.h>
33 #include <rtems/score/schedulernode.h>
34 #include <rtems/score/stack.h>
35 #include <rtems/score/states.h>
36 #include <rtems/score/threadq.h>
37 #include <rtems/score/timestamp.h>
38 #include <rtems/score/watchdog.h>
39 
40 #if defined(RTEMS_SMP)
42 #endif
43 
44 struct _pthread_cleanup_context;
45 
46 struct Per_CPU_Control;
47 
48 struct _Scheduler_Control;
49 
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
73 #define RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE
74 
75 /*
76  * With the addition of the Constant Block Scheduler (CBS),
77  * this feature is needed even when POSIX is disabled.
78  */
79 #define RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT
80 
81 #define RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API
82 
83 #if defined(RTEMS_DEBUG)
84 #define RTEMS_SCORE_THREAD_ENABLE_RESOURCE_COUNT
85 #endif
86 
87 /*
88  * Only provided for backward compatiblity to not break application
89  * configurations.
90  */
91 typedef void *Thread RTEMS_DEPRECATED;
92 
103 
107 typedef struct {
108  void *( *entry )( uintptr_t argument );
110 
114 typedef struct {
115  void ( *entry )( Thread_Entry_numeric_type argument );
116  Thread_Entry_numeric_type argument;
118 
123 typedef struct {
124  void *( *entry )( void *argument );
125  void *argument;
127 
131 typedef struct {
139  void ( *adaptor )( Thread_Control *executing );
140 
145  union {
146  Thread_Entry_idle Idle;
147  Thread_Entry_numeric Numeric;
148  Thread_Entry_pointer Pointer;
149  } Kinds;
151 
159 typedef enum {
160  THREAD_CPU_BUDGET_ALGORITHM_NONE,
161  THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE,
162  #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
163  THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE,
164  #endif
165  #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
166  THREAD_CPU_BUDGET_ALGORITHM_CALLOUT
167  #endif
169 
174 
179 typedef struct {
182  /*-------------- initial execution modes ----------------- */
188  Thread_CPU_budget_algorithms budget_algorithm;
194  uint32_t isr_level;
197  #if defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
198 
200  #endif
201 
203  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
204 
206  #endif
207 
208  void *stack;
210  void *tls_area;
212 
213 #if defined(RTEMS_SMP)
214 
217 typedef enum {
223  THREAD_SCHEDULER_BLOCKED,
224 
232  THREAD_SCHEDULER_SCHEDULED,
233 
239  THREAD_SCHEDULER_READY
240 } Thread_Scheduler_state;
241 #endif
242 
246 typedef struct {
247 #if defined(RTEMS_SMP)
248 
251  ISR_lock_Control Lock;
252 
256  Thread_Scheduler_state state;
257 
261  const struct _Scheduler_Control *home_scheduler;
262 
266  const struct _Scheduler_Control *pinned_scheduler;
267 
271  struct Per_CPU_Control *cpu;
272 
282  Chain_Control Wait_nodes;
283 
297  Chain_Control Scheduler_nodes;
298 
305  Chain_Node Help_node;
306 
312  size_t helping_nodes;
313 
319  Scheduler_Node *requests;
320 
341  int pin_level;
342 
346  Processor_mask Affinity;
347 #endif
348 
356 
367 typedef union {
368  void *mutable_object;
369  const void *immutable_object;
371 
386 typedef unsigned int Thread_Wait_flags;
387 
394 typedef struct {
395 #if defined(RTEMS_MULTIPROCESSING)
396  /*
397  * @brief This field is the identifier of the remote object this thread is
398  * waiting upon.
399  */
400  Objects_Id remote_id;
401 #endif
402 
403  uint32_t count;
410  uint32_t option;
416  uint32_t return_code;
417 
422 #if defined(RTEMS_SMP)
423  Atomic_Uint flags;
424 #else
425  Thread_Wait_flags flags;
426 #endif
427 
428 #if defined(RTEMS_SMP)
429 
446  struct {
450  ISR_lock_Control Default;
451 
456  Chain_Control Pending_requests;
457 
467  Thread_queue_Gate Tranquilizer;
468  } Lock;
469 
474  Thread_queue_Link Link;
475 #endif
476 
486 
495 
496  Thread_queue_Heads *spare_heads;
498 
502 typedef struct {
503  ISR_LOCK_MEMBER( Lock )
504  Watchdog_Header *header;
505  Watchdog_Control Watchdog;
507 
515 typedef struct {
518 
523 
526 
531 
532 #if defined(RTEMS_SCORE_THREAD_ENABLE_RESOURCE_COUNT)
533 
534  uint32_t resource_count;
535 #endif
536 
541 
546 #if defined(RTEMS_MULTIPROCESSING)
547 
548  MP_packet_Prefix *receive_packet;
549  /****************** end of common block ********************/
550 
554  Thread_queue_MP_callout thread_queue_callout;
555 
559  RBTree_Node Active;
560 
565  Scheduler_Node Scheduler_node;
566 
574  Thread_queue_Heads Thread_queue_heads[ RTEMS_ZERO_LENGTH_ARRAY ];
575 #endif
577 
585 typedef enum {
590 } Thread_APIs;
591 
593 #define THREAD_API_FIRST THREAD_API_RTEMS
594 
596 #define THREAD_API_LAST THREAD_API_POSIX
597 
598 typedef struct Thread_Action Thread_Action;
599 
616 typedef void ( *Thread_Action_handler )(
617  Thread_Control *the_thread,
618  Thread_Action *action,
619  ISR_lock_Context *lock_context
620 );
621 
637  Chain_Node Node;
638  Thread_Action_handler handler;
639 };
640 
644 typedef struct {
648  RBTree_Control Key_value_pairs;
649 
653  ISR_LOCK_MEMBER( Lock )
655 
661 typedef struct {
662  Chain_Control Chain;
664 
675 typedef enum {
676  THREAD_LIFE_PROTECTED = 0x1,
677  THREAD_LIFE_RESTARTING = 0x2,
678  THREAD_LIFE_TERMINATING = 0x4,
679  THREAD_LIFE_CHANGE_DEFERRED = 0x8,
680  THREAD_LIFE_DETACHED = 0x10
682 
686 typedef struct {
692 
697 
702 
711  void *exit_value;
713 
714 typedef struct {
715  uint32_t flags;
716  void * control;
718 
731 
750 
753 
758 
759 #if defined(RTEMS_SCORE_THREAD_ENABLE_RESOURCE_COUNT)
760 
761  uint32_t resource_count;
762 #endif
763 
768 
773 #if defined(RTEMS_MULTIPROCESSING)
774 
775  MP_packet_Prefix *receive_packet;
776 #endif
777  /*================= end of common block =================*/
778 
779 #if defined(RTEMS_SMP) && defined(RTEMS_PROFILING)
780 
788  SMP_lock_Stats Potpourri_stats;
789 #endif
790 
792  bool is_idle;
793 #if defined(RTEMS_MULTIPROCESSING)
794 
795  bool is_global;
796 #endif
797 
800  bool is_fp;
801 
807 
812  uint32_t cpu_time_budget;
817  Thread_CPU_budget_algorithms budget_algorithm;
824 
829 
830  Thread_Action_control Post_switch_actions;
831 
834 #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
835 
839 #endif
840 
841  struct _reent *libc_reent;
843  void *API_Extensions[ THREAD_API_LAST + 1 ];
844 
849 
856 
857  Thread_Capture_control Capture;
858 
862  struct _pthread_cleanup_context *last_cleanup_context;
863 
868 
874  void *extensions[ RTEMS_ZERO_LENGTH_ARRAY ];
875 };
876 
877 typedef void (*rtems_per_thread_routine)( Thread_Control * );
878 
879 /* Use rtems_task_iterate() instead */
880 void rtems_iterate_over_all_threads(
881  rtems_per_thread_routine routine
882 ) RTEMS_DEPRECATED;
883 
887 typedef struct {
894 
901 
916 
924 extern const size_t _Thread_Control_add_on_count;
925 
931 extern const size_t _Thread_Initial_thread_count;
932 
939 extern const size_t _Thread_Maximum_name_size;
940 
948 
955 #if defined(RTEMS_SMP)
957 #else
959 #endif
960 
966 #if defined(RTEMS_SMP)
967 extern const size_t _Thread_queue_Heads_size;
968 #else
969 #define _Thread_queue_Heads_size sizeof(Thread_queue_Heads)
970 #endif
971 
975 typedef struct {
980 
984  union {
990  Thread_queue_Configured_heads *initial;
991 
998  } Thread_queue_heads;
1000 
1005 
1006 #define THREAD_INFORMATION_DEFINE_ZERO( name, api, cls ) \
1007 Thread_Information name##_Information = { \
1008  { \
1009  _Objects_Build_id( api, cls, 1, 0 ), \
1010  NULL, \
1011  0, \
1012  0, \
1013  0, \
1014  0, \
1015  CHAIN_INITIALIZER_EMPTY( name##_Information.Objects.Inactive ), \
1016  NULL, \
1017  NULL, \
1018  NULL \
1019  OBJECTS_INFORMATION_MP( name##_Information.Objects, NULL ), \
1020  }, { \
1021  NULL \
1022  } \
1023 }
1024 
1025 #define THREAD_INFORMATION_DEFINE( name, api, cls, max ) \
1026 static Objects_Control * \
1027 name##_Local_table[ _Objects_Maximum_per_allocation( max ) ]; \
1028 static Thread_Configured_control \
1029 name##_Objects[ _Objects_Maximum_per_allocation( max ) ]; \
1030 static Thread_queue_Configured_heads \
1031 name##_Heads[ _Objects_Maximum_per_allocation( max ) ]; \
1032 Thread_Information name##_Information = { \
1033  { \
1034  _Objects_Build_id( api, cls, 1, _Objects_Maximum_per_allocation( max ) ), \
1035  name##_Local_table, \
1036  0, \
1037  _Objects_Is_unlimited( max ) ? _Objects_Maximum_per_allocation( max ) : 0, \
1038  sizeof( Thread_Configured_control ), \
1039  OBJECTS_NO_STRING_NAME, \
1040  CHAIN_INITIALIZER_EMPTY( name##_Information.Objects.Inactive ), \
1041  NULL, \
1042  NULL, \
1043  &name##_Objects[ 0 ].Control.Object \
1044  OBJECTS_INFORMATION_MP( name##_Information.Objects, NULL ) \
1045  }, { \
1046  &name##_Heads[ 0 ] \
1047  } \
1048 }
1049 
1052 #ifdef __cplusplus
1053 }
1054 #endif
1055 
1056 #endif
1057 /* end of include file */
const size_t _Thread_Control_add_on_count
Thread control add-on count.
Context_Control_fp * fp_context
Definition: thread.h:205
Definition: chain.h:65
Definition: mppkt.h:80
Timestamp_Control cpu_time_used
Definition: thread.h:823
Thread_Life_state state
The current thread life state.
Definition: thread.h:696
Definition: thread.h:179
const Thread_queue_Operations * operations
The current thread queue operations.
Definition: thread.h:494
int64_t Timestamp_Control
Definition: timestamp.h:52
Thread_CPU_budget_algorithm_callout budget_callout
Definition: thread.h:819
Thread_Wait_information Wait
Definition: thread.h:543
Information About the Thread Stack Handler.
Definition: objectdata.h:39
Information About Each Thread&#39;s Context.
Data for thread entry with one pointer argument and a pointer return value.
Definition: thread.h:123
bool is_preemptible
Definition: thread.h:798
Thread_Wait_information Wait
Definition: thread.h:770
void(* Thread_Action_handler)(Thread_Control *the_thread, Thread_Action *action, ISR_lock_Context *lock_context)
Thread action handler.
Definition: thread.h:616
The priority node to build up a priority aggregation.
Definition: priority.h:94
Data for idle thread entry.
Definition: thread.h:107
Thread entry information.
Definition: thread.h:131
Thread register context.
Definition: cpu.h:196
Union type to hold a pointer to an immutable or a mutable object.
Definition: thread.h:367
uint32_t pending_life_change_requests
The count of pending life change requests.
Definition: thread.h:701
Definition: thread.h:714
#define ISR_LOCK_MEMBER(_designator)
Defines an ISR lock member.
Definition: isrlock.h:89
Thread_Start_information Start
Definition: thread.h:828
Thread_queue_Control Join_queue
Definition: thread.h:522
The control block used to manage each watchdog timer.
Definition: watchdog.h:87
Definition: chain.h:83
Per-thread information for POSIX Keys.
Definition: thread.h:644
void * return_argument
Definition: thread.h:405
uint32_t return_code
Definition: thread.h:416
size_t destination_offset
Offset of the pointer field in Thread_Control referencing an application configuration dependent memo...
Definition: thread.h:893
Helpers for Manipulating Timestamps.
Thread Execution State Information.
Thread_Entry_information Entry
Definition: thread.h:181
Thread_Keys_information Keys
The POSIX Keys information.
Definition: thread.h:848
Atomic Operations API.
Chain iterator for dynamic user extensions.
Definition: userextimpl.h:45
Definition: threadq.h:547
Thread_Scheduler_control Scheduler
Scheduler related control.
Definition: thread.h:767
Information required to manage a thread timer.
Definition: thread.h:502
void * tls_area
Definition: thread.h:210
Thread_queue_Queue * queue
The current thread queue.
Definition: thread.h:485
Definition: thread.h:587
struct _pthread_cleanup_context * last_cleanup_context
LIFO list of POSIX cleanup contexts.
Definition: thread.h:862
Thread_Timer_information Timer
Definition: thread.h:545
Objects_Information Objects
The object information.
Definition: thread.h:979
Red-black tree node.
Definition: rbtree.h:50
Constants and Structures Needed to Declare a Thread Queue.
Context_Control_fp * fp_context
Definition: thread.h:838
Definition: threadq.h:397
ISR lock control.
Definition: isrlock.h:56
const size_t _Thread_Initial_thread_count
Count of configured threads.
Thread_queue_Heads Thread_queue_Configured_heads
The configured thread queue heads.
Definition: thread.h:958
bool is_fp
Definition: thread.h:800
struct User_extensions_Iterator * last_user_extensions_iterator
LIFO list of user extensions iterators.
Definition: thread.h:867
Information required to manage a thread while it is blocked.
Definition: thread.h:394
Thread queue operations.
Definition: threadq.h:512
Object Handler Data Structures.
struct _reent * libc_reent
Definition: thread.h:841
uint64_t Priority_Control
The thread priority control.
Definition: priority.h:66
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:806
Thread_Information _Thread_Information
The internal thread objects information.
bool core_allocated_stack
Definition: thread.h:199
Thread queue heads.
Definition: threadq.h:355
size_t source_offset
Offset relative to the thread control block begin to an application configuration dependent memory ar...
Definition: thread.h:899
Thread_CPU_budget_algorithms budget_algorithm
Definition: thread.h:817
void(* Thread_CPU_budget_algorithm_callout)(Thread_Control *)
Definition: thread.h:173
Definition: thread.h:728
const size_t _Thread_Maximum_name_size
Maximum size of a thread name in characters (including the terminating &#39;\0&#39; character).
Control block to manage thread actions.
Definition: thread.h:661
Per CPU Core Structure.
Definition: percpu.h:290
bool is_preemptible
Definition: thread.h:186
Thread control add-on.
Definition: thread.h:887
The thread object information.
Definition: thread.h:975
Thread action.
Definition: thread.h:636
Objects_Control Object
Definition: thread.h:730
Thread_Life_state
Thread life states.
Definition: thread.h:675
Specification for the Packet Handler.
States_Control current_state
Definition: thread.h:752
RBTree_Control Key_value_pairs
Key value pairs registered for this thread.
Definition: thread.h:648
Priority Handler API.
The information structure used to manage each API class of objects.
Definition: objectdata.h:160
uint32_t isr_level
Definition: thread.h:194
Constants and Structures Associated with Watchdog Timers.
SPARC basic context.
Definition: cpu.h:242
Definition: intercom.c:74
void * exit_value
The thread exit value.
Definition: thread.h:711
Priority_Control initial_priority
Definition: thread.h:196
Thread_CPU_budget_algorithm_callout budget_callout
Definition: thread.h:192
Freechain_Control Free
The free thread queue heads.
Definition: thread.h:997
#define _Thread_queue_Heads_size
Size of the thread queue heads of a particular application.
Definition: thread.h:969
Definition: thread.h:589
uint32_t States_Control
Definition: states.h:41
Stack_Control Initial_stack
Definition: thread.h:202
Thread_queue_Configured_heads * initial
Contains the initial set of thread queue heads.
Definition: thread.h:990
States_Control current_state
Definition: thread.h:525
uint32_t count
Definition: thread.h:403
Data for thread entry with one numeric argument and no return value.
Definition: thread.h:114
The watchdog header to manage scheduled watchdogs.
Definition: watchdog.h:68
Thread scheduler control.
Definition: thread.h:246
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:102
struct Thread_Configured_control Thread_Configured_control
The configured thread control block.
Definition: thread.h:947
Freechain Handler API.
Priority_Node Real_priority
The base priority of this thread in its home scheduler instance.
Definition: thread.h:530
Thread_queue_Control Join_queue
Thread queue for thread join operations and multi-purpose lock.
Definition: thread.h:749
Scheduler control.
Definition: scheduler.h:266
Scheduler node for per-thread data.
Definition: schedulernode.h:65
Thread_Wait_information_Object_argument_type return_argument_second
Definition: thread.h:408
Thread_Wait_flags flags
This field contains several flags used to control the wait class and state of a thread in case fine-g...
Definition: thread.h:425
Definition: thread.h:515
Processor Mask API.
Definition: stack.h:48
Objects_Control Object
Definition: thread.h:517
uintptr_t CPU_Uint32ptr
Definition: cpu.h:668
Thread_APIs
Definition: thread.h:585
Thread_CPU_budget_algorithms budget_algorithm
Definition: thread.h:188
Scheduler_Node * nodes
The scheduler nodes of this thread.
Definition: thread.h:354
void * stack
Definition: thread.h:208
Thread life control.
Definition: thread.h:686
Local ISR lock context for acquire and release pairs.
Definition: isrlock.h:65
Definition: mmu-config.c:39
Thread_Action Action
Thread life action used to react upon thread restart and delete requests.
Definition: thread.h:691
uint32_t Objects_Id
Definition: object.h:75
Context_Control Registers
Definition: thread.h:833
Thread_Life_control Life
Thread life-cycle control.
Definition: thread.h:855
Priority_Node Real_priority
The base priority of this thread in its home scheduler instance.
Definition: thread.h:757
const Thread_Control_add_on _Thread_Control_add_ons[]
Thread control add-ons.
ISR Locks.
Thread_Scheduler_control Scheduler
Scheduler related control.
Definition: thread.h:540
uint32_t option
Definition: thread.h:410
uint32_t cpu_time_budget
Definition: thread.h:812
Thread_CPU_budget_algorithms
Definition: thread.h:159
#define THREAD_API_LAST
Definition: thread.h:596
Thread_Timer_information Timer
Definition: thread.h:772
The freechain control.
Definition: freechain.h:46
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:386
bool is_idle
Definition: thread.h:792