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.
23 #include "proof/queue.h"
24 #include "proof/queuecontracts.h"
26 BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
27 const void * const pvItemToQueue,
28 TickType_t xTicksToWait,
29 const BaseType_t xCopyPosition )
30 /*@requires [1/2]queuehandle(xQueue, ?N, ?M, ?is_isr) &*& is_isr == false &*&
31 [1/2]queuesuspend(xQueue) &*&
32 chars(pvItemToQueue, M, ?x) &*&
33 (xCopyPosition == queueSEND_TO_BACK || xCopyPosition == queueSEND_TO_FRONT || (xCopyPosition == queueOVERWRITE && N == 1));@*/
34 /*@ensures [1/2]queuehandle(xQueue, N, M, is_isr) &*&
35 [1/2]queuesuspend(xQueue) &*&
36 chars(pvItemToQueue, M, x);@*/
38 BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
40 #ifdef VERIFAST /*< const pointer declaration */
41 Queue_t * pxQueue = xQueue;
43 Queue_t * const pxQueue = xQueue;
45 configASSERT( pxQueue );
46 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
47 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
48 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
50 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
55 /*lint -save -e904 This function relaxes the coding standard somewhat to
56 * allow return statements within the function itself. This is done in the
57 * interest of execution time efficiency. */
59 /*@invariant [1/2]queuehandle(xQueue, N, M, is_isr) &*&
60 [1/2]queuesuspend(xQueue) &*&
61 chars(pvItemToQueue, M, x) &*&
62 u_integer(&xTicksToWait, _) &*&
63 (xCopyPosition == queueSEND_TO_BACK || xCopyPosition == queueSEND_TO_FRONT || (xCopyPosition == queueOVERWRITE && N == 1)) &*&
64 xTIME_OUT(&xTimeOut);@*/
68 /*@assert queue(pxQueue, ?Storage, N, M, ?W, ?R, ?K, ?is_locked, ?abs);@*/
69 /* Is there room on the queue now? The running task must be the
70 * highest priority task wanting to access the queue. If the head item
71 * in the queue is to be overwritten then it does not matter if the
73 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
75 traceQUEUE_SEND( pxQueue );
77 /* VeriFast: we do not verify this configuration option */
78 #if ( configUSE_QUEUE_SETS == 1 )
80 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
82 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
84 if( pxQueue->pxQueueSetContainer != NULL )
86 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
88 /* Do not notify the queue set as an existing item
89 * was overwritten in the queue so the number of items
90 * in the queue has not changed. */
91 mtCOVERAGE_TEST_MARKER();
93 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
95 /* The queue is a member of a queue set, and posting
96 * to the queue set caused a higher priority task to
97 * unblock. A context switch is required. */
98 queueYIELD_IF_USING_PREEMPTION();
102 mtCOVERAGE_TEST_MARKER();
107 /* If there was a task waiting for data to arrive on the
108 * queue then unblock it now. */
109 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
111 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
113 /* The unblocked task has a priority higher than
114 * our own so yield immediately. Yes it is ok to
115 * do this from within the critical section - the
116 * kernel takes care of that. */
117 queueYIELD_IF_USING_PREEMPTION();
121 mtCOVERAGE_TEST_MARKER();
124 else if( xYieldRequired != pdFALSE )
126 /* This path is a special case that will only get
127 * executed if the task was holding multiple mutexes
128 * and the mutexes were given back in an order that is
129 * different to that in which they were taken. */
130 queueYIELD_IF_USING_PREEMPTION();
134 mtCOVERAGE_TEST_MARKER();
138 #else /* configUSE_QUEUE_SETS */
140 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
141 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
143 /* If there was a task waiting for data to arrive on the
144 * queue then unblock it now. */
145 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
147 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
149 /* The unblocked task has a priority higher than
150 * our own so yield immediately. Yes it is ok to do
151 * this from within the critical section - the kernel
152 * takes care of that. */
153 queueYIELD_IF_USING_PREEMPTION();
157 mtCOVERAGE_TEST_MARKER();
160 else if( xYieldRequired != pdFALSE )
162 /* This path is a special case that will only get
163 * executed if the task was holding multiple mutexes and
164 * the mutexes were given back in an order that is
165 * different to that in which they were taken. */
166 queueYIELD_IF_USING_PREEMPTION();
170 mtCOVERAGE_TEST_MARKER();
173 #endif /* configUSE_QUEUE_SETS */
176 if (xCopyPosition == queueSEND_TO_BACK)
178 close queue(pxQueue, Storage, N, M, (W+1)%N, R, (K+1), is_locked, append(abs, singleton(x)));
180 else if (xCopyPosition == queueSEND_TO_FRONT)
182 close queue(pxQueue, Storage, N, M, W, (R == 0 ? (N-1) : (R-1)), (K+1), is_locked, cons(x, abs));
184 else if (xCopyPosition == queueOVERWRITE)
186 close queue(pxQueue, Storage, N, M, W, R, 1, is_locked, singleton(x));
194 if( xTicksToWait == ( TickType_t ) 0 )
196 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
197 /* The queue was full and no block time is specified (or
198 * the block time has expired) so leave now. */
201 /* Return to the original privilege level before exiting
203 traceQUEUE_SEND_FAILED( pxQueue );
204 return errQUEUE_FULL;
206 else if( xEntryTimeSet == pdFALSE )
208 /* The queue was full and a block time was specified so
209 * configure the timeout structure. */
210 vTaskInternalSetTimeOutState( &xTimeOut );
211 xEntryTimeSet = pdTRUE;
215 /* Entry time was already set. */
216 mtCOVERAGE_TEST_MARKER();
219 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
223 /* Interrupts and other tasks can send to and receive from the queue
224 * now the critical section has been exited. */
226 /*@close exists(pxQueue);@*/
228 prvLockQueue( pxQueue );
230 /* Update the timeout state to see if it has expired yet. */
231 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
233 if( prvIsQueueFull( pxQueue ) != pdFALSE )
235 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
236 /*@open queue_locked_invariant(xQueue)();@*/
237 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
239 /* Unlocking the queue means queue events can effect the
240 * event list. It is possible that interrupts occurring now
241 * remove this task from the event list again - but as the
242 * scheduler is suspended the task will go onto the pending
243 * ready last instead of the actual ready list. */
244 /*@close queue_locked_invariant(xQueue)();@*/
245 prvUnlockQueue( pxQueue );
247 /* Resuming the scheduler will move tasks from the pending
248 * ready list into the ready list - so it is feasible that this
249 * task is already in a ready list before it yields - in which
250 * case the yield will not cause a context switch unless there
251 * is also a higher priority task in the pending ready list. */
252 /*@close exists(pxQueue);@*/
253 if( xTaskResumeAll() == pdFALSE )
255 portYIELD_WITHIN_API();
261 prvUnlockQueue( pxQueue );
262 #ifdef VERIFAST /*< void cast of unused return value */
263 /*@close exists(pxQueue);@*/
266 ( void ) xTaskResumeAll();
272 /* The timeout has expired. */
273 prvUnlockQueue( pxQueue );
274 #ifdef VERIFAST /*< void cast of unused return value */
275 /*@close exists(pxQueue);@*/
278 ( void ) xTaskResumeAll();
281 traceQUEUE_SEND_FAILED( pxQueue );
282 return errQUEUE_FULL;