source: rtems/cpukit/score/src/threadinitialize.c @ f031df0e

4.115
Last change on this file since f031df0e was d5ef7ae2, checked in by Sebastian Huber <sebastian.huber@…>, on 06/14/13 at 07:14:31

smp: Delete _SMP_Request_other_cores_to_dispatch()

Use an event triggered unicast to inform remote processors about a
necessary thread dispatch instead.

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Initialize Thread
5 *  @ingroup ScoreThread
6 */
7/*
8 *  COPYRIGHT (c) 1989-2011.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.com/license/LICENSE.
14 */
15
16#if HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include <rtems/score/threadimpl.h>
21#include <rtems/score/schedulerimpl.h>
22#include <rtems/score/stackimpl.h>
23#include <rtems/score/userextimpl.h>
24#include <rtems/score/watchdogimpl.h>
25#include <rtems/score/wkspace.h>
26#include <rtems/config.h>
27
28bool _Thread_Initialize(
29  Objects_Information                  *information,
30  Thread_Control                       *the_thread,
31  void                                 *stack_area,
32  size_t                                stack_size,
33  bool                                  is_fp,
34  Priority_Control                      priority,
35  bool                                  is_preemptible,
36  Thread_CPU_budget_algorithms          budget_algorithm,
37  Thread_CPU_budget_algorithm_callout   budget_callout,
38  uint32_t                              isr_level,
39  Objects_Name                          name
40)
41{
42  size_t               actual_stack_size = 0;
43  void                *stack = NULL;
44  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
45    void              *fp_area;
46  #endif
47  void                *sched = NULL;
48  void                *extensions_area;
49  bool                 extension_status;
50  int                  i;
51
52#if defined( RTEMS_SMP )
53  if ( rtems_configuration_is_smp_enabled() && !is_preemptible ) {
54    return false;
55  }
56#endif
57
58  /*
59   *  Initialize the Ada self pointer
60   */
61  #if __RTEMS_ADA__
62    the_thread->rtems_ada_self = NULL;
63  #endif
64
65  /*
66   *  Zero out all the allocated memory fields
67   */
68  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
69    the_thread->API_Extensions[i] = NULL;
70
71  extensions_area = NULL;
72  the_thread->libc_reent = NULL;
73
74  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
75    fp_area = NULL;
76  #endif
77
78  /*
79   *  Allocate and Initialize the stack for this thread.
80   */
81  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
82    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
83    if ( !actual_stack_size || actual_stack_size < stack_size )
84      return false;                     /* stack allocation failed */
85
86    stack = the_thread->Start.stack;
87  #else
88    if ( !stack_area ) {
89      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
90      if ( !actual_stack_size || actual_stack_size < stack_size )
91        return false;                     /* stack allocation failed */
92
93      stack = the_thread->Start.stack;
94      the_thread->Start.core_allocated_stack = true;
95    } else {
96      stack = stack_area;
97      actual_stack_size = stack_size;
98      the_thread->Start.core_allocated_stack = false;
99    }
100  #endif
101
102  _Stack_Initialize(
103     &the_thread->Start.Initial_stack,
104     stack,
105     actual_stack_size
106  );
107
108  /*
109   *  Allocate the floating point area for this thread
110   */
111  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
112    if ( is_fp ) {
113      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
114      if ( !fp_area )
115        goto failed;
116      fp_area = _Context_Fp_start( fp_area, 0 );
117    }
118    the_thread->fp_context       = fp_area;
119    the_thread->Start.fp_context = fp_area;
120  #endif
121
122  /*
123   *  Initialize the thread timer
124   */
125  _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL );
126
127  #ifdef __RTEMS_STRICT_ORDER_MUTEX__
128    /* Initialize the head of chain of held mutexes */
129    _Chain_Initialize_empty(&the_thread->lock_mutex);
130  #endif
131
132  /*
133   *  Allocate the extensions area for this thread
134   */
135  if ( _Thread_Maximum_extensions ) {
136    extensions_area = _Workspace_Allocate(
137      (_Thread_Maximum_extensions + 1) * sizeof( void * )
138    );
139    if ( !extensions_area )
140      goto failed;
141  }
142  the_thread->extensions = (void **) extensions_area;
143
144  /*
145   * Clear the extensions area so extension users can determine
146   * if they are linked to the thread. An extension user may
147   * create the extension long after tasks have been created
148   * so they cannot rely on the thread create user extension
149   * call.
150   */
151  if ( the_thread->extensions ) {
152    for ( i = 0; i <= _Thread_Maximum_extensions ; i++ )
153      the_thread->extensions[i] = NULL;
154  }
155
156  /*
157   *  General initialization
158   */
159
160  the_thread->Start.is_preemptible   = is_preemptible;
161  the_thread->Start.budget_algorithm = budget_algorithm;
162  the_thread->Start.budget_callout   = budget_callout;
163
164  switch ( budget_algorithm ) {
165    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
166    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
167      break;
168    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
169      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
170        the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice;
171        break;
172    #endif
173    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
174      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
175        break;
176    #endif
177  }
178
179  the_thread->Start.isr_level         = isr_level;
180
181#if defined(RTEMS_SMP)
182  the_thread->is_scheduled            = false;
183  the_thread->is_executing            = false;
184
185  /* Initialize the cpu field for the non-SMP schedulers */
186  the_thread->cpu                     = _Per_CPU_Get_by_index( 0 );
187#endif
188
189  the_thread->current_state           = STATES_DORMANT;
190  the_thread->Wait.queue              = NULL;
191  the_thread->resource_count          = 0;
192  the_thread->real_priority           = priority;
193  the_thread->Start.initial_priority  = priority;
194  sched =_Scheduler_Allocate( the_thread );
195  if ( !sched )
196    goto failed;
197  _Thread_Set_priority( the_thread, priority );
198
199  /*
200   *  Initialize the CPU usage statistics
201   */
202  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
203    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
204  #else
205    the_thread->cpu_time_used = 0;
206  #endif
207
208  /*
209   *  Open the object
210   */
211  _Objects_Open( information, &the_thread->Object, name );
212
213  /*
214   *  We assume the Allocator Mutex is locked and dispatching is
215   *  enabled when we get here.  We want to be able to run the
216   *  user extensions with dispatching enabled.  The Allocator
217   *  Mutex provides sufficient protection to let the user extensions
218   *  run safely.
219   */
220  extension_status = _User_extensions_Thread_create( the_thread );
221  if ( extension_status )
222    return true;
223
224failed:
225  _Workspace_Free( the_thread->libc_reent );
226
227  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
228    _Workspace_Free( the_thread->API_Extensions[i] );
229
230  _Workspace_Free( extensions_area );
231
232  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
233    _Workspace_Free( fp_area );
234  #endif
235
236   _Workspace_Free( sched );
237
238   _Thread_Stack_Free( the_thread );
239  return false;
240}
Note: See TracBrowser for help on using the repository browser.