source: rtems/cpukit/score/src/threadinitialize.c @ 0faa9dad

4.115
Last change on this file since 0faa9dad was 0faa9dad, checked in by Joel Sherrill <joel.sherrill@…>, on 11/24/10 at 15:51:28

2010-11-24 Gedare Bloom <giddyup44@…>

PR 1647/cpukit

  • posix/src/nanosleep.c, posix/src/sched_yield.c, rtems/src/taskwakeafter.c, sapi/include/confdefs.h, sapi/include/rtems/config.h, sapi/src/exinit.c, score/Makefile.am, score/preinstall.am, score/include/rtems/score/prioritybitmap.h, score/include/rtems/score/thread.h, score/inline/rtems/score/thread.inl, score/src/thread.c, score/src/threadchangepriority.c, score/src/threadclearstate.c, score/src/threadclose.c, score/src/threadinitialize.c, score/src/threadready.c, score/src/threadresume.c, score/src/threadsetpriority.c, score/src/threadsetstate.c, score/src/threadsettransient.c, score/src/threadsuspend.c, score/src/threadtickletimeslice.c: Refactor scheduler out of thread handler to facilitate alternate scheduler implementations.
  • score/src/threadyieldprocessor.c: Removed.
  • score/src/schedulerprioritythreadschedulerupdate.c, score/src/schedulerprioritythreadschedulerfree.c, score/src/schedulerpriorityblock.c, score/src/scheduler.c, score/src/schedulerprioritythreadschedulerallocate.c, score/src/schedulerpriorityunblock.c, score/src/schedulerpriority.c, score/src/schedulerpriorityyield.c, score/include/rtems/score/schedulerpriority.h, score/include/rtems/score/scheduler.h, score/inline/rtems/score/scheduler.inl, score/inline/rtems/score/schedulerpriority.inl: New files.
  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 *  Thread Handler
