source: rtems/cpukit/score/src/threadinitialize.c @ 0da9d80

Last change on this file since 0da9d80 was 0da9d80, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 22, 2018 at 5:16:52 AM

score: Rename Objects_Information::size

Rename Objects_Information::size to Objects_Information::object_size.
Change its type from size_t to uint16_t and move it to reduce the size
of Objects_Information.

Update #3621.

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