This commit is contained in:
Sudeep Mohanty 2026-07-09 17:48:49 -07:00 committed by GitHub
commit 77c8e67651
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 3118 additions and 997 deletions

View file

@ -63,10 +63,30 @@
#if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the event group is statically allocated to ensure no attempt is made to free the memory. */
#endif
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
portSPINLOCK_TYPE xTaskSpinlock;
portSPINLOCK_TYPE xISRSpinlock;
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
} EventGroup_t;
/*-----------------------------------------------------------*/
/*
* Macros to mark the start and end of a critical code region.
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define event_groupsENTER_CRITICAL( pxEventBits ) taskDATA_GROUP_ENTER_CRITICAL( &pxEventBits->xTaskSpinlock, &pxEventBits->xISRSpinlock )
#define event_groupsENTER_CRITICAL_FROM_ISR( pxEventBits, puxSavedInterruptStatus ) taskDATA_GROUP_ENTER_CRITICAL_FROM_ISR( &pxEventBits->xISRSpinlock, puxSavedInterruptStatus )
#define event_groupsEXIT_CRITICAL( pxEventBits ) taskDATA_GROUP_EXIT_CRITICAL( &pxEventBits->xTaskSpinlock, &pxEventBits->xISRSpinlock )
#define event_groupsEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxEventBits ) taskDATA_GROUP_EXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, &pxEventBits->xISRSpinlock )
#else /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
#define event_groupsENTER_CRITICAL( pxEventBits ) taskENTER_CRITICAL();
#define event_groupsENTER_CRITICAL_FROM_ISR( pxEventBits, puxSavedInterruptStatus ) do { *( puxSavedInterruptStatus ) = taskENTER_CRITICAL_FROM_ISR(); } while( 0 )
#define event_groupsEXIT_CRITICAL( pxEventBits ) taskEXIT_CRITICAL();
#define event_groupsEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxEventBits ) taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
/*
* Test the bits set in uxCurrentEventBits to see if the wait condition is met.
* The wait condition is defined by xWaitForAllBits. If xWaitForAllBits is
@ -79,6 +99,33 @@
const EventBits_t uxBitsToWaitFor,
const BaseType_t xWaitForAllBits ) PRIVILEGED_FUNCTION;
/*-----------------------------------------------------------*/
/*
* Macros used to lock and unlock an event group. When a task locks an,
* event group, the task will have thread safe non-deterministic access to
* the event group.
* - Concurrent access from other tasks will be blocked by the xTaskSpinlock
* - Concurrent access from ISRs will be pended
*
* When the task unlocks the event group, all pended access attempts are handled.
*/
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#define event_groupsLOCK( pxEventBits ) taskDATA_GROUP_LOCK( &( ( pxEventBits )->xTaskSpinlock ) )
#define event_groupsUNLOCK( pxEventBits ) taskDATA_GROUP_UNLOCK( &( ( pxEventBits )->xTaskSpinlock ) )
#define event_groupsUNLOCK_WITH_YIELD_STATUS( pxEventBits, pxAlreadyYielded ) \
do { \
*( pxAlreadyYielded ) = taskDATA_GROUP_UNLOCK( &( ( pxEventBits )->xTaskSpinlock ) ); \
} while( 0 )
#else /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
#define event_groupsLOCK( pxEventBits ) vTaskSuspendAll()
#define event_groupsUNLOCK( pxEventBits ) xTaskResumeAll()
#define event_groupsUNLOCK_WITH_YIELD_STATUS( pxEventBits, pxAlreadyYielded ) \
do { \
*( pxAlreadyYielded ) = xTaskResumeAll(); \
} while( 0 )
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
/*-----------------------------------------------------------*/
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
@ -122,6 +169,13 @@
}
#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
{
portINIT_SPINLOCK( &( pxEventBits->xTaskSpinlock ) );
portINIT_SPINLOCK( &( pxEventBits->xISRSpinlock ) );
}
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
traceEVENT_GROUP_CREATE( pxEventBits );
}
else
@ -167,6 +221,13 @@
}
#endif /* configSUPPORT_STATIC_ALLOCATION */
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
{
portINIT_SPINLOCK( &( pxEventBits->xTaskSpinlock ) );
portINIT_SPINLOCK( &( pxEventBits->xISRSpinlock ) );
}
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
traceEVENT_GROUP_CREATE( pxEventBits );
}
else
@ -202,7 +263,7 @@
}
#endif
vTaskSuspendAll();
event_groupsLOCK( pxEventBits );
{
uxOriginalBitValue = pxEventBits->uxEventBits;
@ -245,7 +306,7 @@
}
}
}
xAlreadyYielded = xTaskResumeAll();
event_groupsUNLOCK_WITH_YIELD_STATUS( pxEventBits, &xAlreadyYielded );
if( xTicksToWait != ( TickType_t ) 0 )
{
@ -267,7 +328,7 @@
if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
{
/* The task timed out, just return the current event bit value. */
taskENTER_CRITICAL();
event_groupsENTER_CRITICAL( pxEventBits );
{
uxReturn = pxEventBits->uxEventBits;
@ -284,7 +345,7 @@
mtCOVERAGE_TEST_MARKER();
}
}
taskEXIT_CRITICAL();
event_groupsEXIT_CRITICAL( pxEventBits );
xTimeoutOccurred = pdTRUE;
}
@ -333,7 +394,7 @@
}
#endif
vTaskSuspendAll();
event_groupsLOCK( pxEventBits );
{
const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;
@ -401,7 +462,7 @@
traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );
}
}
xAlreadyYielded = xTaskResumeAll();
event_groupsUNLOCK_WITH_YIELD_STATUS( pxEventBits, &xAlreadyYielded );
if( xTicksToWait != ( TickType_t ) 0 )
{
@ -422,7 +483,7 @@
if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )
{
taskENTER_CRITICAL();
event_groupsENTER_CRITICAL( pxEventBits );
{
/* The task timed out, just return the current event bit value. */
uxReturn = pxEventBits->uxEventBits;
@ -447,7 +508,7 @@
xTimeoutOccurred = pdTRUE;
}
taskEXIT_CRITICAL();
event_groupsEXIT_CRITICAL( pxEventBits );
}
else
{
@ -482,7 +543,7 @@
configASSERT( xEventGroup );
configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
taskENTER_CRITICAL();
event_groupsENTER_CRITICAL( pxEventBits );
{
traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );
@ -493,7 +554,7 @@
/* Clear the bits. */
pxEventBits->uxEventBits &= ~uxBitsToClear;
}
taskEXIT_CRITICAL();
event_groupsEXIT_CRITICAL( pxEventBits );
traceRETURN_xEventGroupClearBits( uxReturn );
@ -524,7 +585,7 @@
EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
{
UBaseType_t uxSavedInterruptStatus;
EventGroup_t const * const pxEventBits = xEventGroup;
EventGroup_t * const pxEventBits = xEventGroup;
EventBits_t uxReturn;
traceENTER_xEventGroupGetBitsFromISR( xEventGroup );
@ -532,11 +593,11 @@
/* MISRA Ref 4.7.1 [Return value shall be checked] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
/* coverity[misra_c_2012_directive_4_7_violation] */
uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
event_groupsENTER_CRITICAL_FROM_ISR( pxEventBits, &uxSavedInterruptStatus );
{
uxReturn = pxEventBits->uxEventBits;
}
taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
event_groupsEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxEventBits );
traceRETURN_xEventGroupGetBitsFromISR( uxReturn );
@ -564,10 +625,17 @@
pxList = &( pxEventBits->xTasksWaitingForBits );
pxListEnd = listGET_END_MARKER( pxList );
vTaskSuspendAll();
event_groupsLOCK( pxEventBits );
{
traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
/* We are about to access the kernel data group non-deterministically,
* thus we suspend the kernel data group.*/
vTaskSuspendAll();
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
pxListItem = listGET_HEAD_ENTRY( pxList );
/* Set the bits. */
@ -638,8 +706,12 @@
/* Snapshot resulting bits. */
uxReturnBits = pxEventBits->uxEventBits;
}
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
( void ) xTaskResumeAll();
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
}
event_groupsUNLOCK( pxEventBits );
traceRETURN_xEventGroupSetBits( uxReturnBits );
@ -658,10 +730,17 @@
pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );
vTaskSuspendAll();
event_groupsLOCK( pxEventBits );
{
traceEVENT_GROUP_DELETE( xEventGroup );
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
/* We are about to access the kernel data group non-deterministically,
* thus we suspend the kernel data group.*/
vTaskSuspendAll();
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )
{
/* Unblock the task, returning 0 as the event list is being deleted
@ -669,8 +748,12 @@
configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( const ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );
vTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );
}
}
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
( void ) xTaskResumeAll();
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
}
event_groupsUNLOCK( pxEventBits );
#if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
{
@ -775,6 +858,7 @@
}
/*-----------------------------------------------------------*/
static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits,
const EventBits_t uxBitsToWaitFor,
const BaseType_t xWaitForAllBits )

