Skip to content

Commit 14ee4b2

Browse files
Saiiijchanwangfei_chenkar-rahul-aws
authored
Fix detach race condition (#43)
* Fix detach race condition pxThread->xAttr.usSchedPriorityDetachState may be changed by pthread_detach(). Suspend scheduler to prevent race condition. Signed-off-by: wangfei_chen <[email protected]> * Fix compiler warnings --------- Signed-off-by: wangfei_chen <[email protected]> Co-authored-by: wangfei_chen <[email protected]> Co-authored-by: Rahul Kar <[email protected]>
1 parent 1bb2a09 commit 14ee4b2

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,21 @@ static void prvExitThread( void )
101101
{
102102
pthread_internal_t * pxThread = ( pthread_internal_t * ) pthread_self();
103103

104+
vTaskSuspendAll();
105+
104106
/* If this thread is joinable, wait for a call to pthread_join. */
105107
if( pthreadIS_JOINABLE( pxThread->xAttr.usSchedPriorityDetachState ) )
106108
{
107109
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier );
110+
( void ) xTaskResumeAll();
108111

109112
/* Suspend until the call to pthread_join. The caller of pthread_join
110113
* will perform cleanup. */
111114
vTaskSuspend( NULL );
112115
}
113116
else
114117
{
118+
( void ) xTaskResumeAll();
115119
/* For a detached thread, perform cleanup of thread object. */
116120
vPortFree( pxThread );
117121
vTaskDelete( NULL );
@@ -359,7 +363,7 @@ int pthread_create( pthread_t * thread,
359363
}
360364

361365
/* End the critical section. */
362-
xTaskResumeAll();
366+
( void ) xTaskResumeAll();
363367
}
364368

365369
return iStatus;
@@ -467,7 +471,7 @@ int pthread_join( pthread_t pthread,
467471
vPortFree( pxThread );
468472

469473
/* End the critical section. */
470-
xTaskResumeAll();
474+
( void ) xTaskResumeAll();
471475
}
472476

473477
return iStatus;
@@ -526,7 +530,7 @@ int pthread_detach(pthread_t pthread)
526530
}
527531

528532
/* End the critical section. */
529-
xTaskResumeAll();
533+
( void ) xTaskResumeAll();
530534
}
531535

532536
return iStatus;

0 commit comments

Comments
 (0)