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

Last change on this file since 7851555 was 7851555, checked in by Sebastian Huber <sebastian.huber@…>, on Jul 3, 2017 at 12:05:26 PM

score: Move processor affinity to Thread_Control

Update #3059.

  • Property mode set to 100644
File size: 9.4 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Initialize Thread
5 *
6 *  @ingroup ScoreThread
7 */
8/*
9 *  COPYRIGHT (c) 1989-2014.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/score/threadimpl.h>
22#include <rtems/score/schedulerimpl.h>
23#include <rtems/score/stackimpl.h>
24#include <rtems/score/tls.h>
25#include <rtems/score/userextimpl.h>
26#include <rtems/score/watchdogimpl.h>
27#include <rtems/score/wkspace.h>
28#include <rtems/score/cpusetimpl.h>
29#include <rtems/config.h>
30
31bool _Thread_Initialize(
32  Thread_Information                   *information,
33  Thread_Control                       *the_thread,
34  const Scheduler_Control              *scheduler,
35  void                                 *stack_area,
36  size_t                                stack_size,
37  bool                                  is_fp,
38  Priority_Control                      priority,
39  bool                                  is_preemptible,
40  Thread_CPU_budget_algorithms          budget_algorithm,
41  Thread_CPU_budget_algorithm_callout   budget_callout,
42  uint32_t                              isr_level,
43  Objects_Name                          name
44)
45{
46  uintptr_t                tls_size = _TLS_Get_size();
47  size_t                   actual_stack_size = 0;
48  void                    *stack = NULL;
49  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
50    void                  *fp_area = NULL;
51  #endif
52  bool                     extension_status;
53  size_t                   i;
54  Scheduler_Node          *scheduler_node;
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;
60  Per_CPU_Control         *cpu = _Per_CPU_Get_by_index( 0 );
61
62#if defined( RTEMS_SMP )
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    }
71  }
72#endif
73
74  memset(
75    &the_thread->Join_queue,
76    0,
77    information->Objects.size - offsetof( Thread_Control, Join_queue )
78  );
79
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
87  /*
88   *  Allocate and Initialize the stack for this thread.
89   */
90  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
91    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
92    if ( !actual_stack_size || actual_stack_size < stack_size )
93      return false;                     /* stack allocation failed */
94
95    stack = the_thread->Start.stack;
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
110
111  _Stack_Initialize(
112     &the_thread->Start.Initial_stack,
113     stack,
114     actual_stack_size
115  );
116
117  scheduler_index = 0;
118
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
132  /*
133   *  Allocate the floating point area for this thread
134   */
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;
140    }
141    the_thread->fp_context       = fp_area;
142    the_thread->Start.fp_context = fp_area;
143  #endif
144
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 ),
152    THREAD_QUEUE_HEADS_SIZE( _Scheduler_Count )
153  );
154  if ( the_thread->Wait.spare_heads == NULL ) {
155    goto failed;
156  }
157  _Thread_queue_Heads_initialize( the_thread->Wait.spare_heads );
158
159  /*
160   *  General initialization
161   */
162
163  the_thread->is_fp                  = is_fp;
164  the_thread->Start.isr_level        = isr_level;
165  the_thread->Start.is_preemptible   = is_preemptible;
166  the_thread->Start.budget_algorithm = budget_algorithm;
167  the_thread->Start.budget_callout   = budget_callout;
168
169  _Thread_Timer_initialize( &the_thread->Timer, cpu );
170
171  switch ( budget_algorithm ) {
172    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
173    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
174      break;
175    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
176      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
177        the_thread->cpu_time_budget =
178          rtems_configuration_get_ticks_per_timeslice();
179        break;
180    #endif
181    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
182      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
183        break;
184    #endif
185  }
186
187#if defined(RTEMS_SMP)
188  scheduler_node = NULL;
189  scheduler_node_for_index = the_thread->Scheduler.nodes;
190  scheduler_for_index = &_Scheduler_Table[ 0 ];
191
192  while ( scheduler_index < _Scheduler_Count ) {
193    Priority_Control priority_for_index;
194
195    if ( scheduler_for_index == scheduler ) {
196      priority_for_index = priority;
197      scheduler_node = scheduler_node_for_index;
198    } else {
199      /*
200       * Use the idle thread priority for the non-home scheduler instances by
201       * default.
202       */
203      priority_for_index = _Scheduler_Map_priority(
204        scheduler_for_index,
205        scheduler_for_index->maximum_priority
206      );
207    }
208
209    _Scheduler_Node_initialize(
210      scheduler_for_index,
211      scheduler_node_for_index,
212      the_thread,
213      priority_for_index
214    );
215    scheduler_node_for_index = (Scheduler_Node *)
216      ( (uintptr_t) scheduler_node_for_index + _Scheduler_Node_size );
217    ++scheduler_for_index;
218    ++scheduler_index;
219  }
220
221  _Assert( scheduler_node != NULL );
222  _Chain_Initialize_one(
223    &the_thread->Scheduler.Wait_nodes,
224    &scheduler_node->Thread.Wait_node
225  );
226  _Chain_Initialize_one(
227    &the_thread->Scheduler.Scheduler_nodes,
228    &scheduler_node->Thread.Scheduler_node.Chain
229  );
230#else
231  scheduler_node = _Thread_Scheduler_get_home_node( the_thread );
232  _Scheduler_Node_initialize(
233    scheduler,
234    scheduler_node,
235    the_thread,
236    priority
237  );
238  scheduler_index = 1;
239#endif
240
241  _Priority_Node_initialize( &the_thread->Real_priority, priority );
242  _Priority_Initialize_one(
243    &scheduler_node->Wait.Priority,
244    &the_thread->Real_priority
245  );
246
247#if defined(RTEMS_SMP)
248  RTEMS_STATIC_ASSERT( THREAD_SCHEDULER_BLOCKED == 0, Scheduler_state );
249  the_thread->Scheduler.home = scheduler;
250  _ISR_lock_Initialize( &the_thread->Scheduler.Lock, "Thread Scheduler" );
251  _Processor_mask_Assign( &the_thread->Scheduler.Affinity, &_SMP_Online_processors );
252  _ISR_lock_Initialize( &the_thread->Wait.Lock.Default, "Thread Wait Default" );
253  _Thread_queue_Gate_open( &the_thread->Wait.Lock.Tranquilizer );
254  _RBTree_Initialize_node( &the_thread->Wait.Link.Registry_node );
255  _SMP_lock_Stats_initialize( &the_thread->Potpourri_stats, "Thread Potpourri" );
256  _SMP_lock_Stats_initialize( &the_thread->Join_queue.Lock_stats, "Thread State" );
257#endif
258
259  /* Initialize the CPU for the non-SMP schedulers */
260  _Thread_Set_CPU( the_thread, cpu );
261
262  the_thread->current_state           = STATES_DORMANT;
263  the_thread->Wait.operations         = &_Thread_queue_Operations_default;
264  the_thread->Start.initial_priority  = priority;
265
266  RTEMS_STATIC_ASSERT( THREAD_WAIT_FLAGS_INITIAL == 0, Wait_flags );
267
268  /* POSIX Keys */
269  _RBTree_Initialize_empty( &the_thread->Keys.Key_value_pairs );
270  _ISR_lock_Initialize( &the_thread->Keys.Lock, "POSIX Key Value Pairs" );
271
272  _Thread_Action_control_initialize( &the_thread->Post_switch_actions );
273
274  /*
275   *  Open the object
276   */
277  _Objects_Open( &information->Objects, &the_thread->Object, name );
278
279  /*
280   *  We assume the Allocator Mutex is locked and dispatching is
281   *  enabled when we get here.  We want to be able to run the
282   *  user extensions with dispatching enabled.  The Allocator
283   *  Mutex provides sufficient protection to let the user extensions
284   *  run safely.
285   */
286  extension_status = _User_extensions_Thread_create( the_thread );
287  if ( extension_status )
288    return true;
289
290failed:
291
292#if defined(RTEMS_SMP)
293  while ( scheduler_index > 0 ) {
294    scheduler_node_for_index = (Scheduler_Node *)
295      ( (uintptr_t) scheduler_node_for_index - _Scheduler_Node_size );
296    --scheduler_for_index;
297    --scheduler_index;
298    _Scheduler_Node_destroy( scheduler_for_index, scheduler_node_for_index );
299  }
300#else
301  if ( scheduler_index > 0 ) {
302    _Scheduler_Node_destroy( scheduler, scheduler_node );
303  }
304#endif
305
306  _Workspace_Free( the_thread->Start.tls_area );
307
308  _Freechain_Put(
309    &information->Free_thread_queue_heads,
310    the_thread->Wait.spare_heads
311  );
312
313  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
314    _Workspace_Free( fp_area );
315  #endif
316
317   _Thread_Stack_Free( the_thread );
318  return false;
319}
Note: See TracBrowser for help on using the repository browser.