+ else\r
+ {\r
+ /* Timed out. */\r
+ prvUnlockQueue( pxQueue );\r
+ ( void ) xTaskResumeAll();\r
+\r
+ /* If the semaphore count is 0 exit now as the timeout has\r
+ expired. Otherwise return to attempt to take the semaphore that is\r
+ known to be available. As semaphores are implemented by queues the\r
+ queue being empty is equivalent to the semaphore count being 0. */\r
+ if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
+ {\r
+ #if ( configUSE_MUTEXES == 1 )\r
+ {\r
+ /* xInheritanceOccurred could only have be set if\r
+ pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to\r
+ test the mutex type again to check it is actually a mutex. */\r
+ if( xInheritanceOccurred != pdFALSE )\r
+ {\r
+ taskENTER_CRITICAL();\r
+ {\r
+ UBaseType_t uxHighestWaitingPriority;\r
+\r
+ /* This task blocking on the mutex caused another\r
+ task to inherit this task's priority. Now this task\r
+ has timed out the priority should be disinherited\r
+ again, but only as low as the next highest priority\r
+ task that is waiting for the same mutex. */\r
+ uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );\r
+ vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );\r
+ }\r
+ taskEXIT_CRITICAL();\r
+ }\r
+ }\r
+ #endif /* configUSE_MUTEXES */\r
+\r
+ traceQUEUE_RECEIVE_FAILED( pxQueue );\r
+ return errQUEUE_EMPTY;\r
+ }\r
+ else\r
+ {\r
+ mtCOVERAGE_TEST_MARKER();\r
+ }\r
+ }\r
+ } /*lint -restore */\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+BaseType_t xQueuePeek( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait )\r
+{\r
+BaseType_t xEntryTimeSet = pdFALSE;\r
+TimeOut_t xTimeOut;\r
+int8_t *pcOriginalReadPosition;\r
+Queue_t * const pxQueue = xQueue;\r
+\r
+ /* Check the pointer is not NULL. */\r
+ configASSERT( ( pxQueue ) );\r
+\r
+ /* The buffer into which data is received can only be NULL if the data size\r
+ is zero (so no data is copied into the buffer. */\r
+ configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
+\r
+ /* Cannot block if the scheduler is suspended. */\r
+ #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
+ {\r
+ configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
+ }\r
+ #endif\r
+\r
+\r
+ /*lint -save -e904 This function relaxes the coding standard somewhat to\r
+ allow return statements within the function itself. This is done in the\r
+ interest of execution time efficiency. */\r
+ for( ;; )\r
+ {\r
+ taskENTER_CRITICAL();\r
+ {\r
+ const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
+\r
+ /* Is there data in the queue now? To be running the calling task\r
+ must be the highest priority task wanting to access the queue. */\r
+ if( uxMessagesWaiting > ( UBaseType_t ) 0 )\r
+ {\r
+ /* Remember the read position so it can be reset after the data\r
+ is read from the queue as this function is only peeking the\r
+ data, not removing it. */\r
+ pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;\r
+\r
+ prvCopyDataFromQueue( pxQueue, pvBuffer );\r
+ traceQUEUE_PEEK( pxQueue );\r
+\r
+ /* The data is not being removed, so reset the read pointer. */\r
+ pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;\r
+\r
+ /* The data is being left in the queue, so see if there are\r
+ any other tasks waiting for the data. */\r
+ if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
+ {\r
+ if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
+ {\r
+ /* The task waiting has a higher priority than this task. */\r
+ queueYIELD_IF_USING_PREEMPTION();\r
+ }\r
+ else\r
+ {\r
+ mtCOVERAGE_TEST_MARKER();\r
+ }\r
+ }\r
+ else\r
+ {\r
+ mtCOVERAGE_TEST_MARKER();\r
+ }\r
+\r
+ taskEXIT_CRITICAL();\r
+ return pdPASS;\r
+ }\r
+ else\r
+ {\r
+ if( xTicksToWait == ( TickType_t ) 0 )\r
+ {\r
+ /* The queue was empty and no block time is specified (or\r
+ the block time has expired) so leave now. */\r
+ taskEXIT_CRITICAL();\r
+ traceQUEUE_PEEK_FAILED( pxQueue );\r
+ return errQUEUE_EMPTY;\r
+ }\r
+ else if( xEntryTimeSet == pdFALSE )\r
+ {\r
+ /* The queue was empty and a block time was specified so\r
+ configure the timeout structure ready to enter the blocked\r
+ state. */\r
+ vTaskInternalSetTimeOutState( &xTimeOut );\r
+ xEntryTimeSet = pdTRUE;\r
+ }\r
+ else\r
+ {\r
+ /* Entry time was already set. */\r
+ mtCOVERAGE_TEST_MARKER();\r
+ }\r
+ }\r
+ }\r
+ taskEXIT_CRITICAL();\r
+\r
+ /* Interrupts and other tasks can send to and receive from the queue\r
+ now the critical section has been exited. */\r
+\r
+ vTaskSuspendAll();\r
+ prvLockQueue( pxQueue );\r
+\r
+ /* Update the timeout state to see if it has expired yet. */\r
+ if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
+ {\r
+ /* Timeout has not expired yet, check to see if there is data in the\r
+ queue now, and if not enter the Blocked state to wait for data. */\r
+ if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
+ {\r
+ traceBLOCKING_ON_QUEUE_PEEK( pxQueue );\r
+ vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
+ prvUnlockQueue( pxQueue );\r
+ if( xTaskResumeAll() == pdFALSE )\r
+ {\r
+ portYIELD_WITHIN_API();\r
+ }\r
+ else\r
+ {\r
+ mtCOVERAGE_TEST_MARKER();\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* There is data in the queue now, so don't enter the blocked\r
+ state, instead return to try and obtain the data. */\r
+ prvUnlockQueue( pxQueue );\r
+ ( void ) xTaskResumeAll();\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* The timeout has expired. If there is still no data in the queue\r
+ exit, otherwise go back and try to read the data again. */\r
+ prvUnlockQueue( pxQueue );\r
+ ( void ) xTaskResumeAll();\r
+\r
+ if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
+ {\r
+ traceQUEUE_PEEK_FAILED( pxQueue );\r
+ return errQUEUE_EMPTY;\r
+ }\r
+ else\r
+ {\r
+ mtCOVERAGE_TEST_MARKER();\r
+ }\r
+ }\r
+ } /*lint -restore */\r