3 *
4 *
5 *  COPYRIGHT (c) 1989-2009.
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/scheduler.h>
27#include <rtems/score/states.h>
28#include <rtems/score/sysstate.h>
29#include <rtems/score/thread.h>
30#include <rtems/score/threadq.h>
31#include <rtems/score/userext.h>
32#include <rtems/score/watchdog.h>
33#include <rtems/score/wkspace.h>
34
35/*PAGE
36 *
37 *  _Thread_Initialize
38 *
39 *  This routine initializes the specified the thread.  It allocates
40 *  all memory associated with this thread.  It completes by adding
41 *  the thread to the local object table so operations on this
42 *  thread id are allowed.
43 */
44
45bool _Thread_Initialize(
46  Objects_Information                  *information,
47  Thread_Control                       *the_thread,
48  void                                 *stack_area,
49  size_t                                stack_size,
50  bool                                  is_fp,
51  Priority_Control                      priority,
52  bool                                  is_preemptible,
53  Thread_CPU_budget_algorithms          budget_algorithm,
54  Thread_CPU_budget_algorithm_callout   budget_callout,
55  uint32_t                              isr_level,
56  Objects_Name                          name
57)
58{
59  size_t               actual_stack_size = 0;
60  void                *stack = NULL;
61  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
62    void              *fp_area;
63  #endif
64  void                *sched = NULL;
65  void                *extensions_area;
66  bool                 extension_status;
67  int                  i;
68
69  /*
70   *  Initialize the Ada self pointer
71   */
72  #if __RTEMS_ADA__
73    the_thread->rtems_ada_self = NULL;
74  #endif
75
76  /*
77   *  Zero out all the allocated memory fields
78   */
79  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
80    the_thread->API_Extensions[i] = NULL;
81
82  extensions_area = NULL;
83  the_thread->libc_reent = NULL;
84
85  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
86    fp_area = NULL;
87  #endif
88
89  /*
90   *  Allocate and Initialize the stack for this thread.
91   */
92  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
93    actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
94    if ( !actual_stack_size || actual_stack_size < stack_size )
95      return false;                     /* stack allocation failed */
96
97    stack = the_thread->Start.stack;
98  #else
99    if ( !stack_area ) {
100      actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size );
101      if ( !actual_stack_size || actual_stack_size < stack_size )
102        return false;                     /* stack allocation failed */
103
104      stack = the_thread->Start.stack;
105      the_thread->Start.core_allocated_stack = true;
106    } else {
107      stack = stack_area;
108      actual_stack_size = stack_size;
109      the_thread->Start.core_allocated_stack = false;
110    }
111  #endif
112
113  _Stack_Initialize(
114     &the_thread->Start.Initial_stack,
115     stack,
116     actual_stack_size
117  );
118
119  /*
120   *  Allocate the floating point area for this thread
121   */
122  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
123    if ( is_fp ) {
124      fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE );
125      if ( !fp_area )
126        goto failed;
127      fp_area = _Context_Fp_start( fp_area, 0 );
128    }
129    the_thread->fp_context       = fp_area;
130    the_thread->Start.fp_context = fp_area;
131  #endif
132
133  /*
134   *  Initialize the thread timer
135   */
136  _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL );
137
138  #ifdef __RTEMS_STRICT_ORDER_MUTEX__
139    /* Initialize the head of chain of held mutexes */
140    _Chain_Initialize_empty(&the_thread->lock_mutex);
141  #endif
142
143  /*
144   *  Allocate the extensions area for this thread
145   */
146  if ( _Thread_Maximum_extensions ) {
147    extensions_area = _Workspace_Allocate(
148      (_Thread_Maximum_extensions + 1) * sizeof( void * )
149    );
150    if ( !extensions_area )
151      goto failed;
152  }
153  the_thread->extensions = (void **) extensions_area;
154
155  /*
156   * Clear the extensions area so extension users can determine
157   * if they are linked to the thread. An extension user may
158   * create the extension long after tasks have been created
159   * so they cannot rely on the thread create user extension
160   * call.
161   */
162  if ( the_thread->extensions ) {
163    for ( i = 0; i <= _Thread_Maximum_extensions ; i++ )
164      the_thread->extensions[i] = NULL;
165  }
166
167  /*
168   *  General initialization
169   */
170
171  the_thread->Start.is_preemptible   = is_preemptible;
172  the_thread->Start.budget_algorithm = budget_algorithm;
173  the_thread->Start.budget_callout   = budget_callout;
174
175  switch ( budget_algorithm ) {
176    case THREAD_CPU_BUDGET_ALGORITHM_NONE:
177    case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE:
178      break;
179    #if defined(RTEMS_SCORE_THREAD_ENABLE_EXHAUST_TIMESLICE)
180      case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE:
181        the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice;
182        break;
183    #endif
184    #if defined(RTEMS_SCORE_THREAD_ENABLE_SCHEDULER_CALLOUT)
185      case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT:
186        break;
187    #endif
188  }
189
190  the_thread->Start.isr_level         = isr_level;
191
192  the_thread->current_state           = STATES_DORMANT;
193  the_thread->Wait.queue              = NULL;
194  the_thread->resource_count          = 0;
195  the_thread->real_priority           = priority;
196  the_thread->Start.initial_priority  = priority;
197  sched =_Scheduler_Thread_scheduler_allocate( &_Scheduler, the_thread );
198  if ( !sched )
199    goto failed;
200  _Thread_Set_priority( the_thread, priority );
201
202  /*
203   *  Initialize the CPU usage statistics
204   */
205  #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
206    _Timestamp_Set_to_zero( &the_thread->cpu_time_used );
207  #else
208    the_thread->cpu_time_used = 0;
209  #endif
210
211  /*
212   *  Open the object
213   */
214  _Objects_Open( information, &the_thread->Object, name );
215
216  /*
217   *  We assume the Allocator Mutex is locked and dispatching is
218   *  enabled when we get here.  We want to be able to run the
219   *  user extensions with dispatching enabled.  The Allocator
220   *  Mutex provides sufficient protection to let the user extensions
221   *  run safely.
222   */
223  extension_status = _User_extensions_Thread_create( the_thread );
224  if ( extension_status )
225    return true;
226
227failed:
228  if ( the_thread->libc_reent )
229    _Workspace_Free( the_thread->libc_reent );
230
231  for ( i=0 ; i <= THREAD_API_LAST ; i++ )
232    if ( the_thread->API_Extensions[i] )
233      _Workspace_Free( the_thread->API_Extensions[i] );
234
235  if ( extensions_area )
236    (void) _Workspace_Free( extensions_area );
237
238  #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
239    if ( fp_area )
240      (void) _Workspace_Free( fp_area );
241  #endif
242
243  if ( sched )
244    (void) _Workspace_Free( sched );
245
246   _Thread_Stack_Free( the_thread );
247  return false;
248
249
250}
Note: See TracBrowser for help on using the repository browser.