source: rtems/cpukit/score/src/threadinitialize.c @ 2d36931

4.11
Last change on this file since 2d36931 was 2d36931, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 11, 2014 at 9:03:25 AM

score: Collect scheduler related fields in TCB

Add Thread_Scheduler_control to collect scheduler related fields of the
TCB.

  • Property mode set to 100644
File size: 7.6 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.control = scheduler;
185  _Resource_Node_initialize( &the_thread->Resource_node );
186  _CPU_Context_Set_is_executing( &the_thread->Registers, false );
187#endif
188
189  _Thread_Debug_set_real_processor( the_thread, cpu );
190
191  /* Initialize the CPU for the non-SMP schedulers */
192  _Thread_Set_CPU( the_thread, cpu );
193
194  the_thread->current_state           = STATES_DORMANT;
195  the_thread->Wait.queue              = NULL;
196  the_thread->resource_count          = 0;
197  the_thread->real_priority           = priority;
198  the_thread->Start.initial_priority  = priority;
199
200  _Scheduler_Node_initialize( scheduler, the_thread );
201  scheduler_node_initialized = true;
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_node_initialized ) {
247    _Scheduler_Node_destroy( 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.