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