Mark mutex as robust to prevent deadlocks (#1233)

Mark mutex as robust to prevent deadlocks

Prevent application hangs that occur when a thread dies while holding a
mutex, particularly during vTaskEndScheduler or exit calls. This is
achieved by setting the PTHREAD_MUTEX_ROBUST attribute on the mutex.

Fixes:
- GitHub issue: FreeRTOS/FreeRTOS-Kernel#1217
- Forum thread: freertos.org/t/22287

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
This commit is contained in:
Gaurav-Aggarwal-AWS 2025-01-29 07:27:30 +05:30 committed by GitHub
parent f94bc89108
commit ad4e723829
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 80 additions and 26 deletions

View file

@ -35,9 +35,11 @@
struct event
{
pthread_mutex_t mutex;
pthread_mutexattr_t mutexattr;
pthread_cond_t cond;
bool event_triggered;
};
/*-----------------------------------------------------------*/
struct event * event_create( void )
{
@ -46,23 +48,36 @@ struct event * event_create( void )
if( ev != NULL )
{
ev->event_triggered = false;
pthread_mutex_init( &ev->mutex, NULL );
pthread_mutexattr_init( &ev->mutexattr );
#ifndef __APPLE__
pthread_mutexattr_setrobust( &ev->mutexattr, PTHREAD_MUTEX_ROBUST );
#endif
pthread_mutex_init( &ev->mutex, &ev->mutexattr );
pthread_cond_init( &ev->cond, NULL );
}
return ev;
}
/*-----------------------------------------------------------*/
void event_delete( struct event * ev )
{
pthread_mutex_destroy( &ev->mutex );
pthread_mutexattr_destroy( &ev->mutexattr );
pthread_cond_destroy( &ev->cond );
free( ev );
}
/*-----------------------------------------------------------*/
bool event_wait( struct event * ev )
{
pthread_mutex_lock( &ev->mutex );
if( pthread_mutex_lock( &ev->mutex ) == EOWNERDEAD )
{
#ifndef __APPLE__
/* If the thread owning the mutex died, make the mutex consistent. */
pthread_mutex_consistent( &ev->mutex );
#endif
}
while( ev->event_triggered == false )
{
@ -73,6 +88,8 @@ bool event_wait( struct event * ev )
pthread_mutex_unlock( &ev->mutex );
return true;
}
/*-----------------------------------------------------------*/
bool event_wait_timed( struct event * ev,
time_t ms )
{
@ -82,7 +99,13 @@ bool event_wait_timed( struct event * ev,
clock_gettime( CLOCK_REALTIME, &ts );
ts.tv_sec += ms / 1000;
ts.tv_nsec += ( ( ms % 1000 ) * 1000000 );
pthread_mutex_lock( &ev->mutex );
if( pthread_mutex_lock( &ev->mutex ) == EOWNERDEAD )
{
#ifndef __APPLE__
/* If the thread owning the mutex died, make the mutex consistent. */
pthread_mutex_consistent( &ev->mutex );
#endif
}
while( ( ev->event_triggered == false ) && ( ret == 0 ) )
{
@ -98,11 +121,19 @@ bool event_wait_timed( struct event * ev,
pthread_mutex_unlock( &ev->mutex );
return true;
}
/*-----------------------------------------------------------*/
void event_signal( struct event * ev )
{
pthread_mutex_lock( &ev->mutex );
if( pthread_mutex_lock( &ev->mutex ) == EOWNERDEAD )
{
#ifndef __APPLE__
/* If the thread owning the mutex died, make the mutex consistent. */
pthread_mutex_consistent( &ev->mutex );
#endif
}
ev->event_triggered = true;
pthread_cond_signal( &ev->cond );
pthread_mutex_unlock( &ev->mutex );
}
/*-----------------------------------------------------------*/