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