3 * Copyright (C) Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
27 #include "proof/queue.h"
28 #include "proof/queuecontracts.h"
30 BaseType_t xQueuePeek( QueueHandle_t xQueue,
31 void * const pvBuffer,
32 TickType_t xTicksToWait )
34 /*@requires [1/2]queuehandle(xQueue, ?N, ?M, ?is_isr) &*& is_isr == false &*&
35 * [1/2]queuesuspend(xQueue) &*&
36 * chars(pvBuffer, M, ?x);@*/
38 /*@ensures [1/2]queuehandle(xQueue, N, M, is_isr) &*&
39 * [1/2]queuesuspend(xQueue) &*&
40 * (result == pdPASS ? chars(pvBuffer, M, _) : chars(pvBuffer, M, x));@*/
42 BaseType_t xEntryTimeSet = pdFALSE;
44 int8_t * pcOriginalReadPosition;
46 #ifdef VERIFAST /*< const pointer declaration */
47 Queue_t * pxQueue = xQueue;
49 Queue_t * const pxQueue = xQueue;
51 /* Check the pointer is not NULL. */
52 configASSERT( ( pxQueue ) );
54 /* The buffer into which data is received can only be NULL if the data size
55 * is zero (so no data is copied into the buffer. */
56 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
58 /* Cannot block if the scheduler is suspended. */
59 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
61 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
64 #endif /* ifdef VERIFAST */
66 /*lint -save -e904 This function relaxes the coding standard somewhat to
67 * allow return statements within the function itself. This is done in the
68 * interest of execution time efficiency. */
71 /*@invariant [1/2]queuehandle(xQueue, N, M, is_isr) &*&
72 * [1/2]queuesuspend(xQueue) &*&
73 * chars(pvBuffer, M, x) &*&
74 * u_integer(&xTicksToWait, _) &*&
75 * xTIME_OUT(&xTimeOut);@*/
78 /*@assert queue(pxQueue, ?Storage, N, M, ?W, ?R, ?K, ?is_locked, ?abs);@*/
80 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
82 /* Is there data in the queue now? To be running the calling task
83 * must be the highest priority task wanting to access the queue. */
84 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
86 /* Remember the read position so it can be reset after the data
87 * is read from the queue as this function is only peeking the
88 * data, not removing it. */
89 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
91 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
92 prvCopyDataFromQueue( pxQueue, pvBuffer );
93 traceQUEUE_PEEK( pxQueue );
95 /* The data is not being removed, so reset the read pointer. */
96 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
98 /* The data is being left in the queue, so see if there are
99 * any other tasks waiting for the data. */
100 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
102 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
104 /* The task waiting has a higher priority than this task. */
105 queueYIELD_IF_USING_PREEMPTION();
109 mtCOVERAGE_TEST_MARKER();
114 mtCOVERAGE_TEST_MARKER();
117 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
123 if( xTicksToWait == ( TickType_t ) 0 )
125 /* The queue was empty and no block time is specified (or
126 * the block time has expired) so leave now. */
127 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
129 traceQUEUE_PEEK_FAILED( pxQueue );
130 return errQUEUE_EMPTY;
132 else if( xEntryTimeSet == pdFALSE )
134 /* The queue was empty and a block time was specified so
135 * configure the timeout structure ready to enter the blocked
137 vTaskInternalSetTimeOutState( &xTimeOut );
138 xEntryTimeSet = pdTRUE;
142 /* Entry time was already set. */
143 mtCOVERAGE_TEST_MARKER();
147 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
150 /* Interrupts and other tasks can send to and receive from the queue
151 * now the critical section has been exited. */
153 /*@close exists<QueueHandle_t>(pxQueue);@*/
155 prvLockQueue( pxQueue );
157 /* Update the timeout state to see if it has expired yet. */
158 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
160 /* Timeout has not expired yet, check to see if there is data in the
161 * queue now, and if not enter the Blocked state to wait for data. */
162 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
164 traceBLOCKING_ON_QUEUE_PEEK( pxQueue );
165 /*@open queue_locked_invariant(xQueue)();@*/
166 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
167 /*@close queue_locked_invariant(xQueue)();@*/
168 prvUnlockQueue( pxQueue );
170 /*@close exists<QueueHandle_t>(pxQueue);@*/
171 if( xTaskResumeAll() == pdFALSE )
173 portYIELD_WITHIN_API();
177 mtCOVERAGE_TEST_MARKER();
182 /* There is data in the queue now, so don't enter the blocked
183 * state, instead return to try and obtain the data. */
184 prvUnlockQueue( pxQueue );
185 #ifdef VERIFAST /*< void cast of unused return value */
186 /*@close exists<QueueHandle_t>(pxQueue);@*/
189 ( void ) xTaskResumeAll();
195 /* The timeout has expired. If there is still no data in the queue
196 * exit, otherwise go back and try to read the data again. */
197 prvUnlockQueue( pxQueue );
198 #ifdef VERIFAST /*< void cast of unused return value */
199 /*@close exists<QueueHandle_t>(pxQueue);@*/
202 ( void ) xTaskResumeAll();
205 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
207 traceQUEUE_PEEK_FAILED( pxQueue );
208 return errQUEUE_EMPTY;
212 mtCOVERAGE_TEST_MARKER();