Skip to content

Commit 2f2b7e5

Browse files
change(freertos/smp): Update stream_buffer.c locking
Updated stream_buffer.c to use granular locking - Added xTaskSpinlock and xISRSpinlock - Replaced critical section macros with data group critical section macros such as taskENTER/EXIT_CRITICAL/_FROM_ISR() with sbENTER/EXIT_CRITICAL_FROM_ISR(). - Added vStreambuffersEnterCritical/FromISR() and vStreambuffersExitCritical/FromISR() to map to the data group critical section macros. - Added prvLockStreamBufferForTasks() and prvUnlockStreamBufferForTasks() to suspend the stream buffer when executing non-deterministic code. Co-authored-by: Sudeep Mohanty <sudeep.mohanty@espressif.com>
1 parent 2f58dd5 commit 2f2b7e5

File tree

2 files changed

+117
-19
lines changed

2 files changed

+117
-19
lines changed

include/FreeRTOS.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3413,6 +3413,9 @@ typedef struct xSTATIC_STREAM_BUFFER
34133413
void * pvDummy5[ 2 ];
34143414
#endif
34153415
UBaseType_t uxDummy6;
3416+
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
3417+
portSPINLOCK_TYPE xDummySpinlock[ 2 ];
3418+
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
34163419
} StaticStreamBuffer_t;
34173420

34183421
/* Message buffers are built on stream buffers. */

stream_buffer.c

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,47 @@
5858
#error INCLUDE_xTaskGetCurrentTaskHandle must be set to 1 to build stream_buffer.c
5959
#endif
6060

61+
62+
/*
63+
* Macros to mark the start and end of a critical code region.
64+
*/
65+
#if ( portUSING_GRANULAR_LOCKS == 1 )
66+
#define sbENTER_CRITICAL( pxStreamBuffer ) taskDATA_GROUP_ENTER_CRITICAL( pxStreamBuffer )
67+
#define sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer ) taskDATA_GROUP_ENTER_CRITICAL_FROM_ISR( pxStreamBuffer )
68+
#define sbEXIT_CRITICAL( pxStreamBuffer ) taskDATA_GROUP_EXIT_CRITICAL( pxStreamBuffer )
69+
#define sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer ) taskDATA_GROUP_EXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer )
70+
#else /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
71+
#define sbENTER_CRITICAL( pxEventBits ) taskENTER_CRITICAL();
72+
#define sbENTER_CRITICAL_FROM_ISR( pxEventBits ) taskENTER_CRITICAL_FROM_ISR();
73+
#define sbEXIT_CRITICAL( pxEventBits ) taskEXIT_CRITICAL();
74+
#define sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer ) taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
75+
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
76+
77+
/*
78+
* Macro used to lock and unlock a stream buffer. When a task locks a stream
79+
* buffer, the task will have thread safe non-deterministic access to the stream
80+
* buffer.
81+
* - Concurrent access from other tasks will be blocked by the xTaskSpinlock
82+
* - Concurrent access from ISRs will be pended
83+
*
84+
* When the task unlocks the stream buffer, all pended access attempts are handled.
85+
*/
86+
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
87+
#define sbLOCK( pxStreamBuffer ) prvLockStreamBufferForTasks( pxStreamBuffer )
88+
#define sbUNLOCK( pxStreamBuffer ) prvUnlockStreamBufferForTasks( pxStreamBuffer )
89+
#else /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
90+
#define sbLOCK( pxStreamBuffer ) vTaskSuspendAll()
91+
#define sbUNLOCK( pxStreamBuffer ) ( void ) xTaskResumeAll()
92+
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
93+
6194
/* If the user has not provided application specific Rx notification macros,
6295
* or #defined the notification macros away, then provide default implementations
6396
* that uses task notifications. */
6497
#ifndef sbRECEIVE_COMPLETED
6598
#define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
6699
do \
67100
{ \
68-
vTaskSuspendAll(); \
101+
sbLOCK( pxStreamBuffer ); \
69102
{ \
70103
if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
71104
{ \
@@ -76,7 +109,7 @@
76109
( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
77110
} \
78111
} \
79-
( void ) xTaskResumeAll(); \
112+
( void ) sbUNLOCK( pxStreamBuffer ); \
80113
} while( 0 )
81114
#endif /* sbRECEIVE_COMPLETED */
82115

@@ -105,7 +138,7 @@
105138
do { \
106139
UBaseType_t uxSavedInterruptStatus; \
107140
\
108-
uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
141+
uxSavedInterruptStatus = sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer ); \
109142
{ \
110143
if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
111144
{ \
@@ -117,7 +150,7 @@
117150
( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
118151
} \
119152
} \
120-
taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
153+
sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer ); \
121154
} while( 0 )
122155
#endif /* sbRECEIVE_COMPLETED_FROM_ISR */
123156

