Skip to content

Commit

Permalink
Optimize uxStreamBufferAdd() locking mechanism (#1181)
Browse files Browse the repository at this point in the history
* Optimize uxStreamBufferAdd() locking mechanism

* Fix uxStreamBufferAdd unit tests

---------

Co-authored-by: Tony Josi <tonyjosi@amazon.com>
  • Loading branch information
lzungri and tony-josi-aws authored Sep 11, 2024
1 parent cb8f00c commit 3c9140d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 23 deletions.
4 changes: 2 additions & 2 deletions source/FreeRTOS_Stream_Buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,

/* The below update to the stream buffer members must happen
* atomically. */
vTaskSuspendAll();
taskENTER_CRITICAL();
{
if( uxOffset == 0U )
{
Expand All @@ -328,7 +328,7 @@ size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,
pxBuffer->uxFront = uxNextHead;
}
}
( void ) xTaskResumeAll();
taskEXIT_CRITICAL();
}

return uxCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,6 @@ void test_uxStreamBufferAdd_BufferHasMoreSpaceThanData_ZeroOffset_DataWriteCause

FreeRTOS_min_size_t_Stub( FreeRTOS_min_stub );

vTaskSuspendAll_Expect();
xTaskResumeAll_ExpectAndReturn( pdTRUE );

uxReturn = uxStreamBufferAdd( pxLocalBuffer, uxOffset, pucData, uxByteCount );

/* Only these many bytes should be written. */
Expand Down Expand Up @@ -656,9 +653,6 @@ void test_uxStreamBufferAdd_BufferHasLessSpaceThanData_ZeroOffset( void )

FreeRTOS_min_size_t_Stub( FreeRTOS_min_stub );

vTaskSuspendAll_Expect();
xTaskResumeAll_ExpectAndReturn( pdTRUE );

uxReturn = uxStreamBufferAdd( pxLocalBuffer, uxOffset, pucData, uxByteCount );

/* Only 500 bytes should be written. */
Expand Down Expand Up @@ -702,9 +696,6 @@ void test_uxStreamBufferAdd_BufferHasLessSpaceThanData_NonZeroOffset( void )

FreeRTOS_min_size_t_Stub( FreeRTOS_min_stub );

vTaskSuspendAll_Expect();
xTaskResumeAll_ExpectAndReturn( pdTRUE );

uxReturn = uxStreamBufferAdd( pxLocalBuffer, uxOffset, pucData, uxByteCount );

/* Only these many bytes should be written. */
Expand Down Expand Up @@ -757,9 +748,6 @@ void test_uxStreamBufferAdd_BufferHasLessSpaceThanData_NonZeroOffsetCausesRollov

FreeRTOS_min_size_t_Stub( FreeRTOS_min_stub );

vTaskSuspendAll_Expect();
xTaskResumeAll_ExpectAndReturn( pdTRUE );

uxReturn = uxStreamBufferAdd( pxLocalBuffer, uxOffset, pucData, uxByteCount );

/* Only these many bytes should be written. */
Expand Down Expand Up @@ -813,9 +801,6 @@ void test_uxStreamBufferAdd_BufferHasLessSpaceThanData_ZeroOffset_DataWriteCause

FreeRTOS_min_size_t_Stub( FreeRTOS_min_stub );

vTaskSuspendAll_Expect();
xTaskResumeAll_ExpectAndReturn( pdTRUE );

uxReturn = uxStreamBufferAdd( pxLocalBuffer, uxOffset, pucData, uxByteCount );

/* Only these many bytes should be written. */
Expand Down Expand Up @@ -873,9 +858,6 @@ void test_uxStreamBufferAdd_NULLData_BufferHasLessSpaceThanData_ZeroOffset_DataW

FreeRTOS_min_size_t_Stub( FreeRTOS_min_stub );

vTaskSuspendAll_Expect();
xTaskResumeAll_ExpectAndReturn( pdTRUE );

uxReturn = uxStreamBufferAdd( pxLocalBuffer, uxOffset, NULL, uxByteCount );

/* Nothing should be written but tail will be updated. */
Expand Down Expand Up @@ -929,9 +911,6 @@ void test_uxStreamBufferAdd_NULLData_BufferHasLessSpaceThanData_ZeroOffset_DataW

FreeRTOS_min_size_t_Stub( FreeRTOS_min_stub );

vTaskSuspendAll_Expect();
xTaskResumeAll_ExpectAndReturn( pdTRUE );

uxReturn = uxStreamBufferAdd( pxLocalBuffer, uxOffset, NULL, uxByteCount );

/* Nothing should be written but tail should be updated. */
Expand Down

0 comments on commit 3c9140d

Please sign in to comment.