source: rtems/cpukit/score/src/threadinitialize.c @ c6f7e060

4.104.115
Last change on this file since c6f7e060 was c6f7e060, checked in by Glenn Humphrey <glenn.humphrey@…>, on 12/02/09 at 18:15:16

2009-12-02 Glenn Humphrey <glenn.humphrey@…>

  • configure.ac, libcsupport/src/times.c, libmisc/cpuuse/cpuusagedata.c, libmisc/cpuuse/cpuusagereport.c, libmisc/cpuuse/cpuusagereset.c, rtems/include/rtems/rtems/ratemon.h, rtems/include/rtems/rtems/types.h, rtems/src/ratemongetstatistics.c, rtems/src/ratemongetstatus.c, rtems/src/ratemonperiod.c, rtems/src/ratemonreportstatistics.c, score/include/rtems/score/thread.h, score/src/threaddispatch.c, score/src/threadinitialize.c, score/src/threadtickletimeslice.c: Changed the configuration of statistics granularity to use just one define.
  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 *  Thread Handler
3 *
4 *
5 *  COPYRIGHT (c) 1989-2008.
6 *  On-Line Applications Research Corporation (OAR).
7 *
8 *  The license and distribution terms for this file may be
9 *  found in found in the file LICENSE in this distribution or at
10 *  http://www.rtems.com/license/LICENSE.
11 *
12 *  $Id$
13 */
14
15#if HAVE_CONFIG_H
16#include "config.h"
17#endif
18
19#include <rtems/system.h>
20#include <rtems/score/apiext.h>
21#include <rtems/score/context.h>
22#include <rtems/score/interr.h>
23#include <rtems/score/isr.h>
24#include <rtems/score/object.h>
25#include <rtems/score/priority.h>
26#include <rtems/score/states.h>
27#include <rtems/score/sysstate.h>
28#include <rtems/score/thread.h>
29#include <rtems/score/threadq.h>
30#include <rtems/score/userext.h>
31#include <rtems/score/watchdog.h>
32#include <rtems/score/wkspace.h>
33
34/*PAGE
35 *
36 *  _Thread_Initialize
37 *
38 *  This routine initializes the specified the thread.  It allocates
39 *  all memory associated with this thread.  It completes by adding
40 *  the thread to the local object table so operations on this
41 *  thread id are allowed.
42 */
43
44bool _Thread_Initialize(
45  Objects_Information                  *information,
46  Thread_Control                       *the_thread,
47  void                                 *stack_area,
48  size_t                                stack_size,
49  bool                                  is_fp,
50  Priority_Control                      priority,
51  bool                                  is_preemptible,
52  Thread_CPU_budget_algorithms          budget_algorithm,
53  Thread_CPU_budget_algorithm_callout   budget_callout,
54  uint32_t                              isr_level,
55  Objects_Name                          name
56)
57{
58  size_t               actual_stack_size = 0;
59  void                *stack = NULL;
60  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
61    void              *fp_area;
62  #endif
63  void                *extensions_area;
64  bool                 extension_status;
65  int                  i;
66
67  /*
68   *  Initialize the Ada self pointer
69   */
70  #if __RTEMS_ADA__
71    the_thread->rtems_ada_self = NULL;
72  #endif
73
74  /*
75   *  Zero out all the allocated memory fields
76   */
77  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
78    the_thread->API_Extensions[i] = NULL;
79
80  extensions_area = NULL;
81  the_thread->libc_reent = 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  /*
118   *  Allocate the floating point area for this thread
119   */
120  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
121    if ( is_fp ) {
122      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
123      if ( !fp_area )
124        goto failed;
125      fp_area = _Context_Fp_start( fp_area, 0 );
126    }
127    the_thread->fp_context       = fp_area;
128    the_thread->Start.fp_context = fp_area;
129  #endif
130
131  /*
132   *  Initialize the thread timer
133   */
134  _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL );
135
136  #ifdef __RTEMS_STRICT_ORDER_MUTEX__
137    /* Initialize the head of chain of held mutexes */
138    _Chain_Initialize_empty(&the_thread->lock_mutex);
139  #endif
140
141  /*
142   *  Allocate the extensions area for this thread
143   */
144  if ( _Thread_Maximum_extensions ) {
145    extensions_area = _Workspace_Allocate(
146      (_Thread_Maximum_extensions + 1) * sizeof( void * )
147    );
148    if ( !extensions_area )
149      goto failed;
150  }
151  the_thread->extensions = (void **) extensions_area;
152
153  /*
154   * Clear the extensions area so extension users can determine
155   * if they are linked to the thread. An extension user may
156   * create the extension long after tasks have been created
157   * so they cannot rely on the thread create user extension
158   * call.
159   */
160  if ( the_thread->extensions ) {
161    for ( i = 0; i <= _Thread_Maximum_extensions ; i++ )
162      the_thread->extensions[i] = NULL;
163  }
164
165  /*
166   *  General initialization
167   */
168
169  the_thread->Start.is_preemptible   = is_preemptible;
170  the_thread->Start.budget_algorithm = budget_algorithm;
171  the_thread->Start.budget_callout   = budget_callout;
172
173  switch ( budget_algorithm ) {
174    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
175    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
176      break;
177    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
178      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
179        the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice;
180        break;
181    #endif
182    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
183      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
184        break;
185    #endif
186  }
187
188  the_thread->Start.isr_level         = isr_level;
189
190  the_thread->current_state           = STATES_DORMANT;
191  the_thread->Wait.queue              = NULL;
192  the_thread->resource_count          = 0;
193  #if defined(RTEMS_ITRON_API)
194    the_thread->suspend_count         = 0;
195  #endif
196  the_thread->real_priority           = priority;
197  the_thread->Start.initial_priority  = priority;
198  _Thread_Set_priority( the_thread, priority );
199
200  /*
201   *  Initialize the CPU usage statistics
202   */
203  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
204    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
205  #else
206    the_thread->cpu_time_used = 0;
207  #endif
208
209  /*
210   *  Open the object
211   */
212  _Objects_Open( information, &the_thread->Object, name );
213
214  /*
215   *  We assume the Allocator Mutex is locked and dispatching is
216   *  enabled when we get here.  We want to be able to run the
217   *  user extensions with dispatching enabled.  The Allocator
218   *  Mutex provides sufficient protection to let the user extensions
219   *  run safely.
220   */
221  extension_status = _User_extensions_Thread_create( the_thread );
222  if ( extension_status )
223    return true;
224
225failed:
226  if ( the_thread->libc_reent )
227    _Workspace_Free( the_thread->libc_reent );
228
229  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
230    if ( the_thread->API_Extensions[i] )
231      _Workspace_Free( the_thread->API_Extensions[i] );
232
233  if ( extensions_area )
234    (void) _Workspace_Free( extensions_area );
235
236  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
237    if ( fp_area )
238      (void) _Workspace_Free( fp_area );
239  #endif
240
241   _Thread_Stack_Free( the_thread );
242  return false;
243
244
245}
Note: See TracBrowser for help on using the repository browser.