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 | |
---|
44 | bool _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 | |
---|
66 | #if __RTEMS_ADA__ |
---|
67 | /* |
---|
68 | * Initialize the Ada self pointer |
---|
69 | */ |
---|
70 | |
---|
71 | the_thread->rtems_ada_self = NULL; |
---|
72 | #endif |
---|
73 | |
---|
74 | /* |
---|
75 | * Allocate and Initialize the stack for this thread. |
---|
76 | */ |
---|
77 | |
---|
78 | |
---|
79 | if ( !stack_area ) { |
---|
80 | |
---|
81 | actual_stack_size = _Thread_Stack_Allocate( the_thread, stack_size ); |
---|
82 | |
---|
83 | if ( !actual_stack_size || actual_stack_size < stack_size ) |
---|
84 | return false; /* stack allocation failed */ |
---|
85 | |
---|
86 | stack = the_thread->Start.stack; |
---|
87 | the_thread->Start.core_allocated_stack = true; |
---|
88 | } else { |
---|
89 | stack = stack_area; |
---|
90 | actual_stack_size = stack_size; |
---|
91 | the_thread->Start.core_allocated_stack = false; |
---|
92 | } |
---|
93 | |
---|
94 | _Stack_Initialize( |
---|
95 | &the_thread->Start.Initial_stack, |
---|
96 | stack, |
---|
97 | actual_stack_size |
---|
98 | ); |
---|
99 | |
---|
100 | /* |
---|
101 | * Allocate the floating point area for this thread |
---|
102 | */ |
---|
103 | |
---|
104 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
105 | if ( is_fp ) { |
---|
106 | |
---|
107 | fp_area = _Workspace_Allocate( CONTEXT_FP_SIZE ); |
---|
108 | if ( !fp_area ) { |
---|
109 | _Thread_Stack_Free( the_thread ); |
---|
110 | return false; |
---|
111 | } |
---|
112 | fp_area = _Context_Fp_start( fp_area, 0 ); |
---|
113 | |
---|
114 | } else |
---|
115 | fp_area = NULL; |
---|
116 | |
---|
117 | the_thread->fp_context = fp_area; |
---|
118 | the_thread->Start.fp_context = fp_area; |
---|
119 | #endif |
---|
120 | |
---|
121 | /* |
---|
122 | * Initialize the thread timer |
---|
123 | */ |
---|
124 | _Watchdog_Initialize( &the_thread->Timer, NULL, 0, NULL ); |
---|
125 | |
---|
126 | #ifdef __RTEMS_STRICT_ORDER_MUTEX__ |
---|
127 | /*Initialize the head of chain of mutex */ |
---|
128 | _Chain_Initialize_empty(&the_thread->lock_mutex); |
---|
129 | #endif |
---|
130 | |
---|
131 | /* |
---|
132 | * Clear the libc reent hook. |
---|
133 | */ |
---|
134 | |
---|
135 | the_thread->libc_reent = NULL; |
---|
136 | |
---|
137 | /* |
---|
138 | * Allocate the extensions area for this thread |
---|
139 | */ |
---|
140 | |
---|
141 | if ( _Thread_Maximum_extensions ) { |
---|
142 | extensions_area = _Workspace_Allocate( |
---|
143 | (_Thread_Maximum_extensions + 1) * sizeof( void * ) |
---|
144 | ); |
---|
145 | |
---|
146 | if ( !extensions_area ) { |
---|
147 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
148 | if ( fp_area ) |
---|
149 | (void) _Workspace_Free( fp_area ); |
---|
150 | #endif |
---|
151 | |
---|
152 | _Thread_Stack_Free( the_thread ); |
---|
153 | |
---|
154 | return false; |
---|
155 | } |
---|
156 | } else |
---|
157 | extensions_area = NULL; |
---|
158 | |
---|
159 | the_thread->extensions = (void **) extensions_area; |
---|
160 | |
---|
161 | /* |
---|
162 | * Clear the extensions area so extension users can determine |
---|
163 | * if they are linked to the thread. An extension user may |
---|
164 | * create the extension long after tasks have been created |
---|
165 | * so they cannot rely on the thread create user extension |
---|
166 | * call. |
---|
167 | */ |
---|
168 | |
---|
169 | if ( the_thread->extensions ) { |
---|
170 | uint32_t i; |
---|
171 | for ( i = 0; i < (_Thread_Maximum_extensions + 1); i++ ) |
---|
172 | the_thread->extensions[i] = NULL; |
---|
173 | } |
---|
174 | |
---|
175 | /* |
---|
176 | * General initialization |
---|
177 | */ |
---|
178 | |
---|
179 | the_thread->Start.is_preemptible = is_preemptible; |
---|
180 | the_thread->Start.budget_algorithm = budget_algorithm; |
---|
181 | the_thread->Start.budget_callout = budget_callout; |
---|
182 | |
---|
183 | switch ( budget_algorithm ) { |
---|
184 | case THREAD_CPU_BUDGET_ALGORITHM_NONE: |
---|
185 | case THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE: |
---|
186 | break; |
---|
187 | case THREAD_CPU_BUDGET_ALGORITHM_EXHAUST_TIMESLICE: |
---|
188 | the_thread->cpu_time_budget = _Thread_Ticks_per_timeslice; |
---|
189 | break; |
---|
190 | case THREAD_CPU_BUDGET_ALGORITHM_CALLOUT: |
---|
191 | break; |
---|
192 | } |
---|
193 | |
---|
194 | the_thread->Start.isr_level = isr_level; |
---|
195 | |
---|
196 | the_thread->current_state = STATES_DORMANT; |
---|
197 | the_thread->Wait.queue = NULL; |
---|
198 | the_thread->resource_count = 0; |
---|
199 | #if defined(RTEMS_ITRON_API) |
---|
200 | the_thread->suspend_count = 0; |
---|
201 | #endif |
---|
202 | the_thread->real_priority = priority; |
---|
203 | the_thread->Start.initial_priority = priority; |
---|
204 | _Thread_Set_priority( the_thread, priority ); |
---|
205 | |
---|
206 | /* |
---|
207 | * Initialize the CPU usage statistics |
---|
208 | */ |
---|
209 | |
---|
210 | #ifdef RTEMS_ENABLE_NANOSECOND_CPU_USAGE_STATISTICS |
---|
211 | _Timestamp_Set_to_zero( &the_thread->cpu_time_used ); |
---|
212 | #else |
---|
213 | the_thread->cpu_time_used = 0; |
---|
214 | #endif |
---|
215 | |
---|
216 | /* |
---|
217 | * Open the object |
---|
218 | */ |
---|
219 | |
---|
220 | _Objects_Open( information, &the_thread->Object, name ); |
---|
221 | |
---|
222 | /* |
---|
223 | * We assume the Allocator Mutex is locked and dispatching is |
---|
224 | * enabled when we get here. We want to be able to run the |
---|
225 | * user extensions with dispatching enabled. The Allocator |
---|
226 | * Mutex provides sufficient protection to let the user extensions |
---|
227 | * run safely. |
---|
228 | */ |
---|
229 | extension_status = _User_extensions_Thread_create( the_thread ); |
---|
230 | |
---|
231 | if ( !extension_status ) { |
---|
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 | |
---|
243 | return false; |
---|
244 | } |
---|
245 | |
---|
246 | return true; |
---|
247 | |
---|
248 | } |
---|