@@ -145,7 +178,7 @@
145178
*/
146179
#ifndef sbSEND_COMPLETED
147180
#define sbSEND_COMPLETED( pxStreamBuffer ) \
148-
vTaskSuspendAll(); \
181+
sbLOCK( pxStreamBuffer ); \
149182
{ \
150183
if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
151184
{ \
@@ -156,7 +189,7 @@
156189
( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
157190
} \
158191
} \
159-
( void ) xTaskResumeAll()
192+
( void ) sbUNLOCK( pxStreamBuffer )
160193
#endif /* sbSEND_COMPLETED */
161194

162195
/* If user has provided a per-instance send completed callback, then
@@ -184,7 +217,7 @@
184217
do { \
185218
UBaseType_t uxSavedInterruptStatus; \
186219
\
187-
uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
220+
uxSavedInterruptStatus = sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer ); \
188221
{ \
189222
if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
190223
{ \
@@ -196,7 +229,7 @@
196229
( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
197230
} \
198231
} \
199-
taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
232+
sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer ); \
200233
} while( 0 )
201234
#endif /* sbSEND_COMPLETE_FROM_ISR */
202235

@@ -249,8 +282,30 @@ typedef struct StreamBufferDef_t
249282
StreamBufferCallbackFunction_t pxReceiveCompletedCallback; /* Optional callback called on receive complete. sbRECEIVE_COMPLETED is called if this is NULL. */
250283
#endif
251284
UBaseType_t uxNotificationIndex; /* The index we are using for notification, by default tskDEFAULT_INDEX_TO_NOTIFY. */
285+
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
286+
portSPINLOCK_TYPE xTaskSpinlock;
287+
portSPINLOCK_TYPE xISRSpinlock;
288+
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
252289
} StreamBuffer_t;
253290

291+
/*
292+
* Locks a stream buffer for tasks. Prevents other tasks from accessing the stream buffer
293+
* but allows ISRs to pend access to the stream buffer. Caller cannot be preempted
294+
* by other tasks after locking the stream buffer, thus allowing the caller to
295+
* execute non-deterministic operations.
296+
*/
297+
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
298+
static void prvLockStreamBufferForTasks( StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
299+
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
300+
301+
/*
302+
* Unlocks a stream buffer for tasks. Handles all pended access from ISRs, then reenables preemption
303+
* for the caller.
304+
*/
305+
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
306+
static void prvUnlockStreamBufferForTasks( StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
307+
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
308+
254309
/*
255310
* The number of bytes available to be read from the buffer.
256311
*/
@@ -327,6 +382,32 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
327382
StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
328383

329384
/*-----------------------------------------------------------*/
385+
386+
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
387+
static void prvLockStreamBufferForTasks( StreamBuffer_t * const pxStreamBuffer )
388+
{
389+
/* Disable preemption so that the current task cannot be preempted by another task */
390+
vTaskPreemptionDisable( NULL );
391+
392+
/* Keep holding xTaskSpinlock after unlocking the data group to prevent tasks
393+
* on other cores from accessing the stream buffer while it is suspended. */
394+
portGET_SPINLOCK( portGET_CORE_ID(), &( pxStreamBuffer->xTaskSpinlock ) );
395+
}
396+
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
397+
/*-----------------------------------------------------------*/
398+
399+
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
400+
static void prvUnlockStreamBufferForTasks( StreamBuffer_t * const pxStreamBuffer )
401+
{
402+
/* Release the previously held task spinlock */
403+
portRELEASE_SPINLOCK( portGET_CORE_ID(), &( pxStreamBuffer->xTaskSpinlock ) );
404+
405+
/* Re-enable preemption */
406+
vTaskPreemptionEnable( NULL );
407+
}
408+
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
409+
/*-----------------------------------------------------------*/
410+
330411
#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
331412
StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
332413
size_t xTriggerLevelBytes,
@@ -405,6 +486,13 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
405486
pxSendCompletedCallback,
406487
pxReceiveCompletedCallback );
407488

489+
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
490+
{
491+
portINIT_SPINLOCK( &( ( ( StreamBuffer_t * ) pvAllocatedMemory )->xTaskSpinlock ) );
492+
portINIT_SPINLOCK( &( ( ( StreamBuffer_t * ) pvAllocatedMemory )->xISRSpinlock ) );
493+
}
494+
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
495+
408496
traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pvAllocatedMemory ), xStreamBufferType );
409497
}
410498
else
@@ -499,6 +587,13 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
499587
* again. */
500588
pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
501589

