source: rtems/cpukit/score/include/rtems/score/thread.h @ 17c66867

4.104.114.84.95
Last change on this file since 17c66867 was 17c66867, checked in by Joel Sherrill <joel.sherrill@…>, on Aug 14, 2003 at 8:04:18 PM

2003-08-14 Joel Sherrill <joel@…>

PR 408/filesystem

  • score/Makefile.am, score/include/rtems/score/thread.h: Added sync() service. As part of adding this service, the new RTEMS service rtems_iterate_over_all_threads() was also added. This new service makes it easier to iterate over all the tasks/threads in a system and perform an action on them.
  • score/src/iterateoverthreads.c: New file.
  • ChangeLog?: Fixed screwup.
  • 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.OARcorp.com/rtems/license.html.
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  void                                 *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 */
326
327SCORE_EXTERN void **_Thread_libc_reent;
328
329/*
330 *  _Thread_Handler_initialization
331 *
332 *  DESCRIPTION:
333 *
334 *  This routine performs the initialization necessary for this handler.
335 */
336
337void _Thread_Handler_initialization (
338  unsigned32   ticks_per_timeslice,
339  unsigned32   maximum_extensions,
340  unsigned32   maximum_proxies
341);
342
343/*
344 *  _Thread_Create_idle
345 *
346 *  DESCRIPTION:
347 *
348 *  This routine creates the idle thread.
349 *
350 *  WARNING!! No thread should be created before this one.
351 */
352 
353void _Thread_Create_idle( void );
354
355/*
356 *  _Thread_Start_multitasking
357 *
358 *  DESCRIPTION:
359 *
360 *  This routine initiates multitasking.  It is invoked only as
361 *  part of initialization and its invocation is the last act of
362 *  the non-multitasking part of the system initialization.
363 */
364
365void _Thread_Start_multitasking( void );
366
367/*
368 *  _Thread_Dispatch
369 *
370 *  DESCRIPTION:
371 *
372 *  This routine is responsible for transferring control of the
373 *  processor from the executing thread to the heir thread.  As part
374 *  of this process, it is responsible for the following actions:
375 *
376 *     + saving the context of the executing thread
377 *     + restoring the context of the heir thread
378 *     + dispatching any signals for the resulting executing thread
379 */
380
381void _Thread_Dispatch( void );
382
383/*
384 *  _Thread_Stack_Allocate
385 * 
386 *  DESCRIPTION:
387 *
388 *  Allocate the requested stack space for the thread.
389 *  return the actual size allocated after any adjustment
390 *  or return zero if the allocation failed.
391 *  Set the Start.stack field to the address of the stack
392 *
393 *  NOTES: NONE
394 *
395 */
396
397unsigned32 _Thread_Stack_Allocate(
398  Thread_Control *the_thread,
399  unsigned32 stack_size
400);
401
402/*
403 *  _Thread_Stack_Free
404 *
405 *  DESCRIPTION:
406 *
407 *  Deallocate the Thread's stack.
408 *  NOTES: NONE
409 *
410 */
411
412void _Thread_Stack_Free(
413  Thread_Control *the_thread
414);
415
416/*
417 *  _Thread_Initialize
418 *
419 *  DESCRIPTION:
420 *
421 *  This routine initializes the specified the thread.  It allocates
422 *  all memory associated with this thread.  It completes by adding
423 *  the thread to the local object table so operations on this
424 *  thread id are allowed.
425 *
426 *  NOTES:
427 *
428 *  If stack_area is NULL, it is allocated from the workspace.
429 *
430 *  If the stack is allocated from the workspace, then it is guaranteed to be
431 *  of at least minimum size.
432 */
433
434boolean _Thread_Initialize(
435  Objects_Information                  *information,
436  Thread_Control                       *the_thread,
437  void                                 *stack_area,
438  unsigned32                            stack_size,
439  boolean                               is_fp,
440  Priority_Control                      priority,
441  boolean                               is_preemptible,
442  Thread_CPU_budget_algorithms          budget_algorithm,
443  Thread_CPU_budget_algorithm_callout   budget_callout,
444  unsigned32                            isr_level,
445  Objects_Name                          name
446);
447
448/*
449 *  _Thread_Start
450 *
451 *  DESCRIPTION:
452 *
453 *  This routine initializes the executable information for a thread
454 *  and makes it ready to execute.  After this routine executes, the
455 *  thread competes with all other threads for CPU time.
456 */
457 
458boolean _Thread_Start(
459  Thread_Control           *the_thread,
460  Thread_Start_types        the_prototype,
461  void                     *entry_point,
462  void                     *pointer_argument,
463  unsigned32                numeric_argument
464);
465
466/*
467 *  _Thread_Restart
468 *
469 *  DESCRIPTION:
470 *
471 *  This support routine restarts the specified task in a way that the
472 *  next time this thread executes, it will begin execution at its
473 *  original starting point.
474 */
475 
476/* XXX multiple task arg profiles */
477 
478boolean _Thread_Restart(
479  Thread_Control           *the_thread,
480  void                     *pointer_argument,
481  unsigned32                numeric_argument
482);
483
484/*
485 *  _Thread_Reset
486 *
487 *  DESCRIPTION:
488 *
489 *  This routine resets a thread to its initial state but does
490 *  not restart it.
491 */
492 
493void _Thread_Reset(
494  Thread_Control      *the_thread,
495  void                *pointer_argument,
496  unsigned32           numeric_argument
497);
498
499/*
500 *  _Thread_Close
501 *
502 *  DESCRIPTION:
503 *
504 *  This routine frees all memory associated with the specified
505 *  thread and removes it from the local object table so no further
506 *  operations on this thread are allowed.
507 */
508 
509void _Thread_Close(
510  Objects_Information  *information,
511  Thread_Control       *the_thread
512);
513
514/*
515 *  _Thread_Ready
516 *
517 *  DESCRIPTION:
518 *
519 *  This routine removes any set states for the_thread.  It performs
520 *  any necessary scheduling operations including the selection of
521 *  a new heir thread.
522 */
523
524void _Thread_Ready(
525  Thread_Control *the_thread
526);
527
528/*
529 *  _Thread_Clear_state
530 *
531 *  DESCRIPTION:
532 *
533 *  This routine clears the indicated STATES for the_thread.  It performs
534 *  any necessary scheduling operations including the selection of
535 *  a new heir thread.
536 */
537
538void _Thread_Clear_state(
539  Thread_Control *the_thread,
540  States_Control  state
541);
542
543/*
544 *  _Thread_Set_state
545 *
546 *  DESCRIPTION:
547 *
548 *  This routine sets the indicated states for the_thread.  It performs
549 *  any necessary scheduling operations including the selection of
550 *  a new heir thread.
551 *
552 */
553
554void _Thread_Set_state(
555  Thread_Control *the_thread,
556  States_Control  state
557);
558
559/*
560 *  _Thread_Set_transient
561 *
562 *  DESCRIPTION:
563 *
564 *  This routine sets the TRANSIENT state for the_thread.  It performs
565 *  any necessary scheduling operations including the selection of
566 *  a new heir thread.
567 */
568
569void _Thread_Set_transient(
570  Thread_Control *the_thread
571);
572
573/*
574 *  _Thread_Reset_timeslice
575 *
576 *  DESCRIPTION:
577 *
578 *  This routine is invoked upon expiration of the currently
579 *  executing thread's timeslice.  If no other thread's are ready
580 *  at the priority of the currently executing thread, then the
581 *  executing thread's timeslice is reset.  Otherwise, the
582 *  currently executing thread is placed at the rear of the
583 *  FIFO for this priority and a new heir is selected.
584 */
585
586void _Thread_Reset_timeslice( void );
587
588/*
589 *  _Thread_Tickle_timeslice
590 *
591 *  DESCRIPTION:
592 *
593 *  This routine is invoked as part of processing each clock tick.
594 *  It is responsible for determining if the current thread allows
595 *  timeslicing and, if so, when its timeslice expires.
596 */
597
598void _Thread_Tickle_timeslice( void );
599
600/*
601 *  _Thread_Yield_processor
602 *
603 *  DESCRIPTION:
604 *
605 *  This routine is invoked when a thread wishes to voluntarily
606 *  transfer control of the processor to another thread of equal
607 *  or greater priority.
608 */
609
610void _Thread_Yield_processor( void );
611
612/* 
613 *  _Thread_Rotate_Ready_Queue
614 * 
615 *  DESCRIPTION:
616 * 
617 *  This routine is invoked to rotate the ready queue for the
618 *  given priority.  It can be used to yeild the processor
619 *  by rotating the executing threads ready queue.
620 */
621
622void _Thread_Rotate_Ready_Queue(
623  Priority_Control  priority
624);
625
626/*
627 *  _Thread_Load_environment
628 *
629 *  DESCRIPTION:
630 *
631 *  This routine initializes the context of the_thread to its
632 *  appropriate starting state.
633 */
634
635void _Thread_Load_environment(
636  Thread_Control *the_thread
637);
638
639/*
640 *  _Thread_Handler
641 *
642 *  DESCRIPTION:
643 *
644 *  This routine is the wrapper function for all threads.  It is
645 *  the starting point for all threads.  The user provided thread
646 *  entry point is invoked by this routine.  Operations
647 *  which must be performed immediately before and after the user's
648 *  thread executes are found here.
649 */
650
651void _Thread_Handler( void );
652
653/*
654 *  _Thread_Delay_ended
655 *
656 *  DESCRIPTION:
657 *
658 *  This routine is invoked when a thread must be unblocked at the
659 *  end of a time based delay (i.e. wake after or wake when).
660 */
661
662void _Thread_Delay_ended(
663  Objects_Id  id,
664  void       *ignored
665);
666
667/*
668 *  _Thread_Change_priority
669 *
670 *  DESCRIPTION:
671 *
672 *  This routine changes the current priority of the_thread to
673 *  new_priority.  It performs any necessary scheduling operations
674 *  including the selection of a new heir thread.
675 */
676
677void _Thread_Change_priority (
678  Thread_Control   *the_thread,
679  Priority_Control  new_priority,
680  boolean           prepend_it
681);
682
683/*
684 *  _Thread_Set_priority
685 *
686 *  DESCRIPTION:
687 *
688 *  This routine updates the priority related fields in the_thread
689 *  control block to indicate the current priority is now new_priority.
690 */
691
692void _Thread_Set_priority(
693  Thread_Control   *the_thread,
694  Priority_Control  new_priority
695);
696
697/*
698 *  _Thread_Suspend
699 *
700 *  DESCRIPTION:
701 *
702 *  This routine updates the related suspend fields in the_thread
703 *  control block to indicate the current nested level.
704 */
705
706void _Thread_Suspend(
707  Thread_Control   *the_thread
708);
709
710/*
711 *  _Thread_Resume
712 *
713 *  DESCRIPTION:
714 *
715 *  This routine updates the related suspend fields in the_thread
716 *  control block to indicate the current nested level.  A force
717 *  parameter of TRUE will force a resume and clear the suspend count.
718 */
719
720void _Thread_Resume(
721  Thread_Control   *the_thread,
722  boolean           force
723);
724
725/*
726 *  _Thread_Evaluate_mode
727 *
728 *  DESCRIPTION:
729 *
730 *  This routine evaluates the current scheduling information for the
731 *  system and determines if a context switch is required.  This
732 *  is usually called after changing an execution mode such as preemptability
733 *  for a thread.
734 */
735
736boolean _Thread_Evaluate_mode( void );
737
738/*
739 *  _Thread_Get
740 *
741 *  NOTE:  If we are not using static inlines, this must be a real
742 *         subroutine call.
743 */
744 
745#ifndef USE_INLINES
746Thread_Control *_Thread_Get (
747  Objects_Id           id,
748  Objects_Locations   *location
749);
750#endif
751
752/*
753 *  _Thread_Idle_body
754 *
755 *  DESCRIPTION:
756 *
757 *  This routine is the body of the system idle thread.
758 */
759 
760#if (CPU_PROVIDES_IDLE_THREAD_BODY == FALSE)
761Thread _Thread_Idle_body(
762  unsigned32 ignored
763);
764#endif
765
766/*
767 *  rtems_iterate_over_all_tasks
768 *
769 *  DESCRIPTION:
770 *
771 *  This routine iterates over all threads regardless of API and
772 *  invokes the specified routine.
773 */
774 
775typedef void (*rtems_per_thread_routine)( Thread_Control * );
776
777void rtems_iterate_over_all_threads(
778  rtems_per_thread_routine routine
779);
780
781#ifndef __RTEMS_APPLICATION__
782#include <rtems/score/thread.inl>
783#endif
784#if defined(RTEMS_MULTIPROCESSING)
785#include <rtems/score/threadmp.h>
786#endif
787
788#ifdef __cplusplus
789}
790#endif
791
792#endif
793/* end of include file */
Note: See TracBrowser for help on using the repository browser.