View file

@ -0,0 +1,442 @@
# FreeRTOS SMP Granular Locks — Design
## Introduction
Historically, the SMP FreeRTOS kernel has implemented critical sections using a "Global Locking" approach, where all kernel data is protected by a single pair of spinlocks (the Task Lock and ISR Lock). Every critical section contests for this pair of spinlocks, even when the sections protect unrelated data.
This document describes the **"Dual Spinlock With Data Group Locking"** scheme that replaces global locking with per-data-group locks. The goal is to reduce contention between concurrent operations on independent kernel objects (queues, event groups, stream buffers, etc.) so that the SMP kernel scales better with the number of cores.
Granular locking is opt-in via the new port-layer configuration `portUSING_GRANULAR_LOCKS`. With it disabled the kernel behaves exactly as before; with it enabled the per-data-group locks described below are used.
Source-code changes are layered on the V11.1.0 release of the upstream FreeRTOS kernel.
### Status (as of this branch)
- Implemented in the kernel against `configNUMBER_OF_CORES > 1` builds.
- Verified on the Espressif ESP32 (Xtensa, dual-core) target via the ESP-IDF FreeRTOS unit-test suite.
- Other ports remain to be updated; reference port macros and the porting checklist are listed in the *Porting Interface* section.
# Data Groups
To make the spinlocks granular, FreeRTOS data will be organized into the following data groups, where each data group is protected by their own set of spinlocks.
- Kernel Data Group
- All data in `tasks.c` and all event lists (e.g., `xTasksWaitingToSend` and `xTasksWaitingToReceive` in the queue objects)
- Queue Data Group
- Each queue object (`Queue_t`) is its own data group (excluding the task lists)
- Event Group Data Group
- Each event group object (`EventGroup_t`) is its own data group (excluding the task lists)
- Stream Buffer Data Group
- Each stream buffer object (`StreamBuffer_t`) is its own data group (excluding the task lists)
- Timers
- All data in `timers.c` and timer objects, belong to the same Timer Data Group
- User/Port Data Groups
- The user and ports are free to organize their own data in to data groups of their choosing
# Dual Spinlock With Data Group Locking
The **"Dual Spinlock With Data Group Locking"** uses a pair of spinlocks to protect each data group (namely the `xTaskSpinlock` and `xISRSpinlock` spinlocks).
```c
typedef struct
{
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
portSPINLOCK_TYPE xTaskSpinlock;
portSPINLOCK_TYPE xISRSpinlock;
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
} xSomeDataGroup_t;
```
However, each data group must also allow for non-deterministic access with interrupts **enabled** by providing a pair of lock/unlock functions. These functions must block access to other tasks trying to access members of a data group and must pend access to ISRs trying to access members of the data group.
```c
static void prvLockSomeDataGroupForTasks( xSomeDataGroup_t * const pxSomDataGroup );
static void prvUnlockSomeDataGroupForTasks( xSomeDataGroup_t * const pxSomDataGroup );
```
In simple terms, the "Dual Spinlock With Data Group Locking" is an extension is the existing dual spinlock (i.e., Task and ISR spinlock) approach used in the SMP FreeRTOS kernel, but replicated across different data groups.
## Data Group Critical Sections (Granular Locks)
A critical section for a data group can be achieved as follows:
- When entering a data group critical section from a task
1. Disable interrupts
2. Take `xTaskSpinlock` of data group
3. Take `xISRSpinlock` of data group
4. Increment nesting count
- When entering data group critical section from an ISR
1. Disable interrupts
2. Take `xISRSpinlock` of data group
3. Increment nesting count
When exiting a data group critical section, the procedure is reversed. Furthermore, since yield requests are pended when inside a critical section, exiting a task critical section will also need to handle any pended yields.
- When exiting a data group critical section from a task
1. Release `xISRSpinlock` of data group
2. Release `xTaskSpinlock` of data group
3. Decrement nesting count
4. Reenable interrupts if nesting count is 0
5. Trigger yield if there is a yield pending
- When exiting data group critical section form an ISR
1. Release `xISRSpinlock` of data group
2. Decrement nesting count
3. Reenable interrupts if nesting count is 0
Entering multiple data group critical sections in a nested manner is permitted. This means, if a code path has already entered a critical section in data group A, it can then enter a critical section in data group B. This is analogous to nested critical sections. However, care must be taken to avoid deadlocks. This can be achieved by organizing data groups into a hierarchy, where a higher layer data group cannot nest into a lower one.
```
+-------------------+
| Kernel Data Group |
+-------------------+
+-------------------+ +---------------------------+ +--------------------------+ +------------------+
| Queue Data Groups | | Stream Buffer Data Groups | | Event Groups Data Groups | | Timer Data Group |
+-------------------+ +---------------------------+ +--------------------------+ +------------------+
+------------------------------------------------------------------------------------------------------+
| User Data Groups |
+------------------------------------------------------------------------------------------------------+
```
If nested locking only occurs from bottom up (e.g., User data group can nested into a Queue data group which in turn can nested into Kernel data group), then deadlocking will never occur.
## Data Group Locking
FreeRTOS does not permit walking linked lists while interrupts are disabled to ensure deterministic ISR latency. Therefore, each data group must provide a method of locking so that non-deterministic operations can be executed for a data group. While a data group is locked:
- Preemption is disabled for the current task
- Interrupts remained enabled
- The data group's `xTaskSpinlock` is taken to prevent tasks running on other cores from accessing the data group
- Any ISR that attempts to update the data group will have their access pended. These pended accesses will be handled on resumption
The logic of suspending a data group is analogous to the logic of `vTaskSuspendAll()`/`xTaskResumeAll()` and `prvLockQueue()`/`prvUnlockQueue()` in the existing SMP kernel.
The details of how ISR accesses are pended during suspension will be specific to each data group type, thus the implementation of the suspend/resumption functions also be specified to each data group type. However, the procedure for data group suspension and resumption will typically be as follows:
- Suspension
1. Disable preemption
2. Lock the data group
3. Set a suspension flag that indicates the data group is suspended
4. Unlock the data group, but keep holding `xTaskSpinlock`
- Resumption
1. Lock the data group
2. Clear the suspension flag
3. Handle all pended accesses from ISRs
4. Unlock the data group, thus releasing `xTaskSpinlock`
Locking multiple data groups in a nested manner is permitted, meaning if a code path has already locked data group A, it can then lock data group B. This is analogous to nested `vTaskSuspendAll()` calls. Similar to data group locking, deadlocks can be avoided by organizing data groups into a hierarchy.
## Thread Safety Check
Under SMP, there are four sources of concurrent access for a particular data group:
- Preempting task on the same core
- Preempting ISR on the same core
- Concurrent task on another core
- Concurrent ISR on another core
This section checks that the data group critical section and locking mechanisms mentioned, ensure thread safety from each concurrent source of access.
- Data Group Critical Section from tasks: Interrupts are disabled, `xTaskSpinlock` and `xISRSpinlock` are taken
- Task (same core): Context switch cannot occur because interrupts are disabled
- Task (other cores): The task will spin on `xTaskSpinlock`
- ISR (same core): Interrupts on the current core are disabled
- ISR (other cores): ISR will spin on `xISRSpinlock`
- Data Group Critical Sections from ISRs: Interrupts are disabled, `xISRSpinlock` is taken
- Task (same core): Context switch cannot occur because we are in an ISR
- Task (other cores): The task will spin on `xISRSpinlock`
- ISR (same core): Interrupts on the current core are disabled
- ISR (other cores): ISR will spin on `xISRSpinlock`
- Data Group Locking from tasks: Preemption is disabled, `xTaskSpinlock` is taken
- Task (same core): Context switch cannot occur because preemption is disabled
- Task (other cores): The task will spin on `xTaskSpinlock`
- ISR (same core): Critical section is entered because `xISRSpinlock` is not held, but access is pended
- ISR (other cores): Critical section is entered because `xISRSpinlock` is not held, but access is pended
# Public API Changes
To support **"Dual Spinlock With Data Group Locking"**, the following changes have been made to the public facing API. These changes are non-breaking, meaning that applications that can build against the existing SMP FreeRTOS kernel will still be able to build even with granular locking enabled (albeit less performant).
The following APIs have been added to enter/exit a critical section in a data group. This are called by FreeRTOS source code to mark critical sections in data groups. However, users can also create their own data groups and enter/exit critical sections in the same manner.
If granular locking is disabled, these macros simply revert to being the standard task enter/exit critical macros.
```c
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define data_groupENTER_CRITICAL() portENTER_CRITICAL_DATA_GROUP( ( portSPINLOCK_TYPE * ) pxTaskSpinlock, ( portSPINLOCK_TYPE * ) pxISRSpinlock )
#define data_groupENTER_CRITICAL_FROM_ISR() portENTER_CRITICAL_DATA_GROUP_FROM_ISR( ( portSPINLOCK_TYPE * ) pxISRSpinlock )
#define data_groupEXIT_CRITICAL() portEXIT_CRITICAL_DATA_GROUP( ( portSPINLOCK_TYPE * ) pxTaskSpinlock, ( portSPINLOCK_TYPE * ) pxISRSpinlock )
#define data_groupEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ) portEXIT_CRITICAL_DATA_GROUP_FROM_ISR( uxSavedInterruptStatus, ( portSPINLOCK_TYPE * ) pxISRSpinlock )
#else /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
#define data_groupENTER_CRITICAL() taskENTER_CRITICAL()
#define data_groupENTER_CRITICAL_FROM_ISR() taskENTER_CRITICAL_FROM_ISR()
#define data_groupEXIT_CRITICAL() taskEXIT_CRITICAL()
#define data_groupEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ) taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
```
In case of the kernel data group (tasks.c), the granular locking macros make use of the existing `vTaskEnter/ExitCritical<FromISR>()` functions to establish critical sections.
```c
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define kernelENTER_CRITICAL() vTaskEnterCritical()
#define kernelENTER_CRITICAL_FROM_ISR() vTaskEnterCriticalFromISR()
#define kernelEXIT_CRITICAL() vTaskExitCritical()
#define kernelEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ) vTaskExitCriticalFromISR( uxSavedInterruptStatus )
#else /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
#define kernelENTER_CRITICAL() taskENTER_CRITICAL()
#define kernelENTER_CRITICAL_FROM_ISR() taskENTER_CRITICAL_FROM_ISR()
#define kernelEXIT_CRITICAL() taskEXIT_CRITICAL()
#define kernelEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ) taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
```
The previous critical section macros, viz., `taskENTER/EXIT_CRITICAL()` are still provided and can be called by users. However, FreeRTOS source code will no longer call them. If called by the users, the port should implement a "user" data group. As a result, if an application previously relied on `taskENTER/EXIT_CRITICAL()` for thread safe access to some user data, the same code is still thread safe with granular locking enabled.
```c
#define taskENTER_CRITICAL() portENTER_CRITICAL()
#define taskEXIT_CRITICAL() portEXIT_CRITICAL()
#define taskENTER_CRITICAL_FROM_ISR() portENTER_CRITICAL_FROM_ISR()
#define taskEXIT_CRITICAL_FROM_ISR( x ) portEXIT_CRITICAL_FROM_ISR( x )
```
# Porting Interface
To support **"Dual Spinlock With Data Group Locking"**, ports will need to provide the following macro definitions
## Port Config
The ports will need to provide the following port configuration macros
```c
#define portUSING_GRANULAR_LOCKS 1 // Enables usage of granular locks
#define portCRITICAL_NESTING_IN_TCB 0 // Disable critical nesting in TCB. Ports will need to track their own critical nesting
```
## Spinlocks
Ports will need to provide the following spinlock related macros macros
```c
/*
Data type for the port's implementation of a spinlock
*/
#define portSPINLOCK_TYPE port_spinlock_t
```
Macros are provided for the spinlocks for initializing them either statically or dynamically. This is reflected in the macros API pattern.
```c
#define portINIT_SPINLOCK( pxSpinlock ) _port_spinlock_init( pxSpinlock )
#define portINIT__SPINLOCK_STATIC PORT_SPINLOCK_STATIC_INIT
```
## Critical Section Macros
The port will need to provide implementations for macros to enter/exit a data group critical section according the procedures described above. Typical implementations of each macro is demonstrated below:
```c
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#define portENTER_CRITICAL_DATA_GROUP( pxTaskSpinlock, pxISRSpinlock ) vPortEnterCriticalDataGroup( pxTaskSpinlock, pxISRSpinlock )
#define portENTER_CRITICAL_DATA_GROUP_FROM_ISR( pxISRSpinlock ) uxPortEnterCriticalDataGroupFromISR( pxISRSpinlock )
#define portEXIT_CRITICAL_DATA_GROUP( pxTaskSpinlock, pxISRSpinlock ) vPortExitCriticalDataGroup( pxTaskSpinlock, pxISRSpinlock )
#define portEXIT_CRITICAL_DATA_GROUP_FROM_ISR( x, pxISRSpinlock ) vPortExitCriticalDataGroupFromISR( x, pxISRSpinlock )
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
```
Example implementation of `portENTER_CRITICAL_DATA_GROUP( pxTaskSpinlock, pxISRSpinlock )` and `portEXIT_CRITICAL_DATA_GROUP( pxTaskSpinlock, pxISRSpinlock )`. Note that:
- `pxTaskSpinlock` is made optional in case users want to create their own data groups that are protected only be a single lock.
- The kernel implements the `xTaskUnlockCanYield()` function to indicate whether an yield should occur when a critical section exits. This function takes into account whether there are any pending yields and whether preemption is currently disabled.
```c
void vPortEnterCriticalDataGroup( port_spinlock_t *pxTaskSpinlock, port_spinlock_t *pxISRSpinlock )
{
portDISABLE_INTERRUPTS();
BaseType_t xCoreID = xPortGetCoreID();
/* Task spinlock is optional and is always taken first */
if( pxTaskSpinlock != NULL )
{
vPortSpinlockTake( pxTaskSpinlock, portMUX_NO_TIMEOUT );
uxCriticalNesting[ xCoreID ]++;
}
/* ISR spinlock must always be provided */
vPortSpinlockTake( pxISRSpinlock, portMUX_NO_TIMEOUT );
uxCriticalNesting[ xCoreID ]++;
}
void vPortExitCriticalDataGroup( port_spinlock_t *pxTaskSpinlock, port_spinlock_t *pxISRSpinlock )
{
BaseType_t xCoreID = xPortGetCoreID();
BaseType_t xYieldCurrentTask;
configASSERT( uxCriticalNesting[ xCoreID ] > 0U );
/* Get the xYieldPending stats inside the critical section. */
xYieldCurrentTask = xTaskUnlockCanYield();
/* ISR spinlock must always be provided */
vPortSpinlockRelease( pxISRSpinlock );
uxCriticalNesting[ xCoreID ]--;
/* Task spinlock is optional and is always taken first */
if( pxTaskSpinlock != NULL )
{
vPortSpinlockRelease( pxTaskSpinlock);
uxCriticalNesting[ xCoreID ]--;
}
assert(uxCriticalNesting[ xCoreID ] >= 0);
if( uxCriticalNesting[ xCoreID ] == 0 )
{
portENABLE_INTERRUPTS();
/* When a task yields in a critical section it just sets xYieldPending to
* true. So now that we have exited the critical section check if xYieldPending
* is true, and if so yield. */
if( xYieldCurrentTask != pdFALSE )
{
portYIELD();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
}
```
Example implementation of `portENTER_CRITICAL_DATA_GROUP_FROM_ISR( pxISRSpinlock )` and `portEXIT_CRITICAL_DATA_GROUP_FROM_ISR( x, pxISRSpinlock )`. Note that only `pxISRSpinlock` needs to be provided since ISR critical sections take a single lock.
```c
UBaseType_t uxPortLockDataGroupFromISR( port_spinlock_t *pxISRSpinlock )
{
UBaseType_t uxSavedInterruptStatus = 0;
uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
vPortSpinlockTake( pxISRSpinlock, portMUX_NO_TIMEOUT );
uxCriticalNesting[xPortGetCoreID()]++;
return uxSavedInterruptStatus;
}
```c
void vPortUnlockDataGroupFromISR( UBaseType_t uxSavedInterruptStatus, port_spinlock_t *pxISRSpinlock )
{
BaseType_t xCoreID = xPortGetCoreID();
vPortSpinlockRelease( pxISRSpinlock );
uxCriticalNesting[ xCoreID ]--;
assert(uxCriticalNesting[ xCoreID ] >= 0);
if( uxCriticalNesting[ xCoreID ] == 0 )
{
portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
}
}
```
# Source Specific Changes
- Added a `xTaskSpinlock` and `xISRSpinlock` to the data structures of each data group
- All calls to `taskENTER/EXIT_CRITICAL[_FROM_ISR]()` have been replaced with `data_groupENTER/EXIT_CRITICAL[_FROM_ISR]()`.
- Added `xTaskUnlockCanYield()` which indicates whether a yield should occur when exiting a critical (i.e., unlocking a data group). Yields should not occur if preemption is disabled (such as when exiting a critical section inside a suspension block).
## Tasks (Kernel Data Group)
- Some functions are called from nested critical sections of other data groups, thus an extra critical section call needs to be added to lock/unlock the kernel data group:
- `vTaskInternalSetTimeOutState()`
- `xTaskIncrementTick()`
- `vTaskSwitchContext()`
- `xTaskRemoveFromEventList()`
- `vTaskInternalSetTimeOutState()`
- `eTaskConfirmSleepModeStatus()`
- `xTaskPriorityDisinherit()`
- `pvTaskIncrementMutexHeldCount()`
- Some functions are called from nested suspension blocks of other data gropus, thus an extra suspend/resume call need to be added:
- `vTaskPlaceOnEventList()`
- `vTaskPlaceOnUnorderedEventList()`
- `vTaskPlaceOnEventListRestricted()`
- `prvCheckForRunStateChange()` has been removed
- Updated `vTaskSuspendAll()` and `xTaskResumeAll()`
- Now holds the `xTaskSpinlock` during kernel suspension
- Also increments/decrements `xPreemptionDisable` to prevent yield from occuring when exiting a critical section from inside a kernel suspension block.
## Queue
- Added `queueLOCK()` and `queueUNLOCK()`
- If granular locks are disabled, reverts to the previous `prvLockQueue()` and `prvUnlockQueue()`
- If granular locks are enabled, will lock/unlock the queue data group for tasks
## Event Groups
- Added `eventLOCK()` and `eventUNLOCK()`
- If granular locks are disabled, reverts to the previous `vTaskSuspendAll()` and `xTaskResumeAll()` calls
- If granular locks are enabled, will lock/unlock the event groups data group for tasks
- `xEventGroupSetBits()` and `vEventGroupDelete()` will manually walk the task lists (which belong to the kernel data group). Thus, an extra `vTaskSuspendAll()`/`xTaskResumeAll()` is added to ensure that the kernel data group is suspended while walking those tasks lists.
## Stream Buffer
- Added `sbLOCK()` and `sbUNLOCK()`
- If granular locks are disabled, reverts to the previous `vTaskSuspendAll()` and `xTaskResumeAll()` calls
- If granular locks are enabled, will lock/unlock the stream buffer data group for tasks
## Timers
- Timers don't have a lock/unlock function. The existing `vTaskSuspendAll()`/`xTaskResumeAll()` calls are valid as they rely on freezing the tick count which is part of the kernel data group.
# Prerequisite Refactoring
A number of refactoring commits have been added to make the addition of granular locking changes simpler:
1. Move critical sections inside `xTaskPriorityInherit()`
Currently, `xTaskPriorityInherit()` is called with wrapping critical sections. The critical sections have now be moved inside the function so that they have access to the kernel data group's spinlocks.
2. Move critical section into `vTaskPriorityDisinheritAfterTimeout()`
Currently, `vTaskPriorityDisinheritAfterTimeout()` is called wrapping critical sections, where the highest priority waiting task is separately obtained via `prvGetDisinheritPriorityAfterTimeout()`. The critical section and checking of the highest priority have been all been moved into `vTaskPriorityDisinheritAfterTimeout()` as all of these operations access the kernel data group.
3. Allow `vTaskPreemptionEnable()` to be nested
Currently, nested calls of `vTaskPreemptionEnable()` is not supported. However, nested calls are required for granular locking due the occurrence of nested suspension across multiple data groups.
Thus, `vTaskPreemptionEnable()` has been updated to support nested calls. This is done by changing `xPreemptionDisable` to a count, where a non-zero count means that the current task cannot be preempted.
# TCB Locks
When `portUSING_GRANULAR_LOCKS == 1` and `configNUMBER_OF_CORES > 1`, each TCB carries an additional spinlock (`xTCBSpinlock`) that guards TCB-specific fields touched by preemption-disable / preemption-enable paths (`uxPreemptionDisable`, `uxDeferredStateChange`). The TCB lock is acquired via `vTaskTCBEnterCritical()` / `vTaskTCBExitCritical()` and lives below the kernel data group in the lock hierarchy. Using a per-TCB lock here avoids contention on the kernel data-group lock for the common case where a task only needs to inspect or update its own preemption-disable state.
# Deferred State Changes
Calls that change a task's run state (`vTaskDelete()`, `vTaskSuspend()`) cannot run their full effect while the target task has preemption disabled, because doing so would yield from a TCB whose owner asked not to be preempted. Granular locks introduce `uxDeferredStateChange` on the TCB to record the requested transition (`tskDEFERRED_DELETION`, `tskDEFERRED_SUSPENSION`). The deferred work is then performed at the next `xTaskPreemptionEnableWithYieldStatus()` once the preemption-disable count drops to zero, outside the TCB critical section so that any context switch and the post-state-change ready-list manipulation happen in a regular kernel critical section.
# ISR-Only Kernel Critical Section Helpers
`prvKernelEnterISROnlyCritical()` / `prvKernelExitISROnlyCritical()` are internal helpers used to enter the kernel data group from a context that is already inside a surrounding data-group critical section (and therefore already has interrupts disabled and holds the surrounding data group's ISR spinlock). Unlike `kernelENTER_CRITICAL_FROM_ISR()` / `kernelEXIT_CRITICAL_FROM_ISR()`, they do not touch the interrupt state — the outer section owns that — and they only acquire/release the kernel ISR spinlock and adjust the per-core kernel critical-nesting count. `xTaskRemoveFromEventList()` and `xTaskRemoveFromEventListFromISR()` use these helpers so that callers walking an event list while holding e.g. a queue's spinlock do not need to know whether they are in task or ISR context.
# Event-List TOCTOU Considerations
Under granular locks, an event list (`xTasksWaitingToSend`, `xTasksWaitingToReceive`, queue-set container waiting list) is protected by the kernel ISR spinlock, while the surrounding queue / event-group critical section only holds that data group's own spinlocks. The kernel API `xTaskRemoveFromEventList()` re-checks list emptiness under the kernel ISR lock before removing an entry, so callers must call it directly rather than gating it on an outer `listLIST_IS_EMPTY()` check. The outer check can race with a concurrent removal from the tick ISR on another core; the inner re-check is authoritative.
The only places where an outer empty-check is retained on purpose are the `prvUnlockQueue()` drain loops, where the surrounding queue ISR spinlock prevents concurrent additions to the wait list and the outer check exists for early loop termination, not for race avoidance.
# Known Limitations
- Under `configRUN_MULTIPLE_PRIORITIES == 1`, the relaxed lock hierarchy means that an unblock triggered by a tick on one core may take effect on the unblocking core a little later than under global locking. The single-highest-priority invariant continues to hold under `configRUN_MULTIPLE_PRIORITIES == 0`.
- The design has been exercised on the Xtensa SMP port (ESP32). Other ports — including the upstream POSIX, ARMv7-M and ARMv8-M SMP ports — need to provide the spinlock, critical-nesting, and yield primitives listed in *Porting Interface* before they can opt in.
# Performance Metrics
Performance numbers will be added once the upstream port reference has the porting checklist completed. Initial measurements on ESP32 (Xtensa, dual-core, 240 MHz) show reduced contention on multi-queue workloads where unrelated tasks send to / receive from distinct queues concurrently; full benchmark methodology and numbers will be published with this PR's review pass.

View file

@ -369,6 +369,10 @@
#define portCRITICAL_NESTING_IN_TCB 0
#endif
#ifndef portUSING_GRANULAR_LOCKS
#define portUSING_GRANULAR_LOCKS 0
#endif
#ifndef configMAX_TASK_NAME_LEN
#define configMAX_TASK_NAME_LEN 16
#endif
@ -458,44 +462,68 @@
#ifndef portRELEASE_TASK_LOCK
#if ( configNUMBER_OF_CORES == 1 )
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) || ( configNUMBER_OF_CORES == 1 ) )
#define portRELEASE_TASK_LOCK( xCoreID )
#else
#error portRELEASE_TASK_LOCK is required in SMP
#error portRELEASE_TASK_LOCK is required in SMP without granular locking feature enabled
#endif
#endif /* portRELEASE_TASK_LOCK */
#ifndef portGET_TASK_LOCK
#if ( configNUMBER_OF_CORES == 1 )
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) || ( configNUMBER_OF_CORES == 1 ) )
#define portGET_TASK_LOCK( xCoreID )
#else
#error portGET_TASK_LOCK is required in SMP
#error portGET_TASK_LOCK is required in SMP without granular locking feature enabled
#endif
#endif /* portGET_TASK_LOCK */
#ifndef portRELEASE_ISR_LOCK
#if ( configNUMBER_OF_CORES == 1 )
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) || ( configNUMBER_OF_CORES == 1 ) )
#define portRELEASE_ISR_LOCK( xCoreID )
#else
#error portRELEASE_ISR_LOCK is required in SMP
#error portRELEASE_ISR_LOCK is required in SMP without granular locking feature enabled
#endif
#endif /* portRELEASE_ISR_LOCK */
#ifndef portGET_ISR_LOCK
#if ( configNUMBER_OF_CORES == 1 )
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) || ( configNUMBER_OF_CORES == 1 ) )
#define portGET_ISR_LOCK( xCoreID )
#else
#error portGET_ISR_LOCK is required in SMP
#error portGET_ISR_LOCK is required in SMP without granular locking feature enabled
#endif
#endif /* portGET_ISR_LOCK */
#ifndef portRELEASE_SPINLOCK
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#error portRELEASE_SPINLOCK is required for SMP with granular locking feature enabled
#endif
#endif
#ifndef portGET_SPINLOCK
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#error portGET_SPINLOCK is required for SMP with granular locking feature enabled
#endif
#endif
#ifndef portCHECK_IF_IN_ISR
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#error portCHECK_IF_IN_ISR is required for granular locking
#endif
#endif
#ifndef portENTER_CRITICAL_FROM_ISR
#if ( configNUMBER_OF_CORES > 1 )
@ -512,6 +540,30 @@
#endif
#ifndef portSPINLOCK_TYPE
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#error portSPINLOCK_TYPE is required for SMP with granular locking feature enabled
#endif
#endif
#ifndef portINIT_SPINLOCK
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#error portINIT_SPINLOCK is required for SMP with granular locking feature enabled
#endif
#endif
#ifndef portINIT_SPINLOCK_STATIC
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#error portINIT_SPINLOCK_STATIC is required for SMP with granular locking feature enabled
#endif
#endif
#ifndef configUSE_CORE_AFFINITY
#define configUSE_CORE_AFFINITY 0
#endif /* configUSE_CORE_AFFINITY */
@ -2098,10 +2150,18 @@
#define traceENTER_xTaskRemoveFromEventList( pxEventList )
#endif
#ifndef traceENTER_xTaskRemoveFromEventListFromISR
#define traceENTER_xTaskRemoveFromEventListFromISR( pxEventList )
#endif
#ifndef traceRETURN_xTaskRemoveFromEventList
#define traceRETURN_xTaskRemoveFromEventList( xReturn )
#endif
#ifndef traceRETURN_xTaskRemoveFromEventListFromISR
#define traceRETURN_xTaskRemoveFromEventListFromISR( xReturn )
#endif
#ifndef traceENTER_vTaskRemoveFromUnorderedEventList
#define traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue )
#endif
@ -2890,10 +2950,6 @@
#error configUSE_MUTEXES must be set to 1 to use recursive mutexes
#endif
#if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configUSE_TASK_PREEMPTION_DISABLE != 0 ) )
#error configRUN_MULTIPLE_PRIORITIES must be set to 1 to use task preemption disable
#endif
#if ( ( configUSE_PREEMPTION == 0 ) && ( configUSE_TASK_PREEMPTION_DISABLE != 0 ) )
#error configUSE_PREEMPTION must be set to 1 to use task preemption disable
#endif
@ -2919,11 +2975,16 @@
/* Either variables of tick type cannot be read atomically, or
* portTICK_TYPE_IS_ATOMIC was not set - map the critical sections used when
* the tick count is returned to the standard critical section macros. */
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#define portTICK_TYPE_ENTER_CRITICAL() kernelENTER_CRITICAL()
#define portTICK_TYPE_EXIT_CRITICAL() kernelEXIT_CRITICAL()
#else /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
#define portTICK_TYPE_ENTER_CRITICAL() portENTER_CRITICAL()
#define portTICK_TYPE_EXIT_CRITICAL() portEXIT_CRITICAL()
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
#define portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR() portSET_INTERRUPT_MASK_FROM_ISR()
#define portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( x ) portCLEAR_INTERRUPT_MASK_FROM_ISR( ( x ) )
#else
#else /* if ( portTICK_TYPE_IS_ATOMIC == 0 ) */
/* The tick type can be read atomically, so critical sections used when the
* tick count is returned can be defined away. */
@ -3183,7 +3244,13 @@ typedef struct xSTATIC_TCB
#endif
uint8_t ucDummy7[ configMAX_TASK_NAME_LEN ];
#if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
BaseType_t xDummy25;
UBaseType_t xDummy25;
#endif
#if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
BaseType_t xDummy26;
#endif
#if ( portUSING_GRANULAR_LOCKS == 1 )
portSPINLOCK_TYPE xDummy27;
#endif
#if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
void * pxDummy8;
@ -3265,6 +3332,10 @@ typedef struct xSTATIC_QUEUE
UBaseType_t uxDummy8;
uint8_t ucDummy9;
#endif
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
portSPINLOCK_TYPE xDummySpinlock[ 2 ];
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
} StaticQueue_t;
typedef StaticQueue_t StaticSemaphore_t;
@ -3294,6 +3365,10 @@ typedef struct xSTATIC_EVENT_GROUP
#if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
uint8_t ucDummy4;
#endif
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
portSPINLOCK_TYPE xDummySpinlock[ 2 ];
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
} StaticEventGroup_t;
/*
@ -3349,6 +3424,9 @@ typedef struct xSTATIC_STREAM_BUFFER
void * pvDummy5[ 2 ];
#endif
UBaseType_t uxDummy6;
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
portSPINLOCK_TYPE xDummySpinlock[ 2 ];
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
} StaticStreamBuffer_t;
/* Message buffers are built on stream buffers. */

View file

@ -287,6 +287,138 @@ typedef enum
/* Checks if core ID is valid. */
#define taskVALID_CORE_ID( xCoreID ) ( ( ( ( ( BaseType_t ) 0 <= ( xCoreID ) ) && ( ( xCoreID ) < ( BaseType_t ) configNUMBER_OF_CORES ) ) ) ? ( pdTRUE ) : ( pdFALSE ) )
/**
* task. h
*
* Macro to enter a data group critical section.
*
* \defgroup taskDATA_GROUP_ENTER_CRITICAL taskDATA_GROUP_ENTER_CRITICAL
* \ingroup GranularLocks
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
/* Using a function implementation now since the data group entering critical
* section needs to check for run state change. */
void taskDataGroupEnterCritical( portSPINLOCK_TYPE * pxTaskSpinlock,
portSPINLOCK_TYPE * pxISRSpinlock );
#define taskDATA_GROUP_ENTER_CRITICAL taskDataGroupEnterCritical
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
/**
* task. h
*
* Macro to enter a data group critical section from an interrupt.
*
* \defgroup taskDATA_GROUP_ENTER_CRITICAL_FROM_ISR taskDATA_GROUP_ENTER_CRITICAL_FROM_ISR
* \ingroup GranularLocks
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define taskDATA_GROUP_ENTER_CRITICAL_FROM_ISR( pxISRSpinlock, puxSavedInterruptStatus ) \
do { \
*( puxSavedInterruptStatus ) = portSET_INTERRUPT_MASK_FROM_ISR(); \
{ \
const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID(); \
/* Take the ISR spinlock */ \
portGET_SPINLOCK( xCoreID, ( portSPINLOCK_TYPE * ) ( pxISRSpinlock ) ); \
/* Increment the critical nesting count */ \
portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID ); \
} \
} while( 0 )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
/**
* task. h
*
* Macro to exit a data group critical section.
*
* \defgroup taskDATA_GROUP_EXIT_CRITICAL taskDATA_GROUP_EXIT_CRITICAL
* \ingroup GranularLocks
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define taskDATA_GROUP_EXIT_CRITICAL( pxTaskSpinlock, pxISRSpinlock ) \
do { \
const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID(); \
configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U ); \
/* Release the ISR spinlock */ \
portRELEASE_SPINLOCK( xCoreID, ( portSPINLOCK_TYPE * ) ( pxISRSpinlock ) ); \
/* Release the task spinlock */ \
portRELEASE_SPINLOCK( xCoreID, ( portSPINLOCK_TYPE * ) ( pxTaskSpinlock ) ); \
/* Decrement the critical nesting count */ \
portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID ); \
/* Enable interrupts only if the critical nesting count is 0 */ \
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0 ) \
{ \
portENABLE_INTERRUPTS(); \
} \
else \
{ \
mtCOVERAGE_TEST_MARKER(); \
} \
/* Re-enable preemption */ \
( void ) xTaskPreemptionEnableWithYieldStatus( NULL ); \
} while( 0 )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
/**
* task. h
*
* Macro to exit a data group critical section from an interrupt.
*
* \defgroup taskDATA_GROUP_EXIT_CRITICAL_FROM_ISR taskDATA_GROUP_EXIT_CRITICAL_FROM_ISR
* \ingroup GranularLocks
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define taskDATA_GROUP_EXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxISRSpinlock ) \
do { \
const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID(); \
configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U ); \
/* Decrement the critical nesting count */ \
portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID ); \
/* Release the ISR spinlock */ \
portRELEASE_SPINLOCK( xCoreID, ( portSPINLOCK_TYPE * ) ( pxISRSpinlock ) ); \
if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0 ) \
{ \
portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
} \
} while( 0 )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
/**
* task. h
*
* Macros to lock a data group (task-level lock only).
*
* \defgroup taskDATA_GROUP_LOCK taskDATA_GROUP_LOCK
* \ingroup GranularLocks
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define taskDATA_GROUP_LOCK( pxTaskSpinlock ) \
do { \
/* Disable preemption while holding the task spinlock. */ \
vTaskPreemptionDisable( NULL ); \
{ \
portGET_SPINLOCK( portGET_CORE_ID(), ( portSPINLOCK_TYPE * ) ( pxTaskSpinlock ) ); \
} \
} while( 0 )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
/**
* task. h
*
* Macros to unlock a data group (task-level lock only).
*
* \defgroup taskDATA_GROUP_UNLOCK taskDATA_GROUP_UNLOCK
* \ingroup GranularLocks
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
/* Release the task spinlock and re-enable preemption.
* Returns the yield status reported by xTaskPreemptionEnableWithYieldStatus(). */
#define taskDATA_GROUP_UNLOCK( pxTaskSpinlock ) \
( portRELEASE_SPINLOCK( portGET_CORE_ID(), ( portSPINLOCK_TYPE * ) ( pxTaskSpinlock ) ), \
xTaskPreemptionEnableWithYieldStatus( NULL ) )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
/*-----------------------------------------------------------
* TASK CREATION API
*----------------------------------------------------------*/
@ -1512,6 +1644,23 @@ BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume ) PRIVILEGED_FUNCTION;
void vTaskPreemptionEnable( const TaskHandle_t xTask );
#endif
#if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
/*
* THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS ONLY
* INTENDED FOR USE WHEN IMPLEMENTING A PORT OF THE SCHEDULER AND IS
* AN INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
*
* @param xTask The handle of the task to enable preemption. Passing NULL
* enables preemption for the calling task.
*
* @return pdTRUE if enabling preemption for the task resulted in a context
* switch, otherwise pdFALSE. This is used by the scheduler to determine if a
* context switch may be required following the enable.
*/
BaseType_t xTaskPreemptionEnableWithYieldStatus( const TaskHandle_t xTask );
#endif
/*-----------------------------------------------------------
* SCHEDULER CONTROL
*----------------------------------------------------------*/
@ -3592,6 +3741,8 @@ void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
* Removes a task from both the specified event list and the list of blocked
* tasks, and places it on a ready queue.
*
* Do not call this function from an ISR context. Call xTaskRemoveFromEventListFromISR() instead.
*
* xTaskRemoveFromEventList()/vTaskRemoveFromUnorderedEventList() will be called
* if either an event occurs to unblock a task, or the block timeout period
* expires.
@ -3608,6 +3759,23 @@ void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
* making the call, otherwise pdFALSE.
*/
BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList ) PRIVILEGED_FUNCTION;
/*
* THIS FUNCTION MUST NOT BE USED FROM APPLICATION CODE. IT IS ONLY
* INTENDED FOR USE WHEN IMPLEMENTING A PORT OF THE SCHEDULER AND IS
* AN INTERFACE WHICH IS FOR THE EXCLUSIVE USE OF THE SCHEDULER.
*
* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED.
*
* Removes a task from both the specified event list and the list of blocked
* tasks, and places it on a ready queue. This function is the ISR-safe version
* of xTaskRemoveFromEventList().
*
* @return pdTRUE if the task being removed has a higher priority than the task
* making the call, otherwise pdFALSE.
*/
BaseType_t xTaskRemoveFromEventListFromISR( const List_t * const pxEventList ) PRIVILEGED_FUNCTION;
void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
const TickType_t xItemValue ) PRIVILEGED_FUNCTION;
@ -3786,6 +3954,14 @@ void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut ) PRIVILEGED_FUNC
void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus );
#endif
/*
* Checks whether a yield is required after portUNLOCK_DATA_GROUP() returns.
* To be called while data group is locked.
*/
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
BaseType_t xTaskUnlockCanYield( void );
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
#if ( portUSING_MPU_WRAPPERS == 1 )
/*

518
queue.c

File diff suppressed because it is too large Load diff

View file

@ -58,6 +58,39 @@
#error INCLUDE_xTaskGetCurrentTaskHandle must be set to 1 to build stream_buffer.c
#endif
/*
* Macros to mark the start and end of a critical code region.
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define sbENTER_CRITICAL( pxStreamBuffer ) taskDATA_GROUP_ENTER_CRITICAL( &( ( pxStreamBuffer )->xTaskSpinlock ), &( ( pxStreamBuffer )->xISRSpinlock ) )
#define sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer, puxSavedInterruptStatus ) taskDATA_GROUP_ENTER_CRITICAL_FROM_ISR( &( ( pxStreamBuffer )->xISRSpinlock ), puxSavedInterruptStatus )
#define sbEXIT_CRITICAL( pxStreamBuffer ) taskDATA_GROUP_EXIT_CRITICAL( &( ( pxStreamBuffer )->xTaskSpinlock ), &( ( pxStreamBuffer )->xISRSpinlock ) )
#define sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer ) taskDATA_GROUP_EXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, &( ( pxStreamBuffer )->xISRSpinlock ) )
#else /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
#define sbENTER_CRITICAL( pxStreamBuffer ) taskENTER_CRITICAL()
#define sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer, puxSavedInterruptStatus ) do { *( puxSavedInterruptStatus ) = taskENTER_CRITICAL_FROM_ISR(); } while( 0 )
#define sbEXIT_CRITICAL( pxStreamBuffer ) taskEXIT_CRITICAL()
#define sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer ) taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
/*
* Macro used to lock and unlock a stream buffer. When a task locks a stream
* buffer, the task will have thread safe non-deterministic access to the stream
* buffer.
* - Concurrent access from other tasks will be blocked by the xTaskSpinlock
* - Concurrent access from ISRs will be pended
*
* When the task unlocks the stream buffer, all pended access attempts are handled.
*/
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#define sbLOCK( pxStreamBuffer ) taskDATA_GROUP_LOCK( &( ( pxStreamBuffer )->xTaskSpinlock ) )
#define sbUNLOCK( pxStreamBuffer ) taskDATA_GROUP_UNLOCK( &( ( pxStreamBuffer )->xTaskSpinlock ) )
#else /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
#define sbLOCK( pxStreamBuffer ) vTaskSuspendAll()
#define sbUNLOCK( pxStreamBuffer ) ( void ) xTaskResumeAll()
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
/* If the user has not provided application specific Rx notification macros,
* or #defined the notification macros away, then provide default implementations
* that uses task notifications. */
@ -65,7 +98,7 @@
#define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
do \
{ \
vTaskSuspendAll(); \
sbLOCK( pxStreamBuffer ); \
{ \
if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
{ \
@ -76,7 +109,7 @@
( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
} \
} \
( void ) xTaskResumeAll(); \
sbUNLOCK( pxStreamBuffer ); \
} while( 0 )
#endif /* sbRECEIVE_COMPLETED */
@ -105,7 +138,7 @@
do { \
UBaseType_t uxSavedInterruptStatus; \
\
uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer, &uxSavedInterruptStatus ); \
{ \
if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
{ \
@ -117,7 +150,7 @@
( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
} \
} \
taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer ); \
} while( 0 )
#endif /* sbRECEIVE_COMPLETED_FROM_ISR */
@ -145,7 +178,7 @@
*/
#ifndef sbSEND_COMPLETED
#define sbSEND_COMPLETED( pxStreamBuffer ) \
vTaskSuspendAll(); \
sbLOCK( pxStreamBuffer ); \
{ \
if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
{ \
@ -156,7 +189,7 @@
( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
} \
} \
( void ) xTaskResumeAll()
sbUNLOCK( pxStreamBuffer )
#endif /* sbSEND_COMPLETED */
/* If user has provided a per-instance send completed callback, then
@ -184,7 +217,7 @@
do { \
UBaseType_t uxSavedInterruptStatus; \
\
uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer, &uxSavedInterruptStatus ); \
{ \
if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
{ \
@ -196,7 +229,7 @@
( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
} \
} \
taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer ); \
} while( 0 )
#endif /* sbSEND_COMPLETE_FROM_ISR */
@ -249,6 +282,10 @@ typedef struct StreamBufferDef_t
StreamBufferCallbackFunction_t pxReceiveCompletedCallback; /* Optional callback called on receive complete. sbRECEIVE_COMPLETED is called if this is NULL. */
#endif
UBaseType_t uxNotificationIndex; /* The index we are using for notification, by default tskDEFAULT_INDEX_TO_NOTIFY. */
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
portSPINLOCK_TYPE xTaskSpinlock;
portSPINLOCK_TYPE xISRSpinlock;
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
} StreamBuffer_t;
/*
@ -336,6 +373,7 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
/*-----------------------------------------------------------*/
#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
size_t xTriggerLevelBytes,
@ -414,6 +452,13 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
pxSendCompletedCallback,
pxReceiveCompletedCallback );
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
{
portINIT_SPINLOCK( &( ( ( StreamBuffer_t * ) pvAllocatedMemory )->xTaskSpinlock ) );
portINIT_SPINLOCK( &( ( ( StreamBuffer_t * ) pvAllocatedMemory )->xISRSpinlock ) );
}
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pvAllocatedMemory ), xStreamBufferType );
}
else
@ -508,6 +553,13 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
* again. */
pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
{
portINIT_SPINLOCK( &( pxStreamBuffer->xTaskSpinlock ) );
portINIT_SPINLOCK( &( pxStreamBuffer->xISRSpinlock ) );
}
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xStreamBufferType );
/* MISRA Ref 11.3.1 [Misaligned access] */
@ -623,7 +675,7 @@ BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
#endif
/* Can only reset a message buffer if there are no tasks blocked on it. */
taskENTER_CRITICAL();
sbENTER_CRITICAL( pxStreamBuffer );
{
if( ( pxStreamBuffer->xTaskWaitingToReceive == NULL ) && ( pxStreamBuffer->xTaskWaitingToSend == NULL ) )
{
@ -653,7 +705,7 @@ BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
xReturn = pdPASS;
}
}
taskEXIT_CRITICAL();
sbEXIT_CRITICAL( pxStreamBuffer );
traceRETURN_xStreamBufferReset( xReturn );
@ -881,7 +933,7 @@ size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
{
/* Wait until the required number of bytes are free in the message
* buffer. */
taskENTER_CRITICAL();
sbENTER_CRITICAL( pxStreamBuffer );
{
xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
@ -896,15 +948,19 @@ size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
}
else
{
taskEXIT_CRITICAL();
sbEXIT_CRITICAL( pxStreamBuffer );
break;
}
}
taskEXIT_CRITICAL();
sbEXIT_CRITICAL( pxStreamBuffer );
traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
( void ) xTaskNotifyWaitIndexed( pxStreamBuffer->uxNotificationIndex, ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
sbENTER_CRITICAL( pxStreamBuffer );
{
pxStreamBuffer->xTaskWaitingToSend = NULL;
}
sbEXIT_CRITICAL( pxStreamBuffer );
} while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
}
else
@ -1099,7 +1155,7 @@ size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
{
/* Checking if there is data and clearing the notification state must be
* performed atomically. */
taskENTER_CRITICAL();
sbENTER_CRITICAL( pxStreamBuffer );
{
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
@ -1124,14 +1180,18 @@ size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
mtCOVERAGE_TEST_MARKER();
}
}
taskEXIT_CRITICAL();
sbEXIT_CRITICAL( pxStreamBuffer );
if( xBytesAvailable <= xBytesToStoreMessageLength )
{
/* Wait for data to be available. */
traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
( void ) xTaskNotifyWaitIndexed( pxStreamBuffer->uxNotificationIndex, ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
sbENTER_CRITICAL( pxStreamBuffer );
{
pxStreamBuffer->xTaskWaitingToReceive = NULL;
}
sbEXIT_CRITICAL( pxStreamBuffer );
/* Recheck the data available after blocking. */
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
@ -1421,7 +1481,7 @@ BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer
/* MISRA Ref 4.7.1 [Return value shall be checked] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
/* coverity[misra_c_2012_directive_4_7_violation] */
uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer, &uxSavedInterruptStatus );
{
if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
{
@ -1438,7 +1498,7 @@ BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer
xReturn = pdFALSE;
}
}
taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer );
traceRETURN_xStreamBufferSendCompletedFromISR( xReturn );
@ -1460,7 +1520,7 @@ BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuf
/* MISRA Ref 4.7.1 [Return value shall be checked] */
/* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
/* coverity[misra_c_2012_directive_4_7_violation] */
uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
sbENTER_CRITICAL_FROM_ISR( pxStreamBuffer, &uxSavedInterruptStatus );
{
if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
{
@ -1477,7 +1537,7 @@ BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuf
xReturn = pdFALSE;
}
}
taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
sbEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus, pxStreamBuffer );
traceRETURN_xStreamBufferReceiveCompletedFromISR( xReturn );
@ -1633,6 +1693,13 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
StreamBufferCallbackFunction_t pxSendCompletedCallback,
StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
{
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
/* Preserve the embedded spinlocks across the memset; the reset
* path runs with xTaskSpinlock held by the caller. */
portSPINLOCK_TYPE xTaskSpinlock = pxStreamBuffer->xTaskSpinlock;
portSPINLOCK_TYPE xISRSpinlock = pxStreamBuffer->xISRSpinlock;
#endif
/* Assert here is deliberately writing to the entire buffer to ensure it can
* be written to without generating exceptions, and is setting the buffer to a
* known value to assist in development/debugging. */
@ -1652,6 +1719,11 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
pxStreamBuffer->ucFlags = ucFlags;
pxStreamBuffer->uxNotificationIndex = tskDEFAULT_INDEX_TO_NOTIFY;
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
pxStreamBuffer->xTaskSpinlock = xTaskSpinlock;
pxStreamBuffer->xISRSpinlock = xISRSpinlock;
#endif
#if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
{
pxStreamBuffer->pxSendCompletedCallback = pxSendCompletedCallback;

1448
tasks.c

File diff suppressed because it is too large Load diff

View file

@ -79,6 +79,17 @@
#define tmrSTATUS_IS_STATICALLY_ALLOCATED ( 0x02U )
#define tmrSTATUS_IS_AUTORELOAD ( 0x04U )
/*
* Macros to mark the start and end of a critical code region.
*/
#if ( portUSING_GRANULAR_LOCKS == 1 )
#define tmrENTER_CRITICAL() taskDATA_GROUP_ENTER_CRITICAL( &xTimerTaskSpinlock, &xTimerISRSpinlock )
#define tmrEXIT_CRITICAL() taskDATA_GROUP_EXIT_CRITICAL( &xTimerTaskSpinlock, &xTimerISRSpinlock )
#else /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
#define tmrENTER_CRITICAL() taskENTER_CRITICAL()
#define tmrEXIT_CRITICAL() taskEXIT_CRITICAL()
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
/* The definition of the timers themselves. */
typedef struct tmrTimerControl /* The old naming convention is used to prevent breaking kernel aware debuggers. */
{
@ -149,6 +160,16 @@
PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;
PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#ifdef portREMOVE_STATIC_QUALIFIER
PRIVILEGED_DATA portSPINLOCK_TYPE xTimerTaskSpinlock = portINIT_SPINLOCK_STATIC;
PRIVILEGED_DATA portSPINLOCK_TYPE xTimerISRSpinlock = portINIT_SPINLOCK_STATIC;
#else
PRIVILEGED_DATA static portSPINLOCK_TYPE xTimerTaskSpinlock = portINIT_SPINLOCK_STATIC;
PRIVILEGED_DATA static portSPINLOCK_TYPE xTimerISRSpinlock = portINIT_SPINLOCK_STATIC;
#endif
#endif /* #if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
/*-----------------------------------------------------------*/
/*
@ -572,7 +593,7 @@
traceENTER_vTimerSetReloadMode( xTimer, xAutoReload );
configASSERT( xTimer );
taskENTER_CRITICAL();
tmrENTER_CRITICAL();
{
if( xAutoReload != pdFALSE )
{
@ -583,7 +604,7 @@
pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_AUTORELOAD );
}
}
taskEXIT_CRITICAL();
tmrEXIT_CRITICAL();
traceRETURN_vTimerSetReloadMode();
}
@ -597,7 +618,15 @@
traceENTER_xTimerGetReloadMode( xTimer );
configASSERT( xTimer );
#if ( ( configNUMBER_OF_CORES > 1 ) )
{
tmrENTER_CRITICAL();
}
#else
{
portBASE_TYPE_ENTER_CRITICAL();
}
#endif
{
if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) == 0U )
{
@ -610,7 +639,15 @@
xReturn = pdTRUE;
}
}
#if ( ( configNUMBER_OF_CORES > 1 ) )
{
tmrEXIT_CRITICAL();
}
#else
{
portBASE_TYPE_EXIT_CRITICAL();
}
#endif
traceRETURN_xTimerGetReloadMode( xReturn );
@ -1116,7 +1153,7 @@
/* Check that the list from which active timers are referenced, and the
* queue used to communicate with the timer service, have been
* initialised. */
taskENTER_CRITICAL();
tmrENTER_CRITICAL();
{
if( xTimerQueue == NULL )
{
@ -1158,7 +1195,7 @@
mtCOVERAGE_TEST_MARKER();
}
}
taskEXIT_CRITICAL();
tmrEXIT_CRITICAL();
}
/*-----------------------------------------------------------*/
@ -1172,7 +1209,15 @@
configASSERT( xTimer );
/* Is the timer in the list of active timers? */
#if ( ( configNUMBER_OF_CORES > 1 ) )
{
tmrENTER_CRITICAL();
}
#else
{
portBASE_TYPE_ENTER_CRITICAL();
}
#endif
{
if( ( pxTimer->ucStatus & tmrSTATUS_IS_ACTIVE ) == 0U )
{
@ -1183,7 +1228,15 @@
xReturn = pdTRUE;
}
}
#if ( ( configNUMBER_OF_CORES > 1 ) )
{
tmrEXIT_CRITICAL();
}
#else
{
portBASE_TYPE_EXIT_CRITICAL();
}
#endif
traceRETURN_xTimerIsTimerActive( xReturn );
@ -1200,11 +1253,11 @@
configASSERT( xTimer );
taskENTER_CRITICAL();
tmrENTER_CRITICAL();
{
pvReturn = pxTimer->pvTimerID;
}
taskEXIT_CRITICAL();
tmrEXIT_CRITICAL();
traceRETURN_pvTimerGetTimerID( pvReturn );
@ -1221,11 +1274,11 @@
configASSERT( xTimer );
taskENTER_CRITICAL();
tmrENTER_CRITICAL();
{
pxTimer->pvTimerID = pvNewID;
}
taskEXIT_CRITICAL();
tmrEXIT_CRITICAL();
traceRETURN_vTimerSetTimerID();
}