source: rtems/cpukit/score/include/rtems/score/thread.h @ 3045738

4.11
Last change on this file since 3045738 was 3045738, checked in by Sebastian Huber <sebastian.huber@…>, on May 28, 2014 at 11:38:48 AM

score: Add resource node to thread control block

  • Property mode set to 100644
File size: 22.2 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 *  This structure defines the Thread Control Block (TCB).
450 */
451struct Thread_Control_struct {
452  /** This field is the object management structure for each thread. */
453  Objects_Control          Object;
454  /** This field is the current execution state of this thread. */
455  States_Control           current_state;
456  /** This field is the current priority state of this thread. */
457  Priority_Control         current_priority;
458  /** This field is the base priority of this thread. */
459  Priority_Control         real_priority;
460  /** This field is the number of mutexes currently held by this thread. */
461  uint32_t                 resource_count;
462  /** This field is the blocking information for this thread. */
463  Thread_Wait_information  Wait;
464  /** This field is the Watchdog used to manage thread delays and timeouts. */
465  Watchdog_Control         Timer;
466#if defined(RTEMS_MULTIPROCESSING)
467  /** This field is the received response packet in an MP system. */
468  MP_packet_Prefix        *receive_packet;
469#endif
470#ifdef __RTEMS_STRICT_ORDER_MUTEX__
471  /** This field is the head of queue of priority inheritance mutex
472   *  held by the thread.
473   */
474  Chain_Control            lock_mutex;
475#endif
476#if defined(RTEMS_SMP)
477  /**
478   * @brief Resource node to build a dependency tree in case this thread owns
479   * resources or depends on a resource.
480   */
481  Resource_Node            Resource_node;
482#endif
483     /*================= end of common block =================*/
484#if defined(RTEMS_MULTIPROCESSING)
485  /** This field is true if the thread is offered globally */
486  bool                                  is_global;
487#endif
488  /** This field is true if the thread is preemptible. */
489  bool                                  is_preemptible;
490#if defined(RTEMS_SMP)
491  /**
492   * @brief The scheduler of this thread.
493   */
494  const struct Scheduler_Control       *scheduler;
495#endif
496
497#if __RTEMS_ADA__
498  /** This field is the GNAT self context pointer. */
499  void                                 *rtems_ada_self;
500#endif
501  /** This field is the length of the time quantum that this thread is
502   *  allowed to consume.  The algorithm used to manage limits on CPU usage
503   *  is specified by budget_algorithm.
504   */
505  uint32_t                              cpu_time_budget;
506  /** This field is the algorithm used to manage this thread's time
507   *  quantum.  The algorithm may be specified as none which case,
508   *  no limit is in place.
509   */
510  Thread_CPU_budget_algorithms          budget_algorithm;
511  /** This field is the method invoked with the budgeted time is consumed. */
512  Thread_CPU_budget_algorithm_callout   budget_callout;
513  /** This field is the amount of CPU time consumed by this thread
514   *  since it was created.
515   */
516  Thread_CPU_usage_t                    cpu_time_used;
517
518  /**
519   * @brief The scheduler node of this thread for the real scheduler.
520   */
521  struct Scheduler_Node                *scheduler_node;
522
523#ifdef RTEMS_SMP
524  /**
525   * @brief The processor assigned by the scheduler.
526   */
527  Per_CPU_Control                      *cpu;
528
529#ifdef RTEMS_DEBUG
530  /**
531   * @brief The processor on which this thread executed the last time or is
532   * executing.
533   */
534  Per_CPU_Control                      *debug_real_cpu;
535#endif
536#endif
537
538  /** This field contains information about the starting state of
539   *  this thread.
540   */
541  Thread_Start_information              Start;
542
543  Thread_Action_control                 Post_switch_actions;
544
545  /** This field contains the context of this thread. */
546  Context_Control                       Registers;
547#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
548  /** This field points to the floating point context for this thread.
549   *  If NULL, the thread is integer only.
550   */
551  Context_Control_fp                   *fp_context;
552#endif
553  /** This field points to the newlib reentrancy structure for this thread. */
554  struct _reent                        *libc_reent;
555  /** This array contains the API extension area pointers. */
556  void                                 *API_Extensions[ THREAD_API_LAST + 1 ];
557
558#if !defined(RTEMS_SMP)
559  /** This field points to the set of per task variables. */
560  rtems_task_variable_t                *task_variables;
561#endif
562
563  /**
564   * This is the thread key value chain's control, which is used
565   * to track all key value for specific thread, and when thread
566   * exits, we can remove all key value for specific thread by
567   * iterating this chain, or we have to search a whole rbtree,
568   * which is inefficient.
569   */
570  Chain_Control           Key_Chain;
571
572  /**
573   * @brief Thread life-cycle control.
574   *
575   * Control state changes triggered by thread restart and delete requests.
576   */
577  Thread_Life_control                   Life;
578
579  /**
580   * @brief Variable length array of user extension pointers.
581   *
582   * The length is defined by the application via <rtems/confdefs.h>.
583   */
584  void                                 *extensions[ RTEMS_ZERO_LENGTH_ARRAY ];
585};
586
587#if (CPU_PROVIDES_IDLE_THREAD_BODY == FALSE)
588/**
589 *  This routine is the body of the system idle thread.
590 *
591 *  NOTE: This routine is actually instantiated by confdefs.h when needed.
592 */
593void *_Thread_Idle_body(
594  uintptr_t  ignored
595);
596#endif
597
598/**  This defines the type for a method which operates on a single thread.
599 */
600typedef void (*rtems_per_thread_routine)( Thread_Control * );
601
602/**
603 *  @brief Iterates over all threads.
604 *  This routine iterates over all threads regardless of API and
605 *  invokes the specified routine.
606 */
607void rtems_iterate_over_all_threads(
608  rtems_per_thread_routine routine
609);
610
611/**
612 * @brief Returns the thread control block of the executing thread.
613 *
614 * This function can be called in any context.  On SMP configurations
615 * interrupts are disabled to ensure that the processor index is used
616 * consistently.
617 *
618 * @return The thread control block of the executing thread.
619 */
620RTEMS_INLINE_ROUTINE Thread_Control *_Thread_Get_executing( void )
621{
622  Thread_Control *executing;
623
624  #if defined( RTEMS_SMP )
625    ISR_Level level;
626
627    _ISR_Disable_without_giant( level );
628  #endif
629
630  executing = _Thread_Executing;
631
632  #if defined( RTEMS_SMP )
633    _ISR_Enable_without_giant( level );
634  #endif
635
636  return executing;
637}
638
639/**
640 * @brief Thread control add-on.
641 */
642typedef struct {
643  /**
644   * @brief Offset of the pointer field in Thread_Control referencing an
645   * application configuration dependent memory area in the thread control
646   * block.
647   */
648  size_t destination_offset;
649
650  /**
651   * @brief Offset relative to the thread control block begin to an application
652   * configuration dependent memory area.
653   */
654  size_t source_offset;
655} Thread_Control_add_on;
656
657/**
658 * @brief Thread control add-ons.
659 *
660 * The thread control block contains fields that point to application
661 * configuration dependent memory areas, like the scheduler information, the
662 * API control blocks, the user extension context table, the RTEMS notepads and
663 * the Newlib re-entrancy support.  Account for these areas in the
664 * configuration and avoid extra workspace allocations for these areas.
665 *
666 * This array is provided via <rtems/confdefs.h>.
667 *
668 * @see _Thread_Control_add_on_count and _Thread_Control_size.
669 */
670extern const Thread_Control_add_on _Thread_Control_add_ons[];
671
672/**
673 * @brief Thread control add-on count.
674 *
675 * Count of entries in _Thread_Control_add_ons.
676 *
677 * This value is provided via <rtems/confdefs.h>.
678 */
679extern const size_t _Thread_Control_add_on_count;
680
681/**
682 * @brief Size of the thread control block of a particular application.
683 *
684 * This value is provided via <rtems/confdefs.h>.
685 *
686 * @see _Thread_Control_add_ons.
687 */
688extern const size_t _Thread_Control_size;
689
690/**@}*/
691
692#ifdef __cplusplus
693}
694#endif
695
696#endif
697/* end of include file */
Note: See TracBrowser for help on using the repository browser.