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

4.115
Last change on this file since d7c3883 was 108c4b0, checked in by Joel Sherrill <joel.sherrill@…>, on 02/18/11 at 15:12:44

2011-02-18 Joel Sherrill <joel.sherrill@…>

  • sapi/include/confdefs.h, score/Makefile.am, score/include/rtems/score/scheduler.h, score/include/rtems/score/schedulerpriority.h, score/include/rtems/score/thread.h, score/inline/rtems/score/scheduler.inl, score/inline/rtems/score/schedulerpriority.inl, score/src/scheduler.c, score/src/schedulerpriority.c, score/src/schedulerpriorityblock.c, score/src/schedulerpriorityschedule.c, score/src/schedulerpriorityunblock.c, score/src/schedulerpriorityyield.c, score/src/threadchangepriority.c, score/src/threadclose.c, score/src/threadinitialize.c, score/src/threadsetpriority.c, score/src/threadsettransient.c: Significant clean up on Scheduler Plugin Interface. Names were shortened. Missing operations added. Many scheduler files had unneeded includes removed. Made pointer to scheduler information in Thread_Control and Scheduler_Control a void * pointer because the thread and scheduler wrapper should be unaware of scheduler types AND this is broken for user provided schedulers.
  • score/src/schedulerpriorityallocate.c, score/src/schedulerpriorityenqueue.c, score/src/schedulerpriorityenqueuefirst.c, score/src/schedulerpriorityextract.c, score/src/schedulerpriorityfree.c, score/src/schedulerpriorityupdate.c: New files.
  • score/src/schedulerprioritythreadschedulerallocate.c, score/src/schedulerprioritythreadschedulerfree.c, score/src/schedulerprioritythreadschedulerupdate.c: Removed.
  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 *  Thread Handler / Thread Initialize
3 *
4 *  COPYRIGHT (c) 1989-2011.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <rtems/system.h>
19#include <rtems/score/apiext.h>
20#include <rtems/score/context.h>
21#include <rtems/score/interr.h>
22#include <rtems/score/isr.h>
23#include <rtems/score/object.h>
24#include <rtems/score/priority.h>
25#include <rtems/score/scheduler.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                *sched = NULL;
64  void                *extensions_area;
65  bool                 extension_status;
66  int                  i;
67
68  /*
69   *  Initialize the Ada self pointer
70   */
71  #if __RTEMS_ADA__
72    the_thread->rtems_ada_self = NULL;
73  #endif
74
75  /*
76   *  Zero out all the allocated memory fields
77   */
78  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
79    the_thread->API_Extensions[i] = NULL;
80
81  extensions_area = NULL;
82  the_thread->libc_reent = NULL;
83
84  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
85    fp_area = NULL;
86  #endif
87
88  /*
89   *  Allocate and Initialize the stack for this thread.
90   */
91  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
92    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
93    if ( !actual_stack_size || actual_stack_size < stack_size )
94      return false;                     /* stack allocation failed */
95
96    stack = the_thread->Start.stack;
97  #else
98    if ( !stack_area ) {
99      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
100      if ( !actual_stack_size || actual_stack_size < stack_size )
101        return false;                     /* stack allocation failed */
102
103      stack = the_thread->Start.stack;
104      the_thread->Start.core_allocated_stack = true;
105    } else {
106      stack = stack_area;
107      actual_stack_size = stack_size;
108      the_thread->Start.core_allocated_stack = false;
109    }
110  #endif
111
112  _Stack_Initialize(
113     &the_thread->Start.Initial_stack,
114     stack,
115     actual_stack_size
116  );
117
118  /*
119   *  Allocate the floating point area for this thread
120   */
121  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
122    if ( is_fp ) {
123      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
124      if ( !fp_area )
125        goto failed;
126      fp_area = _Context_Fp_start( fp_area, 0 );
127    }
128    the_thread->fp_context       = fp_area;
129    the_thread->Start.fp_context = fp_area;
130  #endif
131
132  /*
133   *  Initialize the thread timer
134   */
135  _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL );
136
137  #ifdef __RTEMS_STRICT_ORDER_MUTEX__
138    /* Initialize the head of chain of held mutexes */
139    _Chain_Initialize_empty(&the_thread->lock_mutex);
140  #endif
141
142  /*
143   *  Allocate the extensions area for this thread
144   */
145  if ( _Thread_Maximum_extensions ) {
146    extensions_area = _Workspace_Allocate(
147      (_Thread_Maximum_extensions + 1) * sizeof( void * )
148    );
149    if ( !extensions_area )
150      goto failed;
151  }
152  the_thread->extensions = (void **) extensions_area;
153
154  /*
155   * Clear the extensions area so extension users can determine
156   * if they are linked to the thread. An extension user may
157   * create the extension long after tasks have been created
158   * so they cannot rely on the thread create user extension
159   * call.
160   */
161  if ( the_thread->extensions ) {
162    for ( i = 0; i <= _Thread_Maximum_extensions ; i++ )
163      the_thread->extensions[i] = NULL;
164  }
165
166  /*
167   *  General initialization
168   */
169
170  the_thread->Start.is_preemptible   = is_preemptible;
171  the_thread->Start.budget_algorithm = budget_algorithm;
172  the_thread->Start.budget_callout   = budget_callout;
173
174  switch ( budget_algorithm ) {
175    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
176    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
177      break;
178    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
179      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
180        the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice;
181        break;
182    #endif
183    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
184      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
185        break;
186    #endif
187  }
188
189  the_thread->Start.isr_level         = isr_level;
190
191  the_thread->current_state           = STATES_DORMANT;
192  the_thread->Wait.queue              = NULL;
193  the_thread->resource_count          = 0;
194  the_thread->real_priority           = priority;
195  the_thread->Start.initial_priority  = priority;
196  sched =_Scheduler_Allocate( the_thread );
197  if ( !sched )
198    goto failed;
199  _Thread_Set_priority( the_thread, priority );
200
201  /*
202   *  Initialize the CPU usage statistics
203   */
204  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
205    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
206  #else
207    the_thread->cpu_time_used = 0;
208  #endif
209
210  /*
211   *  Open the object
212   */
213  _Objects_Open( information, &the_thread->Object, name );
214
215  /*
216   *  We assume the Allocator Mutex is locked and dispatching is
217   *  enabled when we get here.  We want to be able to run the
218   *  user extensions with dispatching enabled.  The Allocator
219   *  Mutex provides sufficient protection to let the user extensions
220   *  run safely.
221   */
222  extension_status = _User_extensions_Thread_create( the_thread );
223  if ( extension_status )
224    return true;
225
226failed:
227  _Workspace_Free( the_thread->libc_reent );
228
229  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
230    _Workspace_Free( the_thread->API_Extensions[i] );
231
232  _Workspace_Free( extensions_area );
233
234  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
235    _Workspace_Free( fp_area );
236  #endif
237
238   _Workspace_Free( sched );
239
240   _Thread_Stack_Free( the_thread );
241  return false;
242}
Note: See TracBrowser for help on using the repository browser.