source: rtems/cpukit/score/src/threadhandler.c @ 59c9f26

4.104.115
Last change on this file since 59c9f26 was d104778, checked in by Joel Sherrill <joel.sherrill@…>, on 11/05/08 at 21:50:11

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

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