Skip to content

Commit

Permalink
Merge branch 'fix/freertos_race_cond_in_stream_buffers_v5.3' into 're…
Browse files Browse the repository at this point in the history
…lease/v5.3'

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

See merge request espressif/esp-idf!34223
  • Loading branch information
ESP-Marius committed Oct 22, 2024
2 parents 2f2acfa + 39f4581 commit 18d9978
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions components/freertos/FreeRTOS-Kernel/idf_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ The following functions were modified to accommodate SMP behavior:
- In SMP, the function now disables interrupts to ensure that the calling task does not switch cores while checking its own copy of `uxSchedulerSuspended`.
- `prvAddCurrentTaskToDelayedList()`
- 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.

### Critical Section Changes

Expand Down
16 changes: 14 additions & 2 deletions components/freertos/FreeRTOS-Kernel/stream_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* SPDX-License-Identifier: MIT
*
* SPDX-FileContributor: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
Expand Down Expand Up @@ -976,7 +976,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 ( configNUMBER_OF_CORES > 1 )
taskENTER_CRITICAL( &( pxStreamBuffer->xStreamBufferLock ) );
#endif /* configNUMBER_OF_CORES > 1 */
{
pxStreamBuffer->xTaskWaitingToReceive = NULL;
}
#if ( configNUMBER_OF_CORES > 1 )
taskEXIT_CRITICAL( &( pxStreamBuffer->xStreamBufferLock ) );
#endif /* configNUMBER_OF_CORES > 1 */

/* Recheck the data available after blocking. */
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
Expand Down

0 comments on commit 18d9978

Please sign in to comment.