1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Restart Thread |
---|
5 | * @ingroup ScoreThread |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-1999. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * Copyright (c) 2014 embedded brains GmbH. |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #if HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <rtems/score/threadimpl.h> |
---|
24 | #include <rtems/score/apimutex.h> |
---|
25 | #include <rtems/score/assert.h> |
---|
26 | #include <rtems/score/chainimpl.h> |
---|
27 | #include <rtems/score/isrlock.h> |
---|
28 | #include <rtems/score/schedulerimpl.h> |
---|
29 | #include <rtems/score/sysstate.h> |
---|
30 | #include <rtems/score/threadqimpl.h> |
---|
31 | #include <rtems/score/userextimpl.h> |
---|
32 | #include <rtems/score/watchdogimpl.h> |
---|
33 | #include <rtems/score/wkspace.h> |
---|
34 | |
---|
35 | typedef struct { |
---|
36 | Chain_Control Chain; |
---|
37 | ISR_lock_Control Lock; |
---|
38 | } Thread_Zombie_control; |
---|
39 | |
---|
40 | static Thread_Zombie_control _Thread_Zombies = { |
---|
41 | .Chain = CHAIN_INITIALIZER_EMPTY( _Thread_Zombies.Chain ), |
---|
42 | .Lock = ISR_LOCK_INITIALIZER( "thread zombies" ) |
---|
43 | }; |
---|
44 | |
---|
45 | static bool _Thread_Raise_real_priority_filter( |
---|
46 | Thread_Control *the_thread, |
---|
47 | Priority_Control *new_priority_ptr, |
---|
48 | void *arg |
---|
49 | ) |
---|
50 | { |
---|
51 | Priority_Control real_priority; |
---|
52 | Priority_Control new_priority; |
---|
53 | Priority_Control current_priority; |
---|
54 | |
---|
55 | real_priority = the_thread->real_priority; |
---|
56 | new_priority = *new_priority_ptr; |
---|
57 | current_priority = the_thread->current_priority; |
---|
58 | |
---|
59 | new_priority = _Thread_Priority_highest( real_priority, new_priority ); |
---|
60 | *new_priority_ptr = new_priority; |
---|
61 | |
---|
62 | the_thread->real_priority = new_priority; |
---|
63 | |
---|
64 | return _Thread_Priority_less_than( current_priority, new_priority ); |
---|
65 | } |
---|
66 | |
---|
67 | static void _Thread_Make_zombie( Thread_Control *the_thread ) |
---|
68 | { |
---|
69 | ISR_lock_Context lock_context; |
---|
70 | Thread_Zombie_control *zombies = &_Thread_Zombies; |
---|
71 | |
---|
72 | if ( _Thread_Owns_resources( the_thread ) ) { |
---|
73 | _Terminate( |
---|
74 | INTERNAL_ERROR_CORE, |
---|
75 | false, |
---|
76 | INTERNAL_ERROR_RESOURCE_IN_USE |
---|
77 | ); |
---|
78 | } |
---|
79 | |
---|
80 | _Objects_Close( |
---|
81 | _Objects_Get_information_id( the_thread->Object.id ), |
---|
82 | &the_thread->Object |
---|
83 | ); |
---|
84 | |
---|
85 | _Thread_Set_state( the_thread, STATES_ZOMBIE ); |
---|
86 | _Thread_queue_Extract_with_proxy( the_thread ); |
---|
87 | _Thread_Timer_remove( the_thread ); |
---|
88 | |
---|
89 | _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context ); |
---|
90 | _Chain_Append_unprotected( &zombies->Chain, &the_thread->Object.Node ); |
---|
91 | _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context ); |
---|
92 | } |
---|
93 | |
---|
94 | static void _Thread_Free( Thread_Control *the_thread ) |
---|
95 | { |
---|
96 | Thread_Information *information = (Thread_Information *) |
---|
97 | _Objects_Get_information_id( the_thread->Object.id ); |
---|
98 | |
---|
99 | _User_extensions_Thread_delete( the_thread ); |
---|
100 | _User_extensions_Destroy_iterators( the_thread ); |
---|
101 | _ISR_lock_Destroy( &the_thread->Keys.Lock ); |
---|
102 | _Scheduler_Node_destroy( _Scheduler_Get( the_thread ), the_thread ); |
---|
103 | _ISR_lock_Destroy( &the_thread->Timer.Lock ); |
---|
104 | |
---|
105 | /* |
---|
106 | * The thread might have been FP. So deal with that. |
---|
107 | */ |
---|
108 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
109 | #if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE ) |
---|
110 | if ( _Thread_Is_allocated_fp( the_thread ) ) |
---|
111 | _Thread_Deallocate_fp(); |
---|
112 | #endif |
---|
113 | |
---|
114 | _Workspace_Free( the_thread->Start.fp_context ); |
---|
115 | #endif |
---|
116 | |
---|
117 | _Freechain_Put( |
---|
118 | &information->Free_thread_queue_heads, |
---|
119 | the_thread->Wait.spare_heads |
---|
120 | ); |
---|
121 | |
---|
122 | /* |
---|
123 | * Free the rest of the memory associated with this task |
---|
124 | * and set the associated pointers to NULL for safety. |
---|
125 | */ |
---|
126 | _Thread_Stack_Free( the_thread ); |
---|
127 | |
---|
128 | _Workspace_Free( the_thread->Start.tls_area ); |
---|
129 | |
---|
130 | #if defined(RTEMS_SMP) |
---|
131 | _SMP_ticket_lock_Destroy( &the_thread->Lock.Default ); |
---|
132 | _SMP_lock_Stats_destroy( &the_thread->Lock.Stats ); |
---|
133 | _SMP_lock_Stats_destroy( &the_thread->Potpourri_stats ); |
---|
134 | #endif |
---|
135 | |
---|
136 | _Objects_Free( &information->Objects, &the_thread->Object ); |
---|
137 | } |
---|
138 | |
---|
139 | static void _Thread_Wait_for_execution_stop( Thread_Control *the_thread ) |
---|
140 | { |
---|
141 | #if defined(RTEMS_SMP) |
---|
142 | /* |
---|
143 | * It is very unlikely that we see an executing thread here. It can happen |
---|
144 | * in case the thread termination sequence is interrupted by a slow interrupt |
---|
145 | * service on a remote processor. |
---|
146 | */ |
---|
147 | while ( _Thread_Is_executing_on_a_processor( the_thread ) ) { |
---|
148 | /* Wait */ |
---|
149 | } |
---|
150 | #else |
---|
151 | (void) the_thread; |
---|
152 | #endif |
---|
153 | } |
---|
154 | |
---|
155 | void _Thread_Kill_zombies( void ) |
---|
156 | { |
---|
157 | ISR_lock_Context lock_context; |
---|
158 | Thread_Zombie_control *zombies = &_Thread_Zombies; |
---|
159 | Thread_Control *the_thread; |
---|
160 | |
---|
161 | _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context ); |
---|
162 | |
---|
163 | the_thread = (Thread_Control *) _Chain_Get_unprotected( &zombies->Chain ); |
---|
164 | while ( the_thread != NULL ) { |
---|
165 | _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context ); |
---|
166 | |
---|
167 | _Thread_Wait_for_execution_stop( the_thread ); |
---|
168 | _Thread_Free( the_thread ); |
---|
169 | |
---|
170 | _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context ); |
---|
171 | |
---|
172 | the_thread = (Thread_Control *) _Chain_Get_unprotected( &zombies->Chain ); |
---|
173 | } |
---|
174 | |
---|
175 | _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context ); |
---|
176 | } |
---|
177 | |
---|
178 | static void _Thread_Add_life_change_action( |
---|
179 | Thread_Control *the_thread |
---|
180 | ) |
---|
181 | { |
---|
182 | _Thread_Add_post_switch_action( |
---|
183 | the_thread, |
---|
184 | &the_thread->Life.Action, |
---|
185 | _Thread_Life_action_handler |
---|
186 | ); |
---|
187 | } |
---|
188 | |
---|
189 | static void _Thread_Start_life_change_for_executing( |
---|
190 | Thread_Control *executing |
---|
191 | ) |
---|
192 | { |
---|
193 | _Assert( |
---|
194 | _Watchdog_Get_state( &executing->Timer.Watchdog ) == WATCHDOG_INACTIVE |
---|
195 | ); |
---|
196 | _Assert( |
---|
197 | executing->current_state == STATES_READY |
---|
198 | || executing->current_state == STATES_SUSPENDED |
---|
199 | ); |
---|
200 | |
---|
201 | _Thread_Add_life_change_action( executing ); |
---|
202 | } |
---|
203 | |
---|
204 | void _Thread_Life_action_handler( |
---|
205 | Thread_Control *executing, |
---|
206 | Thread_Action *action, |
---|
207 | Per_CPU_Control *cpu, |
---|
208 | ISR_Level level |
---|
209 | ) |
---|
210 | { |
---|
211 | Thread_Life_state previous_life_state; |
---|
212 | |
---|
213 | (void) action; |
---|
214 | |
---|
215 | previous_life_state = executing->Life.state; |
---|
216 | executing->Life.state = THREAD_LIFE_PROTECTED; |
---|
217 | |
---|
218 | _Thread_Action_release_and_ISR_enable( cpu, level ); |
---|
219 | |
---|
220 | if ( _Thread_Is_life_terminating( previous_life_state ) ) { |
---|
221 | _User_extensions_Thread_terminate( executing ); |
---|
222 | } else { |
---|
223 | _Assert( _Thread_Is_life_restarting( previous_life_state ) ); |
---|
224 | |
---|
225 | _User_extensions_Thread_restart( executing ); |
---|
226 | } |
---|
227 | |
---|
228 | _Thread_Disable_dispatch(); |
---|
229 | |
---|
230 | if ( _Thread_Is_life_terminating( previous_life_state ) ) { |
---|
231 | _Thread_Make_zombie( executing ); |
---|
232 | |
---|
233 | if ( executing->Life.terminator != NULL ) { |
---|
234 | _Thread_Clear_state( |
---|
235 | executing->Life.terminator, |
---|
236 | STATES_WAITING_FOR_TERMINATION |
---|
237 | ); |
---|
238 | } |
---|
239 | |
---|
240 | _Thread_Enable_dispatch(); |
---|
241 | RTEMS_UNREACHABLE(); |
---|
242 | } else { |
---|
243 | _Assert( _Thread_Is_life_restarting( previous_life_state ) ); |
---|
244 | |
---|
245 | if ( _Thread_Is_life_terminating( executing->Life.state ) ) { |
---|
246 | /* Someone deleted us in the mean-time */ |
---|
247 | _Thread_Start_life_change_for_executing( executing ); |
---|
248 | } else { |
---|
249 | _Assert( |
---|
250 | _Watchdog_Get_state( &executing->Timer.Watchdog ) == WATCHDOG_INACTIVE |
---|
251 | ); |
---|
252 | _Assert( |
---|
253 | executing->current_state == STATES_READY |
---|
254 | || executing->current_state == STATES_SUSPENDED |
---|
255 | ); |
---|
256 | |
---|
257 | executing->Life.state = THREAD_LIFE_NORMAL; |
---|
258 | |
---|
259 | _User_extensions_Destroy_iterators( executing ); |
---|
260 | _Thread_Load_environment( executing ); |
---|
261 | _Thread_Restart_self( executing ); |
---|
262 | RTEMS_UNREACHABLE(); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | static void _Thread_Start_life_change( |
---|
268 | Thread_Control *the_thread, |
---|
269 | const Scheduler_Control *scheduler, |
---|
270 | Priority_Control priority |
---|
271 | ) |
---|
272 | { |
---|
273 | the_thread->is_preemptible = the_thread->Start.is_preemptible; |
---|
274 | the_thread->budget_algorithm = the_thread->Start.budget_algorithm; |
---|
275 | the_thread->budget_callout = the_thread->Start.budget_callout; |
---|
276 | |
---|
277 | _Thread_Set_state( the_thread, STATES_RESTARTING ); |
---|
278 | _Thread_queue_Extract_with_proxy( the_thread ); |
---|
279 | _Thread_Timer_remove( the_thread ); |
---|
280 | _Thread_Change_priority( |
---|
281 | the_thread, |
---|
282 | priority, |
---|
283 | NULL, |
---|
284 | _Thread_Raise_real_priority_filter, |
---|
285 | false |
---|
286 | ); |
---|
287 | _Thread_Add_life_change_action( the_thread ); |
---|
288 | _Thread_Ready( the_thread ); |
---|
289 | } |
---|
290 | |
---|
291 | static void _Thread_Request_life_change( |
---|
292 | Thread_Control *the_thread, |
---|
293 | Thread_Control *executing, |
---|
294 | Priority_Control priority, |
---|
295 | Thread_Life_state additional_life_state |
---|
296 | ) |
---|
297 | { |
---|
298 | Thread_Life_state previous_life_state; |
---|
299 | Per_CPU_Control *cpu; |
---|
300 | ISR_Level level; |
---|
301 | const Scheduler_Control *scheduler; |
---|
302 | |
---|
303 | cpu = _Thread_Action_ISR_disable_and_acquire( the_thread, &level ); |
---|
304 | previous_life_state = the_thread->Life.state; |
---|
305 | the_thread->Life.state = previous_life_state | additional_life_state; |
---|
306 | _Thread_Action_release_and_ISR_enable( cpu, level ); |
---|
307 | |
---|
308 | scheduler = _Scheduler_Get( the_thread ); |
---|
309 | if ( the_thread == executing ) { |
---|
310 | Priority_Control unused; |
---|
311 | |
---|
312 | _Thread_Set_priority( the_thread, priority, &unused, true ); |
---|
313 | _Thread_Start_life_change_for_executing( executing ); |
---|
314 | } else if ( previous_life_state == THREAD_LIFE_NORMAL ) { |
---|
315 | _Thread_Start_life_change( the_thread, scheduler, priority ); |
---|
316 | } else { |
---|
317 | _Thread_Clear_state( the_thread, STATES_SUSPENDED ); |
---|
318 | |
---|
319 | if ( _Thread_Is_life_terminating( additional_life_state ) ) { |
---|
320 | _Thread_Change_priority( |
---|
321 | the_thread, |
---|
322 | priority, |
---|
323 | NULL, |
---|
324 | _Thread_Raise_real_priority_filter, |
---|
325 | false |
---|
326 | ); |
---|
327 | } |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing ) |
---|
332 | { |
---|
333 | _Assert( _Thread_Is_life_protected( executing->Life.state ) ); |
---|
334 | |
---|
335 | if ( _States_Is_dormant( the_thread->current_state ) ) { |
---|
336 | _Thread_Make_zombie( the_thread ); |
---|
337 | } else { |
---|
338 | if ( |
---|
339 | the_thread != executing |
---|
340 | && !_Thread_Is_life_terminating( executing->Life.state ) |
---|
341 | ) { |
---|
342 | /* |
---|
343 | * Wait for termination of victim thread. If the executing thread is |
---|
344 | * also terminated, then do not wait. This avoids potential cyclic |
---|
345 | * dependencies and thus dead lock. |
---|
346 | */ |
---|
347 | the_thread->Life.terminator = executing; |
---|
348 | _Thread_Set_state( executing, STATES_WAITING_FOR_TERMINATION ); |
---|
349 | } |
---|
350 | |
---|
351 | _Thread_Request_life_change( |
---|
352 | the_thread, |
---|
353 | executing, |
---|
354 | executing->current_priority, |
---|
355 | THREAD_LIFE_TERMINATING |
---|
356 | ); |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | bool _Thread_Restart( |
---|
361 | Thread_Control *the_thread, |
---|
362 | Thread_Control *executing, |
---|
363 | const Thread_Entry_information *entry |
---|
364 | ) |
---|
365 | { |
---|
366 | if ( !_States_Is_dormant( the_thread->current_state ) ) { |
---|
367 | the_thread->Start.Entry = *entry; |
---|
368 | |
---|
369 | _Thread_Request_life_change( |
---|
370 | the_thread, |
---|
371 | executing, |
---|
372 | the_thread->Start.initial_priority, |
---|
373 | THREAD_LIFE_RESTARTING |
---|
374 | ); |
---|
375 | |
---|
376 | return true; |
---|
377 | } |
---|
378 | |
---|
379 | return false; |
---|
380 | } |
---|
381 | |
---|
382 | bool _Thread_Set_life_protection( bool protect ) |
---|
383 | { |
---|
384 | bool previous_life_protection; |
---|
385 | ISR_Level level; |
---|
386 | Per_CPU_Control *cpu; |
---|
387 | Thread_Control *executing; |
---|
388 | Thread_Life_state previous_life_state; |
---|
389 | |
---|
390 | cpu = _Thread_Action_ISR_disable_and_acquire_for_executing( &level ); |
---|
391 | executing = cpu->executing; |
---|
392 | |
---|
393 | previous_life_state = executing->Life.state; |
---|
394 | previous_life_protection = _Thread_Is_life_protected( previous_life_state ); |
---|
395 | |
---|
396 | if ( protect ) { |
---|
397 | executing->Life.state = previous_life_state | THREAD_LIFE_PROTECTED; |
---|
398 | } else { |
---|
399 | executing->Life.state = previous_life_state & ~THREAD_LIFE_PROTECTED; |
---|
400 | } |
---|
401 | |
---|
402 | _Thread_Action_release_and_ISR_enable( cpu, level ); |
---|
403 | |
---|
404 | #if defined(RTEMS_SMP) |
---|
405 | /* |
---|
406 | * On SMP configurations it is possible that a life change of an executing |
---|
407 | * thread is requested, but this thread didn't notice it yet. The life |
---|
408 | * change is first marked in the life state field and then all scheduling and |
---|
409 | * other thread state updates are performed. The last step is to issues an |
---|
410 | * inter-processor interrupt if necessary. Since this takes some time we |
---|
411 | * have to synchronize here. |
---|
412 | */ |
---|
413 | if ( |
---|
414 | !_Thread_Is_life_protected( previous_life_state ) |
---|
415 | && _Thread_Is_life_changing( previous_life_state ) |
---|
416 | ) { |
---|
417 | _Thread_Disable_dispatch(); |
---|
418 | _Thread_Enable_dispatch(); |
---|
419 | } |
---|
420 | #endif |
---|
421 | |
---|
422 | if ( |
---|
423 | !protect |
---|
424 | && _Thread_Is_life_changing( previous_life_state ) |
---|
425 | ) { |
---|
426 | _Thread_Disable_dispatch(); |
---|
427 | _Thread_Start_life_change_for_executing( executing ); |
---|
428 | _Thread_Enable_dispatch(); |
---|
429 | } |
---|
430 | |
---|
431 | return previous_life_protection; |
---|
432 | } |
---|