1 | /** |
---|
2 | * @file rtems/score/thread.h |
---|
3 | * |
---|
4 | * @brief Constants and Structures Related with the Thread Control Block |
---|
5 | * |
---|
6 | * This include file contains all constants and structures associated |
---|
7 | * with the thread control block. |
---|
8 | */ |
---|
9 | |
---|
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/isrlock.h> |
---|
30 | #include <rtems/score/object.h> |
---|
31 | #include <rtems/score/priority.h> |
---|
32 | #include <rtems/score/resource.h> |
---|
33 | #include <rtems/score/stack.h> |
---|
34 | #include <rtems/score/states.h> |
---|
35 | #include <rtems/score/threadq.h> |
---|
36 | #include <rtems/score/timestamp.h> |
---|
37 | #include <rtems/score/watchdog.h> |
---|
38 | |
---|
39 | #if defined(RTEMS_SMP) |
---|
40 | #include <rtems/score/cpuset.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | struct _pthread_cleanup_context; |
---|
44 | |
---|
45 | struct Per_CPU_Control; |
---|
46 | |
---|
47 | struct Scheduler_Control; |
---|
48 | |
---|
49 | struct Scheduler_Node; |
---|
50 | |
---|
51 | struct User_extensions_Iterator; |
---|
52 | |
---|
53 | #ifdef __cplusplus |
---|
54 | extern "C" { |
---|
55 | #endif |
---|
56 | |
---|
57 | /** |
---|
58 | * @defgroup ScoreThread Thread Handler |
---|
59 | * |
---|
60 | * @ingroup Score |
---|
61 | * |
---|
62 | * This handler encapsulates functionality related to the management of |
---|
63 | * threads. This includes the creation, deletion, and scheduling of threads. |
---|
64 | * |
---|
65 | * The following variables are maintained as part of the per cpu data |
---|
66 | * structure. |
---|
67 | * |
---|
68 | * + Idle thread pointer |
---|
69 | * + Executing thread pointer |
---|
70 | * + Heir thread pointer |
---|
71 | */ |
---|
72 | /**@{*/ |
---|
73 | |
---|
74 | #if defined(RTEMS_POSIX_API) |
---|
75 | #define RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE |
---|
76 | #endif |
---|
77 | |
---|
78 | /* |
---|
79 | * With the addition of the Constant Block Scheduler (CBS), |
---|
80 | * this feature is needed even when POSIX is disabled. |
---|
81 | */ |
---|
82 | #define RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT |
---|
83 | |
---|
84 | #if defined(RTEMS_POSIX_API) |
---|
85 | #define RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API |
---|
86 | #endif |
---|
87 | |
---|
88 | /* |
---|
89 | * Only provided for backward compatiblity to not break application |
---|
90 | * configurations. |
---|
91 | */ |
---|
92 | typedef void *Thread RTEMS_DEPRECATED; |
---|
93 | |
---|
94 | /** |
---|
95 | * @brief Type of the numeric argument of a thread entry function with at |
---|
96 | * least one numeric argument. |
---|
97 | * |
---|
98 | * This numeric argument type designates an unsigned integer type with the |
---|
99 | * property that any valid pointer to void can be converted to this type and |
---|
100 | * then converted back to a pointer to void. The result will compare equal to |
---|
101 | * the original pointer. |
---|
102 | */ |
---|
103 | typedef CPU_Uint32ptr Thread_Entry_numeric_type; |
---|
104 | |
---|
105 | /** |
---|
106 | * @brief Data for idle thread entry. |
---|
107 | */ |
---|
108 | typedef struct { |
---|
109 | void *( *entry )( uintptr_t argument ); |
---|
110 | } Thread_Entry_idle; |
---|
111 | |
---|
112 | /** |
---|
113 | * @brief Data for thread entry with one numeric argument and no return value. |
---|
114 | */ |
---|
115 | typedef struct { |
---|
116 | void ( *entry )( Thread_Entry_numeric_type argument ); |
---|
117 | Thread_Entry_numeric_type argument; |
---|
118 | } Thread_Entry_numeric; |
---|
119 | |
---|
120 | /** |
---|
121 | * @brief Data for thread entry with one pointer argument and a pointer return |
---|
122 | * value. |
---|
123 | */ |
---|
124 | typedef struct { |
---|
125 | void *( *entry )( void *argument ); |
---|
126 | void *argument; |
---|
127 | } Thread_Entry_pointer; |
---|
128 | |
---|
129 | /** |
---|
130 | * @brief Thread entry information. |
---|
131 | */ |
---|
132 | typedef struct { |
---|
133 | /** |
---|
134 | * @brief Thread entry adaptor. |
---|
135 | * |
---|
136 | * Calls the corresponding thread entry with the right parameters. |
---|
137 | * |
---|
138 | * @param executing The executing thread. |
---|
139 | */ |
---|
140 | void ( *adaptor )( Thread_Control *executing ); |
---|
141 | |
---|
142 | /** |
---|
143 | * @brief Thread entry data used by the adaptor to call the thread entry |
---|
144 | * function with the right parameters. |
---|
145 | */ |
---|
146 | union { |
---|
147 | Thread_Entry_idle Idle; |
---|
148 | Thread_Entry_numeric Numeric; |
---|
149 | Thread_Entry_pointer Pointer; |
---|
150 | } Kinds; |
---|
151 | } Thread_Entry_information; |
---|
152 | |
---|
153 | /** |
---|
154 | * The following lists the algorithms used to manage the thread cpu budget. |
---|
155 | * |
---|
156 | * Reset Timeslice: At each context switch, reset the time quantum. |
---|
157 | * Exhaust Timeslice: Only reset the quantum once it is consumed. |
---|
158 | * Callout: Execute routine when budget is consumed. |
---|
159 | */ |
---|
160 | typedef enum { |
---|
161 | THREAD_CPU_BUDGET_ALGORITHM_NONE, |
---|
162 | THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE, |
---|
163 | #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE) |
---|
164 | THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE, |
---|
165 | #endif |
---|
166 | #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT) |
---|
167 | THREAD_CPU_BUDGET_ALGORITHM_CALLOUT |
---|
168 | #endif |
---|
169 | } Thread_CPU_budget_algorithms; |
---|
170 | |
---|
171 | /** This defines thes the entry point for the thread specific timeslice |
---|
172 | * budget management algorithm. |
---|
173 | */ |
---|
174 | typedef void (*Thread_CPU_budget_algorithm_callout )( Thread_Control * ); |
---|
175 | |
---|
176 | /** |
---|
177 | * The following structure contains the information which defines |
---|
178 | * the starting state of a thread. |
---|
179 | */ |
---|
180 | typedef struct { |
---|
181 | /** This field contains the thread entry information. */ |
---|
182 | Thread_Entry_information Entry; |
---|
183 | /*-------------- initial execution modes ----------------- */ |
---|
184 | /** This field indicates whether the thread was preemptible when |
---|
185 | * it started. |
---|
186 | */ |
---|
187 | bool is_preemptible; |
---|
188 | /** This field indicates the CPU budget algorith. */ |
---|
189 | Thread_CPU_budget_algorithms budget_algorithm; |
---|
190 | /** This field is the routine to invoke when the CPU allotment is |
---|
191 | * consumed. |
---|
192 | */ |
---|
193 | Thread_CPU_budget_algorithm_callout budget_callout; |
---|
194 | /** This field is the initial ISR disable level of this thread. */ |
---|
195 | uint32_t isr_level; |
---|
196 | /** This field is the initial priority. */ |
---|
197 | Priority_Control initial_priority; |
---|
198 | #if defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API) |
---|
199 | /** This field indicates whether the SuperCore allocated the stack. */ |
---|
200 | bool core_allocated_stack; |
---|
201 | #endif |
---|
202 | /** This field is the stack information. */ |
---|
203 | Stack_Control Initial_stack; |
---|
204 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
205 | /** This field is the initial FP context area address. */ |
---|
206 | Context_Control_fp *fp_context; |
---|
207 | #endif |
---|
208 | /** This field is the initial stack area address. */ |
---|
209 | void *stack; |
---|
210 | /** The thread-local storage (TLS) area */ |
---|
211 | void *tls_area; |
---|
212 | } Thread_Start_information; |
---|
213 | |
---|
214 | /** |
---|
215 | * @brief Union type to hold a pointer to an immutable or a mutable object. |
---|
216 | * |
---|
217 | * The main purpose is to enable passing of pointers to read-only send buffers |
---|
218 | * in the message passing subsystem. This approach is somewhat fragile since |
---|
219 | * it prevents the compiler to check if the operations on objects are valid |
---|
220 | * with respect to the constant qualifier. An alternative would be to add a |
---|
221 | * third pointer argument for immutable objects, but this would increase the |
---|
222 | * structure size. |
---|
223 | */ |
---|
224 | typedef union { |
---|
225 | void *mutable_object; |
---|
226 | const void *immutable_object; |
---|
227 | } Thread_Wait_information_Object_argument_type; |
---|
228 | |
---|
229 | /** |
---|
230 | * @brief This type is able to contain several flags used to control the wait |
---|
231 | * class and state of a thread. |
---|
232 | * |
---|
233 | * The mutually exclusive wait class flags are |
---|
234 | * - @ref THREAD_WAIT_CLASS_EVENT, |
---|
235 | * - @ref THREAD_WAIT_CLASS_SYSTEM_EVENT, and |
---|
236 | * - @ref THREAD_WAIT_CLASS_OBJECT. |
---|
237 | * |
---|
238 | * The mutually exclusive wait state flags are |
---|
239 | * - @ref THREAD_WAIT_STATE_INTEND_TO_BLOCK, |
---|
240 | * - @ref THREAD_WAIT_STATE_BLOCKED, and |
---|
241 | * - @ref THREAD_WAIT_STATE_READY_AGAIN. |
---|
242 | */ |
---|
243 | typedef unsigned int Thread_Wait_flags; |
---|
244 | |
---|
245 | /** |
---|
246 | * @brief Information required to manage a thread while it is blocked. |
---|
247 | * |
---|
248 | * This contains the information required to manage a thread while it is |
---|
249 | * blocked and to return information to it. |
---|
250 | */ |
---|
251 | typedef struct { |
---|
252 | /** |
---|
253 | * @brief Node for thread queues. |
---|
254 | */ |
---|
255 | union { |
---|
256 | /** |
---|
257 | * @brief A node for chains. |
---|
258 | */ |
---|
259 | Chain_Node Chain; |
---|
260 | |
---|
261 | /** |
---|
262 | * @brief A node for red-black trees. |
---|
263 | */ |
---|
264 | RBTree_Node RBTree; |
---|
265 | } Node; |
---|
266 | |
---|
267 | #if defined(RTEMS_MULTIPROCESSING) |
---|
268 | /* |
---|
269 | * @brief This field is the identifier of the remote object this thread is |
---|
270 | * waiting upon. |
---|
271 | */ |
---|
272 | Objects_Id remote_id; |
---|
273 | #endif |
---|
274 | /** This field is used to return an integer while when blocked. */ |
---|
275 | uint32_t count; |
---|
276 | /** This field is for a pointer to a user return argument. */ |
---|
277 | void *return_argument; |
---|
278 | /** This field is for a pointer to a second user return argument. */ |
---|
279 | Thread_Wait_information_Object_argument_type |
---|
280 | return_argument_second; |
---|
281 | /** This field contains any options in effect on this blocking operation. */ |
---|
282 | uint32_t option; |
---|
283 | /** This field will contain the return status from a blocking operation. |
---|
284 | * |
---|
285 | * @note The following assumes that all API return codes can be |
---|
286 | * treated as an uint32_t. |
---|
287 | */ |
---|
288 | uint32_t return_code; |
---|
289 | |
---|
290 | /** |
---|
291 | * @brief This field contains several flags used to control the wait class |
---|
292 | * and state of a thread in case fine-grained locking is used. |
---|
293 | */ |
---|
294 | #if defined(RTEMS_SMP) |
---|
295 | Atomic_Uint flags; |
---|
296 | #else |
---|
297 | Thread_Wait_flags flags; |
---|
298 | #endif |
---|
299 | |
---|
300 | #if defined(RTEMS_SMP) |
---|
301 | /** |
---|
302 | * @brief Thread wait lock control block. |
---|
303 | * |
---|
304 | * Parts of the thread wait information are protected by the thread wait |
---|
305 | * default lock and additionally a thread queue lock in case the thread |
---|
306 | * is enqueued on a thread queue. |
---|
307 | * |
---|
308 | * The thread wait lock mechanism protects the following thread variables |
---|
309 | * - POSIX_API_Control::Attributes, |
---|
310 | * - Thread_Control::current_priority, |
---|
311 | * - Thread_Control::Wait::Lock::Pending_requests, |
---|
312 | * - Thread_Control::Wait::queue, and |
---|
313 | * - Thread_Control::Wait::operations. |
---|
314 | * |
---|
315 | * @see _Thread_Wait_acquire(), _Thread_Wait_release(), _Thread_Wait_claim(), |
---|
316 | * _Thread_Wait_restore_default() and _Thread_Wait_tranquilize(). |
---|
317 | */ |
---|
318 | struct { |
---|
319 | /** |
---|
320 | * @brief Thread wait default lock. |
---|
321 | */ |
---|
322 | ISR_lock_Control Default; |
---|
323 | |
---|
324 | /** |
---|
325 | * @brief The pending thread wait lock acquire or tranquilize requests in |
---|
326 | * case the thread is enqueued on a thread queue. |
---|
327 | */ |
---|
328 | Chain_Control Pending_requests; |
---|
329 | |
---|
330 | /** |
---|
331 | * @brief Tranquilizer gate used by _Thread_Wait_tranquilize(). |
---|
332 | * |
---|
333 | * This gate is closed by _Thread_Wait_claim(). In case there are no |
---|
334 | * pending requests during a _Thread_Wait_restore_default(), then this gate |
---|
335 | * is opened immediately, otherwise it is placed on the pending request |
---|
336 | * chain and opened by _Thread_Wait_remove_request_locked() as the last |
---|
337 | * gate on the chain to signal overall request completion. |
---|
338 | */ |
---|
339 | Thread_queue_Gate Tranquilizer; |
---|
340 | } Lock; |
---|
341 | |
---|
342 | /** |
---|
343 | * @brief Thread queue link provided for use by the thread wait lock owner to |
---|
344 | * build a thread queue path. |
---|
345 | */ |
---|
346 | Thread_queue_Link Link; |
---|
347 | #endif |
---|
348 | |
---|
349 | /** |
---|
350 | * @brief The current thread queue. |
---|
351 | * |
---|
352 | * If this field is NULL the thread is not enqueued on a thread queue. This |
---|
353 | * field is protected by the thread wait default lock. |
---|
354 | * |
---|
355 | * @see _Thread_Wait_claim(). |
---|
356 | */ |
---|
357 | Thread_queue_Queue *queue; |
---|
358 | |
---|
359 | /** |
---|
360 | * @brief The current thread queue operations. |
---|
361 | * |
---|
362 | * This field is protected by the thread lock wait default lock. |
---|
363 | * |
---|
364 | * @see _Thread_Wait_claim(). |
---|
365 | */ |
---|
366 | const Thread_queue_Operations *operations; |
---|
367 | |
---|
368 | Thread_queue_Heads *spare_heads; |
---|
369 | } Thread_Wait_information; |
---|
370 | |
---|
371 | /** |
---|
372 | * @brief Information required to manage a thread timer. |
---|
373 | */ |
---|
374 | typedef struct { |
---|
375 | ISR_LOCK_MEMBER( Lock ) |
---|
376 | Watchdog_Header *header; |
---|
377 | Watchdog_Control Watchdog; |
---|
378 | } Thread_Timer_information; |
---|
379 | |
---|
380 | /** |
---|
381 | * The following defines the control block used to manage |
---|
382 | * each thread proxy. |
---|
383 | * |
---|
384 | * @note It is critical that proxies and threads have identical |
---|
385 | * memory images for the shared part. |
---|
386 | */ |
---|
387 | typedef struct { |
---|
388 | /** This field is the object management structure for each proxy. */ |
---|
389 | Objects_Control Object; |
---|
390 | |
---|
391 | /** |
---|
392 | * @see Thread_Control::Join_queue |
---|
393 | */ |
---|
394 | Thread_queue_Control Join_queue; |
---|
395 | |
---|
396 | /** This field is the current execution state of this proxy. */ |
---|
397 | States_Control current_state; |
---|
398 | /** |
---|
399 | * @brief This field is the current priority state of this thread. |
---|
400 | * |
---|
401 | * Writes to this field are only allowed in _Thread_Initialize() or via |
---|
402 | * _Thread_Change_priority(). |
---|
403 | */ |
---|
404 | Priority_Control current_priority; |
---|
405 | |
---|
406 | /** |
---|
407 | * @brief This field is the base priority of this thread. |
---|
408 | * |
---|
409 | * Writes to this field are only allowed in _Thread_Initialize() or via |
---|
410 | * _Thread_Change_priority(). |
---|
411 | */ |
---|
412 | Priority_Control real_priority; |
---|
413 | |
---|
414 | /** |
---|
415 | * @brief Hints if a priority restore is necessary once the resource count |
---|
416 | * changes from one to zero. |
---|
417 | * |
---|
418 | * This is an optimization to speed up the mutex surrender sequence in case |
---|
419 | * no attempt to change the priority was made during the mutex ownership. On |
---|
420 | * SMP configurations atomic fences must synchronize writes to |
---|
421 | * Thread_Control::priority_restore_hint and Thread_Control::resource_count. |
---|
422 | */ |
---|
423 | bool priority_restore_hint; |
---|
424 | |
---|
425 | /** This field is the number of mutexes currently held by this proxy. */ |
---|
426 | uint32_t resource_count; |
---|
427 | |
---|
428 | /** This field is the blocking information for this proxy. */ |
---|
429 | Thread_Wait_information Wait; |
---|
430 | /** This field is the Watchdog used to manage proxy delays and timeouts. */ |
---|
431 | Thread_Timer_information Timer; |
---|
432 | #if defined(RTEMS_MULTIPROCESSING) |
---|
433 | /** This field is the received response packet in an MP system. */ |
---|
434 | MP_packet_Prefix *receive_packet; |
---|
435 | /****************** end of common block ********************/ |
---|
436 | |
---|
437 | /** |
---|
438 | * @brief Thread queue callout for _Thread_queue_Enqueue_critical(). |
---|
439 | */ |
---|
440 | Thread_queue_MP_callout thread_queue_callout; |
---|
441 | |
---|
442 | /** |
---|
443 | * @brief This field is used to manage the set of active proxies in the system. |
---|
444 | */ |
---|
445 | RBTree_Node Active; |
---|
446 | |
---|
447 | /** |
---|
448 | * @brief Provide thread queue heads for this thread proxy. |
---|
449 | * |
---|
450 | * The actual size of the thread queue heads depends on the application |
---|
451 | * configuration. Since thread proxies are never destroyed we can use the |
---|
452 | * same storage place for the thread queue heads. |
---|
453 | */ |
---|
454 | Thread_queue_Heads Thread_queue_heads[ RTEMS_ZERO_LENGTH_ARRAY ]; |
---|
455 | #endif |
---|
456 | } Thread_Proxy_control; |
---|
457 | |
---|
458 | /** |
---|
459 | * The following record defines the control block used |
---|
460 | * to manage each thread. |
---|
461 | * |
---|
462 | * @note It is critical that proxies and threads have identical |
---|
463 | * memory images for the shared part. |
---|
464 | */ |
---|
465 | typedef enum { |
---|
466 | /** This value is for the Classic RTEMS API. */ |
---|
467 | THREAD_API_RTEMS, |
---|
468 | /** This value is for the POSIX API. */ |
---|
469 | THREAD_API_POSIX |
---|
470 | } Thread_APIs; |
---|
471 | |
---|
472 | /** This macro defines the first API which has threads. */ |
---|
473 | #define THREAD_API_FIRST THREAD_API_RTEMS |
---|
474 | |
---|
475 | /** This macro defines the last API which has threads. */ |
---|
476 | #define THREAD_API_LAST THREAD_API_POSIX |
---|
477 | |
---|
478 | typedef struct Thread_Action Thread_Action; |
---|
479 | |
---|
480 | /** |
---|
481 | * @brief Thread action handler. |
---|
482 | * |
---|
483 | * The thread action handler will be called with interrupts disabled and a |
---|
484 | * corresponding lock acquired, e.g. _Thread_State_acquire(). The handler must |
---|
485 | * release the corresponding lock, e.g. _Thread_State_release(). So, the |
---|
486 | * corresponding lock may be used to protect private data used by the |
---|
487 | * particular action. |
---|
488 | * |
---|
489 | * Since the action is passed to the handler additional data may be accessed |
---|
490 | * via RTEMS_CONTAINER_OF(). |
---|
491 | * |
---|
492 | * @param[in] the_thread The thread performing the action. |
---|
493 | * @param[in] action The thread action. |
---|
494 | * @param[in] lock_context The lock context to use for the lock release. |
---|
495 | */ |
---|
496 | typedef void ( *Thread_Action_handler )( |
---|
497 | Thread_Control *the_thread, |
---|
498 | Thread_Action *action, |
---|
499 | ISR_lock_Context *lock_context |
---|
500 | ); |
---|
501 | |
---|
502 | /** |
---|
503 | * @brief Thread action. |
---|
504 | * |
---|
505 | * Thread actions can be chained together to trigger a set of actions on |
---|
506 | * particular events like for example a thread post-switch. Use |
---|
507 | * _Thread_Action_initialize() to initialize this structure. |
---|
508 | * |
---|
509 | * Thread actions are the building block for efficient implementation of |
---|
510 | * - Classic signals delivery, |
---|
511 | * - POSIX signals delivery, and |
---|
512 | * - thread life-cycle changes. |
---|
513 | * |
---|
514 | * @see _Thread_Add_post_switch_action() and _Thread_Run_post_switch_actions(). |
---|
515 | */ |
---|
516 | struct Thread_Action { |
---|
517 | Chain_Node Node; |
---|
518 | Thread_Action_handler handler; |
---|
519 | }; |
---|
520 | |
---|
521 | /** |
---|
522 | * @brief Per-thread information for POSIX Keys. |
---|
523 | */ |
---|
524 | typedef struct { |
---|
525 | /** |
---|
526 | * @brief Key value pairs registered for this thread. |
---|
527 | */ |
---|
528 | RBTree_Control Key_value_pairs; |
---|
529 | |
---|
530 | /** |
---|
531 | * @brief Lock to protect the tree operations. |
---|
532 | */ |
---|
533 | ISR_LOCK_MEMBER( Lock ) |
---|
534 | } Thread_Keys_information; |
---|
535 | |
---|
536 | /** |
---|
537 | * @brief Control block to manage thread actions. |
---|
538 | * |
---|
539 | * Use _Thread_Action_control_initialize() to initialize this structure. |
---|
540 | */ |
---|
541 | typedef struct { |
---|
542 | Chain_Control Chain; |
---|
543 | } Thread_Action_control; |
---|
544 | |
---|
545 | /** |
---|
546 | * @brief Thread life states. |
---|
547 | * |
---|
548 | * The thread life states are orthogonal to the thread states used for |
---|
549 | * synchronization primitives and blocking operations. They reflect the state |
---|
550 | * changes triggered with thread restart and delete requests. |
---|
551 | * |
---|
552 | * The individual state values must be a power of two to allow use of bit |
---|
553 | * operations to manipulate and evaluate the thread life state. |
---|
554 | */ |
---|
555 | typedef enum { |
---|
556 | THREAD_LIFE_PROTECTED = 0x1, |
---|
557 | THREAD_LIFE_RESTARTING = 0x2, |
---|
558 | THREAD_LIFE_TERMINATING = 0x4, |
---|
559 | THREAD_LIFE_CHANGE_DEFERRED = 0x8, |
---|
560 | THREAD_LIFE_DETACHED = 0x10 |
---|
561 | } Thread_Life_state; |
---|
562 | |
---|
563 | /** |
---|
564 | * @brief Thread life control. |
---|
565 | */ |
---|
566 | typedef struct { |
---|
567 | /** |
---|
568 | * @brief Thread life action used to react upon thread restart and delete |
---|
569 | * requests. |
---|
570 | */ |
---|
571 | Thread_Action Action; |
---|
572 | |
---|
573 | /** |
---|
574 | * @brief The current thread life state. |
---|
575 | */ |
---|
576 | Thread_Life_state state; |
---|
577 | |
---|
578 | /** |
---|
579 | * @brief The count of pending life change requests. |
---|
580 | */ |
---|
581 | uint32_t pending_life_change_requests; |
---|
582 | |
---|
583 | #if defined(RTEMS_POSIX_API) |
---|
584 | /** |
---|
585 | * @brief The thread exit value. |
---|
586 | * |
---|
587 | * It is, |
---|
588 | * - the value passed to pthread_exit(), or |
---|
589 | * - PTHREAD_CANCELED in case it is cancelled via pthread_cancel(), or |
---|
590 | * - NULL. |
---|
591 | */ |
---|
592 | void *exit_value; |
---|
593 | #endif |
---|
594 | } Thread_Life_control; |
---|
595 | |
---|
596 | #if defined(RTEMS_SMP) |
---|
597 | /** |
---|
598 | * @brief The thread state with respect to the scheduler. |
---|
599 | */ |
---|
600 | typedef enum { |
---|
601 | /** |
---|
602 | * @brief This thread is blocked with respect to the scheduler. |
---|
603 | * |
---|
604 | * This thread uses no scheduler nodes. |
---|
605 | */ |
---|
606 | THREAD_SCHEDULER_BLOCKED, |
---|
607 | |
---|
608 | /** |
---|
609 | * @brief This thread is scheduled with respect to the scheduler. |
---|
610 | * |
---|
611 | * This thread executes using one of its scheduler nodes. This could be its |
---|
612 | * own scheduler node or in case it owns resources taking part in the |
---|
613 | * scheduler helping protocol a scheduler node of another thread. |
---|
614 | */ |
---|
615 | THREAD_SCHEDULER_SCHEDULED, |
---|
616 | |
---|
617 | /** |
---|
618 | * @brief This thread is ready with respect to the scheduler. |
---|
619 | * |
---|
620 | * None of the scheduler nodes of this thread is scheduled. |
---|
621 | */ |
---|
622 | THREAD_SCHEDULER_READY |
---|
623 | } Thread_Scheduler_state; |
---|
624 | #endif |
---|
625 | |
---|
626 | /** |
---|
627 | * @brief Thread scheduler control. |
---|
628 | */ |
---|
629 | typedef struct { |
---|
630 | #if defined(RTEMS_SMP) |
---|
631 | /** |
---|
632 | * @brief The current scheduler state of this thread. |
---|
633 | */ |
---|
634 | Thread_Scheduler_state state; |
---|
635 | |
---|
636 | /** |
---|
637 | * @brief The own scheduler control of this thread. |
---|
638 | * |
---|
639 | * This field is constant after initialization. |
---|
640 | */ |
---|
641 | const struct Scheduler_Control *own_control; |
---|
642 | |
---|
643 | /** |
---|
644 | * @brief The scheduler control of this thread. |
---|
645 | * |
---|
646 | * The scheduler helping protocol may change this field. |
---|
647 | */ |
---|
648 | const struct Scheduler_Control *control; |
---|
649 | |
---|
650 | /** |
---|
651 | * @brief The own scheduler node of this thread. |
---|
652 | * |
---|
653 | * This field is constant after initialization. It is used by change |
---|
654 | * priority and ask for help operations. |
---|
655 | */ |
---|
656 | struct Scheduler_Node *own_node; |
---|
657 | #endif |
---|
658 | |
---|
659 | /** |
---|
660 | * @brief The scheduler node of this thread. |
---|
661 | * |
---|
662 | * On uni-processor configurations this field is constant after |
---|
663 | * initialization. |
---|
664 | * |
---|
665 | * On SMP configurations the scheduler helping protocol may change this |
---|
666 | * field. |
---|
667 | */ |
---|
668 | struct Scheduler_Node *node; |
---|
669 | |
---|
670 | #if defined(RTEMS_SMP) |
---|
671 | /** |
---|
672 | * @brief The processor assigned by the current scheduler. |
---|
673 | */ |
---|
674 | struct Per_CPU_Control *cpu; |
---|
675 | |
---|
676 | #if defined(RTEMS_DEBUG) |
---|
677 | /** |
---|
678 | * @brief The processor on which this thread executed the last time or is |
---|
679 | * executing. |
---|
680 | */ |
---|
681 | struct Per_CPU_Control *debug_real_cpu; |
---|
682 | #endif |
---|
683 | #endif |
---|
684 | } Thread_Scheduler_control; |
---|
685 | |
---|
686 | typedef struct { |
---|
687 | uint32_t flags; |
---|
688 | void * control; |
---|
689 | }Thread_Capture_control; |
---|
690 | |
---|
691 | /** |
---|
692 | * This structure defines the Thread Control Block (TCB). |
---|
693 | * |
---|
694 | * Uses a leading underscore in the structure name to allow forward |
---|
695 | * declarations in standard header files provided by Newlib and GCC. |
---|
696 | */ |
---|
697 | struct _Thread_Control { |
---|
698 | /** This field is the object management structure for each thread. */ |
---|
699 | Objects_Control Object; |
---|
700 | |
---|
701 | /** |
---|
702 | * @brief Thread queue for thread join operations and multi-purpose lock. |
---|
703 | * |
---|
704 | * The lock of this thread queue is used for various purposes. It protects |
---|
705 | * the following fields |
---|
706 | * |
---|
707 | * - RTEMS_API_Control::Signal, |
---|
708 | * - Thread_Control::budget_algorithm, |
---|
709 | * - Thread_Control::budget_callout, |
---|
710 | * - Thread_Control::cpu_time_budget, |
---|
711 | * - Thread_Control::current_state, |
---|
712 | * - Thread_Control::Post_switch_actions, |
---|
713 | * - Thread_Control::Scheduler::control, and |
---|
714 | * - Thread_Control::Scheduler::own_control. |
---|
715 | * |
---|
716 | * @see _Thread_State_acquire(). |
---|
717 | */ |
---|
718 | Thread_queue_Control Join_queue; |
---|
719 | |
---|
720 | /** This field is the current execution state of this thread. */ |
---|
721 | States_Control current_state; |
---|
722 | |
---|
723 | /** |
---|
724 | * @brief This field is the current priority state of this thread. |
---|
725 | * |
---|
726 | * Writes to this field are only allowed in _Thread_Initialize() or via |
---|
727 | * _Thread_Change_priority(). |
---|
728 | */ |
---|
729 | Priority_Control current_priority; |
---|
730 | |
---|
731 | /** |
---|
732 | * @brief This field is the base priority of this thread. |
---|
733 | * |
---|
734 | * Writes to this field are only allowed in _Thread_Initialize() or via |
---|
735 | * _Thread_Change_priority(). |
---|
736 | */ |
---|
737 | Priority_Control real_priority; |
---|
738 | |
---|
739 | /** |
---|
740 | * @brief Hints if a priority restore is necessary once the resource count |
---|
741 | * changes from one to zero. |
---|
742 | * |
---|
743 | * This is an optimization to speed up the mutex surrender sequence in case |
---|
744 | * no attempt to change the priority was made during the mutex ownership. On |
---|
745 | * SMP configurations atomic fences must synchronize writes to |
---|
746 | * Thread_Control::priority_restore_hint and Thread_Control::resource_count. |
---|
747 | */ |
---|
748 | bool priority_restore_hint; |
---|
749 | |
---|
750 | /** This field is the number of mutexes currently held by this thread. */ |
---|
751 | uint32_t resource_count; |
---|
752 | /** This field is the blocking information for this thread. */ |
---|
753 | Thread_Wait_information Wait; |
---|
754 | /** This field is the Watchdog used to manage thread delays and timeouts. */ |
---|
755 | Thread_Timer_information Timer; |
---|
756 | #if defined(RTEMS_MULTIPROCESSING) |
---|
757 | /** This field is the received response packet in an MP system. */ |
---|
758 | MP_packet_Prefix *receive_packet; |
---|
759 | #endif |
---|
760 | /*================= end of common block =================*/ |
---|
761 | |
---|
762 | #if defined(RTEMS_SMP) && defined(RTEMS_PROFILING) |
---|
763 | /** |
---|
764 | * @brief Potpourri lock statistics. |
---|
765 | * |
---|
766 | * These SMP lock statistics are used for all lock objects that lack a |
---|
767 | * storage space for the statistics. Examples are lock objects used in |
---|
768 | * external libraries which are independent of the actual RTEMS build |
---|
769 | * configuration. |
---|
770 | */ |
---|
771 | SMP_lock_Stats Potpourri_stats; |
---|
772 | #endif |
---|
773 | |
---|
774 | #if defined(RTEMS_SMP) |
---|
775 | /** |
---|
776 | * @brief Resource node to build a dependency tree in case this thread owns |
---|
777 | * resources or depends on a resource. |
---|
778 | */ |
---|
779 | Resource_Node Resource_node; |
---|
780 | #endif |
---|
781 | #if defined(RTEMS_MULTIPROCESSING) |
---|
782 | /** This field is true if the thread is offered globally */ |
---|
783 | bool is_global; |
---|
784 | #endif |
---|
785 | /** This field is true if the thread is preemptible. */ |
---|
786 | bool is_preemptible; |
---|
787 | /** This field is true if the thread uses the floating point unit. */ |
---|
788 | bool is_fp; |
---|
789 | |
---|
790 | /** |
---|
791 | * @brief Scheduler related control. |
---|
792 | */ |
---|
793 | Thread_Scheduler_control Scheduler; |
---|
794 | |
---|
795 | #if __RTEMS_ADA__ |
---|
796 | /** This field is the GNAT self context pointer. */ |
---|
797 | void *rtems_ada_self; |
---|
798 | #endif |
---|
799 | /** This field is the length of the time quantum that this thread is |
---|
800 | * allowed to consume. The algorithm used to manage limits on CPU usage |
---|
801 | * is specified by budget_algorithm. |
---|
802 | */ |
---|
803 | uint32_t cpu_time_budget; |
---|
804 | /** This field is the algorithm used to manage this thread's time |
---|
805 | * quantum. The algorithm may be specified as none which case, |
---|
806 | * no limit is in place. |
---|
807 | */ |
---|
808 | Thread_CPU_budget_algorithms budget_algorithm; |
---|
809 | /** This field is the method invoked with the budgeted time is consumed. */ |
---|
810 | Thread_CPU_budget_algorithm_callout budget_callout; |
---|
811 | /** This field is the amount of CPU time consumed by this thread |
---|
812 | * since it was created. |
---|
813 | */ |
---|
814 | Timestamp_Control cpu_time_used; |
---|
815 | |
---|
816 | /** This field contains information about the starting state of |
---|
817 | * this thread. |
---|
818 | */ |
---|
819 | Thread_Start_information Start; |
---|
820 | |
---|
821 | Thread_Action_control Post_switch_actions; |
---|
822 | |
---|
823 | /** This field contains the context of this thread. */ |
---|
824 | Context_Control Registers; |
---|
825 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
826 | /** This field points to the floating point context for this thread. |
---|
827 | * If NULL, the thread is integer only. |
---|
828 | */ |
---|
829 | Context_Control_fp *fp_context; |
---|
830 | #endif |
---|
831 | /** This field points to the newlib reentrancy structure for this thread. */ |
---|
832 | struct _reent *libc_reent; |
---|
833 | /** This array contains the API extension area pointers. */ |
---|
834 | void *API_Extensions[ THREAD_API_LAST + 1 ]; |
---|
835 | |
---|
836 | /** |
---|
837 | * @brief The POSIX Keys information. |
---|
838 | */ |
---|
839 | Thread_Keys_information Keys; |
---|
840 | |
---|
841 | /** |
---|
842 | * @brief Thread life-cycle control. |
---|
843 | * |
---|
844 | * Control state changes triggered by thread restart and delete requests. |
---|
845 | */ |
---|
846 | Thread_Life_control Life; |
---|
847 | |
---|
848 | Thread_Capture_control Capture; |
---|
849 | |
---|
850 | /** |
---|
851 | * @brief LIFO list of POSIX cleanup contexts. |
---|
852 | */ |
---|
853 | struct _pthread_cleanup_context *last_cleanup_context; |
---|
854 | |
---|
855 | /** |
---|
856 | * @brief LIFO list of user extensions iterators. |
---|
857 | */ |
---|
858 | struct User_extensions_Iterator *last_user_extensions_iterator; |
---|
859 | |
---|
860 | /** |
---|
861 | * @brief Variable length array of user extension pointers. |
---|
862 | * |
---|
863 | * The length is defined by the application via <rtems/confdefs.h>. |
---|
864 | */ |
---|
865 | void *extensions[ RTEMS_ZERO_LENGTH_ARRAY ]; |
---|
866 | }; |
---|
867 | |
---|
868 | #if (CPU_PROVIDES_IDLE_THREAD_BODY == FALSE) |
---|
869 | /** |
---|
870 | * This routine is the body of the system idle thread. |
---|
871 | * |
---|
872 | * NOTE: This routine is actually instantiated by confdefs.h when needed. |
---|
873 | */ |
---|
874 | void *_Thread_Idle_body( |
---|
875 | uintptr_t ignored |
---|
876 | ); |
---|
877 | #endif |
---|
878 | |
---|
879 | /** This defines the type for a method which operates on a single thread. |
---|
880 | */ |
---|
881 | typedef void (*rtems_per_thread_routine)( Thread_Control * ); |
---|
882 | |
---|
883 | /** |
---|
884 | * @brief Iterates over all threads. |
---|
885 | * This routine iterates over all threads regardless of API and |
---|
886 | * invokes the specified routine. |
---|
887 | */ |
---|
888 | void rtems_iterate_over_all_threads( |
---|
889 | rtems_per_thread_routine routine |
---|
890 | ); |
---|
891 | |
---|
892 | /** |
---|
893 | * @brief Thread control add-on. |
---|
894 | */ |
---|
895 | typedef struct { |
---|
896 | /** |
---|
897 | * @brief Offset of the pointer field in Thread_Control referencing an |
---|
898 | * application configuration dependent memory area in the thread control |
---|
899 | * block. |
---|
900 | */ |
---|
901 | size_t destination_offset; |
---|
902 | |
---|
903 | /** |
---|
904 | * @brief Offset relative to the thread control block begin to an application |
---|
905 | * configuration dependent memory area. |
---|
906 | */ |
---|
907 | size_t source_offset; |
---|
908 | } Thread_Control_add_on; |
---|
909 | |
---|
910 | /** |
---|
911 | * @brief Thread control add-ons. |
---|
912 | * |
---|
913 | * The thread control block contains fields that point to application |
---|
914 | * configuration dependent memory areas, like the scheduler information, the |
---|
915 | * API control blocks, the user extension context table, and the Newlib |
---|
916 | * re-entrancy support. Account for these areas in the configuration and |
---|
917 | * avoid extra workspace allocations for these areas. |
---|
918 | * |
---|
919 | * This array is provided via <rtems/confdefs.h>. |
---|
920 | * |
---|
921 | * @see _Thread_Control_add_on_count and _Thread_Control_size. |
---|
922 | */ |
---|
923 | extern const Thread_Control_add_on _Thread_Control_add_ons[]; |
---|
924 | |
---|
925 | /** |
---|
926 | * @brief Thread control add-on count. |
---|
927 | * |
---|
928 | * Count of entries in _Thread_Control_add_ons. |
---|
929 | * |
---|
930 | * This value is provided via <rtems/confdefs.h>. |
---|
931 | */ |
---|
932 | extern const size_t _Thread_Control_add_on_count; |
---|
933 | |
---|
934 | /** |
---|
935 | * @brief Size of the thread control block of a particular application. |
---|
936 | * |
---|
937 | * This value is provided via <rtems/confdefs.h>. |
---|
938 | * |
---|
939 | * @see _Thread_Control_add_ons. |
---|
940 | */ |
---|
941 | extern const size_t _Thread_Control_size; |
---|
942 | |
---|
943 | /**@}*/ |
---|
944 | |
---|
945 | #ifdef __cplusplus |
---|
946 | } |
---|
947 | #endif |
---|
948 | |
---|
949 | #endif |
---|
950 | /* end of include file */ |
---|