fix(freertos): Fixed SMP race condition in stream_buffers.c

This commit fixes a race condition in dual-core SMP mode where in the
xStreamBufferReceive() makes the xTaskWaitingToReceive NULL but it may
have already been evaluated to not be NULL by xStreamBufferSend()
running on another core and eventually leading to a crash in tasks.c.
This commit is contained in:
Sudeep Mohanty
2024-10-11 11:49:50 +02:00
parent 84e07d40a6
commit 0fe7a9d579

View File

@ -885,7 +885,19 @@ size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
/* Wait for data to be available. */
traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
pxStreamBuffer->xTaskWaitingToReceive = NULL;
/* In SMP mode, the task may have been woken and scheduled on
* another core. Hence, we must clear the xTaskWaitingToReceive
* handle in a critical section. */
#if ( configNUM_CORES > 1 )
taskENTER_CRITICAL();
#endif /* configNUMBER_OF_CORES > 1 */
{
pxStreamBuffer->xTaskWaitingToReceive = NULL;
}
#if ( configNUM_CORES > 1 )
taskEXIT_CRITICAL();
#endif /* configNUMBER_OF_CORES > 1 */
/* Recheck the data available after blocking. */
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );