source: rtems/cpukit/score/src/threadhandler.c @ 842394f

4.115
Last change on this file since 842394f was 842394f, checked in by Sebastian Huber <sebastian.huber@…>, on 09/09/11 at 11:25:23

2011-09-09 Sebastian Huber <sebastian.huber@…>

PR 1901/cpukit

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