fix(freertos): Fixed SMP race condition in xStreamBufferSend()

This commit fixes a race condition in dual-core SMP mode where in the
xStreamBufferSend() makes the xTaskWaitingToSend NULL but it may
have already been evaluated to not be NULL by xStreamBufferReceive()
running on another core and eventually leading to a crash in tasks.c.
This commit is contained in:
Sudeep Mohanty 2024-10-16 09:59:45 +02:00 committed by BOT
parent 69da13031c
commit 1fe57a5d8e
2 changed files with 14 additions and 1 deletions

View File

@ -125,6 +125,8 @@ The following functions were modified to accommodate SMP behavior:
- Added extra check to see if current blocking task has already been deleted by the other core.
- `xStreamBufferReceive()`
- Added a critical section for setting `xTaskWaitingToReceive` to `NULL` so that the write is SMP safe.
- `xStreamBufferSend()`
- Added a critical section for setting `xTaskWaitingToSend` to `NULL` so that the write is SMP safe.
### Critical Section Changes

View File

@ -774,7 +774,18 @@ size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
pxStreamBuffer->xTaskWaitingToSend = NULL;
/* In SMP mode, the task may have been woken and scheduled on
* another core. Hence, we must clear the xTaskWaitingToSend
* handle in a critical section. */
#if ( configNUMBER_OF_CORES > 1 )
taskENTER_CRITICAL( &( pxStreamBuffer->xStreamBufferLock ) );
#endif /* configNUMBER_OF_CORES > 1 */
{
pxStreamBuffer->xTaskWaitingToSend = NULL;
}
#if ( configNUMBER_OF_CORES > 1 )
taskEXIT_CRITICAL( &( pxStreamBuffer->xStreamBufferLock ) );
#endif /* configNUMBER_OF_CORES > 1 */
} while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
}
else