Skip to content

Commit f836da4

Browse files
refactor(freertos/smp): Move critical sections inside xTaskPriorityInherit()
xTaskPriorityInherit() is called inside a critical section from queue.c. This commit moves the critical section into xTaskPriorityInherit(). Co-authored-by: Sudeep Mohanty <[email protected]>
1 parent 2615dcd commit f836da4

File tree

2 files changed

+67
-67
lines changed

2 files changed

+67
-67
lines changed

queue.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,11 +1793,7 @@ BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
17931793
{
17941794
if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
17951795
{
1796-
taskENTER_CRITICAL();
1797-
{
1798-
xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );
1799-
}
1800-
taskEXIT_CRITICAL();
1796+
xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );
18011797
}
18021798
else
18031799
{

tasks.c

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6643,91 +6643,95 @@ static void prvResetNextTaskUnblockTime( void )
66436643

66446644
traceENTER_xTaskPriorityInherit( pxMutexHolder );
66456645

6646-
/* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6647-
* inheritance is not applied in this scenario. */
6648-
if( pxMutexHolder != NULL )
6646+
taskENTER_CRITICAL();
66496647
{
6650-
/* If the holder of the mutex has a priority below the priority of
6651-
* the task attempting to obtain the mutex then it will temporarily
6652-
* inherit the priority of the task attempting to obtain the mutex. */
6653-
if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6648+
/* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6649+
* inheritance is not applied in this scenario. */
6650+
if( pxMutexHolder != NULL )
66546651
{
6655-
/* Adjust the mutex holder state to account for its new
6656-
* priority. Only reset the event list item value if the value is
6657-
* not being used for anything else. */
6658-
if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6659-
{
6660-
listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
6661-
}
6662-
else
6652+
/* If the holder of the mutex has a priority below the priority of
6653+
* the task attempting to obtain the mutex then it will temporarily
6654+
* inherit the priority of the task attempting to obtain the mutex. */
6655+
if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
66636656
{
6664-
mtCOVERAGE_TEST_MARKER();
6665-
}
6666-
6667-
/* If the task being modified is in the ready state it will need
6668-
* to be moved into a new list. */
6669-
if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6670-
{
6671-
if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6657+
/* Adjust the mutex holder state to account for its new
6658+
* priority. Only reset the event list item value if the value is
6659+
* not being used for anything else. */
6660+
if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
66726661
{
6673-
/* It is known that the task is in its ready list so
6674-
* there is no need to check again and the port level
6675-
* reset macro can be called directly. */
6676-
portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6662+
listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
66776663
}
66786664
else
66796665
{
66806666
mtCOVERAGE_TEST_MARKER();
66816667
}
66826668

6683-
/* Inherit the priority before being moved into the new list. */
6684-
pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6685-
prvAddTaskToReadyList( pxMutexHolderTCB );
6686-
#if ( configNUMBER_OF_CORES > 1 )
6669+
/* If the task being modified is in the ready state it will need
6670+
* to be moved into a new list. */
6671+
if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
66876672
{
6688-
/* The priority of the task is raised. Yield for this task
6689-
* if it is not running. */
6690-
if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6673+
if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
66916674
{
6692-
prvYieldForTask( pxMutexHolderTCB );
6675+
/* It is known that the task is in its ready list so
6676+
* there is no need to check again and the port level
6677+
* reset macro can be called directly. */
6678+
portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6679+
}
6680+
else
6681+
{
6682+
mtCOVERAGE_TEST_MARKER();
6683+
}
6684+
6685+
/* Inherit the priority before being moved into the new list. */
6686+
pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6687+
prvAddTaskToReadyList( pxMutexHolderTCB );
6688+
#if ( configNUMBER_OF_CORES > 1 )
6689+
{
6690+
/* The priority of the task is raised. Yield for this task
6691+
* if it is not running. */
6692+
if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6693+
{
6694+
prvYieldForTask( pxMutexHolderTCB );
6695+
}
66936696
}
6697+
#endif /* if ( configNUMBER_OF_CORES > 1 ) */
6698+
}
6699+
else
6700+
{
6701+
/* Just inherit the priority. */
6702+
pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
66946703
}
6695-
#endif /* if ( configNUMBER_OF_CORES > 1 ) */
6696-
}
6697-
else
6698-
{
6699-
/* Just inherit the priority. */
6700-
pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6701-
}
67026704

6703-
traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6705+
traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
67046706

6705-
/* Inheritance occurred. */
6706-
xReturn = pdTRUE;
6707-
}
6708-
else
6709-
{
6710-
if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6711-
{
6712-
/* The base priority of the mutex holder is lower than the
6713-
* priority of the task attempting to take the mutex, but the
6714-
* current priority of the mutex holder is not lower than the
6715-
* priority of the task attempting to take the mutex.
6716-
* Therefore the mutex holder must have already inherited a
6717-
* priority, but inheritance would have occurred if that had
6718-
* not been the case. */
6707+
/* Inheritance occurred. */
67196708
xReturn = pdTRUE;
67206709
}
67216710
else
67226711
{
6723-
mtCOVERAGE_TEST_MARKER();
6712+
if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6713+
{
6714+
/* The base priority of the mutex holder is lower than the
6715+
* priority of the task attempting to take the mutex, but the
6716+
* current priority of the mutex holder is not lower than the
6717+
* priority of the task attempting to take the mutex.
6718+
* Therefore the mutex holder must have already inherited a
6719+
* priority, but inheritance would have occurred if that had
6720+
* not been the case. */
6721+
xReturn = pdTRUE;
6722+
}
6723+
else
6724+
{
6725+
mtCOVERAGE_TEST_MARKER();
6726+
}
67246727
}
67256728
}
6729+
else
6730+
{
6731+
mtCOVERAGE_TEST_MARKER();
6732+
}
67266733
}
6727-
else
6728-
{
6729-
mtCOVERAGE_TEST_MARKER();
6730-
}
6734+
taskEXIT_CRITICAL();
67316735

67326736
traceRETURN_xTaskPriorityInherit( xReturn );
67336737

0 commit comments

Comments
 (0)