source: rtems/cpukit/score/include/rtems/score/thread.h @ 8af72be

4.104.114.84.95
Last change on this file since 8af72be was 8af72be, checked in by Ralf Corsepius <ralf.corsepius@…>, on Nov 23, 2003 at 9:45:00 PM

2003-11-23 Ralf Corsepius <corsepiu@…>

PR 525/rtems

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