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 xQueueGenericSendFromISR( QueueHandle_t xQueue,
27 const void * const pvItemToQueue,
28 BaseType_t * const pxHigherPriorityTaskWoken,
29 const BaseType_t xCopyPosition )
31 [1/2]queuehandle(xQueue, ?N, ?M, ?is_isr) &*& is_isr == true &*&
32 chars(pvItemToQueue, M, ?x) &*&
33 integer(pxHigherPriorityTaskWoken, _) &*&
34 (xCopyPosition == queueSEND_TO_BACK || xCopyPosition == queueSEND_TO_FRONT || (xCopyPosition == queueOVERWRITE && N == 1));@*/
36 [1/2]queuehandle(xQueue, N, M, is_isr) &*&
37 chars(pvItemToQueue, M, x) &*&
38 integer(pxHigherPriorityTaskWoken, _);@*/
41 UBaseType_t uxSavedInterruptStatus;
43 #ifdef VERIFAST /*< const pointer declaration */
44 Queue_t * pxQueue = xQueue;
46 Queue_t * const pxQueue = xQueue;
48 configASSERT( pxQueue );
49 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
50 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
53 /* RTOS ports that support interrupt nesting have the concept of a maximum
54 * system call (or maximum API call) interrupt priority. Interrupts that are
55 * above the maximum system call priority are kept permanently enabled, even
56 * when the RTOS kernel is in a critical section, but cannot make any calls to
57 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
58 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
59 * failure if a FreeRTOS API function is called from an interrupt that has been
60 * assigned a priority above the configured maximum system call priority.
61 * Only FreeRTOS functions that end in FromISR can be called from interrupts
62 * that have been assigned a priority at or (logically) below the maximum
63 * system call interrupt priority. FreeRTOS maintains a separate interrupt
64 * safe API to ensure interrupt entry is as fast and as simple as possible.
65 * More information (albeit Cortex-M specific) is provided on the following
66 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
67 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
69 /* Similar to xQueueGenericSend, except without blocking if there is no room
70 * in the queue. Also don't directly wake a task that was blocked on a queue
71 * read, instead return a flag to say whether a context switch is required or
72 * not (i.e. has a task with a higher priority than us been woken by this
74 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
75 /*@assert queue(pxQueue, ?Storage, N, M, ?W, ?R, ?K, ?is_locked, ?abs);@*/
77 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
79 const int8_t cTxLock = pxQueue->cTxLock;
80 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
82 traceQUEUE_SEND_FROM_ISR( pxQueue );
84 /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a
85 * semaphore or mutex. That means prvCopyDataToQueue() cannot result
86 * in a task disinheriting a priority and prvCopyDataToQueue() can be
87 * called here even though the disinherit function does not check if
88 * the scheduler is suspended before accessing the ready lists. */
89 #ifdef VERIFAST /*< void cast of unused return value */
90 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
91 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
93 ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
95 /*@open queue(pxQueue, _, N, M, _, _, _, _, _);@*/
97 /* The event list is not altered if the queue is locked. This will
98 * be done when the queue is unlocked later. */
99 if( cTxLock == queueUNLOCKED )
101 /* VeriFast: we do not verify this configuration option */
102 #if ( configUSE_QUEUE_SETS == 1 )
104 if( pxQueue->pxQueueSetContainer != NULL )
106 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
108 /* Do not notify the queue set as an existing item
109 * was overwritten in the queue so the number of items
110 * in the queue has not changed. */
111 mtCOVERAGE_TEST_MARKER();
113 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
115 /* The queue is a member of a queue set, and posting
116 * to the queue set caused a higher priority task to
117 * unblock. A context switch is required. */
118 if( pxHigherPriorityTaskWoken != NULL )
120 *pxHigherPriorityTaskWoken = pdTRUE;
124 mtCOVERAGE_TEST_MARKER();
129 mtCOVERAGE_TEST_MARKER();
134 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
136 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
138 /* The task waiting has a higher priority so
139 * record that a context switch is required. */
140 if( pxHigherPriorityTaskWoken != NULL )
142 *pxHigherPriorityTaskWoken = pdTRUE;
146 mtCOVERAGE_TEST_MARKER();
151 mtCOVERAGE_TEST_MARKER();
156 mtCOVERAGE_TEST_MARKER();
160 #else /* configUSE_QUEUE_SETS */
162 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
164 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
166 /* The task waiting has a higher priority so record that a
167 * context switch is required. */
168 if( pxHigherPriorityTaskWoken != NULL )
170 *pxHigherPriorityTaskWoken = pdTRUE;
174 mtCOVERAGE_TEST_MARKER();
179 mtCOVERAGE_TEST_MARKER();
184 mtCOVERAGE_TEST_MARKER();
187 /* Not used in this path. */
188 #ifndef VERIFAST /*< void cast of unused var */
189 ( void ) uxPreviousMessagesWaiting;
192 #endif /* configUSE_QUEUE_SETS */
196 /* Increment the lock count so the task that unlocks the queue
197 * knows that data was posted while it was locked. */
198 configASSERT( cTxLock != queueINT8_MAX );
200 pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );
205 if (xCopyPosition == queueSEND_TO_BACK)
207 close queue(pxQueue, Storage, N, M, (W+1)%N, R, (K+1), is_locked, append(abs, singleton(x)));
209 else if (xCopyPosition == queueSEND_TO_FRONT)
213 close queue(pxQueue, Storage, N, M, W, (N-1), (K+1), is_locked, cons(x, abs));
217 close queue(pxQueue, Storage, N, M, W, (R-1), (K+1), is_locked, cons(x, abs));
219 } else if (xCopyPosition == queueOVERWRITE)
221 close queue(pxQueue, Storage, N, M, W, R, 1, is_locked, singleton(x));
227 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
228 xReturn = errQUEUE_FULL;
229 /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
232 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );