source: rtems/cpukit/score/src/threadinitialize.c @ 78b867e2

4.10
Last change on this file since 78b867e2 was 78b867e2, checked in by Gedare Bloom <gedare@…>, on 12/21/17 at 16:49:30

score: replace current and real priority with priority node

Encapsulate the current_priority and real_priority fields of
the thread control block with a Thread_Priority_node struct.
Propagate modifications throughout the tree where the two
fields are directly accessed.

Updates #3359.

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