source: rtems/cpukit/score/src/threadinitialize.c @ 039a189d

4.11
Last change on this file since 039a189d was 9db8705, checked in by Jennifer Averett <jennifer.averett@…>, on Feb 6, 2014 at 6:42:24 PM

score: Add cpuset support to Score.

This new Score Handler provides a structure to manage a
cpu_set_t plus helper routines to validate the contents
against the current system configuration.

  • Property mode set to 100644
File size: 7.7 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Initialize Thread
5 *  @ingroup ScoreThread
6 */
7/*
8 *  COPYRIGHT (c) 1989-2014.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.com/license/LICENSE.
14 */
15
16#if HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include <rtems/score/threadimpl.h>
21#include <rtems/score/schedulerimpl.h>
22#include <rtems/score/stackimpl.h>
23#include <rtems/score/tls.h>
24#include <rtems/score/userextimpl.h>
25#include <rtems/score/watchdogimpl.h>
26#include <rtems/score/wkspace.h>
27#include <rtems/score/cpusetimpl.h>
28#include <rtems/config.h>
29
30bool _Thread_Initialize(
31  Objects_Information                  *information,
32  Thread_Control                       *the_thread,
33  void                                 *stack_area,
34  size_t                                stack_size,
35  bool                                  is_fp,
36  Priority_Control                      priority,
37  bool                                  is_preemptible,
38  Thread_CPU_budget_algorithms          budget_algorithm,
39  Thread_CPU_budget_algorithm_callout   budget_callout,
40  uint32_t                              isr_level,
41  Objects_Name                          name
42)
43{
44  size_t     actual_stack_size = 0;
45  void      *stack = NULL;
46  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
47    void    *fp_area;
48  #endif
49  void      *sched = NULL;
50  void      *extensions_area;
51  bool       extension_status;
52  int        i;
53
54  /*
55   * Do not use _TLS_Size here since this will lead GCC to assume that this
56   * symbol is not 0 and the later > 0 test will be optimized away.
57   */
58  uintptr_t  tls_size = (uintptr_t) _TLS_BSS_end - (uintptr_t) _TLS_Data_begin;
59
60#if defined( RTEMS_SMP )
61  if ( rtems_configuration_is_smp_enabled() && !is_preemptible ) {
62    return false;
63  }
64#endif
65
66  /*
67   *  Initialize the Ada self pointer
68   */
69  #if __RTEMS_ADA__
70    the_thread->rtems_ada_self = NULL;
71  #endif
72
73  /*
74   *  Zero out all the allocated memory fields
75   */
76  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
77    the_thread->API_Extensions[i] = NULL;
78
79  extensions_area = NULL;
80  the_thread->libc_reent = NULL;
81  the_thread->Start.tls_area = NULL;
82
83  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
84    fp_area = NULL;
85  #endif
86
87  /*
88   *  Allocate and Initialize the stack for this thread.
89   */
90  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
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  #else
97    if ( !stack_area ) {
98      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
99      if ( !actual_stack_size || actual_stack_size < stack_size )
100        return false;                     /* stack allocation failed */
101
102      stack = the_thread->Start.stack;
103      the_thread->Start.core_allocated_stack = true;
104    } else {
105      stack = stack_area;
106      actual_stack_size = stack_size;
107      the_thread->Start.core_allocated_stack = false;
108    }
109  #endif
110
111  _Stack_Initialize(
112     &the_thread->Start.Initial_stack,
113     stack,
114     actual_stack_size
115  );
116
117  /* Thread-local storage (TLS) area allocation */
118  if ( tls_size > 0 ) {
119    uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
120    uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align );
121
122    the_thread->Start.tls_area =
123      _Workspace_Allocate_aligned( tls_alloc, tls_align );
124
125    if ( the_thread->Start.tls_area == NULL ) {
126      goto failed;
127    }
128  }
129
130  /*
131   *  Allocate the floating point area for this thread
132   */
133  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
134    if ( is_fp ) {
135      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
136      if ( !fp_area )
137        goto failed;
138      fp_area = _Context_Fp_start( fp_area, 0 );
139    }
140    the_thread->fp_context       = fp_area;
141    the_thread->Start.fp_context = fp_area;
142  #endif
143
144  /*
145   *  Initialize the thread timer
146   */
147  _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL );
148
149  #ifdef __RTEMS_STRICT_ORDER_MUTEX__
150    /* Initialize the head of chain of held mutexes */
151    _Chain_Initialize_empty(&the_thread->lock_mutex);
152  #endif
153
154  /*
155   *  Allocate the extensions area for this thread
156   */
157  if ( _Thread_Maximum_extensions ) {
158    extensions_area = _Workspace_Allocate(
159      (_Thread_Maximum_extensions + 1) * sizeof( void * )
160    );
161    if ( !extensions_area )
162      goto failed;
163  }
164  the_thread->extensions = (void **) extensions_area;
165
166  /*
167   * Clear the extensions area so extension users can determine
168   * if they are linked to the thread. An extension user may
169   * create the extension long after tasks have been created
170   * so they cannot rely on the thread create user extension
171   * call.
172   */
173  if ( the_thread->extensions ) {
174    for ( i = 0; i <= _Thread_Maximum_extensions ; i++ )
175      the_thread->extensions[i] = NULL;
176  }
177
178  /*
179   *  General initialization
180   */
181
182  the_thread->Start.is_preemptible   = is_preemptible;
183  the_thread->Start.budget_algorithm = budget_algorithm;
184  the_thread->Start.budget_callout   = budget_callout;
185
186  switch ( budget_algorithm ) {
187    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
188    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
189      break;
190    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
191      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
192        the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice;
193        break;
194    #endif
195    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
196      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
197        break;
198    #endif
199  }
200
201  the_thread->Start.isr_level         = isr_level;
202
203#if defined(RTEMS_SMP)
204  the_thread->is_scheduled            = false;
205  the_thread->is_in_the_air           = false;
206  the_thread->is_executing            = false;
207
208  /* Initialize the cpu field for the non-SMP schedulers */
209  the_thread->cpu                     = _Per_CPU_Get_by_index( 0 );
210#if __RTEMS_HAVE_SYS_CPUSET_H__
211   the_thread->affinity               = *(_CPU_set_Default());
212   the_thread->affinity.set           = &the_thread->affinity.preallocated;
213#endif
214#endif
215
216  the_thread->current_state           = STATES_DORMANT;
217  the_thread->Wait.queue              = NULL;
218  the_thread->resource_count          = 0;
219  the_thread->real_priority           = priority;
220  the_thread->Start.initial_priority  = priority;
221  sched =_Scheduler_Allocate( the_thread );
222  if ( !sched )
223    goto failed;
224  _Thread_Set_priority( the_thread, priority );
225
226  /*
227   *  Initialize the CPU usage statistics
228   */
229  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
230    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
231  #else
232    the_thread->cpu_time_used = 0;
233  #endif
234
235  /*
236   *  Open the object
237   */
238  _Objects_Open( information, &the_thread->Object, name );
239
240  /*
241   *  We assume the Allocator Mutex is locked and dispatching is
242   *  enabled when we get here.  We want to be able to run the
243   *  user extensions with dispatching enabled.  The Allocator
244   *  Mutex provides sufficient protection to let the user extensions
245   *  run safely.
246   */
247  extension_status = _User_extensions_Thread_create( the_thread );
248  if ( extension_status )
249    return true;
250
251failed:
252  _Workspace_Free( the_thread->Start.tls_area );
253
254  _Workspace_Free( the_thread->libc_reent );
255
256  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
257    _Workspace_Free( the_thread->API_Extensions[i] );
258
259  _Workspace_Free( extensions_area );
260
261  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
262    _Workspace_Free( fp_area );
263  #endif
264
265   _Workspace_Free( sched );
266
267   _Thread_Stack_Free( the_thread );
268  return false;
269}
Note: See TracBrowser for help on using the repository browser.