From 4b3251adda40e73593eae23b9d4f49643212a7d3 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Thu, 16 Jun 2016 12:05:42 +0200 Subject: score: Fix thread delete race condition on SMP --- cpukit/score/src/threadrestart.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/cpukit/score/src/threadrestart.c b/cpukit/score/src/threadrestart.c index f155980c1a..21e260b10b 100644 --- a/cpukit/score/src/threadrestart.c +++ b/cpukit/score/src/threadrestart.c @@ -135,11 +135,19 @@ static void _Thread_Wake_up_joining_threads( Thread_Control *the_thread ) ); } -static void _Thread_Make_zombie( Thread_Control *the_thread ) +static void _Thread_Add_to_zombie_chain( Thread_Control *the_thread ) { ISR_lock_Context lock_context; Thread_Zombie_control *zombies; + zombies = &_Thread_Zombies; + _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context ); + _Chain_Append_unprotected( &zombies->Chain, &the_thread->Object.Node ); + _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context ); +} + +static void _Thread_Make_zombie( Thread_Control *the_thread ) +{ if ( _Thread_Owns_resources( the_thread ) ) { _Terminate( INTERNAL_ERROR_CORE, @@ -153,15 +161,18 @@ static void _Thread_Make_zombie( Thread_Control *the_thread ) &the_thread->Object ); - _Thread_Wake_up_joining_threads( the_thread ); _Thread_Set_state( the_thread, STATES_ZOMBIE ); _Thread_queue_Extract_with_proxy( the_thread ); _Thread_Timer_remove( the_thread ); - zombies = &_Thread_Zombies; - _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context ); - _Chain_Append_unprotected( &zombies->Chain, &the_thread->Object.Node ); - _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context ); + /* + * Add the thread to the thread zombie chain before we wake up joining + * threads, so that they are able to clean up the thread immediately. This + * matters for SMP configurations. + */ + _Thread_Add_to_zombie_chain( the_thread ); + + _Thread_Wake_up_joining_threads( the_thread ); } static void _Thread_Free( Thread_Control *the_thread ) -- cgit v1.2.3