source: rtems/cpukit/score/include/rtems/score/thread.h @ 89f8eab5

4.115
Last change on this file since 89f8eab5 was 2d36931, checked in by Sebastian Huber <sebastian.huber@…>, on 06/11/14 at 09:03:25

score: Collect scheduler related fields in TCB

Add Thread_Scheduler_control to collect scheduler related fields of the
TCB.

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