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 | |
---|
101 | /* |
---|
102 | * Free the per-thread scheduling information. |
---|
103 | */ |
---|
104 | _Scheduler_Node_destroy( _Scheduler_Get( the_thread ), the_thread ); |
---|
105 | |
---|
106 | /* |
---|
107 | * The thread might have been FP. So deal with that. |
---|
108 | */ |
---|
109 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
110 | #if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE ) |
---|
111 | if ( _Thread_Is_allocated_fp( the_thread ) ) |
---|
112 | _Thread_Deallocate_fp(); |
---|
113 | #endif |
---|
114 | |
---|
115 | _Workspace_Free( the_thread->Start.fp_context ); |
---|
116 | #endif |
---|
117 | |
---|
118 | _Freechain_Put( |
---|
119 | &information->Free_thread_queue_heads, |
---|
120 | the_thread->Wait.spare_heads |
---|
121 | ); |
---|
122 | |
---|
123 | /* |
---|
124 | * Free the rest of the memory associated with this task |
---|
125 | * and set the associated pointers to NULL for safety. |
---|
126 | */ |
---|
127 | _Thread_Stack_Free( the_thread ); |
---|
128 | |
---|
129 | _Workspace_Free( the_thread->Start.tls_area ); |
---|
130 | |
---|
131 | #if defined(RTEMS_SMP) |
---|
132 | _SMP_ticket_lock_Destroy( &the_thread->Lock.Default ); |
---|
133 | _SMP_lock_Stats_destroy( &the_thread->Lock.Stats ); |
---|
134 | _SMP_lock_Stats_destroy( &the_thread->Potpourri_stats ); |
---|
135 | #endif |
---|
136 | |
---|
137 | _Objects_Free( &information->Objects, &the_thread->Object ); |
---|
138 | } |
---|
139 | |
---|
140 | static void _Thread_Wait_for_execution_stop( Thread_Control *the_thread ) |
---|
141 | { |
---|
142 | #if defined(RTEMS_SMP) |
---|
143 | /* |
---|
144 | * It is very unlikely that we see an executing thread here. It can happen |
---|
145 | * in case the thread termination sequence is interrupted by a slow interrupt |
---|
146 | * service on a remote processor. |
---|
147 | */ |
---|
148 | while ( _Thread_Is_executing_on_a_processor( the_thread ) ) { |
---|
149 | /* Wait */ |
---|
150 | } |
---|
151 | #else |
---|
152 | (void) the_thread; |
---|
153 | #endif |
---|
154 | } |
---|
155 | |
---|
156 | void _Thread_Kill_zombies( void ) |
---|
157 | { |
---|
158 | ISR_lock_Context lock_context; |
---|
159 | Thread_Zombie_control *zombies = &_Thread_Zombies; |
---|
160 | Thread_Control *the_thread; |
---|
161 | |
---|
162 | _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context ); |
---|
163 | |
---|
164 | the_thread = (Thread_Control *) _Chain_Get_unprotected( &zombies->Chain ); |
---|
165 | while ( the_thread != NULL ) { |
---|
166 | _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context ); |
---|
167 | |
---|
168 | _Thread_Wait_for_execution_stop( the_thread ); |
---|
169 | _Thread_Free( the_thread ); |
---|
170 | |
---|
171 | _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context ); |
---|
172 | |
---|
173 | the_thread = (Thread_Control *) _Chain_Get_unprotected( &zombies->Chain ); |
---|
174 | } |
---|
175 | |
---|
176 | _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context ); |
---|
177 | } |
---|
178 | |
---|
179 | static void _Thread_Add_life_change_action( |
---|
180 | Thread_Control *the_thread |
---|
181 | ) |
---|
182 | { |
---|
183 | _Thread_Add_post_switch_action( |
---|
184 | the_thread, |
---|
185 | &the_thread->Life.Action, |
---|
186 | _Thread_Life_action_handler |
---|
187 | ); |
---|
188 | } |
---|
189 | |
---|
190 | static void _Thread_Start_life_change_for_executing( |
---|
191 | Thread_Control *executing |
---|
192 | ) |
---|
193 | { |
---|
194 | _Assert( |
---|
195 | _Watchdog_Get_state( &executing->Timer.Watchdog ) == WATCHDOG_INACTIVE |
---|
196 | ); |
---|
197 | _Assert( |
---|
198 | executing->current_state == STATES_READY |
---|
199 | || executing->current_state == STATES_SUSPENDED |
---|
200 | ); |
---|
201 | |
---|
202 | _Thread_Add_life_change_action( executing ); |
---|
203 | } |
---|
204 | |
---|
205 | void _Thread_Life_action_handler( |
---|
206 | Thread_Control *executing, |
---|
207 | Thread_Action *action, |
---|
208 | Per_CPU_Control *cpu, |
---|
209 | ISR_Level level |
---|
210 | ) |
---|
211 | { |
---|
212 | Thread_Life_state previous_life_state; |
---|
213 | |
---|
214 | (void) action; |
---|
215 | |
---|
216 | previous_life_state = executing->Life.state; |
---|
217 | executing->Life.state = THREAD_LIFE_PROTECTED; |
---|
218 | |
---|
219 | _Thread_Action_release_and_ISR_enable( cpu, level ); |
---|
220 | |
---|
221 | if ( _Thread_Is_life_terminating( previous_life_state ) ) { |
---|
222 | _User_extensions_Thread_terminate( executing ); |
---|
223 | } else { |
---|
224 | _Assert( _Thread_Is_life_restarting( previous_life_state ) ); |
---|
225 | |
---|
226 | _User_extensions_Thread_restart( executing ); |
---|
227 | } |
---|
228 | |
---|
229 | _Thread_Disable_dispatch(); |
---|
230 | |
---|
231 | if ( _Thread_Is_life_terminating( previous_life_state ) ) { |
---|
232 | _Thread_Make_zombie( executing ); |
---|
233 | |
---|
234 | if ( executing->Life.terminator != NULL ) { |
---|
235 | _Thread_Clear_state( |
---|
236 | executing->Life.terminator, |
---|
237 | STATES_WAITING_FOR_TERMINATION |
---|
238 | ); |
---|
239 | } |
---|
240 | |
---|
241 | _Thread_Enable_dispatch(); |
---|
242 | |
---|
243 | _Assert_Not_reached(); |
---|
244 | } else { |
---|
245 | _Assert( _Thread_Is_life_restarting( previous_life_state ) ); |
---|
246 | |
---|
247 | if ( _Thread_Is_life_terminating( executing->Life.state ) ) { |
---|
248 | /* Someone deleted us in the mean-time */ |
---|
249 | _Thread_Start_life_change_for_executing( executing ); |
---|
250 | } else { |
---|
251 | _Assert( |
---|
252 | _Watchdog_Get_state( &executing->Timer.Watchdog ) == WATCHDOG_INACTIVE |
---|
253 | ); |
---|
254 | _Assert( |
---|
255 | executing->current_state == STATES_READY |
---|
256 | || executing->current_state == STATES_SUSPENDED |
---|
257 | ); |
---|
258 | |
---|
259 | executing->Life.state = THREAD_LIFE_NORMAL; |
---|
260 | |
---|
261 | _Thread_Load_environment( executing ); |
---|
262 | _Thread_Restart_self( executing ); |
---|
263 | |
---|
264 | _Assert_Not_reached(); |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | static void _Thread_Start_life_change( |
---|
270 | Thread_Control *the_thread, |
---|
271 | const Scheduler_Control *scheduler, |
---|
272 | Priority_Control priority |
---|
273 | ) |
---|
274 | { |
---|
275 | the_thread->is_preemptible = the_thread->Start.is_preemptible; |
---|
276 | the_thread->budget_algorithm = the_thread->Start.budget_algorithm; |
---|
277 | the_thread->budget_callout = the_thread->Start.budget_callout; |
---|
278 | |
---|
279 | _Thread_Set_state( the_thread, STATES_RESTARTING ); |
---|
280 | _Thread_queue_Extract_with_proxy( the_thread ); |
---|
281 | _Thread_Timer_remove( the_thread ); |
---|
282 | _Thread_Change_priority( |
---|
283 | the_thread, |
---|
284 | priority, |
---|
285 | NULL, |
---|
286 | _Thread_Raise_real_priority_filter, |
---|
287 | false |
---|
288 | ); |
---|
289 | _Thread_Add_life_change_action( the_thread ); |
---|
290 | _Thread_Ready( the_thread ); |
---|
291 | } |
---|
292 | |
---|
293 | static void _Thread_Request_life_change( |
---|
294 | Thread_Control *the_thread, |
---|
295 | Thread_Control *executing, |
---|
296 | Priority_Control priority, |
---|
297 | Thread_Life_state additional_life_state |
---|
298 | ) |
---|
299 | { |
---|
300 | Thread_Life_state previous_life_state; |
---|
301 | Per_CPU_Control *cpu; |
---|
302 | ISR_Level level; |
---|
303 | const Scheduler_Control *scheduler; |
---|
304 | |
---|
305 | cpu = _Thread_Action_ISR_disable_and_acquire( the_thread, &level ); |
---|
306 | previous_life_state = the_thread->Life.state; |
---|
307 | the_thread->Life.state = previous_life_state | additional_life_state; |
---|
308 | _Thread_Action_release_and_ISR_enable( cpu, level ); |
---|
309 | |
---|
310 | scheduler = _Scheduler_Get( the_thread ); |
---|
311 | if ( the_thread == executing ) { |
---|
312 | Priority_Control unused; |
---|
313 | |
---|
314 | _Thread_Set_priority( the_thread, priority, &unused, true ); |
---|
315 | _Thread_Start_life_change_for_executing( executing ); |
---|
316 | } else if ( previous_life_state == THREAD_LIFE_NORMAL ) { |
---|
317 | _Thread_Start_life_change( the_thread, scheduler, priority ); |
---|
318 | } else { |
---|
319 | _Thread_Clear_state( the_thread, STATES_SUSPENDED ); |
---|
320 | |
---|
321 | if ( _Thread_Is_life_terminating( additional_life_state ) ) { |
---|
322 | _Thread_Change_priority( |
---|
323 | the_thread, |
---|
324 | priority, |
---|
325 | NULL, |
---|
326 | _Thread_Raise_real_priority_filter, |
---|
327 | false |
---|
328 | ); |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing ) |
---|
334 | { |
---|
335 | _Assert( _Thread_Is_life_protected( executing->Life.state ) ); |
---|
336 | |
---|
337 | if ( _States_Is_dormant( the_thread->current_state ) ) { |
---|
338 | _Thread_Make_zombie( the_thread ); |
---|
339 | } else { |
---|
340 | if ( |
---|
341 | the_thread != executing |
---|
342 | && !_Thread_Is_life_terminating( executing->Life.state ) |
---|
343 | ) { |
---|
344 | /* |
---|
345 | * Wait for termination of victim thread. If the executing thread is |
---|
346 | * also terminated, then do not wait. This avoids potential cyclic |
---|
347 | * dependencies and thus dead lock. |
---|
348 | */ |
---|
349 | the_thread->Life.terminator = executing; |
---|
350 | _Thread_Set_state( executing, STATES_WAITING_FOR_TERMINATION ); |
---|
351 | } |
---|
352 | |
---|
353 | _Thread_Request_life_change( |
---|
354 | the_thread, |
---|
355 | executing, |
---|
356 | executing->current_priority, |
---|
357 | THREAD_LIFE_TERMINATING |
---|
358 | ); |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | bool _Thread_Restart( |
---|
363 | Thread_Control *the_thread, |
---|
364 | Thread_Control *executing, |
---|
365 | const Thread_Entry_information *entry |
---|
366 | ) |
---|
367 | { |
---|
368 | if ( !_States_Is_dormant( the_thread->current_state ) ) { |
---|
369 | the_thread->Start.Entry = *entry; |
---|
370 | |
---|
371 | _Thread_Request_life_change( |
---|
372 | the_thread, |
---|
373 | executing, |
---|
374 | the_thread->Start.initial_priority, |
---|
375 | THREAD_LIFE_RESTARTING |
---|
376 | ); |
---|
377 | |
---|
378 | return true; |
---|
379 | } |
---|
380 | |
---|
381 | return false; |
---|
382 | } |
---|
383 | |
---|
384 | bool _Thread_Set_life_protection( bool protect ) |
---|
385 | { |
---|
386 | bool previous_life_protection; |
---|
387 | ISR_Level level; |
---|
388 | Per_CPU_Control *cpu; |
---|
389 | Thread_Control *executing; |
---|
390 | Thread_Life_state previous_life_state; |
---|
391 | |
---|
392 | cpu = _Thread_Action_ISR_disable_and_acquire_for_executing( &level ); |
---|
393 | executing = cpu->executing; |
---|
394 | |
---|
395 | previous_life_state = executing->Life.state; |
---|
396 | previous_life_protection = _Thread_Is_life_protected( previous_life_state ); |
---|
397 | |
---|
398 | if ( protect ) { |
---|
399 | executing->Life.state = previous_life_state | THREAD_LIFE_PROTECTED; |
---|
400 | } else { |
---|
401 | executing->Life.state = previous_life_state & ~THREAD_LIFE_PROTECTED; |
---|
402 | } |
---|
403 | |
---|
404 | _Thread_Action_release_and_ISR_enable( cpu, level ); |
---|
405 | |
---|
406 | #if defined(RTEMS_SMP) |
---|
407 | /* |
---|
408 | * On SMP configurations it is possible that a life change of an executing |
---|
409 | * thread is requested, but this thread didn't notice it yet. The life |
---|
410 | * change is first marked in the life state field and then all scheduling and |
---|
411 | * other thread state updates are performed. The last step is to issues an |
---|
412 | * inter-processor interrupt if necessary. Since this takes some time we |
---|
413 | * have to synchronize here. |
---|
414 | */ |
---|
415 | if ( |
---|
416 | !_Thread_Is_life_protected( previous_life_state ) |
---|
417 | && _Thread_Is_life_changing( previous_life_state ) |
---|
418 | ) { |
---|
419 | _Thread_Disable_dispatch(); |
---|
420 | _Thread_Enable_dispatch(); |
---|
421 | } |
---|
422 | #endif |
---|
423 | |
---|
424 | if ( |
---|
425 | !protect |
---|
426 | && _Thread_Is_life_changing( previous_life_state ) |
---|
427 | ) { |
---|
428 | _Thread_Disable_dispatch(); |
---|
429 | _Thread_Start_life_change_for_executing( executing ); |
---|
430 | _Thread_Enable_dispatch(); |
---|
431 | } |
---|
432 | |
---|
433 | return previous_life_protection; |
---|
434 | } |
---|