source: rtems/cpukit/score/include/rtems/score/thread.h @ 5618c37a

4.115
Last change on this file since 5618c37a was 5618c37a, checked in by Sebastian Huber <sebastian.huber@…>, on 07/24/13 at 13:14:48

score: Create thread implementation header

Move implementation specific parts of thread.h and thread.inl into new
header file threadimpl.h. The thread.h contains now only the
application visible API.

Remove superfluous header file includes from various files.

  • Property mode set to 100644
File size: 17.3 KB
RevLine 
[20f02c6]1/**
[11874561]2 *  @file  rtems/score/thread.h
[ac7d5ef0]3 *
[319cb20]4 *  @brief Constants and Structures Related with the Thread Control Block
5 *
[ac7d5ef0]6 *  This include file contains all constants and structures associated
7 *  with the thread control block.
[baff4da]8 */
9
10/*
[3203e09]11 *  COPYRIGHT (c) 1989-2009.
[ac7d5ef0]12 *  On-Line Applications Research Corporation (OAR).
13 *
[98e4ebf5]14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
[dd687d97]16 *  http://www.rtems.com/license/LICENSE.
[ac7d5ef0]17 */
18
[092f142a]19#ifndef _RTEMS_SCORE_THREAD_H
20#define _RTEMS_SCORE_THREAD_H
[ac7d5ef0]21
[5618c37a]22#include <rtems/score/context.h>
23#if defined(RTEMS_MULTIPROCESSING)
24#include <rtems/score/mppkt.h>
25#endif
26#include <rtems/score/object.h>
27#include <rtems/score/percpu.h>
28#include <rtems/score/priority.h>
29#include <rtems/score/stack.h>
30#include <rtems/score/states.h>
31#include <rtems/score/tqdata.h>
32#include <rtems/score/watchdog.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
[baff4da]38/**
39 *  @defgroup ScoreThread Thread Handler
40 *
[d8cd045c]41 *  @ingroup Score
42 *
[6a07436]43 *  This handler encapsulates functionality related to the management of
44 *  threads.  This includes the creation, deletion, and scheduling of threads.
[11e8bc5]45 *
46 *  The following variables are maintained as part of the per cpu data
47 *  structure.
48 *
49 *  + Idle thread pointer
50 *  + Executing thread pointer
51 *  + Heir thread pointer
[baff4da]52 */
53/**@{*/
54
[1ada6163]55#if defined(RTEMS_POSIX_API)
[442eac69]56  #define RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE
57#endif
58
[fdc70e2]59/*
60 * With the addition of the Constant Block Scheduler (CBS),
61 * this feature is needed even when POSIX is disabled.
62 */
63#define RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT
[442eac69]64
[bacf79e]65#if defined(RTEMS_POSIX_API)
66  #define RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API
67#endif
[442eac69]68
[196fe59]69/*
70 *  The user can define this at configure time and go back to ticks
71 *  resolution.
72 */
73#ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
74  #include <rtems/score/timestamp.h>
75
76  typedef Timestamp_Control Thread_CPU_usage_t;
77#else
78  typedef uint32_t Thread_CPU_usage_t;
79#endif
80
[baff4da]81/**
[3a4ae6c]82 *  The following defines the "return type" of a thread.
[260b0c2]83 *
[baff4da]84 *  @note  This cannot always be right.  Some APIs have void
[a0ed4ed]85 *         tasks/threads, others return pointers, others may
[260b0c2]86 *         return a numeric value.  Hopefully a pointer is
[d6154c7]87 *         always at least as big as an uint32_t  . :)
[ac7d5ef0]88 */
[260b0c2]89typedef void *Thread;
[ac7d5ef0]90
[3b14b7ad]91/**
92 *  @brief Type of the numeric argument of a thread entry function with at
[20f02c6]93 *  least one numeric argument.
[3b14b7ad]94 *
95 *  This numeric argument type designates an unsigned integer type with the
96 *  property that any valid pointer to void can be converted to this type and
97 *  then converted back to a pointer to void.  The result will compare equal to
98 *  the original pointer.
99 */
100typedef uintptr_t Thread_Entry_numeric_type;
101
[baff4da]102/**
[3a4ae6c]103 *  The following defines the ways in which the entry point for a
104 *  thread can be invoked.  Basically, it can be passed any
[d6154c7]105 *  combination/permutation of a pointer and an uint32_t   value.
[3a4ae6c]106 *
[baff4da]107 *  @note For now, we are ignoring the return type.
[ac7d5ef0]108 */
[3a4ae6c]109typedef enum {
110  THREAD_START_NUMERIC,
111  THREAD_START_POINTER,
[46a67b19]112  #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API)
113    THREAD_START_BOTH_POINTER_FIRST,
114    THREAD_START_BOTH_NUMERIC_FIRST
115  #endif
[3a4ae6c]116} Thread_Start_types;
[ac7d5ef0]117
[6a07436]118/** This type corresponds to a very simple style thread entry point. */
[5742e5e]119typedef Thread ( *Thread_Entry )( void );   /* basic type */
[0d051533]120
[6a07436]121/** This type corresponds to a thread entry point which takes a single
122 *  unsigned thirty-two bit integer as an argument.
123 */
[3b14b7ad]124typedef Thread ( *Thread_Entry_numeric )( Thread_Entry_numeric_type );
[baff4da]125
[6a07436]126/** This type corresponds to a thread entry point which takes a single
127 *  untyped pointer as an argument.
[baff4da]128 */
[0d051533]129typedef Thread ( *Thread_Entry_pointer )( void * );
[baff4da]130
[6a07436]131/** This type corresponds to a thread entry point which takes a single
132 *  untyped pointer and an unsigned thirty-two bit integer as arguments.
[baff4da]133 */
[3b14b7ad]134typedef Thread ( *Thread_Entry_both_pointer_first )( void *, Thread_Entry_numeric_type );
[baff4da]135
[6a07436]136/** This type corresponds to a thread entry point which takes a single
137 *  unsigned thirty-two bit integer and an untyped pointer and an
138 *  as arguments.
[baff4da]139 */
[3b14b7ad]140typedef Thread ( *Thread_Entry_both_numeric_first )( Thread_Entry_numeric_type, void * );
[ac7d5ef0]141
[baff4da]142/**
[2f200c7]143 *  The following lists the algorithms used to manage the thread cpu budget.
144 *
145 *  Reset Timeslice:   At each context switch, reset the time quantum.
146 *  Exhaust Timeslice: Only reset the quantum once it is consumed.
147 *  Callout:           Execute routine when budget is consumed.
148 */
149typedef enum {
150  THREAD_CPU_BUDGET_ALGORITHM_NONE,
151  THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE,
[442eac69]152  #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
153    THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE,
154  #endif
155  #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
156    THREAD_CPU_BUDGET_ALGORITHM_CALLOUT
157  #endif
[2f200c7]158}  Thread_CPU_budget_algorithms;
159
[6a07436]160/**  This defines thes the entry point for the thread specific timeslice
161 *   budget management algorithm.
[baff4da]162 */
[2f200c7]163typedef void (*Thread_CPU_budget_algorithm_callout )( Thread_Control * );
164
[4b72da4]165/**
[319cb20]166 *  @brief Forward reference to the per task variable structure..
[baff4da]167 *
168 *  Forward reference to the per task variable structure.
[aad726e]169 */
170struct rtems_task_variable_tt;
171
[4b72da4]172/**
[319cb20]173 *  @brief Internal structure used to manager per task variables.
[baff4da]174 *
175 *  This is the internal structure used to manager per Task Variables.
176 */
[6a07436]177typedef struct {
[baff4da]178  /** This field points to the next per task variable for this task. */
[aad726e]179  struct rtems_task_variable_tt  *next;
[baff4da]180  /** This field points to the physical memory location of this per
181   *  task variable.
182   */
[c941a98]183  void                          **ptr;
[baff4da]184  /** This field is to the global value for this per task variable. */
[df49c60]185  void                           *gval;
[baff4da]186  /** This field is to this thread's value for this per task variable. */
[df49c60]187  void                           *tval;
[baff4da]188  /** This field points to the destructor for this per task variable. */
[c941a98]189  void                          (*dtor)(void *);
[6a07436]190} rtems_task_variable_t;
[aad726e]191
[baff4da]192/**
[ac7d5ef0]193 *  The following structure contains the information which defines
194 *  the starting state of a thread.
195 */
196typedef struct {
[6a07436]197  /** This field is the starting address for the thread. */
[3b14b7ad]198  Thread_Entry                         entry_point;
[6cd8bbe]199  /** This field indicates the how task is invoked. */
[3b14b7ad]200  Thread_Start_types                   prototype;
[6a07436]201  /** This field is the pointer argument passed at thread start. */
[3b14b7ad]202  void                                *pointer_argument;
[6a07436]203  /** This field is the numeric argument passed at thread start. */
[3b14b7ad]204  Thread_Entry_numeric_type            numeric_argument;
[6a07436]205  /*-------------- initial execution modes ----------------- */
206  /** This field indicates whether the thread was preemptible when
207    * it started.
208    */
[484a769]209  bool                                 is_preemptible;
[6a07436]210  /** This field indicates the CPU budget algorith. */
[3b14b7ad]211  Thread_CPU_budget_algorithms         budget_algorithm;
[6a07436]212  /** This field is the routine to invoke when the CPU allotment is
213   *  consumed.
214   */
[3b14b7ad]215  Thread_CPU_budget_algorithm_callout  budget_callout;
[6a07436]216  /** This field is the initial ISR disable level of this thread. */
[3b14b7ad]217  uint32_t                             isr_level;
[6a07436]218  /** This field is the initial priority. */
[3b14b7ad]219  Priority_Control                     initial_priority;
[bacf79e]220  #if defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
221    /** This field indicates whether the SuperCore allocated the stack. */
222    bool                                 core_allocated_stack;
223  #endif
[6a07436]224  /** This field is the stack information. */
[3b14b7ad]225  Stack_Control                        Initial_stack;
[bacf79e]226  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
227    /** This field is the initial FP context area address. */
228    Context_Control_fp                  *fp_context;
229  #endif
[6a07436]230  /** This field is the initial stack area address. */
[3b14b7ad]231  void                                *stack;
232} Thread_Start_information;
[ac7d5ef0]233
[f773c012]234/**
235 *  @brief Union type to hold a pointer to an immutable or a mutable object.
236 *
237 *  The main purpose is to enable passing of pointers to read-only send buffers
238 *  in the message passing subsystem.  This approach is somewhat fragile since
239 *  it prevents the compiler to check if the operations on objects are valid
240 *  with respect to the constant qualifier.  An alternative would be to add a
241 *  third pointer argument for immutable objects, but this would increase the
242 *  structure size.
243 */
244typedef union {
245  void       *mutable_object;
246  const void *immutable_object;
247} Thread_Wait_information_Object_argument_type;
248
[4b72da4]249/**
[319cb20]250 *  @brief Information required to manage a thread while it is blocked.
[baff4da]251 *
252 *  This contains the information required to manage a thread while it is
253 *  blocked and to return information to it.
254 */
[ac7d5ef0]255typedef struct {
[baff4da]256  /** This field is the Id of the object this thread is waiting upon. */
257  Objects_Id            id;
258  /** This field is used to return an integer while when blocked. */
259  uint32_t              count;
[f773c012]260  /** This field is for a pointer to a user return argument. */
[baff4da]261  void                 *return_argument;
[f773c012]262  /** This field is for a pointer to a second user return argument. */
263  Thread_Wait_information_Object_argument_type
264                        return_argument_second;
[baff4da]265  /** This field contains any options in effect on this blocking operation. */
[d6154c7]266  uint32_t              option;
[baff4da]267  /** This field will contain the return status from a blocking operation.
[20f02c6]268   *
[baff4da]269   *  @note The following assumes that all API return codes can be
270   *        treated as an uint32_t.
[3a4ae6c]271   */
[baff4da]272  uint32_t              return_code;
[3a4ae6c]273
[20f02c6]274  /** This field is the chain header for the second through Nth tasks
[baff4da]275   *  of the same priority blocked waiting on the same object.
276   */
277  Chain_Control         Block2n;
278  /** This field points to the thread queue on which this thread is blocked. */
279  Thread_queue_Control *queue;
[ac7d5ef0]280}   Thread_Wait_information;
281
[baff4da]282/**
[ac7d5ef0]283 *  The following defines the control block used to manage
284 *  each thread proxy.
285 *
[baff4da]286 *  @note It is critical that proxies and threads have identical
[ac7d5ef0]287 *        memory images for the shared part.
288 */
289typedef struct {
[6a07436]290  /** This field is the object management structure for each proxy. */
[ac7d5ef0]291  Objects_Control          Object;
[6a07436]292  /** This field is the current execution state of this proxy. */
[ac7d5ef0]293  States_Control           current_state;
[6a07436]294  /** This field is the current priority state of this proxy. */
[7f6a24ab]295  Priority_Control         current_priority;
[6a07436]296  /** This field is the base priority of this proxy. */
[7f6a24ab]297  Priority_Control         real_priority;
[6a07436]298  /** This field is the number of mutexes currently held by this proxy. */
[d6154c7]299  uint32_t                 resource_count;
[fd84982]300
[6a07436]301  /** This field is the blocking information for this proxy. */
[ac7d5ef0]302  Thread_Wait_information  Wait;
[6a07436]303  /** This field is the Watchdog used to manage proxy delays and timeouts. */
[ac7d5ef0]304  Watchdog_Control         Timer;
[97e2729d]305#if defined(RTEMS_MULTIPROCESSING)
[6a07436]306  /** This field is the received response packet in an MP system. */
[3a4ae6c]307  MP_packet_Prefix        *receive_packet;
[97e2729d]308#endif
[ac7d5ef0]309     /****************** end of common block ********************/
[6a07436]310  /** This field is used to manage the set of proxies in the system. */
[ac7d5ef0]311  Chain_Node               Active;
312}   Thread_Proxy_control;
313
[baff4da]314/**
[ac7d5ef0]315 *  The following record defines the control block used
316 *  to manage each thread.
317 *
[baff4da]318 *  @note It is critical that proxies and threads have identical
[ac7d5ef0]319 *        memory images for the shared part.
320 */
[3a4ae6c]321typedef enum {
[6a07436]322  /** This value is for the Classic RTEMS API. */
[5e9b32b]323  THREAD_API_RTEMS,
[6a07436]324  /** This value is for the POSIX API. */
[f0caa05]325  THREAD_API_POSIX
[3a4ae6c]326}  Thread_APIs;
[7f6a24ab]327
[6a07436]328/** This macro defines the first API which has threads. */
[3a4ae6c]329#define THREAD_API_FIRST THREAD_API_RTEMS
[baff4da]330
[6a07436]331/** This macro defines the last API which has threads. */
[f0caa05]332#define THREAD_API_LAST  THREAD_API_POSIX
[7f6a24ab]333
[baff4da]334/**
[6a07436]335 *  This structure defines the Thread Control Block (TCB).
[baff4da]336 */
[2f200c7]337struct Thread_Control_struct {
[6a07436]338  /** This field is the object management structure for each thread. */
339  Objects_Control          Object;
340  /** This field is the current execution state of this thread. */
341  States_Control           current_state;
342  /** This field is the current priority state of this thread. */
343  Priority_Control         current_priority;
344  /** This field is the base priority of this thread. */
345  Priority_Control         real_priority;
346  /** This field is the number of mutexes currently held by this thread. */
347  uint32_t                 resource_count;
348  /** This field is the blocking information for this thread. */
349  Thread_Wait_information  Wait;
350  /** This field is the Watchdog used to manage thread delays and timeouts. */
351  Watchdog_Control         Timer;
[97e2729d]352#if defined(RTEMS_MULTIPROCESSING)
[6a07436]353  /** This field is the received response packet in an MP system. */
354  MP_packet_Prefix        *receive_packet;
[fd84982]355#endif
[66a9239a]356#ifdef __RTEMS_STRICT_ORDER_MUTEX__
[931dd97]357  /** This field is the head of queue of priority inheritance mutex
358   *  held by the thread.
359   */
[fd84982]360  Chain_Control            lock_mutex;
[97e2729d]361#endif
[6a07436]362     /*================= end of common block =================*/
[931dd97]363#if defined(RTEMS_MULTIPROCESSING)
[6a07436]364  /** This field is true if the thread is offered globally */
[484a769]365  bool                                  is_global;
[931dd97]366#endif
[6a07436]367  /** This field is true if the thread is preemptible. */
[484a769]368  bool                                  is_preemptible;
[a936aa49]369#if defined(RTEMS_SMP)
370  /**
371   * @brief This field is true if the thread is scheduled.
372   *
373   * A thread is scheduled if it is ready and the scheduler allocated a
374   * processor for it.  A scheduled thread is assigned to exactly one
375   * processor.  There are exactly processor count scheduled threads in the
376   * system.
377   */
378  bool                                  is_scheduled;
379
380  /**
381   * @brief This field is true if the thread is executing.
382   *
383   * A thread is executing if it executes on a processor.  An executing thread
384   * executes on exactly one processor.  There are exactly processor count
385   * executing threads in the system.  An executing thread may have a heir
386   * thread and thread dispatching is necessary.  On SMP a thread dispatch on a
387   * remote processor needs help from an inter-processor interrupt, thus it
388   * will take some time to complete the state change.  A lot of things can
389   * happen in the meantime.
390   */
391  bool                                  is_executing;
392#endif
[81b329a]393#if __RTEMS_ADA__
[6a07436]394  /** This field is the GNAT self context pointer. */
[847375f]395  void                                 *rtems_ada_self;
[81b329a]396#endif
[6a07436]397  /** This field is the length of the time quantum that this thread is
398   *  allowed to consume.  The algorithm used to manage limits on CPU usage
399   *  is specified by budget_algorithm.
400   */
[d6154c7]401  uint32_t                              cpu_time_budget;
[6a07436]402  /** This field is the algorithm used to manage this thread's time
403   *  quantum.  The algorithm may be specified as none which case,
404   *  no limit is in place.
405   */
[2f200c7]406  Thread_CPU_budget_algorithms          budget_algorithm;
[6a07436]407  /** This field is the method invoked with the budgeted time is consumed. */
[2f200c7]408  Thread_CPU_budget_algorithm_callout   budget_callout;
[c3330a8]409  /** This field is the amount of CPU time consumed by this thread
[6a07436]410   *  since it was created.
411   */
[9dc2c8d]412  Thread_CPU_usage_t                    cpu_time_used;
[108c4b0]413
414  /** This pointer holds per-thread data for the scheduler and ready queue. */
415  void                                 *scheduler_info;
416
[a936aa49]417#ifdef RTEMS_SMP
418  Per_CPU_Control                      *cpu;
419#endif
420
[6a07436]421  /** This field contains information about the starting state of
422   *  this thread.
423   */
[2f200c7]424  Thread_Start_information              Start;
[6a07436]425  /** This field contains the context of this thread. */
[2f200c7]426  Context_Control                       Registers;
[499d443]427#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
[6a07436]428  /** This field points to the floating point context for this thread.
429   *  If NULL, the thread is integer only.
430   */
[3c86f88]431  Context_Control_fp                   *fp_context;
[17508d02]432#endif
[6a07436]433  /** This field points to the newlib reentrancy structure for this thread. */
[d36b3152]434  struct _reent                        *libc_reent;
[6a07436]435  /** This array contains the API extension area pointers. */
[2f200c7]436  void                                 *API_Extensions[ THREAD_API_LAST + 1 ];
[6a07436]437  /** This field points to the user extension pointers. */
[2f200c7]438  void                                **extensions;
[6a07436]439  /** This field points to the set of per task variables. */
[aad726e]440  rtems_task_variable_t                *task_variables;
[2f200c7]441};
[ac7d5ef0]442
[baff4da]443#if (CPU_PROVIDES_IDLE_THREAD_BODY == FALSE)
444/**
[adf98bd]445 *  This routine is the body of the system idle thread.
[93f7ea15]446 *
447 *  NOTE: This routine is actually instantiated by confdefs.h when needed.
[adf98bd]448 */
[3aaf479]449void *_Thread_Idle_body(
450  uintptr_t  ignored
[adf98bd]451);
452#endif
453
[6a07436]454/**  This defines the type for a method which operates on a single thread.
[17c66867]455 */
456typedef void (*rtems_per_thread_routine)( Thread_Control * );
457
[baff4da]458/**
[319cb20]459 *  @brief Iterates over all threads.
[baff4da]460 *  This routine iterates over all threads regardless of API and
461 *  invokes the specified routine.
462 */
[17c66867]463void rtems_iterate_over_all_threads(
464  rtems_per_thread_routine routine
465);
466
[f74d9496]467/**
468 * @brief Returns the thread control block of the executing thread.
469 *
470 * This function can be called in any context.  On SMP configurations
471 * interrupts are disabled to ensure that the processor index is used
472 * consistently.
473 *
474 * @return The thread control block of the executing thread.
475 */
476RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Get_executing( void )
477{
478  Thread_Control *executing;
479
480  #if defined( RTEMS_SMP )
481    ISR_Level level;
482
483    _ISR_Disable( level );
484  #endif
485
486  executing = _Thread_Executing;
487
488  #if defined( RTEMS_SMP )
489    _ISR_Enable( level );
490  #endif
491
492  return executing;
493}
494
[5618c37a]495/**@}*/
[ac7d5ef0]496
497#ifdef __cplusplus
498}
499#endif
500
501#endif
[b10825c]502/* end of include file */
Note: See TracBrowser for help on using the repository browser.