source: rtems/cpukit/score/src/threadinitialize.c @ 27bfcd8

5
Last change on this file since 27bfcd8 was 27bfcd8, checked in by Sebastian Huber <sebastian.huber@…>, on 01/25/17 at 13:32:02

score: Delete _CPU_Context_Fp_start()

Since the FP area pointer is passed by reference in
_CPU_Context_Initialize_fp() the optional FP area adjustment via
_CPU_Context_Fp_start() is superfluous. It is also wrong with respect
to memory management, e.g. pointer passed to _Workspace_Free() may be
not the one returned by _Workspace_Allocate().

Close #1400.

  • Property mode set to 100644
File size: 9.3 KB
RevLine 
[5a58b1e]1/**
2 *  @file
[05df0a8]3 *
[5a58b1e]4 *  @brief Initialize Thread
[e6c87f7]5 *
[5a58b1e]6 *  @ingroup ScoreThread
7 */
8/*
[9db8705]9 *  COPYRIGHT (c) 1989-2014.
[05df0a8]10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
[dcf3687]13 *  found in the file LICENSE in this distribution or at
[c499856]14 *  http://www.rtems.org/license/LICENSE.
[05df0a8]15 */
16
[a8eed23]17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
[5618c37a]21#include <rtems/score/threadimpl.h>
[c6e21ee1]22#include <rtems/score/schedulerimpl.h>
[218286bc]23#include <rtems/score/stackimpl.h>
[022851a]24#include <rtems/score/tls.h>
[3be0c9a]25#include <rtems/score/userextimpl.h>
[4b48ece0]26#include <rtems/score/watchdogimpl.h>
[05df0a8]27#include <rtems/score/wkspace.h>
[9db8705]28#include <rtems/score/cpusetimpl.h>
[5618c37a]29#include <rtems/config.h>
[05df0a8]30
[484a769]31bool _Thread_Initialize(
[d7665823]32  Thread_Information                   *information,
[05df0a8]33  Thread_Control                       *the_thread,
[c5831a3f]34  const Scheduler_Control              *scheduler,
[05df0a8]35  void                                 *stack_area,
[728a0bd3]36  size_t                                stack_size,
[484a769]37  bool                                  is_fp,
[05df0a8]38  Priority_Control                      priority,
[484a769]39  bool                                  is_preemptible,
[05df0a8]40  Thread_CPU_budget_algorithms          budget_algorithm,
41  Thread_CPU_budget_algorithm_callout   budget_callout,
[3127180]42  uint32_t                              isr_level,
[05df0a8]43  Objects_Name                          name
44)
45{
[bee71f8e]46  uintptr_t                tls_size = _TLS_Get_size();
[69aa3349]47  size_t                   actual_stack_size = 0;
48  void                    *stack = NULL;
[cedfd802]49  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
[69aa3349]50    void                  *fp_area = NULL;
[cedfd802]51  #endif
[69aa3349]52  bool                     extension_status;
53  size_t                   i;
[df2177ab]54  Scheduler_Node          *scheduler_node;
[5d6b211]55#if defined(RTEMS_SMP)
56  Scheduler_Node          *scheduler_node_for_index;
57  const Scheduler_Control *scheduler_for_index;
58#endif
59  size_t                   scheduler_index;
[38b59a6]60  Per_CPU_Control         *cpu = _Per_CPU_Get_by_index( 0 );
[95d0c98]61
[05e82bd7]62#if defined( RTEMS_SMP )
[537f00eb]63  if ( rtems_configuration_is_smp_enabled() ) {
64    if ( !is_preemptible ) {
65      return false;
66    }
67
68    if ( isr_level != 0 ) {
69      return false;
70    }
[05e82bd7]71  }
72#endif
73
[54c3fbd]74  memset(
75    &the_thread->current_state,
76    0,
77    information->Objects.size - offsetof( Thread_Control, current_state )
78  );
79
[69aa3349]80  for ( i = 0 ; i < _Thread_Control_add_on_count ; ++i ) {
81    const Thread_Control_add_on *add_on = &_Thread_Control_add_ons[ i ];
82
83    *(void **) ( (char *) the_thread + add_on->destination_offset ) =
84      (char *) the_thread + add_on->source_offset;
85  }
86
[cedfd802]87  /*
88   *  Allocate and Initialize the stack for this thread.
89   */
[bacf79e]90  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
[464ec0d]91    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
[8a4a349e]92    if ( !actual_stack_size || actual_stack_size < stack_size )
[aae7f1a1]93      return false;                     /* stack allocation failed */
[05df0a8]94
95    stack = the_thread->Start.stack;
[bacf79e]96  #else
97    if ( !stack_area ) {
98      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
99      if ( !actual_stack_size || actual_stack_size < stack_size )
100        return false;                     /* stack allocation failed */
101
102      stack = the_thread->Start.stack;
103      the_thread->Start.core_allocated_stack = true;
104    } else {
105      stack = stack_area;
106      actual_stack_size = stack_size;
107      the_thread->Start.core_allocated_stack = false;
108    }
109  #endif
[05df0a8]110
111  _Stack_Initialize(
112     &the_thread->Start.Initial_stack,
113     stack,
114     actual_stack_size
115  );
116
[5d6b211]117  scheduler_index = 0;
118
[022851a]119  /* Thread-local storage (TLS) area allocation */
120  if ( tls_size > 0 ) {
121    uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
122    uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align );
123
124    the_thread->Start.tls_area =
125      _Workspace_Allocate_aligned( tls_alloc, tls_align );
126
127    if ( the_thread->Start.tls_area == NULL ) {
128      goto failed;
129    }
130  }
131
[05df0a8]132  /*
133   *  Allocate the floating point area for this thread
134   */
[cedfd802]135  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
136    if ( is_fp ) {
137      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
138      if ( !fp_area )
139        goto failed;
[05df0a8]140    }
[cedfd802]141    the_thread->fp_context       = fp_area;
142    the_thread->Start.fp_context = fp_area;
143  #endif
[05df0a8]144
[d7665823]145  /*
146   *  Get thread queue heads
147   */
148  the_thread->Wait.spare_heads = _Freechain_Get(
149    &information->Free_thread_queue_heads,
150    _Workspace_Allocate,
151    _Objects_Extend_size( &information->Objects ),
[3995e6d]152    THREAD_QUEUE_HEADS_SIZE( _Scheduler_Count )
[d7665823]153  );
154  if ( the_thread->Wait.spare_heads == NULL ) {
155    goto failed;
156  }
[3995e6d]157  _Thread_queue_Heads_initialize( the_thread->Wait.spare_heads );
[d7665823]158
[05df0a8]159  /*
160   *  General initialization
161   */
162
[335e5ca]163  the_thread->is_fp                  = is_fp;
[2d36931]164  the_thread->Start.isr_level        = isr_level;
[05df0a8]165  the_thread->Start.is_preemptible   = is_preemptible;
166  the_thread->Start.budget_algorithm = budget_algorithm;
167  the_thread->Start.budget_callout   = budget_callout;
[f94e76ba]168
[16832b0]169  _Thread_Timer_initialize( &the_thread->Timer, cpu );
[03b900d]170
[f94e76ba]171  switch ( budget_algorithm ) {
172    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
173    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
174      break;
[442eac69]175    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
176      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
[e785fbaa]177        the_thread->cpu_time_budget =
178          rtems_configuration_get_ticks_per_timeslice();
[442eac69]179        break;
180    #endif
181    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
182      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
183        break;
184    #endif
[f94e76ba]185  }
186
[5d6b211]187#if defined(RTEMS_SMP)
188  scheduler_node_for_index = the_thread->Scheduler.nodes;
189  scheduler_for_index = &_Scheduler_Table[ 0 ];
190
191  while ( scheduler_index < _Scheduler_Count ) {
192    Priority_Control priority_for_index;
193
194    if ( scheduler_for_index == scheduler ) {
195      priority_for_index = priority;
196      scheduler_node = scheduler_node_for_index;
197    } else {
[05ca53d]198      /*
199       * Use the idle thread priority for the non-home scheduler instances by
200       * default.
201       */
202      priority_for_index = _Scheduler_Map_priority(
203        scheduler_for_index,
204        scheduler_for_index->maximum_priority
205      );
[5d6b211]206    }
207
208    _Scheduler_Node_initialize(
209      scheduler_for_index,
210      scheduler_node_for_index,
211      the_thread,
212      priority_for_index
213    );
214    scheduler_node_for_index = (Scheduler_Node *)
215      ( (uintptr_t) scheduler_node_for_index + _Scheduler_Node_size );
216    ++scheduler_for_index;
217    ++scheduler_index;
218  }
[266d383]219
220  _Chain_Initialize_one(
221    &the_thread->Scheduler.Wait_nodes,
222    &scheduler_node->Thread.Wait_node
223  );
[ebdd2a3]224  _Chain_Initialize_one(
225    &the_thread->Scheduler.Scheduler_nodes,
226    &scheduler_node->Thread.Scheduler_node.Chain
227  );
[5d6b211]228#else
[7f742432]229  scheduler_node = _Thread_Scheduler_get_home_node( the_thread );
[5d6b211]230  _Scheduler_Node_initialize(
231    scheduler,
232    scheduler_node,
233    the_thread,
234    priority
235  );
236  scheduler_index = 1;
237#endif
238
239  _Priority_Node_initialize( &the_thread->Real_priority, priority );
240  _Priority_Initialize_one(
241    &scheduler_node->Wait.Priority,
242    &the_thread->Real_priority
243  );
[df2177ab]244
[a936aa49]245#if defined(RTEMS_SMP)
[54c3fbd]246  RTEMS_STATIC_ASSERT( THREAD_SCHEDULER_BLOCKED == 0, Scheduler_state );
[2dd098a]247  the_thread->Scheduler.home = scheduler;
[07a32d19]248  _ISR_lock_Initialize( &the_thread->Scheduler.Lock, "Thread Scheduler" );
[4e02681]249  _ISR_lock_Initialize( &the_thread->Wait.Lock.Default, "Thread Wait Default" );
[ff2e6c64]250  _Thread_queue_Gate_open( &the_thread->Wait.Lock.Tranquilizer );
[aaaf9610]251  _RBTree_Initialize_node( &the_thread->Wait.Link.Registry_node );
[10e32a26]252  _SMP_lock_Stats_initialize( &the_thread->Potpourri_stats, "Thread Potpourri" );
[a936aa49]253#endif
254
[a5ac9da]255  /* Initialize the CPU for the non-SMP schedulers */
[38b59a6]256  _Thread_Set_CPU( the_thread, cpu );
[a5ac9da]257
[6e4f929]258  _Thread_queue_Initialize( &the_thread->Join_queue );
259
[c3330a8]260  the_thread->current_state           = STATES_DORMANT;
[568af83]261  the_thread->Wait.operations         = &_Thread_queue_Operations_default;
[c3330a8]262  the_thread->Start.initial_priority  = priority;
[69aa3349]263
[54c3fbd]264  RTEMS_STATIC_ASSERT( THREAD_WAIT_FLAGS_INITIAL == 0, Wait_flags );
[4c8a0ac]265
[5eaf0e7]266  /* POSIX Keys */
267  _RBTree_Initialize_empty( &the_thread->Keys.Key_value_pairs );
268  _ISR_lock_Initialize( &the_thread->Keys.Lock, "POSIX Key Value Pairs" );
[e6c87f7]269
[0dd732d]270  _Thread_Action_control_initialize( &the_thread->Post_switch_actions );
271
[05df0a8]272  /*
273   *  Open the object
274   */
[d7665823]275  _Objects_Open( &information->Objects, &the_thread->Object, name );
[05df0a8]276
277  /*
[5088d97]278   *  We assume the Allocator Mutex is locked and dispatching is
279   *  enabled when we get here.  We want to be able to run the
[28352fae]280   *  user extensions with dispatching enabled.  The Allocator
[5088d97]281   *  Mutex provides sufficient protection to let the user extensions
282   *  run safely.
[05df0a8]283   */
[5088d97]284  extension_status = _User_extensions_Thread_create( the_thread );
[cedfd802]285  if ( extension_status )
286    return true;
[05df0a8]287
[cedfd802]288failed:
[05df0a8]289
[5d6b211]290#if defined(RTEMS_SMP)
291  while ( scheduler_index > 0 ) {
292    scheduler_node_for_index = (Scheduler_Node *)
293      ( (uintptr_t) scheduler_node_for_index - _Scheduler_Node_size );
294    --scheduler_for_index;
295    --scheduler_index;
296    _Scheduler_Node_destroy( scheduler_for_index, scheduler_node_for_index );
297  }
298#else
299  if ( scheduler_index > 0 ) {
[df2177ab]300    _Scheduler_Node_destroy( scheduler, scheduler_node );
[69aa3349]301  }
[5d6b211]302#endif
[28352fae]303
[69aa3349]304  _Workspace_Free( the_thread->Start.tls_area );
[05df0a8]305
[d7665823]306  _Freechain_Put(
307    &information->Free_thread_queue_heads,
308    the_thread->Wait.spare_heads
309  );
310
[cedfd802]311  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
[a0323a9f]312    _Workspace_Free( fp_area );
[cedfd802]313  #endif
[05df0a8]314
[cedfd802]315   _Thread_Stack_Free( the_thread );
316  return false;
[05df0a8]317}
Note: See TracBrowser for help on using the repository browser.