source: rtems/cpukit/score/src/threadinitialize.c @ 4c8a0ac

4.115
Last change on this file since 4c8a0ac was 4c8a0ac, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 4, 2015 at 7:02:09 AM

score: Add thread wait flags

Update #2273.

  • 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/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_Initialize( &the_thread->Timer, NULL, 0, NULL );
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->Start.isr_level        = isr_level;
163  the_thread->Start.is_preemptible   = is_preemptible;
164  the_thread->Start.budget_algorithm = budget_algorithm;
165  the_thread->Start.budget_callout   = budget_callout;
166
167  switch ( budget_algorithm ) {
168    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
169    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
170      break;
171    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
172      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
173        the_thread->cpu_time_budget =
174          rtems_configuration_get_ticks_per_timeslice();
175        break;
176    #endif
177    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
178      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
179        break;
180    #endif
181  }
182
183#if defined(RTEMS_SMP)
184  the_thread->Scheduler.state = THREAD_SCHEDULER_BLOCKED;
185  the_thread->Scheduler.own_control = scheduler;
186  the_thread->Scheduler.control = scheduler;
187  the_thread->Scheduler.own_node = the_thread->Scheduler.node;
188  _Resource_Node_initialize( &the_thread->Resource_node );
189  _CPU_Context_Set_is_executing( &the_thread->Registers, false );
190#endif
191
192  _Thread_Debug_set_real_processor( the_thread, cpu );
193
194  /* Initialize the CPU for the non-SMP schedulers */
195  _Thread_Set_CPU( the_thread, cpu );
196
197  the_thread->current_state           = STATES_DORMANT;
198  the_thread->Wait.queue              = NULL;
199  the_thread->resource_count          = 0;
200  the_thread->real_priority           = priority;
201  the_thread->Start.initial_priority  = priority;
202
203  _Thread_Wait_flags_set( the_thread, THREAD_WAIT_FLAGS_INITIAL );
204
205  _Scheduler_Node_initialize( scheduler, the_thread );
206  scheduler_node_initialized = true;
207
208  _Thread_Set_priority( the_thread, priority );
209
210  /*
211   *  Initialize the CPU usage statistics
212   */
213  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
214    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
215  #else
216    the_thread->cpu_time_used = 0;
217  #endif
218
219  /*
220   * initialize thread's key vaule node chain
221   */
222  _Chain_Initialize_empty( &the_thread->Key_Chain );
223
224  _Thread_Action_control_initialize( &the_thread->Post_switch_actions );
225
226  _Thread_Action_initialize(
227    &the_thread->Life.Action,
228    _Thread_Life_action_handler
229  );
230  the_thread->Life.state = THREAD_LIFE_NORMAL;
231  the_thread->Life.terminator = NULL;
232
233  the_thread->Capture.flags = 0;
234  the_thread->Capture.control = NULL;
235
236  /*
237   *  Open the object
238   */
239  _Objects_Open( information, &the_thread->Object, name );
240
241  /*
242   *  We assume the Allocator Mutex is locked and dispatching is
243   *  enabled when we get here.  We want to be able to run the
244   *  user extensions with dispatching enabled.  The Allocator
245   *  Mutex provides sufficient protection to let the user extensions
246   *  run safely.
247   */
248  extension_status = _User_extensions_Thread_create( the_thread );
249  if ( extension_status )
250    return true;
251
252failed:
253
254  if ( scheduler_node_initialized ) {
255    _Scheduler_Node_destroy( scheduler, the_thread );
256  }
257
258  _Workspace_Free( the_thread->Start.tls_area );
259
260  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
261    _Workspace_Free( fp_area );
262  #endif
263
264   _Thread_Stack_Free( the_thread );
265  return false;
266}
Note: See TracBrowser for help on using the repository browser.