source: rtems/cpukit/score/src/threadinitialize.c @ 5c731a83

4.115
Last change on this file since 5c731a83 was 5c731a83, checked in by Sebastian Huber <sebastian.huber@…>, on 03/17/14 at 09:12:14

score: Use thread action for thread restart

The thread restart is now supported on SMP. New test
smptests/smpthreadlife01.

  • Property mode set to 100644
File size: 7.9 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  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  size_t     actual_stack_size = 0;
46  void      *stack = NULL;
47  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
48    void    *fp_area;
49  #endif
50  void      *sched = NULL;
51  void      *extensions_area;
52  bool       extension_status;
53  int        i;
54
55  /*
56   * Do not use _TLS_Size here since this will lead GCC to assume that this
57   * symbol is not 0 and the later > 0 test will be optimized away.
58   */
59  uintptr_t  tls_size = (uintptr_t) _TLS_BSS_end - (uintptr_t) _TLS_Data_begin;
60
61#if defined( RTEMS_SMP )
62  if ( rtems_configuration_is_smp_enabled() && !is_preemptible ) {
63    return false;
64  }
65#endif
66
67  /*
68   *  Initialize the Ada self pointer
69   */
70  #if __RTEMS_ADA__
71    the_thread->rtems_ada_self = NULL;
72  #endif
73
74  /*
75   *  Zero out all the allocated memory fields
76   */
77  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
78    the_thread->API_Extensions[i] = NULL;
79
80  extensions_area = NULL;
81  the_thread->libc_reent = NULL;
82  the_thread->Start.tls_area = NULL;
83
84  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
85    fp_area = NULL;
86  #endif
87
88  /*
89   *  Allocate and Initialize the stack for this thread.
90   */
91  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
92    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
93    if ( !actual_stack_size || actual_stack_size < stack_size )
94      return false;                     /* stack allocation failed */
95
96    stack = the_thread->Start.stack;
97  #else
98    if ( !stack_area ) {
99      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
100      if ( !actual_stack_size || actual_stack_size < stack_size )
101        return false;                     /* stack allocation failed */
102
103      stack = the_thread->Start.stack;
104      the_thread->Start.core_allocated_stack = true;
105    } else {
106      stack = stack_area;
107      actual_stack_size = stack_size;
108      the_thread->Start.core_allocated_stack = false;
109    }
110  #endif
111
112  _Stack_Initialize(
113     &the_thread->Start.Initial_stack,
114     stack,
115     actual_stack_size
116  );
117
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
131  /*
132   *  Allocate the floating point area for this thread
133   */
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;
139      fp_area = _Context_Fp_start( fp_area, 0 );
140    }
141    the_thread->fp_context       = fp_area;
142    the_thread->Start.fp_context = fp_area;
143  #endif
144
145  /*
146   *  Initialize the thread timer
147   */
148  _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL );
149
150  #ifdef __RTEMS_STRICT_ORDER_MUTEX__
151    /* Initialize the head of chain of held mutexes */
152    _Chain_Initialize_empty(&the_thread->lock_mutex);
153  #endif
154
155  /*
156   *  Allocate the extensions area for this thread
157   */
158  if ( _Thread_Maximum_extensions ) {
159    extensions_area = _Workspace_Allocate(
160      (_Thread_Maximum_extensions + 1) * sizeof( void * )
161    );
162    if ( !extensions_area )
163      goto failed;
164  }
165  the_thread->extensions = (void **) extensions_area;
166
167  /*
168   * Clear the extensions area so extension users can determine
169   * if they are linked to the thread. An extension user may
170   * create the extension long after tasks have been created
171   * so they cannot rely on the thread create user extension
172   * call.
173   */
174  if ( the_thread->extensions ) {
175    for ( i = 0; i <= _Thread_Maximum_extensions ; i++ )
176      the_thread->extensions[i] = NULL;
177  }
178
179  /*
180   *  General initialization
181   */
182
183  the_thread->Start.is_preemptible   = is_preemptible;
184  the_thread->Start.budget_algorithm = budget_algorithm;
185  the_thread->Start.budget_callout   = budget_callout;
186
187  switch ( budget_algorithm ) {
188    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
189    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
190      break;
191    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
192      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
193        the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice;
194        break;
195    #endif
196    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
197      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
198        break;
199    #endif
200  }
201
202  the_thread->Start.isr_level         = isr_level;
203
204#if defined(RTEMS_SMP)
205  the_thread->is_scheduled            = false;
206  the_thread->is_in_the_air           = false;
207  the_thread->is_executing            = false;
208#if __RTEMS_HAVE_SYS_CPUSET_H__
209   the_thread->affinity               = *(_CPU_set_Default());
210   the_thread->affinity.set           = &the_thread->affinity.preallocated;
211#endif
212#endif
213
214  /* Initialize the CPU for the non-SMP schedulers */
215  _Thread_Set_CPU( the_thread, _Per_CPU_Get_by_index( 0 ) );
216
217  the_thread->current_state           = STATES_DORMANT;
218  the_thread->Wait.queue              = NULL;
219  the_thread->resource_count          = 0;
220  the_thread->real_priority           = priority;
221  the_thread->Start.initial_priority  = priority;
222  sched =_Scheduler_Allocate( the_thread );
223  if ( !sched )
224    goto failed;
225  _Thread_Set_priority( the_thread, priority );
226
227  /*
228   *  Initialize the CPU usage statistics
229   */
230  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
231    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
232  #else
233    the_thread->cpu_time_used = 0;
234  #endif
235
236  /*
237   * initialize thread's key vaule node chain
238   */
239  _Chain_Initialize_empty( &the_thread->Key_Chain );
240
241  _Thread_Action_control_initialize( &the_thread->Post_switch_actions );
242
243  _Thread_Action_initialize(
244    &the_thread->Life.Action,
245    _Thread_Life_action_handler
246  );
247
248  /*
249   *  Open the object
250   */
251  _Objects_Open( information, &the_thread->Object, name );
252
253  /*
254   *  We assume the Allocator Mutex is locked and dispatching is
255   *  enabled when we get here.  We want to be able to run the
256   *  user extensions with dispatching enabled.  The Allocator
257   *  Mutex provides sufficient protection to let the user extensions
258   *  run safely.
259   */
260  extension_status = _User_extensions_Thread_create( the_thread );
261  if ( extension_status )
262    return true;
263
264failed:
265  _Workspace_Free( the_thread->Start.tls_area );
266
267  _Workspace_Free( the_thread->libc_reent );
268
269  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
270    _Workspace_Free( the_thread->API_Extensions[i] );
271
272  _Workspace_Free( extensions_area );
273
274  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
275    _Workspace_Free( fp_area );
276  #endif
277
278   _Workspace_Free( sched );
279
280   _Thread_Stack_Free( the_thread );
281  return false;
282}
Note: See TracBrowser for help on using the repository browser.