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

Last change on this file was 4c20da4, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 4, 2019 at 7:18:11 AM

doxygen: Rename Score* groups in RTEMSScore*

Update #3706

  • Property mode set to 100644
File size: 9.6 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Initialize Thread
5 *
6 *  @ingroup RTEMSScoreThread
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/config.h>
29
30bool _Thread_Initialize(
31  Thread_Information                   *information,
32  Thread_Control                       *the_thread,
33  const Scheduler_Control              *scheduler,
34  void                                 *stack_area,
35  size_t                                stack_size,
36  bool                                  is_fp,
37  Priority_Control                      priority,
38  bool                                  is_preemptible,
39  Thread_CPU_budget_algorithms          budget_algorithm,
40  Thread_CPU_budget_algorithm_callout   budget_callout,
41  uint32_t                              isr_level,
42  Objects_Name                          name
43)
44{
45  uintptr_t                tls_size = _TLS_Get_size();
46  size_t                   actual_stack_size = 0;
47  void                    *stack = NULL;
48  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
49    void                  *fp_area = NULL;
50  #endif
51  bool                     extension_status;
52  size_t                   i;
53  Scheduler_Node          *scheduler_node;
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;
59  Per_CPU_Control         *cpu = _Per_CPU_Get_by_index( 0 );
60
61#if defined(RTEMS_SMP)
62  if ( !is_preemptible && rtems_configuration_is_smp_enabled() ) {
63    return false;
64  }
65#endif
66
67#if defined(RTEMS_SMP) || CPU_ENABLE_ROBUST_THREAD_DISPATCH == TRUE
68  if (
69    isr_level != 0
70#if CPU_ENABLE_ROBUST_THREAD_DISPATCH == FALSE
71      && rtems_configuration_is_smp_enabled()
72#endif
73  ) {
74    return false;
75  }
76#endif
77
78  memset(
79    &the_thread->Join_queue,
80    0,
81    information->Objects.object_size - offsetof( Thread_Control, Join_queue )
82  );
83
84  for ( i = 0 ; i < _Thread_Control_add_on_count ; ++i ) {
85    const Thread_Control_add_on *add_on = &_Thread_Control_add_ons[ i ];
86
87    *(void **) ( (char *) the_thread + add_on->destination_offset ) =
88      (char *) the_thread + add_on->source_offset;
89  }
90
91  /*
92   *  Allocate and Initialize the stack for this thread.
93   */
94  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
95    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
96    if ( !actual_stack_size || actual_stack_size < stack_size )
97      return false;                     /* stack allocation failed */
98
99    stack = the_thread->Start.stack;
100  #else
101    if ( !stack_area ) {
102      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
103      if ( !actual_stack_size || actual_stack_size < stack_size )
104        return false;                     /* stack allocation failed */
105
106      stack = the_thread->Start.stack;
107      the_thread->Start.core_allocated_stack = true;
108    } else {
109      stack = stack_area;
110      actual_stack_size = stack_size;
111      the_thread->Start.core_allocated_stack = false;
112    }
113  #endif
114
115  _Stack_Initialize(
116     &the_thread->Start.Initial_stack,
117     stack,
118     actual_stack_size
119  );
120
121  scheduler_index = 0;
122
123  /* Thread-local storage (TLS) area allocation */
124  if ( tls_size > 0 ) {
125    uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
126    uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align );
127
128    the_thread->Start.tls_area =
129      _Workspace_Allocate_aligned( tls_alloc, tls_align );
130
131    if ( the_thread->Start.tls_area == NULL ) {
132      goto failed;
133    }
134  }
135
136  /*
137   *  Allocate the floating point area for this thread
138   */
139  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
140    if ( is_fp ) {
141      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
142      if ( !fp_area )
143        goto failed;
144    }
145    the_thread->fp_context       = fp_area;
146    the_thread->Start.fp_context = fp_area;
147  #endif
148
149  /*
150   *  Get thread queue heads
151   */
152  the_thread->Wait.spare_heads = _Freechain_Get(
153    &information->Thread_queue_heads.Free,
154    _Workspace_Allocate,
155    _Objects_Extend_size( &information->Objects ),
156    _Thread_queue_Heads_size
157  );
158  if ( the_thread->Wait.spare_heads == NULL ) {
159    goto failed;
160  }
161  _Thread_queue_Heads_initialize( the_thread->Wait.spare_heads );
162
163  /*
164   *  General initialization
165   */
166
167  the_thread->is_fp                  = is_fp;
168  the_thread->Start.isr_level        = isr_level;
169  the_thread->Start.is_preemptible   = is_preemptible;
170  the_thread->Start.budget_algorithm = budget_algorithm;
171  the_thread->Start.budget_callout   = budget_callout;
172
173  _Thread_Timer_initialize( &the_thread->Timer, cpu );
174
175  switch ( budget_algorithm ) {
176    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
177    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
178      break;
179    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
180      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
181        the_thread->cpu_time_budget =
182          rtems_configuration_get_ticks_per_timeslice();
183        break;
184    #endif
185    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
186      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
187        break;
188    #endif
189  }
190
191#if defined(RTEMS_SMP)
192  scheduler_node = NULL;
193  scheduler_node_for_index = the_thread->Scheduler.nodes;
194  scheduler_for_index = &_Scheduler_Table[ 0 ];
195
196  while ( scheduler_index < _Scheduler_Count ) {
197    Priority_Control priority_for_index;
198
199    if ( scheduler_for_index == scheduler ) {
200      priority_for_index = priority;
201      scheduler_node = scheduler_node_for_index;
202    } else {
203      /*
204       * Use the idle thread priority for the non-home scheduler instances by
205       * default.
206       */
207      priority_for_index = _Scheduler_Map_priority(
208        scheduler_for_index,
209        scheduler_for_index->maximum_priority
210      );
211    }
212
213    _Scheduler_Node_initialize(
214      scheduler_for_index,
215      scheduler_node_for_index,
216      the_thread,
217      priority_for_index
218    );
219    scheduler_node_for_index = (Scheduler_Node *)
220      ( (uintptr_t) scheduler_node_for_index + _Scheduler_Node_size );
221    ++scheduler_for_index;
222    ++scheduler_index;
223  }
224
225  _Assert( scheduler_node != NULL );
226  _Chain_Initialize_one(
227    &the_thread->Scheduler.Wait_nodes,
228    &scheduler_node->Thread.Wait_node
229  );
230  _Chain_Initialize_one(
231    &the_thread->Scheduler.Scheduler_nodes,
232    &scheduler_node->Thread.Scheduler_node.Chain
233  );
234#else
235  scheduler_node = _Thread_Scheduler_get_home_node( the_thread );
236  _Scheduler_Node_initialize(
237    scheduler,
238    scheduler_node,
239    the_thread,
240    priority
241  );
242  scheduler_index = 1;
243#endif
244
245  _Priority_Node_initialize( &the_thread->Real_priority, priority );
246  _Priority_Initialize_one(
247    &scheduler_node->Wait.Priority,
248    &the_thread->Real_priority
249  );
250
251#if defined(RTEMS_SMP)
252  RTEMS_STATIC_ASSERT( THREAD_SCHEDULER_BLOCKED == 0, Scheduler_state );
253  the_thread->Scheduler.home_scheduler = scheduler;
254  _ISR_lock_Initialize( &the_thread->Scheduler.Lock, "Thread Scheduler" );
255  _Processor_mask_Assign(
256    &the_thread->Scheduler.Affinity,
257    _SMP_Get_online_processors()
258   );
259  _ISR_lock_Initialize( &the_thread->Wait.Lock.Default, "Thread Wait Default" );
260  _Thread_queue_Gate_open( &the_thread->Wait.Lock.Tranquilizer );
261  _RBTree_Initialize_node( &the_thread->Wait.Link.Registry_node );
262  _SMP_lock_Stats_initialize( &the_thread->Potpourri_stats, "Thread Potpourri" );
263  _SMP_lock_Stats_initialize( &the_thread->Join_queue.Lock_stats, "Thread State" );
264#endif
265
266  /* Initialize the CPU for the non-SMP schedulers */
267  _Thread_Set_CPU( the_thread, cpu );
268
269  the_thread->current_state           = STATES_DORMANT;
270  the_thread->Wait.operations         = &_Thread_queue_Operations_default;
271  the_thread->Start.initial_priority  = priority;
272
273  RTEMS_STATIC_ASSERT( THREAD_WAIT_FLAGS_INITIAL == 0, Wait_flags );
274
275  /* POSIX Keys */
276  _RBTree_Initialize_empty( &the_thread->Keys.Key_value_pairs );
277  _ISR_lock_Initialize( &the_thread->Keys.Lock, "POSIX Key Value Pairs" );
278
279  _Thread_Action_control_initialize( &the_thread->Post_switch_actions );
280
281  /*
282   *  Open the object
283   */
284  _Objects_Open( &information->Objects, &the_thread->Object, name );
285
286  /*
287   *  We assume the Allocator Mutex is locked and dispatching is
288   *  enabled when we get here.  We want to be able to run the
289   *  user extensions with dispatching enabled.  The Allocator
290   *  Mutex provides sufficient protection to let the user extensions
291   *  run safely.
292   */
293  extension_status = _User_extensions_Thread_create( the_thread );
294  if ( extension_status )
295    return true;
296
297failed:
298
299#if defined(RTEMS_SMP)
300  while ( scheduler_index > 0 ) {
301    scheduler_node_for_index = (Scheduler_Node *)
302      ( (uintptr_t) scheduler_node_for_index - _Scheduler_Node_size );
303    --scheduler_for_index;
304    --scheduler_index;
305    _Scheduler_Node_destroy( scheduler_for_index, scheduler_node_for_index );
306  }
307#else
308  if ( scheduler_index > 0 ) {
309    _Scheduler_Node_destroy( scheduler, scheduler_node );
310  }
311#endif
312
313  _Workspace_Free( the_thread->Start.tls_area );
314
315  _Freechain_Put(
316    &information->Thread_queue_heads.Free,
317    the_thread->Wait.spare_heads
318  );
319
320  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
321    _Workspace_Free( fp_area );
322  #endif
323
324   _Thread_Stack_Free( the_thread );
325  return false;
326}
Note: See TracBrowser for help on using the repository browser.