Skip to content

Commit 5b9a8ca

Browse files
Remove post-timeout re-check in queue receive paths
xQueueReceive, xQueueSemaphoreTake, and xQueuePeek re-checked the queue state after timeout expiry and looped back if data had arrived, while xQueueGenericSend returned errQUEUE_FULL directly on timeout. Remove the post-timeout re-check from all three receive-side functions to make timeout behavior symmetric with the send path. Once the timeout has expired, return errQUEUE_EMPTY directly. For xQueueSemaphoreTake, the mutex priority disinheritance logic is retained and now runs unconditionally on timeout. Refs FreeRTOS/FreeRTOS#65 Forum: https://forums.freertos.org/t/xqueuegenericsend-and-xqueuereceive/24856
1 parent 587fe6d commit 5b9a8ca

1 file changed

Lines changed: 37 additions & 64 deletions

File tree

queue.c

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,22 +1635,14 @@ BaseType_t xQueueReceive( QueueHandle_t xQueue,
16351635
}
16361636
else
16371637
{
1638-
/* Timed out. If there is no data in the queue exit, otherwise loop
1639-
* back and attempt to read the data. */
1638+
/* The timeout has expired. */
16401639
prvUnlockQueue( pxQueue );
16411640
( void ) xTaskResumeAll();
16421641

1643-
if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1644-
{
1645-
traceQUEUE_RECEIVE_FAILED( pxQueue );
1646-
traceRETURN_xQueueReceive( errQUEUE_EMPTY );
1642+
traceQUEUE_RECEIVE_FAILED( pxQueue );
1643+
traceRETURN_xQueueReceive( errQUEUE_EMPTY );
16471644

1648-
return errQUEUE_EMPTY;
1649-
}
1650-
else
1651-
{
1652-
mtCOVERAGE_TEST_MARKER();
1653-
}
1645+
return errQUEUE_EMPTY;
16541646
}
16551647
}
16561648
}
@@ -1829,53 +1821,42 @@ BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
18291821
prvUnlockQueue( pxQueue );
18301822
( void ) xTaskResumeAll();
18311823

1832-
/* If the semaphore count is 0 exit now as the timeout has
1833-
* expired. Otherwise return to attempt to take the semaphore that is
1834-
* known to be available. As semaphores are implemented by queues the
1835-
* queue being empty is equivalent to the semaphore count being 0. */
1836-
if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1824+
#if ( configUSE_MUTEXES == 1 )
18371825
{
1838-
#if ( configUSE_MUTEXES == 1 )
1826+
/* xInheritanceOccurred could only have be set if
1827+
* pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
1828+
* test the mutex type again to check it is actually a mutex. */
1829+
if( xInheritanceOccurred != pdFALSE )
18391830
{
1840-
/* xInheritanceOccurred could only have be set if
1841-
* pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
1842-
* test the mutex type again to check it is actually a mutex. */
1843-
if( xInheritanceOccurred != pdFALSE )
1831+
taskENTER_CRITICAL();
18441832
{
1845-
taskENTER_CRITICAL();
1846-
{
1847-
UBaseType_t uxHighestWaitingPriority;
1848-
1849-
/* This task blocking on the mutex caused another
1850-
* task to inherit this task's priority. Now this task
1851-
* has timed out the priority should be disinherited
1852-
* again, but only as low as the next highest priority
1853-
* task that is waiting for the same mutex. */
1854-
uxHighestWaitingPriority = prvGetHighestPriorityOfWaitToReceiveList( pxQueue );
1855-
1856-
/* vTaskPriorityDisinheritAfterTimeout uses the uxHighestWaitingPriority
1857-
* parameter to index pxReadyTasksLists when adding the task holding
1858-
* mutex to the ready list for its new priority. Coverity thinks that
1859-
* it can result in out-of-bounds access which is not true because
1860-
* uxHighestWaitingPriority, as returned by prvGetHighestPriorityOfWaitToReceiveList,
1861-
* is capped at ( configMAX_PRIORITIES - 1 ). */
1862-
/* coverity[overrun] */
1863-
vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );
1864-
}
1865-
taskEXIT_CRITICAL();
1833+
UBaseType_t uxHighestWaitingPriority;
1834+
1835+
/* This task blocking on the mutex caused another
1836+
* task to inherit this task's priority. Now this task
1837+
* has timed out the priority should be disinherited
1838+
* again, but only as low as the next highest priority
1839+
* task that is waiting for the same mutex. */
1840+
uxHighestWaitingPriority = prvGetHighestPriorityOfWaitToReceiveList( pxQueue );
1841+
1842+
/* vTaskPriorityDisinheritAfterTimeout uses the uxHighestWaitingPriority
1843+
* parameter to index pxReadyTasksLists when adding the task holding
1844+
* mutex to the ready list for its new priority. Coverity thinks that
1845+
* it can result in out-of-bounds access which is not true because
1846+
* uxHighestWaitingPriority, as returned by prvGetHighestPriorityOfWaitToReceiveList,
1847+
* is capped at ( configMAX_PRIORITIES - 1 ). */
1848+
/* coverity[overrun] */
1849+
vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );
18661850
}
1851+
taskEXIT_CRITICAL();
18671852
}
1868-
#endif /* configUSE_MUTEXES */
1853+
}
1854+
#endif /* configUSE_MUTEXES */
18691855

1870-
traceQUEUE_RECEIVE_FAILED( pxQueue );
1871-
traceRETURN_xQueueSemaphoreTake( errQUEUE_EMPTY );
1856+
traceQUEUE_RECEIVE_FAILED( pxQueue );
1857+
traceRETURN_xQueueSemaphoreTake( errQUEUE_EMPTY );
18721858

1873-
return errQUEUE_EMPTY;
1874-
}
1875-
else
1876-
{
1877-
mtCOVERAGE_TEST_MARKER();
1878-
}
1859+
return errQUEUE_EMPTY;
18791860
}
18801861
}
18811862
}
@@ -2015,22 +1996,14 @@ BaseType_t xQueuePeek( QueueHandle_t xQueue,
20151996
}
20161997
else
20171998
{
2018-
/* The timeout has expired. If there is still no data in the queue
2019-
* exit, otherwise go back and try to read the data again. */
1999+
/* The timeout has expired. */
20202000
prvUnlockQueue( pxQueue );
20212001
( void ) xTaskResumeAll();
20222002

2023-
if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
2024-
{
2025-
traceQUEUE_PEEK_FAILED( pxQueue );
2026-
traceRETURN_xQueuePeek( errQUEUE_EMPTY );
2003+
traceQUEUE_PEEK_FAILED( pxQueue );
2004+
traceRETURN_xQueuePeek( errQUEUE_EMPTY );
20272005

2028-
return errQUEUE_EMPTY;
2029-
}
2030-
else
2031-
{
2032-
mtCOVERAGE_TEST_MARKER();
2033-
}
2006+
return errQUEUE_EMPTY;
20342007
}
20352008
}
20362009
}

0 commit comments

Comments
 (0)