590+
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
591+
{
592+
portINIT_SPINLOCK( &( pxStreamBuffer->xTaskSpinlock ) );
593+
portINIT_SPINLOCK( &( pxStreamBuffer->xISRSpinlock ) );
594+
}
595+
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
596+
502597
traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xStreamBufferType );
503598

504599
/* MISRA Ref 11.3.1 [Misaligned access] */
@@ -614,7 +709,7 @@ BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
614709
#endif
615710

616711
/* Can only reset a message buffer if there are no tasks blocked on it. */
617-
taskENTER_CRITICAL();
712+
sbENTER_CRITICAL( pxStreamBuffer );
618713
{
619714
if( ( pxStreamBuffer->xTaskWaitingToReceive == NULL ) && ( pxStreamBuffer->xTaskWaitingToSend == NULL ) )
620715
{
@@ -644,7 +739,7 @@ BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
644739
xReturn = pdPASS;
645740
}
646741
}
647-
taskEXIT_CRITICAL();
742+
sbEXIT_CRITICAL( pxStreamBuffer );
648743

649744
traceRETURN_xStreamBufferReset( xReturn );
650745

@@ -872,7 +967,7 @@ size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
872967
{
873968
/* Wait until the required number of bytes are free in the message
874969
* buffer. */
875-
taskENTER_CRITICAL();
970+
sbENTER_CRITICAL( pxStreamBuffer );
876971
{
877972
xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
878973

@@ -887,11 +982,11 @@ size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
887982
}
888983
else
889984
{
890-
taskEXIT_CRITICAL();
985+
sbEXIT_CRITICAL( pxStreamBuffer );
891986
break;
892987
}
893988
}
894-
taskEXIT_CRITICAL();
989+
sbEXIT_CRITICAL( pxStreamBuffer );
895990

896991
traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
897992
( void ) xTaskNotifyWaitIndexed( pxStreamBuffer->uxNotificationIndex, ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
@@ -1087,7 +1182,7 @@ size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
10871182
{
10881183
/* Checking if there is data and clearing the notification state must be
10891184
* performed atomically. */
1090-
taskENTER_CRITICAL();
1185+
sbENTER_CRITICAL( pxStreamBuffer );
10911186
{
10921187
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
10931188

@@ -1112,7 +1207,7 @@ size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
11121207
mtCOVERAGE_TEST_MARKER();
11131208
}
11141209
}
1115-
taskEXIT_CRITICAL();
1210+
sbEXIT_CRITICAL( pxStreamBuffer );
11161211

11171212
if( xBytesAvailable <= xBytesToStoreMessageLength )
11181213
{
@@ -1409,7 +1504,7 @@ BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer
14091504
/* MISRA Ref 4.7.1 [Return value shall be checked] */
14101505
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
14111506
/* coverity[misra_c_2012_directive_4_7_violation] */
1412-
uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1507+
uxSavedInterruptStatus = sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer );
14131508
{
14141509
if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
14151510
{
@@ -1426,7 +1521,7 @@ BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer
14261521
xReturn = pdFALSE;
14271522
}
14281523
}
1429-
taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1524+
sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer );
14301525

14311526
traceRETURN_xStreamBufferSendCompletedFromISR( xReturn );
14321527

@@ -1448,7 +1543,7 @@ BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuf
14481543
/* MISRA Ref 4.7.1 [Return value shall be checked] */
14491544
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
14501545
/* coverity[misra_c_2012_directive_4_7_violation] */
1451-
uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1546+
uxSavedInterruptStatus = sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer );
14521547
{
14531548
if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
14541549
{
@@ -1465,7 +1560,7 @@ BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuf
14651560
xReturn = pdFALSE;
14661561
}
14671562
}
1468-
taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1563+
sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer );
14691564

14701565
traceRETURN_xStreamBufferReceiveCompletedFromISR( xReturn );
14711566

0 commit comments

Comments
 (0)