source: rtems/cpukit/score/include/rtems/score/thread.h @ 69c3f076

4.115
Last change on this file since 69c3f076 was 69c3f076, checked in by Joel Sherrill <joel.sherrill@…>, on 05/28/14 at 21:53:58

rtems/score/thread*.h: Really correct spelling error

  • Property mode set to 100644
File size: 22.0 KB
Line 
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 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/context.h>
25#if defined(RTEMS_MULTIPROCESSING)
26#include <rtems/score/mppkt.h>
27#endif
28#include <rtems/score/object.h>
29#include <rtems/score/percpu.h>
30#include <rtems/score/priority.h>
31#include <rtems/score/stack.h>
32#include <rtems/score/states.h>
33#include <rtems/score/threadq.h>
34#include <rtems/score/watchdog.h>
35
36#if defined(RTEMS_SMP)
37  #include <rtems/score/cpuset.h>
38#endif
39
40struct Scheduler_Control;
41
42struct Scheduler_Node;
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/**
49 *  @defgroup ScoreThread Thread Handler
50 *
51 *  @ingroup Score
52 *
53 *  This handler encapsulates functionality related to the management of
54 *  threads.  This includes the creation, deletion, and scheduling of threads.
55 *
56 *  The following variables are maintained as part of the per cpu data
57 *  structure.
58 *
59 *  + Idle thread pointer
60 *  + Executing thread pointer
61 *  + Heir thread pointer
62 */
63/**@{*/
64
65#if defined(RTEMS_POSIX_API)
66  #define RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE
67#endif
68
69/*
70 * With the addition of the Constant Block Scheduler (CBS),
71 * this feature is needed even when POSIX is disabled.
72 */
73#define RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT
74
75#if defined(RTEMS_POSIX_API)
76  #define RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API
77#endif
78
79/*
80 *  The user can define this at configure time and go back to ticks
81 *  resolution.
82 */
83#ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
84  #include <rtems/score/timestamp.h>
85
86  typedef Timestamp_Control Thread_CPU_usage_t;
87#else
88  typedef uint32_t Thread_CPU_usage_t;
89#endif
90
91/**
92 *  The following defines the "return type" of a thread.
93 *
94 *  @note  This cannot always be right.  Some APIs have void
95 *         tasks/threads, others return pointers, others may
96 *         return a numeric value.  Hopefully a pointer is
97 *         always at least as big as an uint32_t  . :)
98 */
99typedef void *Thread;
100
101/**
102 *  @brief Type of the numeric argument of a thread entry function with at
103 *  least one numeric argument.
104 *
105 *  This numeric argument type designates an unsigned integer type with the
106 *  property that any valid pointer to void can be converted to this type and
107 *  then converted back to a pointer to void.  The result will compare equal to
108 *  the original pointer.
109 */
110typedef uintptr_t Thread_Entry_numeric_type;
111
112/**
113 *  The following defines the ways in which the entry point for a
114 *  thread can be invoked.  Basically, it can be passed any
115 *  combination/permutation of a pointer and an uint32_t   value.
116 *
117 *  @note For now, we are ignoring the return type.
118 */
119typedef enum {
120  THREAD_START_NUMERIC,
121  THREAD_START_POINTER,
122  #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API)
123    THREAD_START_BOTH_POINTER_FIRST,
124    THREAD_START_BOTH_NUMERIC_FIRST
125  #endif
126} Thread_Start_types;
127
128/** This type corresponds to a very simple style thread entry point. */
129typedef Thread ( *Thread_Entry )( void );   /* basic type */
130
131/** This type corresponds to a thread entry point which takes a single
132 *  unsigned thirty-two bit integer as an argument.
133 */
134typedef Thread ( *Thread_Entry_numeric )( Thread_Entry_numeric_type );
135
136/** This type corresponds to a thread entry point which takes a single
137 *  untyped pointer as an argument.
138 */
139typedef Thread ( *Thread_Entry_pointer )( void * );
140
141/** This type corresponds to a thread entry point which takes a single
142 *  untyped pointer and an unsigned thirty-two bit integer as arguments.
143 */
144typedef Thread ( *Thread_Entry_both_pointer_first )( void *, Thread_Entry_numeric_type );
145
146/** This type corresponds to a thread entry point which takes a single
147 *  unsigned thirty-two bit integer and an untyped pointer and an
148 *  as arguments.
149 */
150typedef Thread ( *Thread_Entry_both_numeric_first )( Thread_Entry_numeric_type, void * );
151
152/**
153 *  The following lists the algorithms used to manage the thread cpu budget.
154 *
155 *  Reset Timeslice:   At each context switch, reset the time quantum.
156 *  Exhaust Timeslice: Only reset the quantum once it is consumed.
157 *  Callout:           Execute routine when budget is consumed.
158 */
159typedef 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
168}  Thread_CPU_budget_algorithms;
169
170/**  This defines thes the entry point for the thread specific timeslice
171 *   budget management algorithm.
172 */
173typedef void (*Thread_CPU_budget_algorithm_callout )( Thread_Control * );
174
175#if !defined(RTEMS_SMP)
176/**
177 *  @brief Forward reference to the per task variable structure..
178 *
179 *  Forward reference to the per task variable structure.
180 */
181struct rtems_task_variable_tt;
182
183/**
184 *  @brief Internal structure used to manager per task variables.
185 *
186 *  This is the internal structure used to manager per Task Variables.
187 */
188typedef struct {
189  /** This field points to the next per task variable for this task. */
190  struct rtems_task_variable_tt  *next;
191  /** This field points to the physical memory location of this per
192   *  task variable.
193   */
194  void                          **ptr;
195  /** This field is to the global value for this per task variable. */
196  void                           *gval;
197  /** This field is to this thread's value for this per task variable. */
198  void                           *tval;
199  /** This field points to the destructor for this per task variable. */
200  void                          (*dtor)(void *);
201} rtems_task_variable_t;
202#endif
203
204/**
205 *  The following structure contains the information which defines
206 *  the starting state of a thread.
207 */
208typedef struct {
209  /** This field is the starting address for the thread. */
210  Thread_Entry                         entry_point;
211  /** This field indicates the how task is invoked. */
212  Thread_Start_types                   prototype;
213  /** This field is the pointer argument passed at thread start. */
214  void                                *pointer_argument;
215  /** This field is the numeric argument passed at thread start. */
216  Thread_Entry_numeric_type            numeric_argument;
217  /*-------------- initial execution modes ----------------- */
218  /** This field indicates whether the thread was preemptible when
219    * it started.
220    */
221  bool                                 is_preemptible;
222  /** This field indicates the CPU budget algorith. */
223  Thread_CPU_budget_algorithms         budget_algorithm;
224  /** This field is the routine to invoke when the CPU allotment is
225   *  consumed.
226   */
227  Thread_CPU_budget_algorithm_callout  budget_callout;
228  /** This field is the initial ISR disable level of this thread. */
229  uint32_t                             isr_level;
230  /** This field is the initial priority. */
231  Priority_Control                     initial_priority;
232  #if defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
233    /** This field indicates whether the SuperCore allocated the stack. */
234    bool                                 core_allocated_stack;
235  #endif
236  /** This field is the stack information. */
237  Stack_Control                        Initial_stack;
238  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
239    /** This field is the initial FP context area address. */
240    Context_Control_fp                  *fp_context;
241  #endif
242  /** This field is the initial stack area address. */
243  void                                *stack;
244  /** The thread-local storage (TLS) area */
245  void                                *tls_area;
246} Thread_Start_information;
247
248/**
249 *  @brief Union type to hold a pointer to an immutable or a mutable object.
250 *
251 *  The main purpose is to enable passing of pointers to read-only send buffers
252 *  in the message passing subsystem.  This approach is somewhat fragile since
253 *  it prevents the compiler to check if the operations on objects are valid
254 *  with respect to the constant qualifier.  An alternative would be to add a
255 *  third pointer argument for immutable objects, but this would increase the
256 *  structure size.
257 */
258typedef union {
259  void       *mutable_object;
260  const void *immutable_object;
261} Thread_Wait_information_Object_argument_type;
262
263/**
264 *  @brief Information required to manage a thread while it is blocked.
265 *
266 *  This contains the information required to manage a thread while it is
267 *  blocked and to return information to it.
268 */
269typedef struct {
270  /** This field is the Id of the object this thread is waiting upon. */
271  Objects_Id            id;
272  /** This field is used to return an integer while when blocked. */
273  uint32_t              count;
274  /** This field is for a pointer to a user return argument. */
275  void                 *return_argument;
276  /** This field is for a pointer to a second user return argument. */
277  Thread_Wait_information_Object_argument_type
278                        return_argument_second;
279  /** This field contains any options in effect on this blocking operation. */
280  uint32_t              option;
281  /** This field will contain the return status from a blocking operation.
282   *
283   *  @note The following assumes that all API return codes can be
284   *        treated as an uint32_t.
285   */
286  uint32_t              return_code;
287
288  /** This field is the chain header for the second through Nth tasks
289   *  of the same priority blocked waiting on the same object.
290   */
291  Chain_Control         Block2n;
292  /** This field points to the thread queue on which this thread is blocked. */
293  Thread_queue_Control *queue;
294}   Thread_Wait_information;
295
296/**
297 *  The following defines the control block used to manage
298 *  each thread proxy.
299 *
300 *  @note It is critical that proxies and threads have identical
301 *        memory images for the shared part.
302 */
303typedef struct {
304  /** This field is the object management structure for each proxy. */
305  Objects_Control          Object;
306  /** This field is the current execution state of this proxy. */
307  States_Control           current_state;
308  /** This field is the current priority state of this proxy. */
309  Priority_Control         current_priority;
310  /** This field is the base priority of this proxy. */
311  Priority_Control         real_priority;
312  /** This field is the number of mutexes currently held by this proxy. */
313  uint32_t                 resource_count;
314
315  /** This field is the blocking information for this proxy. */
316  Thread_Wait_information  Wait;
317  /** This field is the Watchdog used to manage proxy delays and timeouts. */
318  Watchdog_Control         Timer;
319#if defined(RTEMS_MULTIPROCESSING)
320  /** This field is the received response packet in an MP system. */
321  MP_packet_Prefix        *receive_packet;
322#endif
323     /****************** end of common block ********************/
324  /** This field is used to manage the set of proxies in the system. */
325  Chain_Node               Active;
326}   Thread_Proxy_control;
327
328/**
329 *  The following record defines the control block used
330 *  to manage each thread.
331 *
332 *  @note It is critical that proxies and threads have identical
333 *        memory images for the shared part.
334 */
335typedef enum {
336  /** This value is for the Classic RTEMS API. */
337  THREAD_API_RTEMS,
338  /** This value is for the POSIX API. */
339  THREAD_API_POSIX
340}  Thread_APIs;
341
342/** This macro defines the first API which has threads. */
343#define THREAD_API_FIRST THREAD_API_RTEMS
344
345/** This macro defines the last API which has threads. */
346#define THREAD_API_LAST  THREAD_API_POSIX
347
348typedef struct Thread_Action Thread_Action;
349
350/**
351 * @brief Thread action handler.
352 *
353 * The thread action handler will be called with interrupts disabled and the
354 * thread action lock acquired.  The handler must release the thread action
355 * lock with _Thread_Action_release_and_ISR_enable().  So the thread action
356 * lock can be used to protect private data fields of the particular action.
357 *
358 * Since the action is passed to the handler private data fields can be added
359 * below the common thread action fields.
360 *
361 * @param[in] thread The thread performing the action.
362 * @param[in] action The thread action.
363 * @param[in] cpu The processor of the thread.
364 * @param[in] level The ISR level for _Thread_Action_release_and_ISR_enable().
365 */
366typedef void ( *Thread_Action_handler )(
367  Thread_Control  *thread,
368  Thread_Action   *action,
369  Per_CPU_Control *cpu,
370  ISR_Level        level
371);
372
373/**
374 * @brief Thread action.
375 *
376 * Thread actions can be chained together to trigger a set of actions on
377 * particular events like for example a thread post-switch.  Use
378 * _Thread_Action_initialize() to initialize this structure.
379 *
380 * Thread actions are the building block for efficient implementation of
381 * - Classic signals delivery,
382 * - POSIX signals delivery,
383 * - thread restart notification,
384 * - thread delete notification,
385 * - forced thread migration on SMP configurations, and
386 * - the Multiprocessor Resource Sharing Protocol (MrsP).
387 *
388 * @see _Thread_Run_post_switch_actions().
389 */
390struct Thread_Action {
391  Chain_Node            Node;
392  Thread_Action_handler handler;
393};
394
395/**
396 * @brief Control block to manage thread actions.
397 *
398 * Use _Thread_Action_control_initialize() to initialize this structure.
399 */
400typedef struct {
401  Chain_Control Chain;
402} Thread_Action_control;
403
404/**
405 * @brief Thread life states.
406 *
407 * The thread life states are orthogonal to the thread states used for
408 * synchronization primitives and blocking operations.  They reflect the state
409 * changes triggered with thread restart and delete requests.
410 */
411typedef enum {
412  THREAD_LIFE_NORMAL = 0x0,
413  THREAD_LIFE_PROTECTED = 0x1,
414  THREAD_LIFE_RESTARTING = 0x2,
415  THREAD_LIFE_PROTECTED_RESTARTING = 0x3,
416  THREAD_LIFE_TERMINATING = 0x4,
417  THREAD_LIFE_PROTECTED_TERMINATING = 0x5,
418  THREAD_LIFE_RESTARTING_TERMINATING = 0x6,
419  THREAD_LIFE_PROTECTED_RESTARTING_TERMINATING = 0x7
420} Thread_Life_state;
421
422/**
423 * @brief Thread life control.
424 */
425typedef struct {
426  /**
427   * @brief Thread life action used to react upon thread restart and delete
428   * requests.
429   */
430  Thread_Action      Action;
431
432  /**
433   * @brief The current thread life state.
434   */
435  Thread_Life_state  state;
436
437  /**
438   * @brief The terminator thread of this thread.
439   *
440   * In case the thread is terminated and another thread (the terminator) waits
441   * for the actual termination completion, then this field references the
442   * terminator thread.
443   */
444  Thread_Control    *terminator;
445} Thread_Life_control;
446
447/**
448 *  This structure defines the Thread Control Block (TCB).
449 */
450struct Thread_Control_struct {
451  /** This field is the object management structure for each thread. */
452  Objects_Control          Object;
453  /** This field is the current execution state of this thread. */
454  States_Control           current_state;
455  /** This field is the current priority state of this thread. */
456  Priority_Control         current_priority;
457  /** This field is the base priority of this thread. */
458  Priority_Control         real_priority;
459  /** This field is the number of mutexes currently held by this thread. */
460  uint32_t                 resource_count;
461  /** This field is the blocking information for this thread. */
462  Thread_Wait_information  Wait;
463  /** This field is the Watchdog used to manage thread delays and timeouts. */
464  Watchdog_Control         Timer;
465#if defined(RTEMS_MULTIPROCESSING)
466  /** This field is the received response packet in an MP system. */
467  MP_packet_Prefix        *receive_packet;
468#endif
469#ifdef __RTEMS_STRICT_ORDER_MUTEX__
470  /** This field is the head of queue of priority inheritance mutex
471   *  held by the thread.
472   */
473  Chain_Control            lock_mutex;
474#endif
475     /*================= end of common block =================*/
476#if defined(RTEMS_MULTIPROCESSING)
477  /** This field is true if the thread is offered globally */
478  bool                                  is_global;
479#endif
480  /** This field is true if the thread is preemptible. */
481  bool                                  is_preemptible;
482#if defined(RTEMS_SMP)
483  /**
484   * @brief The scheduler of this thread.
485   */
486  const struct Scheduler_Control       *scheduler;
487#endif
488
489#if __RTEMS_ADA__
490  /** This field is the GNAT self context pointer. */
491  void                                 *rtems_ada_self;
492#endif
493  /** This field is the length of the time quantum that this thread is
494   *  allowed to consume.  The algorithm used to manage limits on CPU usage
495   *  is specified by budget_algorithm.
496   */
497  uint32_t                              cpu_time_budget;
498  /** This field is the algorithm used to manage this thread's time
499   *  quantum.  The algorithm may be specified as none which case,
500   *  no limit is in place.
501   */
502  Thread_CPU_budget_algorithms          budget_algorithm;
503  /** This field is the method invoked with the budgeted time is consumed. */
504  Thread_CPU_budget_algorithm_callout   budget_callout;
505  /** This field is the amount of CPU time consumed by this thread
506   *  since it was created.
507   */
508  Thread_CPU_usage_t                    cpu_time_used;
509
510  /**
511   * @brief The scheduler node of this thread for the real scheduler.
512   */
513  struct Scheduler_Node                *scheduler_node;
514
515#ifdef RTEMS_SMP
516  /**
517   * @brief The processor assigned by the scheduler.
518   */
519  Per_CPU_Control                      *cpu;
520
521#ifdef RTEMS_DEBUG
522  /**
523   * @brief The processor on which this thread executed the last time or is
524   * executing.
525   */
526  Per_CPU_Control                      *debug_real_cpu;
527#endif
528#endif
529
530  /** This field contains information about the starting state of
531   *  this thread.
532   */
533  Thread_Start_information              Start;
534
535  Thread_Action_control                 Post_switch_actions;
536
537  /** This field contains the context of this thread. */
538  Context_Control                       Registers;
539#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
540  /** This field points to the floating point context for this thread.
541   *  If NULL, the thread is integer only.
542   */
543  Context_Control_fp                   *fp_context;
544#endif
545  /** This field points to the newlib reentrancy structure for this thread. */
546  struct _reent                        *libc_reent;
547  /** This array contains the API extension area pointers. */
548  void                                 *API_Extensions[ THREAD_API_LAST + 1 ];
549
550#if !defined(RTEMS_SMP)
551  /** This field points to the set of per task variables. */
552  rtems_task_variable_t                *task_variables;
553#endif
554
555  /**
556   * This is the thread key value chain's control, which is used
557   * to track all key value for specific thread, and when thread
558   * exits, we can remove all key value for specific thread by
559   * iterating this chain, or we have to search a whole rbtree,
560   * which is inefficient.
561   */
562  Chain_Control           Key_Chain;
563
564  /**
565   * @brief Thread life-cycle control.
566   *
567   * Control state changes triggered by thread restart and delete requests.
568   */
569  Thread_Life_control                   Life;
570
571  /**
572   * @brief Variable length array of user extension pointers.
573   *
574   * The length is defined by the application via <rtems/confdefs.h>.
575   */
576  void                                 *extensions[ RTEMS_ZERO_LENGTH_ARRAY ];
577};
578
579#if (CPU_PROVIDES_IDLE_THREAD_BODY == FALSE)
580/**
581 *  This routine is the body of the system idle thread.
582 *
583 *  NOTE: This routine is actually instantiated by confdefs.h when needed.
584 */
585void *_Thread_Idle_body(
586  uintptr_t  ignored
587);
588#endif
589
590/**  This defines the type for a method which operates on a single thread.
591 */
592typedef void (*rtems_per_thread_routine)( Thread_Control * );
593
594/**
595 *  @brief Iterates over all threads.
596 *  This routine iterates over all threads regardless of API and
597 *  invokes the specified routine.
598 */
599void rtems_iterate_over_all_threads(
600  rtems_per_thread_routine routine
601);
602
603/**
604 * @brief Returns the thread control block of the executing thread.
605 *
606 * This function can be called in any context.  On SMP configurations
607 * interrupts are disabled to ensure that the processor index is used
608 * consistently.
609 *
610 * @return The thread control block of the executing thread.
611 */
612RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Get_executing( void )
613{
614  Thread_Control *executing;
615
616  #if defined( RTEMS_SMP )
617    ISR_Level level;
618
619    _ISR_Disable_without_giant( level );
620  #endif
621
622  executing = _Thread_Executing;
623
624  #if defined( RTEMS_SMP )
625    _ISR_Enable_without_giant( level );
626  #endif
627
628  return executing;
629}
630
631/**
632 * @brief Thread control add-on.
633 */
634typedef struct {
635  /**
636   * @brief Offset of the pointer field in Thread_Control referencing an
637   * application configuration dependent memory area in the thread control
638   * block.
639   */
640  size_t destination_offset;
641
642  /**
643   * @brief Offset relative to the thread control block begin to an application
644   * configuration dependent memory area.
645   */
646  size_t source_offset;
647} Thread_Control_add_on;
648
649/**
650 * @brief Thread control add-ons.
651 *
652 * The thread control block contains fields that point to application
653 * configuration dependent memory areas, like the scheduler information, the
654 * API control blocks, the user extension context table, the RTEMS notepads and
655 * the Newlib re-entrancy support.  Account for these areas in the
656 * configuration and avoid extra workspace allocations for these areas.
657 *
658 * This array is provided via <rtems/confdefs.h>.
659 *
660 * @see _Thread_Control_add_on_count and _Thread_Control_size.
661 */
662extern const Thread_Control_add_on _Thread_Control_add_ons[];
663
664/**
665 * @brief Thread control add-on count.
666 *
667 * Count of entries in _Thread_Control_add_ons.
668 *
669 * This value is provided via <rtems/confdefs.h>.
670 */
671extern const size_t _Thread_Control_add_on_count;
672
673/**
674 * @brief Size of the thread control block of a particular application.
675 *
676 * This value is provided via <rtems/confdefs.h>.
677 *
678 * @see _Thread_Control_add_ons.
679 */
680extern const size_t _Thread_Control_size;
681
682/**@}*/
683
684#ifdef __cplusplus
685}
686#endif
687
688#endif
689/* end of include file */
Note: See TracBrowser for help on using the repository browser.