source: rtems/cpukit/score/include/rtems/score/thread.h @ 039a189d

4.115
Last change on this file since 039a189d was 9db8705, checked in by Jennifer Averett <jennifer.averett@…>, on 02/06/14 at 18:42:24

score: Add cpuset support to Score.

This new Score Handler provides a structure to manage a
cpu_set_t plus helper routines to validate the contents
against the current system configuration.

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