source: rtems/cpukit/score/include/rtems/score/thread.h @ 728a0bd3

4.104.114.84.95
Last change on this file since 728a0bd3 was 728a0bd3, checked in by Ralf Corsepius <ralf.corsepius@…>, on May 8, 2007 at 10:43:06 AM

Use size_t for stacksizes.

  • Property mode set to 100644
File size: 24.0 KB
Line 
1/**
2 *  @file  rtems/score/thread.h
3 *
4 *  This include file contains all constants and structures associated
5 *  with the thread control block.
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2006.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.com/license/LICENSE.
15 *
16 *  $Id$
17 */
18
19#ifndef _RTEMS_SCORE_THREAD_H
20#define _RTEMS_SCORE_THREAD_H
21
22/**
23 *  @defgroup ScoreThread Thread Handler
24 *
25 *  This handler encapsulates functionality related to the management of
26 *  threads.  This includes the creation, deletion, and scheduling of threads.
27 */
28/**@{*/
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include <rtems/score/context.h>
35#include <rtems/score/cpu.h>
36#if defined(RTEMS_MULTIPROCESSING)
37#include <rtems/score/mppkt.h>
38#endif
39#include <rtems/score/object.h>
40#include <rtems/score/priority.h>
41#include <rtems/score/stack.h>
42#include <rtems/score/states.h>
43#include <rtems/score/tod.h>
44#include <rtems/score/tqdata.h>
45#include <rtems/score/watchdog.h>
46
47/**
48 *  The following defines the "return type" of a thread.
49 *
50 *  @note  This cannot always be right.  Some APIs have void
51 *         tasks/threads, others return pointers, others may
52 *         return a numeric value.  Hopefully a pointer is
53 *         always at least as big as an uint32_t  . :)
54 */
55typedef void *Thread;
56
57/**
58 *  The following defines the ways in which the entry point for a
59 *  thread can be invoked.  Basically, it can be passed any
60 *  combination/permutation of a pointer and an uint32_t   value.
61 *
62 *  @note For now, we are ignoring the return type.
63 */
64typedef enum {
65  THREAD_START_NUMERIC,
66  THREAD_START_POINTER,
67  THREAD_START_BOTH_POINTER_FIRST,
68  THREAD_START_BOTH_NUMERIC_FIRST
69} Thread_Start_types;
70
71/** This type corresponds to a very simple style thread entry point. */
72typedef Thread ( *Thread_Entry )();   /* basic type */
73
74/** This type corresponds to a thread entry point which takes a single
75 *  unsigned thirty-two bit integer as an argument.
76 */
77typedef Thread ( *Thread_Entry_numeric )( uint32_t   );
78
79/** This type corresponds to a thread entry point which takes a single
80 *  untyped pointer as an argument.
81 */
82typedef Thread ( *Thread_Entry_pointer )( void * );
83
84/** This type corresponds to a thread entry point which takes a single
85 *  untyped pointer and an unsigned thirty-two bit integer as arguments.
86 */
87typedef Thread ( *Thread_Entry_both_pointer_first )( void *, uint32_t   );
88
89/** This type corresponds to a thread entry point which takes a single
90 *  unsigned thirty-two bit integer and an untyped pointer and an
91 *  as arguments.
92 */
93typedef Thread ( *Thread_Entry_both_numeric_first )( uint32_t  , void * );
94
95/**
96 *  The following lists the algorithms used to manage the thread cpu budget.
97 *
98 *  Reset Timeslice:   At each context switch, reset the time quantum.
99 *  Exhaust Timeslice: Only reset the quantum once it is consumed.
100 *  Callout:           Execute routine when budget is consumed.
101 */
102typedef enum {
103  THREAD_CPU_BUDGET_ALGORITHM_NONE,
104  THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE,
105  THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE,
106  THREAD_CPU_BUDGET_ALGORITHM_CALLOUT
107}  Thread_CPU_budget_algorithms;
108
109/** This type defines the Thread Control Block structure.
110 */
111typedef struct Thread_Control_struct Thread_Control;
112
113/**  This defines thes the entry point for the thread specific timeslice
114 *   budget management algorithm.
115 */
116typedef void (*Thread_CPU_budget_algorithm_callout )( Thread_Control * );
117
118/** @brief Per Task Variable Manager Structure Forward Reference
119 *
120 *  Forward reference to the per task variable structure.
121 */
122struct rtems_task_variable_tt;
123
124/** @brief Per Task Variable Manager Structure
125 *
126 *  This is the internal structure used to manager per Task Variables.
127 */
128typedef struct {
129  /** This field points to the next per task variable for this task. */
130  struct rtems_task_variable_tt  *next;
131  /** This field points to the physical memory location of this per
132   *  task variable.
133   */
134  void                          **ptr;
135  /** This field is to the global value for this per task variable. */
136  void                           *gval;
137  /** This field is to this thread's value for this per task variable. */
138  void                           *tval;
139  /** This field points to the destructor for this per task variable. */
140  void                          (*dtor)(void *);
141} rtems_task_variable_t;
142
143/**
144 *  The following structure contains the information which defines
145 *  the starting state of a thread.
146 */
147typedef struct {
148  /** This field is the starting address for the thread. */
149  Thread_Entry         entry_point;
150  /** This field indicatres the how task is invoked. */
151  Thread_Start_types   prototype;
152  /** This field is the pointer argument passed at thread start. */
153  void                *pointer_argument;
154  /** This field is the numeric argument passed at thread start. */
155  uint32_t             numeric_argument;
156  /*-------------- initial execution modes ----------------- */
157  /** This field indicates whether the thread was preemptible when
158    * it started.
159    */
160  boolean              is_preemptible;
161  /** This field indicates the CPU budget algorith. */
162  Thread_CPU_budget_algorithms          budget_algorithm;
163  /** This field is the routine to invoke when the CPU allotment is
164   *  consumed.
165   */
166  Thread_CPU_budget_algorithm_callout   budget_callout;
167  /** This field is the initial ISR disable level of this thread. */
168  uint32_t             isr_level;
169  /** This field is the initial priority. */
170  Priority_Control     initial_priority;
171  /** This field indicates whether the SuperCore allocated the stack. */
172  boolean              core_allocated_stack;
173  /** This field is the stack information. */
174  Stack_Control        Initial_stack;
175#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
176  /** This field is the initial FP context area address. */
177  Context_Control_fp  *fp_context;
178#endif
179  /** This field is the initial stack area address. */
180  void                *stack;
181}   Thread_Start_information;
182
183/**
184 *  The following structure contains the information necessary to manage
185 *  a thread which it is  waiting for a resource.
186 */
187#define THREAD_STATUS_PROXY_BLOCKING 0x1111111
188
189/** @brief Thread Blocking Management Information
190 *
191 *  This contains the information required to manage a thread while it is
192 *  blocked and to return information to it.
193 */
194typedef struct {
195  /** This field is the Id of the object this thread is waiting upon. */
196  Objects_Id            id;
197  /** This field is used to return an integer while when blocked. */
198  uint32_t              count;
199  /** This field is the first pointer to a user return argument. */
200  void                 *return_argument;
201  /** This field is the second pointer to a user return argument. */
202  void                 *return_argument_1;
203  /** This field contains any options in effect on this blocking operation. */
204  uint32_t              option;
205  /** This field will contain the return status from a blocking operation.
206   *
207   *  @note The following assumes that all API return codes can be
208   *        treated as an uint32_t.
209   */
210  uint32_t              return_code;
211
212  /** This field is the chain header for the second through Nth tasks
213   *  of the same priority blocked waiting on the same object.
214   */
215  Chain_Control         Block2n;
216  /** This field points to the thread queue on which this thread is blocked. */
217  Thread_queue_Control *queue;
218}   Thread_Wait_information;
219
220/**
221 *  The following defines the control block used to manage
222 *  each thread proxy.
223 *
224 *  @note It is critical that proxies and threads have identical
225 *        memory images for the shared part.
226 */
227typedef struct {
228  /** This field is the object management structure for each proxy. */
229  Objects_Control          Object;
230  /** This field is the current execution state of this proxy. */
231  States_Control           current_state;
232  /** This field is the current priority state of this proxy. */
233  Priority_Control         current_priority;
234  /** This field is the base priority of this proxy. */
235  Priority_Control         real_priority;
236  /** This field is the number of mutexes currently held by this proxy. */
237  uint32_t                 resource_count;
238  /** This field is the blocking information for this proxy. */
239  Thread_Wait_information  Wait;
240  /** This field is the Watchdog used to manage proxy delays and timeouts. */
241  Watchdog_Control         Timer;
242#if defined(RTEMS_MULTIPROCESSING)
243  /** This field is the received response packet in an MP system. */
244  MP_packet_Prefix        *receive_packet;
245#endif
246     /****************** end of common block ********************/
247  /** This field is used to manage the set of proxies in the system. */
248  Chain_Node               Active;
249}   Thread_Proxy_control;
250
251/**
252 *  The following record defines the control block used
253 *  to manage each thread.
254 *
255 *  @note It is critical that proxies and threads have identical
256 *        memory images for the shared part.
257 */
258typedef enum {
259  /** This value is for the Classic RTEMS API. */
260  THREAD_API_RTEMS,
261  /** This value is for the POSIX API. */
262  THREAD_API_POSIX,
263  /** This value is for the ITRON API. */
264  THREAD_API_ITRON
265}  Thread_APIs;
266
267/** This macro defines the first API which has threads. */
268#define THREAD_API_FIRST THREAD_API_RTEMS
269
270/** This macro defines the last API which has threads. */
271#define THREAD_API_LAST  THREAD_API_ITRON
272
273/**
274 *  This structure defines the Thread Control Block (TCB).
275 */
276struct Thread_Control_struct {
277  /** This field is the object management structure for each thread. */
278  Objects_Control          Object;
279  /** This field is the current execution state of this thread. */
280  States_Control           current_state;
281  /** This field is the current priority state of this thread. */
282  Priority_Control         current_priority;
283  /** This field is the base priority of this thread. */
284  Priority_Control         real_priority;
285  /** This field is the number of mutexes currently held by this thread. */
286  uint32_t                 resource_count;
287  /** This field is the blocking information for this thread. */
288  Thread_Wait_information  Wait;
289  /** This field is the Watchdog used to manage thread delays and timeouts. */
290  Watchdog_Control         Timer;
291#if defined(RTEMS_MULTIPROCESSING)
292  /** This field is the received response packet in an MP system. */
293  MP_packet_Prefix        *receive_packet;
294#endif
295     /*================= end of common block =================*/
296  /** This field is the number of nested suspend calls. */
297  uint32_t                              suspend_count;
298  /** This field is true if the thread is offered globally */
299  boolean                               is_global;
300  /** This field is is true if the post task context switch should be
301   *  executed for this thread at the next context switch.
302   */
303  boolean                               do_post_task_switch_extension;
304  /** This field is true if the thread is preemptible. */
305  boolean                               is_preemptible;
306  /** This field is the GNAT self context pointer. */
307  void                                 *rtems_ada_self;
308  /** This field is the length of the time quantum that this thread is
309   *  allowed to consume.  The algorithm used to manage limits on CPU usage
310   *  is specified by budget_algorithm.
311   */
312  uint32_t                              cpu_time_budget;
313  /** This field is the algorithm used to manage this thread's time
314   *  quantum.  The algorithm may be specified as none which case,
315   *  no limit is in place.
316   */
317  Thread_CPU_budget_algorithms          budget_algorithm;
318  /** This field is the method invoked with the budgeted time is consumed. */
319  Thread_CPU_budget_algorithm_callout   budget_callout;
320
321  /** This field is the number of clock ticks executed by this thread
322   *  since it was created.
323   */
324  uint32_t                              ticks_executed;
325  /** This field points to the Ready FIFO for this priority. */
326  Chain_Control                        *ready;
327  /** This field contains precalculated priority map indices. */
328  Priority_Information                  Priority_map;
329  /** This field contains information about the starting state of
330   *  this thread.
331   */
332  Thread_Start_information              Start;
333  /** This field contains the context of this thread. */
334  Context_Control                       Registers;
335#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
336  /** This field points to the floating point context for this thread.
337   *  If NULL, the thread is integer only.
338   */
339  Context_Control_fp                   *fp_context;
340#endif
341  /** This field points to the newlib reentrancy structure for this thread. */
342  struct _reent                        *libc_reent;
343  /** This array contains the API extension area pointers. */
344  void                                 *API_Extensions[ THREAD_API_LAST + 1 ];
345  /** This field points to the user extension pointers. */
346  void                                **extensions;
347  /** This field points to the set of per task variables. */
348  rtems_task_variable_t                *task_variables;
349};
350
351/**
352 *  Self for the GNU Ada Run-Time
353 */
354SCORE_EXTERN void *rtems_ada_self;
355
356/**
357 *  The following defines the information control block used to
358 *  manage this class of objects.
359 */
360SCORE_EXTERN Objects_Information _Thread_Internal_information;
361
362/**
363 *  The following define the thread control pointers used to access
364 *  and manipulate the idle thread.
365 */
366SCORE_EXTERN Thread_Control *_Thread_Idle;
367
368/**
369 *  The following context area contains the context of the "thread"
370 *  which invoked the start multitasking routine.  This context is
371 *  restored as the last action of the stop multitasking routine.  Thus
372 *  control of the processor can be returned to the environment
373 *  which initiated the system.
374 */
375SCORE_EXTERN Context_Control _Thread_BSP_context;
376
377/**
378 *  The following declares the dispatch critical section nesting
379 *  counter which is used to prevent context switches at inopportune
380 *  moments.
381 */
382SCORE_EXTERN volatile uint32_t   _Thread_Dispatch_disable_level;
383
384/**
385 *  If this is non-zero, then the post-task switch extension
386 *  is run regardless of the state of the per thread flag.
387 */
388SCORE_EXTERN uint32_t   _Thread_Do_post_task_switch_extension;
389
390/**
391 *  The following holds how many user extensions are in the system.  This
392 *  is used to determine how many user extension data areas to allocate
393 *  per thread.
394 */
395SCORE_EXTERN uint32_t   _Thread_Maximum_extensions;
396
397/**
398 *  The following is used to manage the length of a timeslice quantum.
399 */
400SCORE_EXTERN uint32_t   _Thread_Ticks_per_timeslice;
401
402/**
403 *  The following points to the array of FIFOs used to manage the
404 *  set of ready threads.
405 */
406SCORE_EXTERN Chain_Control *_Thread_Ready_chain;
407
408/**
409 *  The following points to the thread which is currently executing.
410 *  This thread is implicitly manipulated by numerous directives.
411 */
412SCORE_EXTERN Thread_Control *_Thread_Executing;
413
414/**
415 *  The following points to the highest priority ready thread
416 *  in the system.  Unless the current thread is not preemptibl,
417 *  then this thread will be context switched to when the next
418 *  dispatch occurs.
419 */
420SCORE_EXTERN Thread_Control *_Thread_Heir;
421
422/**
423 *  The following points to the thread whose floating point
424 *  context is currently loaded.
425 */
426#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
427SCORE_EXTERN Thread_Control *_Thread_Allocated_fp;
428#endif
429
430/**
431 * The C library re-enter-rant global pointer. Some C library implementations
432 * such as newlib have a single global pointer that changed during a context
433 * switch. The pointer points to that global pointer. The Thread control block
434 * holds a pointer to the task specific data.
435 */
436SCORE_EXTERN struct _reent **_Thread_libc_reent;
437
438/**
439 *  This routine performs the initialization necessary for this handler.
440 */
441void _Thread_Handler_initialization (
442  uint32_t     ticks_per_timeslice,
443  uint32_t     maximum_extensions,
444  uint32_t     maximum_proxies
445);
446
447/**
448 *  This routine creates the idle thread.
449 *
450 *  @warning No thread should be created before this one.
451 */
452void _Thread_Create_idle( void );
453
454/**
455 *  This routine initiates multitasking.  It is invoked only as
456 *  part of initialization and its invocation is the last act of
457 *  the non-multitasking part of the system initialization.
458 */
459void _Thread_Start_multitasking( void );
460
461/**
462 *  This routine is responsible for transferring control of the
463 *  processor from the executing thread to the heir thread.  As part
464 *  of this process, it is responsible for the following actions:
465 *
466 *     + saving the context of the executing thread
467 *     + restoring the context of the heir thread
468 *     + dispatching any signals for the resulting executing thread
469 */
470void _Thread_Dispatch( void );
471
472/**
473 *  Allocate the requested stack space for the thread.
474 *  return the actual size allocated after any adjustment
475 *  or return zero if the allocation failed.
476 *  Set the Start.stack field to the address of the stack
477 */
478
479size_t _Thread_Stack_Allocate(
480  Thread_Control *the_thread,
481  size_t          stack_size
482);
483
484/**
485 *  Deallocate the Thread's stack.
486 */
487void _Thread_Stack_Free(
488  Thread_Control *the_thread
489);
490
491/**
492 *  This routine initializes the specified the thread.  It allocates
493 *  all memory associated with this thread.  It completes by adding
494 *  the thread to the local object table so operations on this
495 *  thread id are allowed.
496 *
497 *  @note If stack_area is NULL, it is allocated from the workspace.
498 *
499 *  @note If the stack is allocated from the workspace, then it is
500 *        guaranteed to be of at least minimum size.
501 */
502boolean _Thread_Initialize(
503  Objects_Information                  *information,
504  Thread_Control                       *the_thread,
505  void                                 *stack_area,
506  size_t                                stack_size,
507  boolean                               is_fp,
508  Priority_Control                      priority,
509  boolean                               is_preemptible,
510  Thread_CPU_budget_algorithms          budget_algorithm,
511  Thread_CPU_budget_algorithm_callout   budget_callout,
512  uint32_t                              isr_level,
513  Objects_Name                          name
514);
515
516/**
517 *  This routine initializes the executable information for a thread
518 *  and makes it ready to execute.  After this routine executes, the
519 *  thread competes with all other threads for CPU time.
520 */
521boolean _Thread_Start(
522  Thread_Control           *the_thread,
523  Thread_Start_types        the_prototype,
524  void                     *entry_point,
525  void                     *pointer_argument,
526  uint32_t                  numeric_argument
527);
528
529/**
530 *  This support routine restarts the specified task in a way that the
531 *  next time this thread executes, it will begin execution at its
532 *  original starting point.
533 *
534 *  TODO:  multiple task arg profiles
535 */
536boolean _Thread_Restart(
537  Thread_Control           *the_thread,
538  void                     *pointer_argument,
539  uint32_t                  numeric_argument
540);
541
542/**
543 *  This routine resets a thread to its initial state but does
544 *  not restart it.
545 */
546void _Thread_Reset(
547  Thread_Control      *the_thread,
548  void                *pointer_argument,
549  uint32_t             numeric_argument
550);
551
552/**
553 *  This routine frees all memory associated with the specified
554 *  thread and removes it from the local object table so no further
555 *  operations on this thread are allowed.
556 */
557void _Thread_Close(
558  Objects_Information  *information,
559  Thread_Control       *the_thread
560);
561
562/**
563 *  This routine removes any set states for the_thread.  It performs
564 *  any necessary scheduling operations including the selection of
565 *  a new heir thread.
566 */
567void _Thread_Ready(
568  Thread_Control *the_thread
569);
570
571/**
572 *  This routine clears the indicated STATES for the_thread.  It performs
573 *  any necessary scheduling operations including the selection of
574 *  a new heir thread.
575 */
576void _Thread_Clear_state(
577  Thread_Control *the_thread,
578  States_Control  state
579);
580
581/**
582 *  This routine sets the indicated states for the_thread.  It performs
583 *  any necessary scheduling operations including the selection of
584 *  a new heir thread.
585 */
586void _Thread_Set_state(
587  Thread_Control *the_thread,
588  States_Control  state
589);
590
591/**
592 *  This routine sets the TRANSIENT state for the_thread.  It performs
593 *  any necessary scheduling operations including the selection of
594 *  a new heir thread.
595 */
596void _Thread_Set_transient(
597  Thread_Control *the_thread
598);
599
600/**
601 *  This routine is invoked upon expiration of the currently
602 *  executing thread's timeslice.  If no other thread's are ready
603 *  at the priority of the currently executing thread, then the
604 *  executing thread's timeslice is reset.  Otherwise, the
605 *  currently executing thread is placed at the rear of the
606 *  FIFO for this priority and a new heir is selected.
607 */
608void _Thread_Reset_timeslice( void );
609
610/**
611 *  This routine is invoked as part of processing each clock tick.
612 *  It is responsible for determining if the current thread allows
613 *  timeslicing and, if so, when its timeslice expires.
614 */
615void _Thread_Tickle_timeslice( void );
616
617/**
618 *  This routine is invoked when a thread wishes to voluntarily
619 *  transfer control of the processor to another thread of equal
620 *  or greater priority.
621 */
622void _Thread_Yield_processor( void );
623
624/**
625 *  This routine is invoked to rotate the ready queue for the
626 *  given priority.  It can be used to yeild the processor
627 *  by rotating the executing threads ready queue.
628 */
629void _Thread_Rotate_Ready_Queue(
630  Priority_Control  priority
631);
632
633/**
634 *  This routine initializes the context of the_thread to its
635 *  appropriate starting state.
636 */
637void _Thread_Load_environment(
638  Thread_Control *the_thread
639);
640
641/**
642 *  This routine is the wrapper function for all threads.  It is
643 *  the starting point for all threads.  The user provided thread
644 *  entry point is invoked by this routine.  Operations
645 *  which must be performed immediately before and after the user's
646 *  thread executes are found here.
647 */
648void _Thread_Handler( void );
649
650/**
651 *  This routine is invoked when a thread must be unblocked at the
652 *  end of a time based delay (i.e. wake after or wake when).
653 */
654void _Thread_Delay_ended(
655  Objects_Id  id,
656  void       *ignored
657);
658
659/**
660 *  This routine changes the current priority of the_thread to
661 *  new_priority.  It performs any necessary scheduling operations
662 *  including the selection of a new heir thread.
663 */
664void _Thread_Change_priority (
665  Thread_Control   *the_thread,
666  Priority_Control  new_priority,
667  boolean           prepend_it
668);
669
670/**
671 *  This routine updates the priority related fields in the_thread
672 *  control block to indicate the current priority is now new_priority.
673 */
674void _Thread_Set_priority(
675  Thread_Control   *the_thread,
676  Priority_Control  new_priority
677);
678
679/**
680 *  This routine updates the related suspend fields in the_thread
681 *  control block to indicate the current nested level.
682 */
683void _Thread_Suspend(
684  Thread_Control   *the_thread
685);
686
687/**
688 *  This routine updates the related suspend fields in the_thread
689 *  control block to indicate the current nested level.  A force
690 *  parameter of TRUE will force a resume and clear the suspend count.
691 */
692void _Thread_Resume(
693  Thread_Control   *the_thread,
694  boolean           force
695);
696
697/**
698 *  This routine evaluates the current scheduling information for the
699 *  system and determines if a context switch is required.  This
700 *  is usually called after changing an execution mode such as preemptability
701 *  for a thread.
702 */
703boolean _Thread_Evaluate_mode( void );
704
705#if (CPU_PROVIDES_IDLE_THREAD_BODY == FALSE)
706/**
707 *  This routine is the body of the system idle thread.
708 */
709Thread _Thread_Idle_body(
710  uint32_t   ignored
711);
712#endif
713
714/**  This defines the type for a method which operates on a single thread.
715 */
716typedef void (*rtems_per_thread_routine)( Thread_Control * );
717
718/**
719 *  This routine iterates over all threads regardless of API and
720 *  invokes the specified routine.
721 */
722void rtems_iterate_over_all_threads(
723  rtems_per_thread_routine routine
724);
725
726#ifndef __RTEMS_APPLICATION__
727#include <rtems/score/thread.inl>
728#endif
729#if defined(RTEMS_MULTIPROCESSING)
730#include <rtems/score/threadmp.h>
731#endif
732
733#ifdef __cplusplus
734}
735#endif
736
737/**@}*/
738
739#endif
740/* end of include file */
Note: See TracBrowser for help on using the repository browser.