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 xQueueGenericSend( QueueHandle_t xQueue,
31 const void * const pvItemToQueue,
32 TickType_t xTicksToWait,
33 const BaseType_t xCopyPosition )
35 /*@requires [1/2]queuehandle(xQueue, ?N, ?M, ?is_isr) &*& is_isr == false &*&
36 * [1/2]queuesuspend(xQueue) &*&
37 * chars(pvItemToQueue, M, ?x) &*&
38 * (xCopyPosition == queueSEND_TO_BACK || xCopyPosition == queueSEND_TO_FRONT || (xCopyPosition == queueOVERWRITE && N == 1));@*/
40 /*@ensures [1/2]queuehandle(xQueue, N, M, is_isr) &*&
41 * [1/2]queuesuspend(xQueue) &*&
42 * chars(pvItemToQueue, M, x);@*/
44 BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
47 #ifdef VERIFAST /*< const pointer declaration */
48 Queue_t * pxQueue = xQueue;
50 Queue_t * const pxQueue = xQueue;
52 configASSERT( pxQueue );
53 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
54 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
55 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
57 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
60 #endif /* ifdef VERIFAST */
62 /*lint -save -e904 This function relaxes the coding standard somewhat to
63 * allow return statements within the function itself. This is done in the
64 * interest of execution time efficiency. */
67 /*@invariant [1/2]queuehandle(xQueue, N, M, is_isr) &*&
68 * [1/2]queuesuspend(xQueue) &*&
69 * chars(pvItemToQueue, M, x) &*&
70 * u_integer(&xTicksToWait, _) &*&
71 * (xCopyPosition == queueSEND_TO_BACK || xCopyPosition == queueSEND_TO_FRONT || (xCopyPosition == queueOVERWRITE && N == 1)) &*&
72 * xTIME_OUT(&xTimeOut);@*/
76 /*@assert queue(pxQueue, ?Storage, N, M, ?W, ?R, ?K, ?is_locked, ?abs);@*/
78 /* Is there room on the queue now? The running task must be the
79 * highest priority task wanting to access the queue. If the head item
80 * in the queue is to be overwritten then it does not matter if the
82 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
84 traceQUEUE_SEND( pxQueue );
86 /* VeriFast: we do not verify this configuration option */
87 #if ( configUSE_QUEUE_SETS == 1 )
89 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
91 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
93 if( pxQueue->pxQueueSetContainer != NULL )
95 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
97 /* Do not notify the queue set as an existing item
98 * was overwritten in the queue so the number of items
99 * in the queue has not changed. */
100 mtCOVERAGE_TEST_MARKER();
102 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
104 /* The queue is a member of a queue set, and posting
105 * to the queue set caused a higher priority task to
106 * unblock. A context switch is required. */
107 queueYIELD_IF_USING_PREEMPTION();
111 mtCOVERAGE_TEST_MARKER();
116 /* If there was a task waiting for data to arrive on the
117 * queue then unblock it now. */
118 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
120 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
122 /* The unblocked task has a priority higher than
123 * our own so yield immediately. Yes it is ok to
124 * do this from within the critical section - the
125 * kernel takes care of that. */
126 queueYIELD_IF_USING_PREEMPTION();
130 mtCOVERAGE_TEST_MARKER();
133 else if( xYieldRequired != pdFALSE )
135 /* This path is a special case that will only get
136 * executed if the task was holding multiple mutexes
137 * and the mutexes were given back in an order that is
138 * different to that in which they were taken. */
139 queueYIELD_IF_USING_PREEMPTION();
143 mtCOVERAGE_TEST_MARKER();
147 #else /* configUSE_QUEUE_SETS */
149 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
150 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
152 /* If there was a task waiting for data to arrive on the
153 * queue then unblock it now. */
154 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
156 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
158 /* The unblocked task has a priority higher than
159 * our own so yield immediately. Yes it is ok to do
160 * this from within the critical section - the kernel
161 * takes care of that. */
162 queueYIELD_IF_USING_PREEMPTION();
166 mtCOVERAGE_TEST_MARKER();
169 else if( xYieldRequired != pdFALSE )
171 /* This path is a special case that will only get
172 * executed if the task was holding multiple mutexes and
173 * the mutexes were given back in an order that is
174 * different to that in which they were taken. */
175 queueYIELD_IF_USING_PREEMPTION();
179 mtCOVERAGE_TEST_MARKER();
182 #endif /* configUSE_QUEUE_SETS */
185 * if (xCopyPosition == queueSEND_TO_BACK)
187 * close queue(pxQueue, Storage, N, M, (W+1)%N, R, (K+1), is_locked, append(abs, singleton(x)));
189 * else if (xCopyPosition == queueSEND_TO_FRONT)
191 * close queue(pxQueue, Storage, N, M, W, (R == 0 ? (N-1) : (R-1)), (K+1), is_locked, cons(x, abs));
193 * else if (xCopyPosition == queueOVERWRITE)
195 * close queue(pxQueue, Storage, N, M, W, R, 1, is_locked, singleton(x));
203 if( xTicksToWait == ( TickType_t ) 0 )
205 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
207 /* The queue was full and no block time is specified (or
208 * the block time has expired) so leave now. */
211 /* Return to the original privilege level before exiting
213 traceQUEUE_SEND_FAILED( pxQueue );
214 return errQUEUE_FULL;
216 else if( xEntryTimeSet == pdFALSE )
218 /* The queue was full and a block time was specified so
219 * configure the timeout structure. */
220 vTaskInternalSetTimeOutState( &xTimeOut );
221 xEntryTimeSet = pdTRUE;
225 /* Entry time was already set. */
226 mtCOVERAGE_TEST_MARKER();
230 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
234 /* Interrupts and other tasks can send to and receive from the queue
235 * now the critical section has been exited. */
237 /*@close exists(pxQueue);@*/
239 prvLockQueue( pxQueue );
241 /* Update the timeout state to see if it has expired yet. */
242 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
244 if( prvIsQueueFull( pxQueue ) != pdFALSE )
246 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
247 /*@open queue_locked_invariant(xQueue)();@*/
248 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
250 /* Unlocking the queue means queue events can effect the
251 * event list. It is possible that interrupts occurring now
252 * remove this task from the event list again - but as the
253 * scheduler is suspended the task will go onto the pending
254 * ready last instead of the actual ready list. */
255 /*@close queue_locked_invariant(xQueue)();@*/
256 prvUnlockQueue( pxQueue );
258 /* Resuming the scheduler will move tasks from the pending
259 * ready list into the ready list - so it is feasible that this
260 * task is already in a ready list before it yields - in which
261 * case the yield will not cause a context switch unless there
262 * is also a higher priority task in the pending ready list. */
263 /*@close exists(pxQueue);@*/
264 if( xTaskResumeAll() == pdFALSE )
266 portYIELD_WITHIN_API();
272 prvUnlockQueue( pxQueue );
273 #ifdef VERIFAST /*< void cast of unused return value */
274 /*@close exists(pxQueue);@*/
277 ( void ) xTaskResumeAll();
283 /* The timeout has expired. */
284 prvUnlockQueue( pxQueue );
285 #ifdef VERIFAST /*< void cast of unused return value */
286 /*@close exists(pxQueue);@*/
289 ( void ) xTaskResumeAll();
292 traceQUEUE_SEND_FAILED( pxQueue );
293 return errQUEUE_FULL;