source: rtems/cpukit/score/src/threadinitialize.c @ 80fca28

4.115
Last change on this file since 80fca28 was 80fca28, checked in by Sebastian Huber <sebastian.huber@…>, on 06/13/15 at 13:29:04

score: Add _Watchdog_Preinitialize()

Add an assert to ensure that the watchdog is the proper state for a
_Watchdog_Initialize(). This helps to detect invalid initializations
which may lead to a corrupt watchdog chain.

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