source: rtems/cpukit/score/src/threadinitialize.c @ 38cb59e

5
Last change on this file since 38cb59e was 38cb59e, checked in by Sebastian Huber <sebastian.huber@…>, on 01/08/19 at 07:36:31

Separate task mode checks

Update #3000.

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