]> begriffs open source - cmsis-freertos/blob - Test/VeriFast/queue/xQueueGenericSendFromISR.c
Updated pack to FreeRTOS 10.4.3
[cmsis-freertos] / Test / VeriFast / queue / xQueueGenericSendFromISR.c
1 /*
2  * FreeRTOS V202104.00
3  * Copyright (C) Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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.
21  */
22
23 #include "proof/queue.h"
24 #include "proof/queuecontracts.h"
25
26 BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,
27                                      const void * const pvItemToQueue,
28                                      BaseType_t * const pxHigherPriorityTaskWoken,
29                                      const BaseType_t xCopyPosition )
30 /*@requires
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));@*/
35 /*@ensures
36     [1/2]queuehandle(xQueue, N, M, is_isr) &*&
37     chars(pvItemToQueue, M, x) &*&
38     integer(pxHigherPriorityTaskWoken, _);@*/
39 {
40     BaseType_t xReturn;
41     UBaseType_t uxSavedInterruptStatus;
42
43 #ifdef VERIFAST /*< const pointer declaration */
44     Queue_t * pxQueue = xQueue;
45 #else
46     Queue_t * const pxQueue = xQueue;
47
48     configASSERT( pxQueue );
49     configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
50     configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
51 #endif
52
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();
68
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
73      * post). */
74     uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
75     /*@assert queue(pxQueue, ?Storage, N, M, ?W, ?R, ?K, ?is_locked, ?abs);@*/
76     {
77         if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
78         {
79             const int8_t cTxLock = pxQueue->cTxLock;
80             const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
81
82             traceQUEUE_SEND_FROM_ISR( pxQueue );
83
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 );
92 #else
93             ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
94 #endif
95             /*@open queue(pxQueue, _, N, M, _, _, _, _, _);@*/
96
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 )
100             {
101                 /* VeriFast: we do not verify this configuration option */
102                 #if ( configUSE_QUEUE_SETS == 1 )
103                     {
104                         if( pxQueue->pxQueueSetContainer != NULL )
105                         {
106                             if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
107                             {
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();
112                             }
113                             else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
114                             {
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 )
119                                 {
120                                     *pxHigherPriorityTaskWoken = pdTRUE;
121                                 }
122                                 else
123                                 {
124                                     mtCOVERAGE_TEST_MARKER();
125                                 }
126                             }
127                             else
128                             {
129                                 mtCOVERAGE_TEST_MARKER();
130                             }
131                         }
132                         else
133                         {
134                             if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
135                             {
136                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
137                                 {
138                                     /* The task waiting has a higher priority so
139                                      *  record that a context switch is required. */
140                                     if( pxHigherPriorityTaskWoken != NULL )
141                                     {
142                                         *pxHigherPriorityTaskWoken = pdTRUE;
143                                     }
144                                     else
145                                     {
146                                         mtCOVERAGE_TEST_MARKER();
147                                     }
148                                 }
149                                 else
150                                 {
151                                     mtCOVERAGE_TEST_MARKER();
152                                 }
153                             }
154                             else
155                             {
156                                 mtCOVERAGE_TEST_MARKER();
157                             }
158                         }
159                     }
160                 #else /* configUSE_QUEUE_SETS */
161                     {
162                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
163                         {
164                             if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
165                             {
166                                 /* The task waiting has a higher priority so record that a
167                                  * context switch is required. */
168                                 if( pxHigherPriorityTaskWoken != NULL )
169                                 {
170                                     *pxHigherPriorityTaskWoken = pdTRUE;
171                                 }
172                                 else
173                                 {
174                                     mtCOVERAGE_TEST_MARKER();
175                                 }
176                             }
177                             else
178                             {
179                                 mtCOVERAGE_TEST_MARKER();
180                             }
181                         }
182                         else
183                         {
184                             mtCOVERAGE_TEST_MARKER();
185                         }
186
187                         /* Not used in this path. */
188 #ifndef VERIFAST /*< void cast of unused var */
189                         ( void ) uxPreviousMessagesWaiting;
190 #endif
191                     }
192                 #endif /* configUSE_QUEUE_SETS */
193             }
194             else
195             {
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 );
199
200                 pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );
201             }
202
203             xReturn = pdPASS;
204             /*@
205             if (xCopyPosition == queueSEND_TO_BACK)
206             {
207                 close queue(pxQueue, Storage, N, M, (W+1)%N, R, (K+1), is_locked, append(abs, singleton(x)));
208             }
209             else if (xCopyPosition == queueSEND_TO_FRONT)
210             {
211                 if (R == 0)
212                 {
213                     close queue(pxQueue, Storage, N, M, W, (N-1), (K+1), is_locked, cons(x, abs));
214                 }
215                 else
216                 {
217                     close queue(pxQueue, Storage, N, M, W, (R-1), (K+1), is_locked, cons(x, abs));
218                 }
219             } else if (xCopyPosition == queueOVERWRITE)
220             {
221                 close queue(pxQueue, Storage, N, M, W, R, 1, is_locked, singleton(x));
222             }
223             @*/
224         }
225         else
226         {
227             traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
228             xReturn = errQUEUE_FULL;
229             /*@close queue(pxQueue, Storage, N, M, W, R, K, is_locked, abs);@*/
230         }
231     }
232     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
233
234     return xReturn;
235 }