source: rtems/cpukit/score/src/threadhandler.c @ 76deaf0c

4.104.114.95
Last change on this file since 76deaf0c was 76deaf0c, checked in by Joel Sherrill <joel.sherrill@…>, on 05/06/08 at 19:38:19

2008-05-06 Joel Sherrill <joel.sherrill@…>

  • rtems/include/rtems/rtems/message.h, sapi/include/confdefs.h, score/src/coresemseize.c, score/src/threadhandler.c: Comment improvements from class.
  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 *  Thread Handler
3 *
4 *
5 *  COPYRIGHT (c) 1989-2008.
6 *  On-Line Applications Research Corporation (OAR).
7 *
8 *  The license and distribution terms for this file may be
9 *  found in found in the file LICENSE in this distribution or at
10 *  http://www.rtems.com/license/LICENSE.
11 *
12 *  $Id$
13 */
14
15#if HAVE_CONFIG_H
16#include "config.h"
17#endif
18
19#include <rtems/system.h>
20#include <rtems/score/apiext.h>
21#include <rtems/score/context.h>
22#include <rtems/score/interr.h>
23#include <rtems/score/isr.h>
24#include <rtems/score/object.h>
25#include <rtems/score/priority.h>
26#include <rtems/score/states.h>
27#include <rtems/score/sysstate.h>
28#include <rtems/score/thread.h>
29#include <rtems/score/threadq.h>
30#include <rtems/score/userext.h>
31#include <rtems/score/wkspace.h>
32
33#if defined(__USE_INIT_FINI__)
34#include <stdlib.h> /* for atexit() */
35#endif
36
37/*PAGE
38 *
39 *  _Thread_Handler
40 *
41 *  This routine is the "primal" entry point for all threads.
42 *  _Context_Initialize() dummies up the thread's initial context
43 *  to cause the first Context_Switch() to jump to _Thread_Handler().
44 *
45 *  This routine is the default thread exitted error handler.  It is
46 *  returned to when a thread exits.  The configured fatal error handler
47 *  is invoked to process the exit.
48 *
49 *  NOTE:
50 *
51 *  On entry, it is assumed all interrupts are blocked and that this
52 *  routine needs to set the initial isr level.  This may or may not
53 *  actually be needed by the context switch routine and as a result
54 *  interrupts may already be at there proper level.  Either way,
55 *  setting the initial isr level properly here is safe.
56 *
57 *  Currently this is only really needed for the posix port,
58 *  ref: _Context_Switch in unix/cpu.c
59 *
60 *  Input parameters:   NONE
61 *
62 *  Output parameters:  NONE
63 */
64
65void _Thread_Handler( void )
66{
67  ISR_Level  level;
68  Thread_Control *executing;
69#if defined(__USE_INIT_FINI__) || defined(__USE__MAIN__)
70  static char doneConstructors;
71  char doneCons;
72#endif
73#if defined(__USE_INIT_FINI__)
74  extern void _init(void);
75#endif
76#if defined(__USE__MAIN__)
77  extern void _main(void);
78#endif
79
80  executing = _Thread_Executing;
81
82  /*
83   * Some CPUs need to tinker with the call frame or registers when the
84   * thread actually begins to execute for the first time.  This is a
85   * hook point where the port gets a shot at doing whatever it requires.
86   */
87  _Context_Initialization_at_thread_begin();
88
89  /*
90   * have to put level into a register for those cpu's that use
91   * inline asm here
92   */
93
94  level = executing->Start.isr_level;
95  _ISR_Set_level(level);
96
97#if defined(__USE_INIT_FINI__) || defined(__USE__MAIN__)
98  doneCons = doneConstructors;
99  doneConstructors = 1;
100#endif
101
102#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
103#if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE )
104  if ( (executing->fp_context != NULL) && !_Thread_Is_allocated_fp( executing ) ) {
105    if ( _Thread_Allocated_fp != NULL )
106      _Context_Save_fp( &_Thread_Allocated_fp->fp_context );
107    _Thread_Allocated_fp = executing;
108  }
109#endif
110#endif
111
112  /*
113   * Take care that 'begin' extensions get to complete before
114   * 'switch' extensions can run.  This means must keep dispatch
115   * disabled until all 'begin' extensions complete.
116   */
117
118  _User_extensions_Thread_begin( executing );
119
120  /*
121   *  At this point, the dispatch disable level BETTER be 1.
122   */
123
124  _Thread_Enable_dispatch();
125#if defined(__USE_INIT_FINI__)
126  /*
127   *  _init could be a weak symbol and we SHOULD test it but it isn't
128   *  in any configuration I know of and it generates a warning on every
129   *  RTEMS target configuration.  --joel (12 May 2007)
130   */
131  if (!doneCons) /* && (volatile void *)_init) */
132  {
133    extern void _fini( void );
134    _init ();
135    atexit( _fini );
136  }
137#endif
138#if defined(__USE__MAIN__)
139  if (!doneCons && _main)
140    __main ();
141#endif
142
143  switch ( executing->Start.prototype ) {
144    case THREAD_START_NUMERIC:
145      executing->Wait.return_argument =
146        (*(Thread_Entry_numeric) executing->Start.entry_point)(
147          executing->Start.numeric_argument
148      );
149      break;
150    case THREAD_START_POINTER:
151      executing->Wait.return_argument =
152        (*(Thread_Entry_pointer) executing->Start.entry_point)(
153          executing->Start.pointer_argument
154        );
155      break;
156    case THREAD_START_BOTH_POINTER_FIRST:
157      executing->Wait.return_argument =
158         (*(Thread_Entry_both_pointer_first) executing->Start.entry_point)(
159           executing->Start.pointer_argument,
160           executing->Start.numeric_argument
161         );
162      break;
163    case THREAD_START_BOTH_NUMERIC_FIRST:
164      executing->Wait.return_argument =
165         (*(Thread_Entry_both_numeric_first) executing->Start.entry_point)(
166           executing->Start.numeric_argument,
167           executing->Start.pointer_argument
168         );
169      break;
170  }
171
172  /*
173   *  In the switch above, the return code from the user thread body
174   *  was placed in return_argument.  This assumed that if it returned
175   *  anything (which is not supporting in all APIs), then it would be
176   *  able to fit in a (void *).
177   */
178
179  _User_extensions_Thread_exitted( executing );
180
181  _Internal_error_Occurred(
182    INTERNAL_ERROR_CORE,
183    TRUE,
184    INTERNAL_ERROR_THREAD_EXITTED
185  );
186}
Note: See TracBrowser for help on using the repository browser.