source: rtems/cpukit/score/src/threadrestart.c @ a92989a

4.115
Last change on this file since a92989a was a92989a, checked in by Sebastian Huber <sebastian.huber@…>, on 06/18/14 at 13:16:02

score: Fix thread deletion on SMP

Close the thread object in _Thread_Make_zombie() so that all blocking
operations that use _Thread_Get() in the corresponding release directive
can find a terminating thread and can complete the operation.

  • Property mode set to 100644
File size: 10.6 KB
Line 
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
35typedef struct {
36  Chain_Control Chain;
37  ISR_lock_Control Lock;
38} Thread_Zombie_control;
39
40static Thread_Zombie_control _Thread_Zombies = {
41  .Chain = CHAIN_INITIALIZER_EMPTY( _Thread_Zombies.Chain ),
42  .Lock = ISR_LOCK_INITIALIZER( "thread zombies" )
43};
44
45static void _Thread_Make_zombie( Thread_Control *the_thread )
46{
47  ISR_lock_Context lock_context;
48  Thread_Zombie_control *zombies = &_Thread_Zombies;
49
50  if ( _Thread_Owns_resources( the_thread ) ) {
51    _Terminate(
52      INTERNAL_ERROR_CORE,
53      false,
54      INTERNAL_ERROR_RESOURCE_IN_USE
55    );
56  }
57
58  _Objects_Close(
59    _Objects_Get_information_id( the_thread->Object.id ),
60    &the_thread->Object
61  );
62
63  _Thread_Set_state( the_thread, STATES_ZOMBIE );
64  _Thread_queue_Extract_with_proxy( the_thread );
65  _Watchdog_Remove( &the_thread->Timer );
66
67  _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context );
68  _Chain_Append_unprotected( &zombies->Chain, &the_thread->Object.Node );
69  _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context );
70}
71
72static void _Thread_Free( Thread_Control *the_thread )
73{
74  _User_extensions_Thread_delete( the_thread );
75
76  /*
77   * Free the per-thread scheduling information.
78   */
79  _Scheduler_Node_destroy( _Scheduler_Get( the_thread ), the_thread );
80
81  /*
82   *  The thread might have been FP.  So deal with that.
83   */
84#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
85#if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE )
86  if ( _Thread_Is_allocated_fp( the_thread ) )
87    _Thread_Deallocate_fp();
88#endif
89
90  _Workspace_Free( the_thread->Start.fp_context );
91#endif
92
93  /*
94   *  Free the rest of the memory associated with this task
95   *  and set the associated pointers to NULL for safety.
96   */
97  _Thread_Stack_Free( the_thread );
98
99  _Workspace_Free( the_thread->Start.tls_area );
100
101  _Objects_Free(
102    _Objects_Get_information_id( the_thread->Object.id ),
103    &the_thread->Object
104  );
105}
106
107static void _Thread_Wait_for_execution_stop( Thread_Control *the_thread )
108{
109#if defined(RTEMS_SMP)
110  /*
111   * It is very unlikely that we see an executing thread here.  It can happen
112   * in case the thread termination sequence is interrupted by a slow interrupt
113   * service on a remote processor.
114   */
115  while ( _Thread_Is_executing_on_a_processor( the_thread ) ) {
116    /* Wait */
117  }
118#else
119  (void) the_thread;
120#endif
121}
122
123void _Thread_Kill_zombies( void )
124{
125  ISR_lock_Context lock_context;
126  Thread_Zombie_control *zombies = &_Thread_Zombies;
127  Thread_Control *the_thread;
128
129  _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context );
130
131  the_thread = (Thread_Control *) _Chain_Get_unprotected( &zombies->Chain );
132  while ( the_thread != NULL ) {
133    _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context );
134
135    _Thread_Wait_for_execution_stop( the_thread );
136    _Thread_Free( the_thread );
137
138    _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context );
139
140    the_thread = (Thread_Control *) _Chain_Get_unprotected( &zombies->Chain );
141  }
142
143  _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context );
144}
145
146static void _Thread_Start_life_change_for_executing(
147  Thread_Control *executing
148)
149{
150  _Assert( executing->Timer.state == WATCHDOG_INACTIVE );
151  _Assert(
152    executing->current_state == STATES_READY
153      || executing->current_state == STATES_SUSPENDED
154  );
155
156  _Thread_Add_post_switch_action( executing, &executing->Life.Action );
157}
158
159void _Thread_Life_action_handler(
160  Thread_Control  *executing,
161  Thread_Action   *action,
162  Per_CPU_Control *cpu,
163  ISR_Level        level
164)
165{
166  Thread_Life_state previous_life_state;
167
168  (void) action;
169
170  previous_life_state = executing->Life.state;
171  executing->Life.state = THREAD_LIFE_PROTECTED;
172
173  _Thread_Action_release_and_ISR_enable( cpu, level );
174
175  if ( _Thread_Is_life_terminating( previous_life_state ) ) {
176    _User_extensions_Thread_terminate( executing );
177  } else {
178    _Assert( _Thread_Is_life_restarting( previous_life_state ) );
179
180    _User_extensions_Thread_restart( executing );
181  }
182
183  _Thread_Disable_dispatch();
184
185  if ( _Thread_Is_life_terminating( previous_life_state ) ) {
186    _Thread_Make_zombie( executing );
187
188    if ( executing->Life.terminator != NULL ) {
189      _Thread_Clear_state(
190        executing->Life.terminator,
191        STATES_WAITING_FOR_TERMINATION
192      );
193    }
194
195    _Thread_Enable_dispatch();
196
197    _Assert_Not_reached();
198  } else {
199    _Assert( _Thread_Is_life_restarting( previous_life_state ) );
200
201    if ( _Thread_Is_life_terminating( executing->Life.state ) ) {
202      /* Someone deleted us in the mean-time */
203      _Thread_Start_life_change_for_executing( executing );
204    } else {
205      _Assert( executing->Timer.state == WATCHDOG_INACTIVE );
206      _Assert(
207        executing->current_state == STATES_READY
208          || executing->current_state == STATES_SUSPENDED
209      );
210
211      executing->Life.state = THREAD_LIFE_NORMAL;
212
213      _Thread_Load_environment( executing );
214      _Thread_Restart_self( executing );
215
216      _Assert_Not_reached();
217    }
218  }
219}
220
221static void _Thread_Start_life_change(
222  Thread_Control          *the_thread,
223  const Scheduler_Control *scheduler,
224  Priority_Control         priority
225)
226{
227  the_thread->is_preemptible   = the_thread->Start.is_preemptible;
228  the_thread->budget_algorithm = the_thread->Start.budget_algorithm;
229  the_thread->budget_callout   = the_thread->Start.budget_callout;
230  the_thread->real_priority    = priority;
231
232  _Thread_Set_state( the_thread, STATES_RESTARTING );
233  _Thread_queue_Extract_with_proxy( the_thread );
234  _Watchdog_Remove( &the_thread->Timer );
235  _Scheduler_Set_priority_if_higher( scheduler, the_thread, priority );
236  _Thread_Add_post_switch_action( the_thread, &the_thread->Life.Action );
237  _Thread_Ready( the_thread );
238  _Thread_Request_dispatch_if_executing( the_thread );
239}
240
241static void _Thread_Request_life_change(
242  Thread_Control    *the_thread,
243  Thread_Control    *executing,
244  Priority_Control   priority,
245  Thread_Life_state  additional_life_state
246)
247{
248  Thread_Life_state previous_life_state;
249  Per_CPU_Control *cpu;
250  ISR_Level level;
251  const Scheduler_Control *scheduler;
252
253  cpu = _Thread_Action_ISR_disable_and_acquire( the_thread, &level );
254  previous_life_state = the_thread->Life.state;
255  the_thread->Life.state = previous_life_state | additional_life_state;
256  _Thread_Action_release_and_ISR_enable( cpu, level );
257
258  scheduler = _Scheduler_Get( the_thread );
259  if ( the_thread == executing ) {
260    executing->real_priority = priority;
261
262    _Scheduler_Set_priority_if_higher( scheduler, the_thread, priority );
263    _Thread_Start_life_change_for_executing( executing );
264  } else if ( previous_life_state == THREAD_LIFE_NORMAL ) {
265    _Thread_Start_life_change( the_thread, scheduler, priority );
266  } else {
267    _Thread_Clear_state( the_thread, STATES_SUSPENDED );
268
269    if ( _Thread_Is_life_terminating( additional_life_state ) ) {
270      the_thread->real_priority = _Scheduler_Highest_priority_of_two(
271        scheduler,
272        the_thread->real_priority,
273        priority
274      );
275
276      _Scheduler_Change_priority_if_higher(
277        scheduler,
278        the_thread,
279        priority,
280        false
281      );
282    }
283  }
284}
285
286void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing )
287{
288  _Assert( _Thread_Is_life_protected( executing->Life.state ) );
289
290  if ( _States_Is_dormant( the_thread->current_state ) ) {
291    _Thread_Make_zombie( the_thread );
292  } else {
293    if (
294      the_thread != executing
295        && !_Thread_Is_life_terminating( executing->Life.state )
296    ) {
297      /*
298       * Wait for termination of victim thread.  If the executing thread is
299       * also terminated, then do not wait.  This avoids potential cyclic
300       * dependencies and thus dead lock.
301       */
302       the_thread->Life.terminator = executing;
303       _Thread_Set_state( executing, STATES_WAITING_FOR_TERMINATION );
304    }
305
306    _Thread_Request_life_change(
307      the_thread,
308      executing,
309      executing->current_priority,
310      THREAD_LIFE_TERMINATING
311    );
312  }
313}
314
315bool _Thread_Restart(
316  Thread_Control            *the_thread,
317  Thread_Control            *executing,
318  void                      *pointer_argument,
319  Thread_Entry_numeric_type  numeric_argument
320)
321{
322  if ( !_States_Is_dormant( the_thread->current_state ) ) {
323    the_thread->Start.pointer_argument = pointer_argument;
324    the_thread->Start.numeric_argument = numeric_argument;
325
326    _Thread_Request_life_change(
327      the_thread,
328      executing,
329      the_thread->Start.initial_priority,
330      THREAD_LIFE_RESTARTING
331    );
332
333    return true;
334  }
335
336  return false;
337}
338
339bool _Thread_Set_life_protection( bool protect )
340{
341  bool previous_life_protection;
342  ISR_Level level;
343  Per_CPU_Control *cpu;
344  Thread_Control *executing;
345  Thread_Life_state previous_life_state;
346
347  cpu = _Thread_Action_ISR_disable_and_acquire_for_executing( &level );
348  executing = cpu->executing;
349
350  previous_life_state = executing->Life.state;
351  previous_life_protection = _Thread_Is_life_protected( previous_life_state );
352
353  if ( protect ) {
354    executing->Life.state = previous_life_state | THREAD_LIFE_PROTECTED;
355  } else {
356    executing->Life.state = previous_life_state & ~THREAD_LIFE_PROTECTED;
357  }
358
359  _Thread_Action_release_and_ISR_enable( cpu, level );
360
361#if defined(RTEMS_SMP)
362  /*
363   * On SMP configurations it is possible that a life change of an executing
364   * thread is requested, but this thread didn't notice it yet.  The life
365   * change is first marked in the life state field and then all scheduling and
366   * other thread state updates are performed.  The last step is to issues an
367   * inter-processor interrupt if necessary.  Since this takes some time we
368   * have to synchronize here.
369   */
370  if (
371    !_Thread_Is_life_protected( previous_life_state )
372      && _Thread_Is_life_changing( previous_life_state )
373  ) {
374    _Thread_Disable_dispatch();
375    _Thread_Enable_dispatch();
376  }
377#endif
378
379  if (
380    !protect
381      && _Thread_Is_life_changing( previous_life_state )
382  ) {
383    _Thread_Disable_dispatch();
384    _Thread_Start_life_change_for_executing( executing );
385    _Thread_Enable_dispatch();
386  }
387
388  return previous_life_protection;
389}
Note: See TracBrowser for help on using the repository browser.