From 31036f1dc8a963fb0bc3fc103f63028988314fea Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Mon, 18 Jul 2022 09:41:39 +0200 Subject: score: Use priority inheritance for thread join Threads may join the thread termination of another thread using the pthread_join() or rtems_task_delete() directives. The thread cancel operation used a special case priority boosting mechanism implemented by _Thread_Raise_real_priority(). The problem was that this approach * is not transitive, * does not account for priority adjustments of the calling task while waiting for the join, * does not support clustered scheduling, and * does not detect deadlocks. All these problems are fixed by using a priority inheritance thread queue for the join operation. Close #4679. --- cpukit/posix/src/cancel.c | 2 +- cpukit/posix/src/pthreadjoin.c | 47 +++++++++++------------------------------- 2 files changed, 13 insertions(+), 36 deletions(-) (limited to 'cpukit/posix') diff --git a/cpukit/posix/src/cancel.c b/cpukit/posix/src/cancel.c index 300055d68a..0fb2199f0a 100644 --- a/cpukit/posix/src/cancel.c +++ b/cpukit/posix/src/cancel.c @@ -75,7 +75,7 @@ int pthread_cancel( pthread_t thread ) } else { _Thread_Dispatch_disable_with_CPU( cpu_self, &lock_context ); _ISR_lock_ISR_enable( &lock_context ); - _Thread_Cancel( the_thread, executing, PTHREAD_CANCELED ); + (void) _Thread_Cancel( the_thread, executing, 0, PTHREAD_CANCELED ); _Thread_Dispatch_enable( cpu_self ); } return 0; diff --git a/cpukit/posix/src/pthreadjoin.c b/cpukit/posix/src/pthreadjoin.c index 84ff15ceec..e4f978f6b4 100644 --- a/cpukit/posix/src/pthreadjoin.c +++ b/cpukit/posix/src/pthreadjoin.c @@ -54,26 +54,16 @@ static int _POSIX_Threads_Join( pthread_t thread, void **value_ptr ) { Thread_Control *the_thread; Thread_queue_Context queue_context; - Per_CPU_Control *cpu_self; Thread_Control *executing; - void *value; + Status_Control status; _Thread_queue_Context_initialize( &queue_context ); - _Thread_queue_Context_set_enqueue_do_nothing_extra( &queue_context ); the_thread = _Thread_Get( thread, &queue_context.Lock_context.Lock_context ); if ( the_thread == NULL ) { return ESRCH; } - cpu_self = _Per_CPU_Get(); - executing = _Per_CPU_Get_executing( cpu_self ); - - if ( executing == the_thread ) { - _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context ); - return EDEADLK; - } - _Thread_State_acquire_critical( the_thread, &queue_context.Lock_context.Lock_context @@ -84,33 +74,20 @@ static int _POSIX_Threads_Join( pthread_t thread, void **value_ptr ) return EINVAL; } - if ( _States_Is_waiting_for_join_at_exit( the_thread->current_state ) ) { - value = the_thread->Life.exit_value; - _Thread_Clear_state_locked( the_thread, STATES_WAITING_FOR_JOIN_AT_EXIT ); - _Thread_Dispatch_disable_with_CPU( - cpu_self, - &queue_context.Lock_context.Lock_context - ); - _Thread_State_release( the_thread, &queue_context.Lock_context.Lock_context ); - _Thread_Dispatch_direct( cpu_self ); - } else { - _Thread_Join( - the_thread, - STATES_INTERRUPTIBLE_BY_SIGNAL | STATES_WAITING_FOR_JOIN, - executing, - &queue_context - ); - - if ( _POSIX_Get_error_after_wait( executing ) != 0 ) { - _Assert( _POSIX_Get_error_after_wait( executing ) == EINTR ); - return EINTR; - } - - value = executing->Wait.return_argument; + executing = _Thread_Executing; + status = _Thread_Join( + the_thread, + STATES_INTERRUPTIBLE_BY_SIGNAL | STATES_WAITING_FOR_JOIN, + executing, + &queue_context + ); + + if ( status != STATUS_SUCCESSFUL ) { + return _POSIX_Get_error( status ); } if ( value_ptr != NULL ) { - *value_ptr = value; + *value_ptr = executing->Wait.return_argument; } return 0; -- cgit v1.2.3