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

4.115
Last change on this file since bee71f8e was bee71f8e, checked in by Sebastian Huber <sebastian.huber@…>, on 04/30/14 at 12:54:58

score: Fix TLS size usage

  • Property mode set to 100644
File size: 7.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/score/cpusetimpl.h>
29#include <rtems/config.h>
30
31bool _Thread_Initialize(
32  Objects_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  bool                     scheduler_allocated = false;
55
56#if defined( RTEMS_SMP )
57  if ( rtems_configuration_is_smp_enabled() && !is_preemptible ) {
58    return false;
59  }
60#endif
61
62  for ( i = 0 ; i < _Thread_Control_add_on_count ; ++i ) {
63    const Thread_Control_add_on *add_on = &_Thread_Control_add_ons[ i ];
64
65    *(void **) ( (char *) the_thread + add_on->destination_offset ) =
66      (char *) the_thread + add_on->source_offset;
67  }
68
69  /*
70   *  Initialize the Ada self pointer
71   */
72  #if __RTEMS_ADA__
73    the_thread->rtems_ada_self = NULL;
74  #endif
75
76  the_thread->Start.tls_area = NULL;
77
78  /*
79   *  Allocate and Initialize the stack for this thread.
80   */
81  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
82    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
83    if ( !actual_stack_size || actual_stack_size < stack_size )
84      return false;                     /* stack allocation failed */
85
86    stack = the_thread->Start.stack;
87  #else
88    if ( !stack_area ) {
89      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
90      if ( !actual_stack_size || actual_stack_size < stack_size )
91        return false;                     /* stack allocation failed */
92
93      stack = the_thread->Start.stack;
94      the_thread->Start.core_allocated_stack = true;
95    } else {
96      stack = stack_area;
97      actual_stack_size = stack_size;
98      the_thread->Start.core_allocated_stack = false;
99    }
100  #endif
101
102  _Stack_Initialize(
103     &the_thread->Start.Initial_stack,
104     stack,
105     actual_stack_size
106  );
107
108  /* Thread-local storage (TLS) area allocation */
109  if ( tls_size > 0 ) {
110    uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
111    uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align );
112
113    the_thread->Start.tls_area =
114      _Workspace_Allocate_aligned( tls_alloc, tls_align );
115
116    if ( the_thread->Start.tls_area == NULL ) {
117      goto failed;
118    }
119  }
120
121  /*
122   *  Allocate the floating point area for this thread
123   */
124  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
125    if ( is_fp ) {
126      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
127      if ( !fp_area )
128        goto failed;
129      fp_area = _Context_Fp_start( fp_area, 0 );
130    }
131    the_thread->fp_context       = fp_area;
132    the_thread->Start.fp_context = fp_area;
133  #endif
134
135  /*
136   *  Initialize the thread timer
137   */
138  _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL );
139
140  #ifdef __RTEMS_STRICT_ORDER_MUTEX__
141    /* Initialize the head of chain of held mutexes */
142    _Chain_Initialize_empty(&the_thread->lock_mutex);
143  #endif
144
145  /*
146   * Clear the extensions area so extension users can determine
147   * if they are linked to the thread. An extension user may
148   * create the extension long after tasks have been created
149   * so they cannot rely on the thread create user extension
150   * call.  The object index starts with one, so the first extension context is
151   * unused.
152   */
153  for ( i = 1 ; i <= rtems_configuration_get_maximum_extensions() ; ++i )
154    the_thread->extensions[ i ] = NULL;
155
156  /*
157   *  General initialization
158   */
159
160  the_thread->Start.is_preemptible   = is_preemptible;
161  the_thread->Start.budget_algorithm = budget_algorithm;
162  the_thread->Start.budget_callout   = budget_callout;
163
164  switch ( budget_algorithm ) {
165    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
166    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
167      break;
168    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
169      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
170        the_thread->cpu_time_budget =
171          rtems_configuration_get_ticks_per_timeslice();
172        break;
173    #endif
174    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
175      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
176        break;
177    #endif
178  }
179
180  the_thread->Start.isr_level         = isr_level;
181
182#if defined(RTEMS_SMP)
183  the_thread->is_scheduled            = false;
184  the_thread->is_in_the_air           = false;
185  the_thread->is_executing            = false;
186  the_thread->scheduler               = scheduler;
187#endif
188
189  /* Initialize the CPU for the non-SMP schedulers */
190  _Thread_Set_CPU( the_thread, _Per_CPU_Get_by_index( 0 ) );
191
192  the_thread->current_state           = STATES_DORMANT;
193  the_thread->Wait.queue              = NULL;
194  the_thread->resource_count          = 0;
195  the_thread->real_priority           = priority;
196  the_thread->Start.initial_priority  = priority;
197
198  scheduler_allocated = _Scheduler_Allocate( scheduler, the_thread );
199  if ( !scheduler_allocated ) {
200    goto failed;
201  }
202
203  _Thread_Set_priority( the_thread, priority );
204
205  /*
206   *  Initialize the CPU usage statistics
207   */
208  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
209    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
210  #else
211    the_thread->cpu_time_used = 0;
212  #endif
213
214  /*
215   * initialize thread's key vaule node chain
216   */
217  _Chain_Initialize_empty( &the_thread->Key_Chain );
218
219  _Thread_Action_control_initialize( &the_thread->Post_switch_actions );
220
221  _Thread_Action_initialize(
222    &the_thread->Life.Action,
223    _Thread_Life_action_handler
224  );
225  the_thread->Life.state = THREAD_LIFE_NORMAL;
226  the_thread->Life.terminator = NULL;
227
228  /*
229   *  Open the object
230   */
231  _Objects_Open( information, &the_thread->Object, name );
232
233  /*
234   *  We assume the Allocator Mutex is locked and dispatching is
235   *  enabled when we get here.  We want to be able to run the
236   *  user extensions with dispatching enabled.  The Allocator
237   *  Mutex provides sufficient protection to let the user extensions
238   *  run safely.
239   */
240  extension_status = _User_extensions_Thread_create( the_thread );
241  if ( extension_status )
242    return true;
243
244failed:
245
246  if ( scheduler_allocated ) {
247    _Scheduler_Free( scheduler, the_thread );
248  }
249
250  _Workspace_Free( the_thread->Start.tls_area );
251
252  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
253    _Workspace_Free( fp_area );
254  #endif
255
256   _Thread_Stack_Free( the_thread );
257  return false;
258}
Note: See TracBrowser for help on using the repository browser.