source: rtems/cpukit/score/src/threadinitialize.c @ 9db8705

4.11
Last change on this file since 9db8705 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
RevLine 
[5a58b1e]1/**
2 *  @file
[05df0a8]3 *
[5a58b1e]4 *  @brief Initialize Thread
5 *  @ingroup ScoreThread
6 */
7/*
[9db8705]8 *  COPYRIGHT (c) 1989-2014.
[05df0a8]9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
[dcf3687]12 *  found in the file LICENSE in this distribution or at
[dd687d9]13 *  http://www.rtems.com/license/LICENSE.
[05df0a8]14 */
15
[a8eed23]16#if HAVE_CONFIG_H
17#include "config.h"
18#endif
19
[5618c37a]20#include <rtems/score/threadimpl.h>
[c6e21ee1]21#include <rtems/score/schedulerimpl.h>
[218286bc]22#include <rtems/score/stackimpl.h>
[022851a]23#include <rtems/score/tls.h>
[3be0c9a]24#include <rtems/score/userextimpl.h>
[4b48ece0]25#include <rtems/score/watchdogimpl.h>
[05df0a8]26#include <rtems/score/wkspace.h>
[9db8705]27#include <rtems/score/cpusetimpl.h>
[5618c37a]28#include <rtems/config.h>
[05df0a8]29
[484a769]30bool _Thread_Initialize(
[05df0a8]31  Objects_Information                  *information,
32  Thread_Control                       *the_thread,
33  void                                 *stack_area,
[728a0bd3]34  size_t                                stack_size,
[484a769]35  bool                                  is_fp,
[05df0a8]36  Priority_Control                      priority,
[484a769]37  bool                                  is_preemptible,
[05df0a8]38  Thread_CPU_budget_algorithms          budget_algorithm,
39  Thread_CPU_budget_algorithm_callout   budget_callout,
[3127180]40  uint32_t                              isr_level,
[05df0a8]41  Objects_Name                          name
42)
43{
[95d0c98]44  size_t     actual_stack_size = 0;
45  void      *stack = NULL;
[cedfd802]46  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
[95d0c98]47    void    *fp_area;
[cedfd802]48  #endif
[95d0c98]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;
[05df0a8]59
[05e82bd7]60#if defined( RTEMS_SMP )
61  if ( rtems_configuration_is_smp_enabled() && !is_preemptible ) {
62    return false;
63  }
64#endif
65
[05df0a8]66  /*
67   *  Initialize the Ada self pointer
68   */
[cedfd802]69  #if __RTEMS_ADA__
70    the_thread->rtems_ada_self = NULL;
71  #endif
[05df0a8]72
73  /*
[cedfd802]74   *  Zero out all the allocated memory fields
[05df0a8]75   */
[cedfd802]76  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
77    the_thread->API_Extensions[i] = NULL;
[05df0a8]78
[cedfd802]79  extensions_area = NULL;
80  the_thread->libc_reent = NULL;
[022851a]81  the_thread->Start.tls_area = NULL;
[05df0a8]82
[cedfd802]83  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
84    fp_area = NULL;
85  #endif
[ecf0f4c]86
[cedfd802]87  /*
88   *  Allocate and Initialize the stack for this thread.
89   */
[bacf79e]90  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
[464ec0d]91    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
[8a4a349e]92    if ( !actual_stack_size || actual_stack_size < stack_size )
[aae7f1a1]93      return false;                     /* stack allocation failed */
[05df0a8]94
95    stack = the_thread->Start.stack;
[bacf79e]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
[05df0a8]110
111  _Stack_Initialize(
112     &the_thread->Start.Initial_stack,
113     stack,
114     actual_stack_size
115  );
116
[022851a]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
[05df0a8]130  /*
131   *  Allocate the floating point area for this thread
132   */
[cedfd802]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 );
[05df0a8]139    }
[cedfd802]140    the_thread->fp_context       = fp_area;
141    the_thread->Start.fp_context = fp_area;
142  #endif
[05df0a8]143
[bdffb59]144  /*
145   *  Initialize the thread timer
146   */
147  _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL );
148
[cedfd802]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
[05279b84]153
[05df0a8]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    );
[cedfd802]161    if ( !extensions_area )
162      goto failed;
163  }
[05df0a8]164  the_thread->extensions = (void **) extensions_area;
165
[f918e74]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 ) {
[cedfd802]174    for ( i = 0; i <= _Thread_Maximum_extensions ; i++ )
[f918e74]175      the_thread->extensions[i] = NULL;
176  }
177
[05df0a8]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;
[f94e76ba]185
186  switch ( budget_algorithm ) {
187    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
188    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
189      break;
[442eac69]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
[f94e76ba]199  }
200
[c3330a8]201  the_thread->Start.isr_level         = isr_level;
202
[a936aa49]203#if defined(RTEMS_SMP)
204  the_thread->is_scheduled            = false;
[aea4a91]205  the_thread->is_in_the_air           = false;
[a936aa49]206  the_thread->is_executing            = false;
[d5ef7ae2]207
208  /* Initialize the cpu field for the non-SMP schedulers */
209  the_thread->cpu                     = _Per_CPU_Get_by_index( 0 );
[9db8705]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
[a936aa49]214#endif
215
[c3330a8]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;
[108c4b0]221  sched =_Scheduler_Allocate( the_thread );
[0faa9dad]222  if ( !sched )
223    goto failed;
[c3330a8]224  _Thread_Set_priority( the_thread, priority );
[05df0a8]225
[c3330a8]226  /*
227   *  Initialize the CPU usage statistics
228   */
[c6f7e060]229  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
[c16bcc0]230    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
[c3330a8]231  #else
[c6f7e060]232    the_thread->cpu_time_used = 0;
[c3330a8]233  #endif
[05df0a8]234
235  /*
236   *  Open the object
237   */
238  _Objects_Open( information, &the_thread->Object, name );
239
240  /*
[5088d97]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
[28352fae]243   *  user extensions with dispatching enabled.  The Allocator
[5088d97]244   *  Mutex provides sufficient protection to let the user extensions
245   *  run safely.
[05df0a8]246   */
[5088d97]247  extension_status = _User_extensions_Thread_create( the_thread );
[cedfd802]248  if ( extension_status )
249    return true;
[05df0a8]250
[cedfd802]251failed:
[022851a]252  _Workspace_Free( the_thread->Start.tls_area );
253
[a0323a9f]254  _Workspace_Free( the_thread->libc_reent );
[05df0a8]255
[cedfd802]256  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
[a0323a9f]257    _Workspace_Free( the_thread->API_Extensions[i] );
[28352fae]258
[a0323a9f]259  _Workspace_Free( extensions_area );
[05df0a8]260
[cedfd802]261  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
[a0323a9f]262    _Workspace_Free( fp_area );
[cedfd802]263  #endif
[05df0a8]264
[a0323a9f]265   _Workspace_Free( sched );
[0faa9dad]266
[cedfd802]267   _Thread_Stack_Free( the_thread );
268  return false;
[05df0a8]269}
Note: See TracBrowser for help on using the repository browser.