source: rtems/cpukit/score/include/rtems/score/thread.h @ 022851a

4.11
Last change on this file since 022851a was 022851a, checked in by Sebastian Huber <sebastian.huber@…>, on Jan 28, 2014 at 11:10:08 AM

Add thread-local storage (TLS) support

Tested and implemented on ARM, m68k, PowerPC and SPARC. Other
architectures need more work.

  • Property mode set to 100644
File size: 18.1 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-2009.
12 *  On-Line Applications Research Corporation (OAR).
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.com/license/LICENSE.
17 */
18
19#ifndef _RTEMS_SCORE_THREAD_H
20#define _RTEMS_SCORE_THREAD_H
21
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/threadq.h>
32#include <rtems/score/watchdog.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/**
39 *  @defgroup ScoreThread Thread Handler
40 *
41 *  @ingroup Score
42 *
43 *  This handler encapsulates functionality related to the management of
44 *  threads.  This includes the creation, deletion, and scheduling of threads.
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
52 */
53/**@{*/
54
55#if defined(RTEMS_POSIX_API)
56  #define RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE
57#endif
58
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
64
65#if defined(RTEMS_POSIX_API)
66  #define RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API
67#endif
68
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
81/**
82 *  The following defines the "return type" of a thread.
83 *
84 *  @note  This cannot always be right.  Some APIs have void
85 *         tasks/threads, others return pointers, others may
86 *         return a numeric value.  Hopefully a pointer is
87 *         always at least as big as an uint32_t  . :)
88 */
89typedef void *Thread;
90
91/**
92 *  @brief Type of the numeric argument of a thread entry function with at
93 *  least one numeric argument.
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
102/**
103 *  The following defines the ways in which the entry point for a
104 *  thread can be invoked.  Basically, it can be passed any
105 *  combination/permutation of a pointer and an uint32_t   value.
106 *
107 *  @note For now, we are ignoring the return type.
108 */
109typedef enum {
110  THREAD_START_NUMERIC,
111  THREAD_START_POINTER,
112  #if defined(FUNCTIONALITY_NOT_CURRENTLY_USED_BY_ANY_API)
113    THREAD_START_BOTH_POINTER_FIRST,
114    THREAD_START_BOTH_NUMERIC_FIRST
115  #endif
116} Thread_Start_types;
117
118/** This type corresponds to a very simple style thread entry point. */
119typedef Thread ( *Thread_Entry )( void );   /* basic type */
120
121/** This type corresponds to a thread entry point which takes a single
122 *  unsigned thirty-two bit integer as an argument.
123 */
124typedef Thread ( *Thread_Entry_numeric )( Thread_Entry_numeric_type );
125
126/** This type corresponds to a thread entry point which takes a single
127 *  untyped pointer as an argument.
128 */
129typedef Thread ( *Thread_Entry_pointer )( void * );
130
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.
133 */
134typedef Thread ( *Thread_Entry_both_pointer_first )( void *, Thread_Entry_numeric_type );
135
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.
139 */
140typedef Thread ( *Thread_Entry_both_numeric_first )( Thread_Entry_numeric_type, void * );
141
142/**
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,
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
158}  Thread_CPU_budget_algorithms;
159
160/**  This defines thes the entry point for the thread specific timeslice
161 *   budget management algorithm.
162 */
163typedef void (*Thread_CPU_budget_algorithm_callout )( Thread_Control * );
164
165/**
166 *  @brief Forward reference to the per task variable structure..
167 *
168 *  Forward reference to the per task variable structure.
169 */
170struct rtems_task_variable_tt;
171
172/**
173 *  @brief Internal structure used to manager per task variables.
174 *
175 *  This is the internal structure used to manager per Task Variables.
176 */
177typedef struct {
178  /** This field points to the next per task variable for this task. */
179  struct rtems_task_variable_tt  *next;
180  /** This field points to the physical memory location of this per
181   *  task variable.
182   */
183  void                          **ptr;
184  /** This field is to the global value for this per task variable. */
185  void                           *gval;
186  /** This field is to this thread's value for this per task variable. */
187  void                           *tval;
188  /** This field points to the destructor for this per task variable. */
189  void                          (*dtor)(void *);
190} rtems_task_variable_t;
191
192/**
193 *  The following structure contains the information which defines
194 *  the starting state of a thread.
195 */
196typedef struct {
197  /** This field is the starting address for the thread. */
198  Thread_Entry                         entry_point;
199  /** This field indicates the how task is invoked. */
200  Thread_Start_types                   prototype;
201  /** This field is the pointer argument passed at thread start. */
202  void                                *pointer_argument;
203  /** This field is the numeric argument passed at thread start. */
204  Thread_Entry_numeric_type            numeric_argument;
205  /*-------------- initial execution modes ----------------- */
206  /** This field indicates whether the thread was preemptible when
207    * it started.
208    */
209  bool                                 is_preemptible;
210  /** This field indicates the CPU budget algorith. */
211  Thread_CPU_budget_algorithms         budget_algorithm;
212  /** This field is the routine to invoke when the CPU allotment is
213   *  consumed.
214   */
215  Thread_CPU_budget_algorithm_callout  budget_callout;
216  /** This field is the initial ISR disable level of this thread. */
217  uint32_t                             isr_level;
218  /** This field is the initial priority. */
219  Priority_Control                     initial_priority;
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
224  /** This field is the stack information. */
225  Stack_Control                        Initial_stack;
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
230  /** This field is the initial stack area address. */
231  void                                *stack;
232  /** The thread-local storage (TLS) area */
233  void                                *tls_area;
234} Thread_Start_information;
235
236/**
237 *  @brief Union type to hold a pointer to an immutable or a mutable object.
238 *
239 *  The main purpose is to enable passing of pointers to read-only send buffers
240 *  in the message passing subsystem.  This approach is somewhat fragile since
241 *  it prevents the compiler to check if the operations on objects are valid
242 *  with respect to the constant qualifier.  An alternative would be to add a
243 *  third pointer argument for immutable objects, but this would increase the
244 *  structure size.
245 */
246typedef union {
247  void       *mutable_object;
248  const void *immutable_object;
249} Thread_Wait_information_Object_argument_type;
250
251/**
252 *  @brief Information required to manage a thread while it is blocked.
253 *
254 *  This contains the information required to manage a thread while it is
255 *  blocked and to return information to it.
256 */
257typedef struct {
258  /** This field is the Id of the object this thread is waiting upon. */
259  Objects_Id            id;
260  /** This field is used to return an integer while when blocked. */
261  uint32_t              count;
262  /** This field is for a pointer to a user return argument. */
263  void                 *return_argument;
264  /** This field is for a pointer to a second user return argument. */
265  Thread_Wait_information_Object_argument_type
266                        return_argument_second;
267  /** This field contains any options in effect on this blocking operation. */
268  uint32_t              option;
269  /** This field will contain the return status from a blocking operation.
270   *
271   *  @note The following assumes that all API return codes can be
272   *        treated as an uint32_t.
273   */
274  uint32_t              return_code;
275
276  /** This field is the chain header for the second through Nth tasks
277   *  of the same priority blocked waiting on the same object.
278   */
279  Chain_Control         Block2n;
280  /** This field points to the thread queue on which this thread is blocked. */
281  Thread_queue_Control *queue;
282}   Thread_Wait_information;
283
284/**
285 *  The following defines the control block used to manage
286 *  each thread proxy.
287 *
288 *  @note It is critical that proxies and threads have identical
289 *        memory images for the shared part.
290 */
291typedef struct {
292  /** This field is the object management structure for each proxy. */
293  Objects_Control          Object;
294  /** This field is the current execution state of this proxy. */
295  States_Control           current_state;
296  /** This field is the current priority state of this proxy. */
297  Priority_Control         current_priority;
298  /** This field is the base priority of this proxy. */
299  Priority_Control         real_priority;
300  /** This field is the number of mutexes currently held by this proxy. */
301  uint32_t                 resource_count;
302
303  /** This field is the blocking information for this proxy. */
304  Thread_Wait_information  Wait;
305  /** This field is the Watchdog used to manage proxy delays and timeouts. */
306  Watchdog_Control         Timer;
307#if defined(RTEMS_MULTIPROCESSING)
308  /** This field is the received response packet in an MP system. */
309  MP_packet_Prefix        *receive_packet;
310#endif
311     /****************** end of common block ********************/
312  /** This field is used to manage the set of proxies in the system. */
313  Chain_Node               Active;
314}   Thread_Proxy_control;
315
316/**
317 *  The following record defines the control block used
318 *  to manage each thread.
319 *
320 *  @note It is critical that proxies and threads have identical
321 *        memory images for the shared part.
322 */
323typedef enum {
324  /** This value is for the Classic RTEMS API. */
325  THREAD_API_RTEMS,
326  /** This value is for the POSIX API. */
327  THREAD_API_POSIX
328}  Thread_APIs;
329
330/** This macro defines the first API which has threads. */
331#define THREAD_API_FIRST THREAD_API_RTEMS
332
333/** This macro defines the last API which has threads. */
334#define THREAD_API_LAST  THREAD_API_POSIX
335
336/**
337 *  This structure defines the Thread Control Block (TCB).
338 */
339struct Thread_Control_struct {
340  /** This field is the object management structure for each thread. */
341  Objects_Control          Object;
342  /** This field is the current execution state of this thread. */
343  States_Control           current_state;
344  /** This field is the current priority state of this thread. */
345  Priority_Control         current_priority;
346  /** This field is the base priority of this thread. */
347  Priority_Control         real_priority;
348  /** This field is the number of mutexes currently held by this thread. */
349  uint32_t                 resource_count;
350  /** This field is the blocking information for this thread. */
351  Thread_Wait_information  Wait;
352  /** This field is the Watchdog used to manage thread delays and timeouts. */
353  Watchdog_Control         Timer;
354#if defined(RTEMS_MULTIPROCESSING)
355  /** This field is the received response packet in an MP system. */
356  MP_packet_Prefix        *receive_packet;
357#endif
358#ifdef __RTEMS_STRICT_ORDER_MUTEX__
359  /** This field is the head of queue of priority inheritance mutex
360   *  held by the thread.
361   */
362  Chain_Control            lock_mutex;
363#endif
364     /*================= end of common block =================*/
365#if defined(RTEMS_MULTIPROCESSING)
366  /** This field is true if the thread is offered globally */
367  bool                                  is_global;
368#endif
369  /** This field is true if the thread is preemptible. */
370  bool                                  is_preemptible;
371#if defined(RTEMS_SMP)
372  /**
373   * @brief This field is true if the thread is scheduled.
374   *
375   * A thread is scheduled if it is ready and the scheduler allocated a
376   * processor for it.  A scheduled thread is assigned to exactly one
377   * processor.  There are exactly processor count scheduled threads in the
378   * system.
379   */
380  bool                                  is_scheduled;
381
382  /**
383   * @brief This field is true if the thread is in the air.
384   *
385   * A thread is in the air if it has an allocated processor (it is an
386   * executing or heir thread on exactly one processor) and it is not a member
387   * of the scheduled chain.  The extract operation on a scheduled thread will
388   * produce threads in the air (see also _Thread_Set_transient()).  The next
389   * enqueue or schedule operation will decide what to do based on this state
390   * indication.  It can either place the thread back on the scheduled chain
391   * and the thread can keep its allocated processor, or it can take the
392   * processor away from the thread and give the processor to another thread of
393   * higher priority.
394   */
395  bool                                  is_in_the_air;
396
397  /**
398   * @brief This field is true if the thread is executing.
399   *
400   * A thread is executing if it executes on a processor.  An executing thread
401   * executes on exactly one processor.  There are exactly processor count
402   * executing threads in the system.  An executing thread may have a heir
403   * thread and thread dispatching is necessary.  On SMP a thread dispatch on a
404   * remote processor needs help from an inter-processor interrupt, thus it
405   * will take some time to complete the state change.  A lot of things can
406   * happen in the meantime.
407   */
408  bool                                  is_executing;
409#endif
410#if __RTEMS_ADA__
411  /** This field is the GNAT self context pointer. */
412  void                                 *rtems_ada_self;
413#endif
414  /** This field is the length of the time quantum that this thread is
415   *  allowed to consume.  The algorithm used to manage limits on CPU usage
416   *  is specified by budget_algorithm.
417   */
418  uint32_t                              cpu_time_budget;
419  /** This field is the algorithm used to manage this thread's time
420   *  quantum.  The algorithm may be specified as none which case,
421   *  no limit is in place.
422   */
423  Thread_CPU_budget_algorithms          budget_algorithm;
424  /** This field is the method invoked with the budgeted time is consumed. */
425  Thread_CPU_budget_algorithm_callout   budget_callout;
426  /** This field is the amount of CPU time consumed by this thread
427   *  since it was created.
428   */
429  Thread_CPU_usage_t                    cpu_time_used;
430
431  /** This pointer holds per-thread data for the scheduler and ready queue. */
432  void                                 *scheduler_info;
433
434#ifdef RTEMS_SMP
435  Per_CPU_Control                      *cpu;
436#endif
437
438  /** This field contains information about the starting state of
439   *  this thread.
440   */
441  Thread_Start_information              Start;
442  /** This field contains the context of this thread. */
443  Context_Control                       Registers;
444#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
445  /** This field points to the floating point context for this thread.
446   *  If NULL, the thread is integer only.
447   */
448  Context_Control_fp                   *fp_context;
449#endif
450  /** This field points to the newlib reentrancy structure for this thread. */
451  struct _reent                        *libc_reent;
452  /** This array contains the API extension area pointers. */
453  void                                 *API_Extensions[ THREAD_API_LAST + 1 ];
454  /** This field points to the user extension pointers. */
455  void                                **extensions;
456  /** This field points to the set of per task variables. */
457  rtems_task_variable_t                *task_variables;
458};
459
460#if (CPU_PROVIDES_IDLE_THREAD_BODY == FALSE)
461/**
462 *  This routine is the body of the system idle thread.
463 *
464 *  NOTE: This routine is actually instantiated by confdefs.h when needed.
465 */
466void *_Thread_Idle_body(
467  uintptr_t  ignored
468);
469#endif
470
471/**  This defines the type for a method which operates on a single thread.
472 */
473typedef void (*rtems_per_thread_routine)( Thread_Control * );
474
475/**
476 *  @brief Iterates over all threads.
477 *  This routine iterates over all threads regardless of API and
478 *  invokes the specified routine.
479 */
480void rtems_iterate_over_all_threads(
481  rtems_per_thread_routine routine
482);
483
484/**
485 * @brief Returns the thread control block of the executing thread.
486 *
487 * This function can be called in any context.  On SMP configurations
488 * interrupts are disabled to ensure that the processor index is used
489 * consistently.
490 *
491 * @return The thread control block of the executing thread.
492 */
493RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Get_executing( void )
494{
495  Thread_Control *executing;
496
497  #if defined( RTEMS_SMP )
498    ISR_Level level;
499
500    _ISR_Disable_without_giant( level );
501  #endif
502
503  executing = _Thread_Executing;
504
505  #if defined( RTEMS_SMP )
506    _ISR_Enable_without_giant( level );
507  #endif
508
509  return executing;
510}
511
512/**@}*/
513
514#ifdef __cplusplus
515}
516#endif
517
518#endif
519/* end of include file */
Note: See TracBrowser for help on using the repository browser.