1 /*
2  * FreeRTOS Kernel V10.4.3
3  * Copyright (C) 2020 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  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 
27 #include <stdlib.h>
28 #include <string.h>
29 
30 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
31  * all the API functions to use the MPU wrappers.  That should only be done when
32  * task.h is included from an application file. */
33 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
34 
35 #include "FreeRTOS.h"
36 #include "task.h"
37 #include "queue.h"
38 
39 #if ( configUSE_CO_ROUTINES == 1 )
40     #include "croutine.h"
41 #endif
42 
43 #ifdef ESP_PLATFORM
44 #define taskCRITICAL_MUX &((Queue_t *)pxQueue)->mux
45 #undef taskENTER_CRITICAL
46 #undef taskEXIT_CRITICAL
47 #undef taskENTER_CRITICAL_ISR
48 #undef taskEXIT_CRITICAL_ISR
49 #define taskENTER_CRITICAL( )     portENTER_CRITICAL( taskCRITICAL_MUX )
50 #define taskEXIT_CRITICAL( )            portEXIT_CRITICAL( taskCRITICAL_MUX )
51 #define taskENTER_CRITICAL_ISR( )     portENTER_CRITICAL_ISR( taskCRITICAL_MUX )
52 #define taskEXIT_CRITICAL_ISR( )        portEXIT_CRITICAL_ISR( taskCRITICAL_MUX )
53 #endif
54 
55 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
56  * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
57  * for the header files above, but not in this file, in order to generate the
58  * correct privileged Vs unprivileged linkage and placement. */
59 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
60 
61 
62 /* Constants used with the cRxLock and cTxLock structure members. */
63 #define queueUNLOCKED             ( ( int8_t ) -1 )
64 #define queueLOCKED_UNMODIFIED    ( ( int8_t ) 0 )
65 #define queueINT8_MAX             ( ( int8_t ) 127 )
66 
67 /* When the Queue_t structure is used to represent a base queue its pcHead and
68  * pcTail members are used as pointers into the queue storage area.  When the
69  * Queue_t structure is used to represent a mutex pcHead and pcTail pointers are
70  * not necessary, and the pcHead pointer is set to NULL to indicate that the
71  * structure instead holds a pointer to the mutex holder (if any).  Map alternative
72  * names to the pcHead and structure member to ensure the readability of the code
73  * is maintained.  The QueuePointers_t and SemaphoreData_t types are used to form
74  * a union as their usage is mutually exclusive dependent on what the queue is
75  * being used for. */
76 #define uxQueueType               pcHead
77 #define queueQUEUE_IS_MUTEX       NULL
78 
79 typedef struct QueuePointers
80 {
81     int8_t * pcTail;     /*< Points to the byte at the end of the queue storage area.  Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
82     int8_t * pcReadFrom; /*< Points to the last place that a queued item was read from when the structure is used as a queue. */
83 } QueuePointers_t;
84 
85 typedef struct SemaphoreData
86 {
87     TaskHandle_t xMutexHolder;        /*< The handle of the task that holds the mutex. */
88     UBaseType_t uxRecursiveCallCount; /*< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */
89 } SemaphoreData_t;
90 
91 /* Semaphores do not actually store or copy data, so have an item size of
92  * zero. */
93 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH    ( ( UBaseType_t ) 0 )
94 #define queueMUTEX_GIVE_BLOCK_TIME          ( ( TickType_t ) 0U )
95 
96 #if ( configUSE_PREEMPTION == 0 )
97 
98 /* If the cooperative scheduler is being used then a yield should not be
99  * performed just because a higher priority task has been woken. */
100     #define queueYIELD_IF_USING_PREEMPTION()
101 #else
102     #define queueYIELD_IF_USING_PREEMPTION()    portYIELD_WITHIN_API()
103 #endif
104 
105 /*
106  * Definition of the queue used by the scheduler.
107  * Items are queued by copy, not reference.  See the following link for the
108  * rationale: https://www.FreeRTOS.org/Embedded-RTOS-Queues.html
109  */
110 typedef struct QueueDefinition /* The old naming convention is used to prevent breaking kernel aware debuggers. */
111 {
112     int8_t * pcHead;           /*< Points to the beginning of the queue storage area. */
113     int8_t * pcWriteTo;        /*< Points to the free next place in the storage area. */
114 
115     union
116     {
117         QueuePointers_t xQueue;     /*< Data required exclusively when this structure is used as a queue. */
118         SemaphoreData_t xSemaphore; /*< Data required exclusively when this structure is used as a semaphore. */
119     } u;
120 
121     List_t xTasksWaitingToSend;             /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */
122     List_t xTasksWaitingToReceive;          /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */
123 
124     volatile UBaseType_t uxMessagesWaiting; /*< The number of items currently in the queue. */
125     UBaseType_t uxLength;                   /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
126     UBaseType_t uxItemSize;                 /*< The size of each items that the queue will hold. */
127 
128     volatile int8_t cRxLock;                /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */
129     volatile int8_t cTxLock;                /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */
130 
131     #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
132         uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */
133     #endif
134 
135     #if ( configUSE_QUEUE_SETS == 1 )
136         struct QueueDefinition * pxQueueSetContainer;
137     #endif
138 
139     #if ( configUSE_TRACE_FACILITY == 1 )
140         UBaseType_t uxQueueNumber;
141         uint8_t ucQueueType;
142     #endif
143 #ifdef ESP_PLATFORM
144     portMUX_TYPE mux;       //Mutex required due to SMP
145 #endif // ESP_PLATFORM
146 } xQUEUE;
147 
148 /* The old xQUEUE name is maintained above then typedefed to the new Queue_t
149  * name below to enable the use of older kernel aware debuggers. */
150 typedef xQUEUE Queue_t;
151 
152 /*-----------------------------------------------------------*/
153 
154 /*
155  * The queue registry is just a means for kernel aware debuggers to locate
156  * queue structures.  It has no other purpose so is an optional component.
157  */
158 #if ( configQUEUE_REGISTRY_SIZE > 0 )
159 
160 /* The type stored within the queue registry array.  This allows a name
161  * to be assigned to each queue making kernel aware debugging a little
162  * more user friendly. */
163     typedef struct QUEUE_REGISTRY_ITEM
164     {
165         const char * pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
166         QueueHandle_t xHandle;
167     } xQueueRegistryItem;
168 
169 /* The old xQueueRegistryItem name is maintained above then typedefed to the
170  * new xQueueRegistryItem name below to enable the use of older kernel aware
171  * debuggers. */
172     typedef xQueueRegistryItem QueueRegistryItem_t;
173 
174 /* The queue registry is simply an array of QueueRegistryItem_t structures.
175  * The pcQueueName member of a structure being NULL is indicative of the
176  * array position being vacant. */
177     PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
178 #ifdef ESP_PLATFORM
179     //Need to add queue registry mutex to protect against simultaneous access
180     static portMUX_TYPE queue_registry_spinlock = portMUX_INITIALIZER_UNLOCKED;
181 #endif // ESP_PLATFORM
182 #endif /* configQUEUE_REGISTRY_SIZE */
183 
184 /*
185  * Unlocks a queue locked by a call to prvLockQueue.  Locking a queue does not
186  * prevent an ISR from adding or removing items to the queue, but does prevent
187  * an ISR from removing tasks from the queue event lists.  If an ISR finds a
188  * queue is locked it will instead increment the appropriate queue lock count
189  * to indicate that a task may require unblocking.  When the queue in unlocked
190  * these lock counts are inspected, and the appropriate action taken.
191  */
192 static void prvUnlockQueue( Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
193 
194 /*
195  * Uses a critical section to determine if there is any data in a queue.
196  *
197  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
198  */
199 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
200 
201 /*
202  * Uses a critical section to determine if there is any space in a queue.
203  *
204  * @return pdTRUE if there is no space, otherwise pdFALSE;
205  */
206 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
207 
208 /*
209  * Copies an item into the queue, either at the front of the queue or the
210  * back of the queue.
211  */
212 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
213                                       const void * pvItemToQueue,
214                                       const BaseType_t xPosition ) PRIVILEGED_FUNCTION;
215 
216 /*
217  * Copies an item out of a queue.
218  */
219 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
220                                   void * const pvBuffer ) PRIVILEGED_FUNCTION;
221 
222 #if ( configUSE_QUEUE_SETS == 1 )
223 
224 /*
225  * Checks to see if a queue is a member of a queue set, and if so, notifies
226  * the queue set that the queue contains data.
227  */
228     static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue, const BaseType_t xCopyPosition ) PRIVILEGED_FUNCTION;
229 #endif
230 
231 /*
232  * Called after a Queue_t structure has been allocated either statically or
233  * dynamically to fill in the structure's members.
234  */
235 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
236                                    const UBaseType_t uxItemSize,
237                                    uint8_t * pucQueueStorage,
238                                    const uint8_t ucQueueType,
239                                    Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
240 
241 /*
242  * Mutexes are a special type of queue.  When a mutex is created, first the
243  * queue is created, then prvInitialiseMutex() is called to configure the queue
244  * as a mutex.
245  */
246 #if ( configUSE_MUTEXES == 1 )
247     static void prvInitialiseMutex( Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
248 #endif
249 
250 #if ( configUSE_MUTEXES == 1 )
251 
252 /*
253  * If a task waiting for a mutex causes the mutex holder to inherit a
254  * priority, but the waiting task times out, then the holder should
255  * disinherit the priority - but only down to the highest priority of any
256  * other tasks that are waiting for the same mutex.  This function returns
257  * that priority.
258  */
259     static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
260 #endif
261 /*-----------------------------------------------------------*/
262 
263 /*
264  * Macro to mark a queue as locked.  Locking a queue prevents an ISR from
265  * accessing the queue event lists.
266  */
267 #define prvLockQueue( pxQueue )                            \
268     taskENTER_CRITICAL();                                  \
269     {                                                      \
270         if( ( pxQueue )->cRxLock == queueUNLOCKED )        \
271         {                                                  \
272             ( pxQueue )->cRxLock = queueLOCKED_UNMODIFIED; \
273         }                                                  \
274         if( ( pxQueue )->cTxLock == queueUNLOCKED )        \
275         {                                                  \
276             ( pxQueue )->cTxLock = queueLOCKED_UNMODIFIED; \
277         }                                                  \
278     }                                                      \
279     taskEXIT_CRITICAL()
280 /*-----------------------------------------------------------*/
281 
xQueueGenericReset(QueueHandle_t xQueue,BaseType_t xNewQueue)282 BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
283                                BaseType_t xNewQueue )
284 {
285     Queue_t * const pxQueue = xQueue;
286 
287     configASSERT( pxQueue );
288 
289 #ifdef ESP_PLATFORM
290     if( xNewQueue == pdTRUE )
291     {
292         portMUX_INITIALIZE(&pxQueue->mux);
293     }
294 #endif // ESP_PLATFORM
295 
296     taskENTER_CRITICAL();
297     {
298         pxQueue->u.xQueue.pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
299         pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
300         pxQueue->pcWriteTo = pxQueue->pcHead;
301         pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - 1U ) * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
302         pxQueue->cRxLock = queueUNLOCKED;
303         pxQueue->cTxLock = queueUNLOCKED;
304 
305         if( xNewQueue == pdFALSE )
306         {
307             /* If there are tasks blocked waiting to read from the queue, then
308              * the tasks will remain blocked as after this function exits the queue
309              * will still be empty.  If there are tasks blocked waiting to write to
310              * the queue, then one should be unblocked as after this function exits
311              * it will be possible to write to it. */
312             if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
313             {
314                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
315                 {
316                     queueYIELD_IF_USING_PREEMPTION();
317                 }
318                 else
319                 {
320                     mtCOVERAGE_TEST_MARKER();
321                 }
322             }
323             else
324             {
325                 mtCOVERAGE_TEST_MARKER();
326             }
327         }
328         else
329         {
330             /* Ensure the event queues start in the correct state. */
331             vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
332             vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
333         }
334     }
335     taskEXIT_CRITICAL();
336 
337     /* A value is returned for calling semantic consistency with previous
338      * versions. */
339     return pdPASS;
340 }
341 /*-----------------------------------------------------------*/
342 
343 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
344 
xQueueGenericCreateStatic(const UBaseType_t uxQueueLength,const UBaseType_t uxItemSize,uint8_t * pucQueueStorage,StaticQueue_t * pxStaticQueue,const uint8_t ucQueueType)345     QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,
346                                              const UBaseType_t uxItemSize,
347                                              uint8_t * pucQueueStorage,
348                                              StaticQueue_t * pxStaticQueue,
349                                              const uint8_t ucQueueType )
350     {
351         Queue_t * pxNewQueue;
352 
353         configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
354 
355         /* The StaticQueue_t structure and the queue storage area must be
356          * supplied. */
357         configASSERT( pxStaticQueue != NULL );
358 
359         /* A queue storage area should be provided if the item size is not 0, and
360          * should not be provided if the item size is 0. */
361         configASSERT( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) );
362         configASSERT( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) );
363 
364         #if ( configASSERT_DEFINED == 1 )
365             {
366                 /* Sanity check that the size of the structure used to declare a
367                  * variable of type StaticQueue_t or StaticSemaphore_t equals the size of
368                  * the real queue and semaphore structures. */
369                 volatile size_t xSize = sizeof( StaticQueue_t );
370 
371                 /* This assertion cannot be branch covered in unit tests */
372                 configASSERT( xSize == sizeof( Queue_t ) ); /* LCOV_EXCL_BR_LINE */
373                 ( void ) xSize;                             /* Keeps lint quiet when configASSERT() is not defined. */
374             }
375         #endif /* configASSERT_DEFINED */
376 
377         /* The address of a statically allocated queue was passed in, use it.
378          * The address of a statically allocated storage area was also passed in
379          * but is already set. */
380         pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
381 
382         if( pxNewQueue != NULL )
383         {
384             #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
385                 {
386                     /* Queues can be allocated wither statically or dynamically, so
387                      * note this queue was allocated statically in case the queue is
388                      * later deleted. */
389                     pxNewQueue->ucStaticallyAllocated = pdTRUE;
390                 }
391             #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
392 
393             prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
394         }
395         else
396         {
397             traceQUEUE_CREATE_FAILED( ucQueueType );
398             mtCOVERAGE_TEST_MARKER();
399         }
400 
401         return pxNewQueue;
402     }
403 
404 #endif /* configSUPPORT_STATIC_ALLOCATION */
405 /*-----------------------------------------------------------*/
406 
407 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
408 
xQueueGenericCreate(const UBaseType_t uxQueueLength,const UBaseType_t uxItemSize,const uint8_t ucQueueType)409     QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
410                                        const UBaseType_t uxItemSize,
411                                        const uint8_t ucQueueType )
412     {
413         Queue_t * pxNewQueue = NULL;
414         size_t xQueueSizeInBytes;
415         uint8_t * pucQueueStorage;
416 
417         configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
418 
419         if( uxItemSize == ( UBaseType_t ) 0 )
420         {
421             /* There is not going to be a queue storage area. */
422             xQueueSizeInBytes = ( size_t ) 0;
423         }
424         else
425         {
426             /* Allocate enough space to hold the maximum number of items that
427              * can be in the queue at any time.  It is valid for uxItemSize to be
428              * zero in the case the queue is used as a semaphore. */
429             xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
430         }
431 
432         /* Check for multiplication overflow. */
433         configASSERT( ( uxItemSize == 0 ) || ( uxQueueLength == ( xQueueSizeInBytes / uxItemSize ) ) );
434 
435         /* Check for addition overflow. */
436         configASSERT( ( sizeof( Queue_t ) + xQueueSizeInBytes ) > xQueueSizeInBytes );
437 
438         /* Allocate the queue and storage area.  Justification for MISRA
439          * deviation as follows:  pvPortMalloc() always ensures returned memory
440          * blocks are aligned per the requirements of the MCU stack.  In this case
441          * pvPortMalloc() must return a pointer that is guaranteed to meet the
442          * alignment requirements of the Queue_t structure - which in this case
443          * is an int8_t *.  Therefore, whenever the stack alignment requirements
444          * are greater than or equal to the pointer to char requirements the cast
445          * is safe.  In other cases alignment requirements are not strict (one or
446          * two bytes). */
447         pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */
448 
449         if( pxNewQueue != NULL )
450         {
451             /* Jump past the queue structure to find the location of the queue
452              * storage area. */
453             pucQueueStorage = ( uint8_t * ) pxNewQueue;
454             pucQueueStorage += sizeof( Queue_t ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
455 
456             #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
457                 {
458                     /* Queues can be created either statically or dynamically, so
459                      * note this task was created dynamically in case it is later
460                      * deleted. */
461                     pxNewQueue->ucStaticallyAllocated = pdFALSE;
462                 }
463             #endif /* configSUPPORT_STATIC_ALLOCATION */
464 
465             prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
466         }
467         else
468         {
469             traceQUEUE_CREATE_FAILED( ucQueueType );
470             mtCOVERAGE_TEST_MARKER();
471         }
472 
473         return pxNewQueue;
474     }
475 
476 #endif /* configSUPPORT_STATIC_ALLOCATION */
477 /*-----------------------------------------------------------*/
478 
prvInitialiseNewQueue(const UBaseType_t uxQueueLength,const UBaseType_t uxItemSize,uint8_t * pucQueueStorage,const uint8_t ucQueueType,Queue_t * pxNewQueue)479 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
480                                    const UBaseType_t uxItemSize,
481                                    uint8_t * pucQueueStorage,
482                                    const uint8_t ucQueueType,
483                                    Queue_t * pxNewQueue )
484 {
485     /* Remove compiler warnings about unused parameters should
486      * configUSE_TRACE_FACILITY not be set to 1. */
487     ( void ) ucQueueType;
488 
489     if( uxItemSize == ( UBaseType_t ) 0 )
490     {
491         /* No RAM was allocated for the queue storage area, but PC head cannot
492          * be set to NULL because NULL is used as a key to say the queue is used as
493          * a mutex.  Therefore just set pcHead to point to the queue as a benign
494          * value that is known to be within the memory map. */
495         pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
496     }
497     else
498     {
499         /* Set the head to the start of the queue storage area. */
500         pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
501     }
502 
503     /* Initialise the queue members as described where the queue type is
504      * defined. */
505     pxNewQueue->uxLength = uxQueueLength;
506     pxNewQueue->uxItemSize = uxItemSize;
507     ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
508 
509     #if ( configUSE_TRACE_FACILITY == 1 )
510         {
511             pxNewQueue->ucQueueType = ucQueueType;
512         }
513     #endif /* configUSE_TRACE_FACILITY */
514 
515     #if ( configUSE_QUEUE_SETS == 1 )
516         {
517             pxNewQueue->pxQueueSetContainer = NULL;
518         }
519     #endif /* configUSE_QUEUE_SETS */
520 
521     traceQUEUE_CREATE( pxNewQueue );
522 }
523 /*-----------------------------------------------------------*/
524 
525 #if ( configUSE_MUTEXES == 1 )
526 
prvInitialiseMutex(Queue_t * pxNewQueue)527     static void prvInitialiseMutex( Queue_t * pxNewQueue )
528     {
529         if( pxNewQueue != NULL )
530         {
531             /* The queue create function will set all the queue structure members
532             * correctly for a generic queue, but this function is creating a
533             * mutex.  Overwrite those members that need to be set differently -
534             * in particular the information required for priority inheritance. */
535             pxNewQueue->u.xSemaphore.xMutexHolder = NULL;
536             pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
537 
538             /* In case this is a recursive mutex. */
539             pxNewQueue->u.xSemaphore.uxRecursiveCallCount = 0;
540 #ifdef ESP_PLATFORM
541             portMUX_INITIALIZE(&pxNewQueue->mux);
542 #endif // ESP_PLATFORM
543             traceCREATE_MUTEX( pxNewQueue );
544 
545             /* Start with the semaphore in the expected state. */
546             ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
547         }
548         else
549         {
550             traceCREATE_MUTEX_FAILED();
551         }
552     }
553 
554 #endif /* configUSE_MUTEXES */
555 /*-----------------------------------------------------------*/
556 
557 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
558 
xQueueCreateMutex(const uint8_t ucQueueType)559     QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
560     {
561         QueueHandle_t xNewQueue;
562         const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
563 
564         xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
565         prvInitialiseMutex( ( Queue_t * ) xNewQueue );
566 
567         return xNewQueue;
568     }
569 
570 #endif /* configUSE_MUTEXES */
571 /*-----------------------------------------------------------*/
572 
573 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
574 
xQueueCreateMutexStatic(const uint8_t ucQueueType,StaticQueue_t * pxStaticQueue)575     QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
576                                            StaticQueue_t * pxStaticQueue )
577     {
578         QueueHandle_t xNewQueue;
579         const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
580 
581         /* Prevent compiler warnings about unused parameters if
582          * configUSE_TRACE_FACILITY does not equal 1. */
583         ( void ) ucQueueType;
584 
585         xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
586         prvInitialiseMutex( ( Queue_t * ) xNewQueue );
587 
588         return xNewQueue;
589     }
590 
591 #endif /* configUSE_MUTEXES */
592 /*-----------------------------------------------------------*/
593 
594 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
595 
xQueueGetMutexHolder(QueueHandle_t xSemaphore)596     TaskHandle_t xQueueGetMutexHolder( QueueHandle_t xSemaphore )
597     {
598         TaskHandle_t pxReturn;
599         Queue_t * const pxSemaphore = ( Queue_t * ) xSemaphore;
600 
601         /* This function is called by xSemaphoreGetMutexHolder(), and should not
602          * be called directly.  Note:  This is a good way of determining if the
603          * calling task is the mutex holder, but not a good way of determining the
604          * identity of the mutex holder, as the holder may change between the
605          * following critical section exiting and the function returning. */
606 #ifdef ESP_PLATFORM
607         Queue_t * const pxQueue = (Queue_t *)pxSemaphore;
608 #endif
609         taskENTER_CRITICAL();
610         {
611             if( pxSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
612             {
613                 pxReturn = pxSemaphore->u.xSemaphore.xMutexHolder;
614             }
615             else
616             {
617                 pxReturn = NULL;
618             }
619         }
620         taskEXIT_CRITICAL();
621 
622         return pxReturn;
623     } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
624 
625 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
626 /*-----------------------------------------------------------*/
627 
628 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
629 
xQueueGetMutexHolderFromISR(QueueHandle_t xSemaphore)630     TaskHandle_t xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )
631     {
632         TaskHandle_t pxReturn;
633 
634         configASSERT( xSemaphore );
635 
636         /* Mutexes cannot be used in interrupt service routines, so the mutex
637          * holder should not change in an ISR, and therefore a critical section is
638          * not required here. */
639         if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
640         {
641             pxReturn = ( ( Queue_t * ) xSemaphore )->u.xSemaphore.xMutexHolder;
642         }
643         else
644         {
645             pxReturn = NULL;
646         }
647 
648         return pxReturn;
649     } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
650 
651 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
652 /*-----------------------------------------------------------*/
653 
654 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
655 
xQueueGiveMutexRecursive(QueueHandle_t xMutex)656     BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
657     {
658         BaseType_t xReturn;
659         Queue_t * const pxMutex = ( Queue_t * ) xMutex;
660 
661         configASSERT( pxMutex );
662 
663         /* If this is the task that holds the mutex then xMutexHolder will not
664          * change outside of this task.  If this task does not hold the mutex then
665          * pxMutexHolder can never coincidentally equal the tasks handle, and as
666          * this is the only condition we are interested in it does not matter if
667          * pxMutexHolder is accessed simultaneously by another task.  Therefore no
668          * mutual exclusion is required to test the pxMutexHolder variable. */
669         if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
670         {
671             traceGIVE_MUTEX_RECURSIVE( pxMutex );
672 
673             /* uxRecursiveCallCount cannot be zero if xMutexHolder is equal to
674              * the task handle, therefore no underflow check is required.  Also,
675              * uxRecursiveCallCount is only modified by the mutex holder, and as
676              * there can only be one, no mutual exclusion is required to modify the
677              * uxRecursiveCallCount member. */
678             ( pxMutex->u.xSemaphore.uxRecursiveCallCount )--;
679 
680             /* Has the recursive call count unwound to 0? */
681             if( pxMutex->u.xSemaphore.uxRecursiveCallCount == ( UBaseType_t ) 0 )
682             {
683                 /* Return the mutex.  This will automatically unblock any other
684                  * task that might be waiting to access the mutex. */
685                 ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
686             }
687             else
688             {
689                 mtCOVERAGE_TEST_MARKER();
690             }
691 
692             xReturn = pdPASS;
693         }
694         else
695         {
696             /* The mutex cannot be given because the calling task is not the
697              * holder. */
698             xReturn = pdFAIL;
699 
700             traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
701         }
702 
703         return xReturn;
704     }
705 
706 #endif /* configUSE_RECURSIVE_MUTEXES */
707 /*-----------------------------------------------------------*/
708 
709 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
710 
xQueueTakeMutexRecursive(QueueHandle_t xMutex,TickType_t xTicksToWait)711     BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
712                                          TickType_t xTicksToWait )
713     {
714         BaseType_t xReturn;
715         Queue_t * const pxMutex = ( Queue_t * ) xMutex;
716 
717         configASSERT( pxMutex );
718 
719         /* Comments regarding mutual exclusion as per those within
720          * xQueueGiveMutexRecursive(). */
721 
722         traceTAKE_MUTEX_RECURSIVE( pxMutex );
723 
724         if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
725         {
726             ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
727             xReturn = pdPASS;
728         }
729         else
730         {
731             xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );
732 
733             /* pdPASS will only be returned if the mutex was successfully
734              * obtained.  The calling task may have entered the Blocked state
735              * before reaching here. */
736             if( xReturn != pdFAIL )
737             {
738                 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
739             }
740             else
741             {
742                 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
743             }
744         }
745 
746         return xReturn;
747     }
748 
749 #endif /* configUSE_RECURSIVE_MUTEXES */
750 /*-----------------------------------------------------------*/
751 
752 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
753 
xQueueCreateCountingSemaphoreStatic(const UBaseType_t uxMaxCount,const UBaseType_t uxInitialCount,StaticQueue_t * pxStaticQueue)754     QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
755                                                        const UBaseType_t uxInitialCount,
756                                                        StaticQueue_t * pxStaticQueue )
757     {
758         QueueHandle_t xHandle = NULL;
759 
760         configASSERT( uxMaxCount != 0 );
761         configASSERT( uxInitialCount <= uxMaxCount );
762 
763         xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
764 
765         if( xHandle != NULL )
766         {
767             ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
768 
769             traceCREATE_COUNTING_SEMAPHORE();
770         }
771         else
772         {
773             traceCREATE_COUNTING_SEMAPHORE_FAILED();
774         }
775 
776         return xHandle;
777     }
778 
779 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
780 /*-----------------------------------------------------------*/
781 
782 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
783 
xQueueCreateCountingSemaphore(const UBaseType_t uxMaxCount,const UBaseType_t uxInitialCount)784     QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
785                                                  const UBaseType_t uxInitialCount )
786     {
787         QueueHandle_t xHandle = NULL;
788 
789         configASSERT( uxMaxCount != 0 );
790         configASSERT( uxInitialCount <= uxMaxCount );
791 
792         xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
793 
794         if( xHandle != NULL )
795         {
796             ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
797 
798             traceCREATE_COUNTING_SEMAPHORE();
799         }
800         else
801         {
802             traceCREATE_COUNTING_SEMAPHORE_FAILED();
803         }
804 
805         return xHandle;
806     }
807 
808 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
809 /*-----------------------------------------------------------*/
810 
xQueueGenericSend(QueueHandle_t xQueue,const void * const pvItemToQueue,TickType_t xTicksToWait,const BaseType_t xCopyPosition)811 BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
812                               const void * const pvItemToQueue,
813                               TickType_t xTicksToWait,
814                               const BaseType_t xCopyPosition )
815 {
816     BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
817     TimeOut_t xTimeOut;
818     Queue_t * const pxQueue = xQueue;
819 
820     configASSERT( pxQueue );
821     configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
822     configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
823     #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
824         {
825             configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
826         }
827     #endif
828 
829 #if ( configUSE_MUTEXES == 1 && configCHECK_MUTEX_GIVEN_BY_OWNER == 1)
830     configASSERT(pxQueue->uxQueueType != queueQUEUE_IS_MUTEX
831                  || pxQueue->u.xSemaphore.xMutexHolder == NULL
832                  || pxQueue->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle());
833 #endif
834 
835     /*lint -save -e904 This function relaxes the coding standard somewhat to
836      * allow return statements within the function itself.  This is done in the
837      * interest of execution time efficiency. */
838     for( ; ; )
839     {
840         taskENTER_CRITICAL();
841         {
842             /* Is there room on the queue now?  The running task must be the
843              * highest priority task wanting to access the queue.  If the head item
844              * in the queue is to be overwritten then it does not matter if the
845              * queue is full. */
846             if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
847             {
848                 traceQUEUE_SEND( pxQueue );
849 
850                 #if ( configUSE_QUEUE_SETS == 1 )
851                     {
852                         UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
853 
854                         xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
855 
856                         if( pxQueue->pxQueueSetContainer != NULL )
857                         {
858                             if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
859                             {
860                                 /* Do not notify the queue set as an existing item
861                                  * was overwritten in the queue so the number of items
862                                  * in the queue has not changed. */
863                                 mtCOVERAGE_TEST_MARKER();
864                             }
865                             else if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) != pdFALSE )
866                             {
867                                 /* The queue is a member of a queue set, and posting
868                                  * to the queue set caused a higher priority task to
869                                  * unblock. A context switch is required. */
870                                 queueYIELD_IF_USING_PREEMPTION();
871                             }
872                             else
873                             {
874                                 mtCOVERAGE_TEST_MARKER();
875                             }
876                         }
877                         else
878                         {
879                             /* If there was a task waiting for data to arrive on the
880                              * queue then unblock it now. */
881                             if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
882                             {
883                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
884                                 {
885                                     /* The unblocked task has a priority higher than
886                                      * our own so yield immediately.  Yes it is ok to
887                                      * do this from within the critical section - the
888                                      * kernel takes care of that. */
889                                     queueYIELD_IF_USING_PREEMPTION();
890                                 }
891                                 else
892                                 {
893                                     mtCOVERAGE_TEST_MARKER();
894                                 }
895                             }
896                             else if( xYieldRequired != pdFALSE )
897                             {
898                                 /* This path is a special case that will only get
899                                  * executed if the task was holding multiple mutexes
900                                  * and the mutexes were given back in an order that is
901                                  * different to that in which they were taken. */
902                                 queueYIELD_IF_USING_PREEMPTION();
903                             }
904                             else
905                             {
906                                 mtCOVERAGE_TEST_MARKER();
907                             }
908                         }
909                     }
910                 #else /* configUSE_QUEUE_SETS */
911                     {
912                         xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
913 
914                         /* If there was a task waiting for data to arrive on the
915                          * queue then unblock it now. */
916                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
917                         {
918                             if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
919                             {
920                                 /* The unblocked task has a priority higher than
921                                  * our own so yield immediately.  Yes it is ok to do
922                                  * this from within the critical section - the kernel
923                                  * takes care of that. */
924                                 queueYIELD_IF_USING_PREEMPTION();
925                             }
926                             else
927                             {
928                                 mtCOVERAGE_TEST_MARKER();
929                             }
930                         }
931                         else if( xYieldRequired != pdFALSE )
932                         {
933                             /* This path is a special case that will only get
934                              * executed if the task was holding multiple mutexes and
935                              * the mutexes were given back in an order that is
936                              * different to that in which they were taken. */
937                             queueYIELD_IF_USING_PREEMPTION();
938                         }
939                         else
940                         {
941                             mtCOVERAGE_TEST_MARKER();
942                         }
943                     }
944                 #endif /* configUSE_QUEUE_SETS */
945 
946                 taskEXIT_CRITICAL();
947                 return pdPASS;
948             }
949             else
950             {
951                 if( xTicksToWait == ( TickType_t ) 0 )
952                 {
953                     /* The queue was full and no block time is specified (or
954                      * the block time has expired) so leave now. */
955                     taskEXIT_CRITICAL();
956 
957                     /* Return to the original privilege level before exiting
958                      * the function. */
959                     traceQUEUE_SEND_FAILED( pxQueue );
960                     return errQUEUE_FULL;
961                 }
962                 else if( xEntryTimeSet == pdFALSE )
963                 {
964                     /* The queue was full and a block time was specified so
965                      * configure the timeout structure. */
966                     vTaskInternalSetTimeOutState( &xTimeOut );
967                     xEntryTimeSet = pdTRUE;
968                 }
969                 else
970                 {
971                     /* Entry time was already set. */
972                     mtCOVERAGE_TEST_MARKER();
973                 }
974             }
975         }
976         taskEXIT_CRITICAL();
977 
978         /* Interrupts and other tasks can send to and receive from the queue
979          * now the critical section has been exited. */
980 
981 #ifdef ESP_PLATFORM // IDF-3755
982         taskENTER_CRITICAL();
983 #else
984         vTaskSuspendAll();
985 #endif // ESP_PLATFORM
986         prvLockQueue( pxQueue );
987 
988         /* Update the timeout state to see if it has expired yet. */
989         if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
990         {
991             if( prvIsQueueFull( pxQueue ) != pdFALSE )
992             {
993                 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
994                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
995 
996                 /* Unlocking the queue means queue events can effect the
997                  * event list.  It is possible that interrupts occurring now
998                  * remove this task from the event list again - but as the
999                  * scheduler is suspended the task will go onto the pending
1000                  * ready list instead of the actual ready list. */
1001                 prvUnlockQueue( pxQueue );
1002 
1003                 /* Resuming the scheduler will move tasks from the pending
1004                  * ready list into the ready list - so it is feasible that this
1005                  * task is already in the ready list before it yields - in which
1006                  * case the yield will not cause a context switch unless there
1007                  * is also a higher priority task in the pending ready list. */
1008 #ifdef ESP_PLATFORM // IDF-3755
1009                 taskEXIT_CRITICAL();
1010 #else
1011                 if( xTaskResumeAll() == pdFALSE )
1012 #endif // ESP_PLATFORM
1013                 {
1014                     portYIELD_WITHIN_API();
1015                 }
1016 
1017             }
1018             else
1019             {
1020                 /* Try again. */
1021                 prvUnlockQueue( pxQueue );
1022 #ifdef ESP_PLATFORM // IDF-3755
1023                 taskEXIT_CRITICAL();
1024 #else
1025                 ( void ) xTaskResumeAll();
1026 #endif // ESP_PLATFORM
1027             }
1028         }
1029         else
1030         {
1031             /* The timeout has expired. */
1032             prvUnlockQueue( pxQueue );
1033 #ifdef ESP_PLATFORM // IDF-3755
1034             taskEXIT_CRITICAL();
1035 #else
1036             ( void ) xTaskResumeAll();
1037 #endif // ESP_PLATFORM
1038 
1039             traceQUEUE_SEND_FAILED( pxQueue );
1040             return errQUEUE_FULL;
1041         }
1042     } /*lint -restore */
1043 }
1044 /*-----------------------------------------------------------*/
1045 
xQueueGenericSendFromISR(QueueHandle_t xQueue,const void * const pvItemToQueue,BaseType_t * const pxHigherPriorityTaskWoken,const BaseType_t xCopyPosition)1046 BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,
1047                                      const void * const pvItemToQueue,
1048                                      BaseType_t * const pxHigherPriorityTaskWoken,
1049                                      const BaseType_t xCopyPosition )
1050 {
1051     BaseType_t xReturn;
1052     UBaseType_t uxSavedInterruptStatus;
1053     Queue_t * const pxQueue = xQueue;
1054 
1055     configASSERT( pxQueue );
1056     configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1057     configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
1058 
1059     /* RTOS ports that support interrupt nesting have the concept of a maximum
1060      * system call (or maximum API call) interrupt priority.  Interrupts that are
1061      * above the maximum system call priority are kept permanently enabled, even
1062      * when the RTOS kernel is in a critical section, but cannot make any calls to
1063      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h
1064      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1065      * failure if a FreeRTOS API function is called from an interrupt that has been
1066      * assigned a priority above the configured maximum system call priority.
1067      * Only FreeRTOS functions that end in FromISR can be called from interrupts
1068      * that have been assigned a priority at or (logically) below the maximum
1069      * system call interrupt priority.  FreeRTOS maintains a separate interrupt
1070      * safe API to ensure interrupt entry is as fast and as simple as possible.
1071      * More information (albeit Cortex-M specific) is provided on the following
1072      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1073     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1074 
1075     /* Similar to xQueueGenericSend, except without blocking if there is no room
1076      * in the queue.  Also don't directly wake a task that was blocked on a queue
1077      * read, instead return a flag to say whether a context switch is required or
1078      * not (i.e. has a task with a higher priority than us been woken by this
1079      * post). */
1080     uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1081     {
1082         taskENTER_CRITICAL_ISR();
1083 
1084         if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
1085         {
1086             const int8_t cTxLock = pxQueue->cTxLock;
1087 
1088             traceQUEUE_SEND_FROM_ISR( pxQueue );
1089 
1090             /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a
1091              *  semaphore or mutex.  That means prvCopyDataToQueue() cannot result
1092              *  in a task disinheriting a priority and prvCopyDataToQueue() can be
1093              *  called here even though the disinherit function does not check if
1094              *  the scheduler is suspended before accessing the ready lists. */
1095             ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
1096 
1097             /* The event list is not altered if the queue is locked.  This will
1098              * be done when the queue is unlocked later. */
1099             if( cTxLock == queueUNLOCKED )
1100             {
1101                 #if ( configUSE_QUEUE_SETS == 1 )
1102                     {
1103                         if( pxQueue->pxQueueSetContainer != NULL )
1104                         {
1105                             if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) != pdFALSE )
1106                             {
1107                                 /* The queue is a member of a queue set, and posting
1108                                  * to the queue set caused a higher priority task to
1109                                  * unblock.  A context switch is required. */
1110                                 if( pxHigherPriorityTaskWoken != NULL )
1111                                 {
1112                                     *pxHigherPriorityTaskWoken = pdTRUE;
1113                                 }
1114                                 else
1115                                 {
1116                                     mtCOVERAGE_TEST_MARKER();
1117                                 }
1118                             }
1119                             else
1120                             {
1121                                 mtCOVERAGE_TEST_MARKER();
1122                             }
1123                         }
1124                         else
1125                         {
1126                             if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1127                             {
1128                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1129                                 {
1130                                     /* The task waiting has a higher priority so
1131                                      *  record that a context switch is required. */
1132                                     if( pxHigherPriorityTaskWoken != NULL )
1133                                     {
1134                                         *pxHigherPriorityTaskWoken = pdTRUE;
1135                                     }
1136                                     else
1137                                     {
1138                                         mtCOVERAGE_TEST_MARKER();
1139                                     }
1140                                 }
1141                                 else
1142                                 {
1143                                     mtCOVERAGE_TEST_MARKER();
1144                                 }
1145                             }
1146                             else
1147                             {
1148                                 mtCOVERAGE_TEST_MARKER();
1149                             }
1150                         }
1151                     }
1152                 #else /* configUSE_QUEUE_SETS */
1153                     {
1154                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1155                         {
1156                             if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1157                             {
1158                                 /* The task waiting has a higher priority so record that a
1159                                  * context switch is required. */
1160                                 if( pxHigherPriorityTaskWoken != NULL )
1161                                 {
1162                                     *pxHigherPriorityTaskWoken = pdTRUE;
1163                                 }
1164                                 else
1165                                 {
1166                                     mtCOVERAGE_TEST_MARKER();
1167                                 }
1168                             }
1169                             else
1170                             {
1171                                 mtCOVERAGE_TEST_MARKER();
1172                             }
1173                         }
1174                         else
1175                         {
1176                             mtCOVERAGE_TEST_MARKER();
1177                         }
1178                     }
1179                 #endif /* configUSE_QUEUE_SETS */
1180             }
1181             else
1182             {
1183                 /* Increment the lock count so the task that unlocks the queue
1184                  * knows that data was posted while it was locked. */
1185                 pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );
1186             }
1187 
1188             xReturn = pdPASS;
1189         }
1190         else
1191         {
1192             traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1193             xReturn = errQUEUE_FULL;
1194         }
1195 
1196         taskEXIT_CRITICAL_ISR();
1197     }
1198     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1199 
1200     return xReturn;
1201 }
1202 /*-----------------------------------------------------------*/
1203 
xQueueGiveFromISR(QueueHandle_t xQueue,BaseType_t * const pxHigherPriorityTaskWoken)1204 BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,
1205                               BaseType_t * const pxHigherPriorityTaskWoken )
1206 {
1207     BaseType_t xReturn;
1208     UBaseType_t uxSavedInterruptStatus;
1209     Queue_t * const pxQueue = xQueue;
1210 
1211     /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
1212      * item size is 0.  Don't directly wake a task that was blocked on a queue
1213      * read, instead return a flag to say whether a context switch is required or
1214      * not (i.e. has a task with a higher priority than us been woken by this
1215      * post). */
1216 
1217     configASSERT( pxQueue );
1218 
1219     /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()
1220      * if the item size is not 0. */
1221     configASSERT( pxQueue->uxItemSize == 0 );
1222 
1223     /* Normally a mutex would not be given from an interrupt, especially if
1224      * there is a mutex holder, as priority inheritance makes no sense for an
1225      * interrupts, only tasks. */
1226     configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->u.xSemaphore.xMutexHolder != NULL ) ) );
1227 
1228     /* RTOS ports that support interrupt nesting have the concept of a maximum
1229      * system call (or maximum API call) interrupt priority.  Interrupts that are
1230      * above the maximum system call priority are kept permanently enabled, even
1231      * when the RTOS kernel is in a critical section, but cannot make any calls to
1232      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h
1233      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1234      * failure if a FreeRTOS API function is called from an interrupt that has been
1235      * assigned a priority above the configured maximum system call priority.
1236      * Only FreeRTOS functions that end in FromISR can be called from interrupts
1237      * that have been assigned a priority at or (logically) below the maximum
1238      * system call interrupt priority.  FreeRTOS maintains a separate interrupt
1239      * safe API to ensure interrupt entry is as fast and as simple as possible.
1240      * More information (albeit Cortex-M specific) is provided on the following
1241      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1242     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1243 
1244     uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1245     {
1246         taskENTER_CRITICAL_ISR();
1247 
1248         const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1249 
1250         /* When the queue is used to implement a semaphore no data is ever
1251          * moved through the queue but it is still valid to see if the queue 'has
1252          * space'. */
1253         if( uxMessagesWaiting < pxQueue->uxLength )
1254         {
1255             const int8_t cTxLock = pxQueue->cTxLock;
1256 
1257             traceQUEUE_GIVE_FROM_ISR( pxQueue );
1258 
1259             /* A task can only have an inherited priority if it is a mutex
1260              * holder - and if there is a mutex holder then the mutex cannot be
1261              * given from an ISR.  As this is the ISR version of the function it
1262              * can be assumed there is no mutex holder and no need to determine if
1263              * priority disinheritance is needed.  Simply increase the count of
1264              * messages (semaphores) available. */
1265             pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
1266 
1267             /* The event list is not altered if the queue is locked.  This will
1268              * be done when the queue is unlocked later. */
1269             if( cTxLock == queueUNLOCKED )
1270             {
1271                 #if ( configUSE_QUEUE_SETS == 1 )
1272                     {
1273                         if( pxQueue->pxQueueSetContainer != NULL )
1274                         {
1275                             if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) != pdFALSE )
1276                             {
1277                                 /* The semaphore is a member of a queue set, and
1278                                  * posting to the queue set caused a higher priority
1279                                  * task to unblock.  A context switch is required. */
1280                                 if( pxHigherPriorityTaskWoken != NULL )
1281                                 {
1282                                     *pxHigherPriorityTaskWoken = pdTRUE;
1283                                 }
1284                                 else
1285                                 {
1286                                     mtCOVERAGE_TEST_MARKER();
1287                                 }
1288                             }
1289                             else
1290                             {
1291                                 mtCOVERAGE_TEST_MARKER();
1292                             }
1293                         }
1294                         else
1295                         {
1296                             if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1297                             {
1298                                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1299                                 {
1300                                     /* The task waiting has a higher priority so
1301                                      *  record that a context switch is required. */
1302                                     if( pxHigherPriorityTaskWoken != NULL )
1303                                     {
1304                                         *pxHigherPriorityTaskWoken = pdTRUE;
1305                                     }
1306                                     else
1307                                     {
1308                                         mtCOVERAGE_TEST_MARKER();
1309                                     }
1310                                 }
1311                                 else
1312                                 {
1313                                     mtCOVERAGE_TEST_MARKER();
1314                                 }
1315                             }
1316                             else
1317                             {
1318                                 mtCOVERAGE_TEST_MARKER();
1319                             }
1320                         }
1321                     }
1322                 #else /* configUSE_QUEUE_SETS */
1323                     {
1324                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1325                         {
1326                             if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1327                             {
1328                                 /* The task waiting has a higher priority so record that a
1329                                  * context switch is required. */
1330                                 if( pxHigherPriorityTaskWoken != NULL )
1331                                 {
1332                                     *pxHigherPriorityTaskWoken = pdTRUE;
1333                                 }
1334                                 else
1335                                 {
1336                                     mtCOVERAGE_TEST_MARKER();
1337                                 }
1338                             }
1339                             else
1340                             {
1341                                 mtCOVERAGE_TEST_MARKER();
1342                             }
1343                         }
1344                         else
1345                         {
1346                             mtCOVERAGE_TEST_MARKER();
1347                         }
1348                     }
1349                 #endif /* configUSE_QUEUE_SETS */
1350             }
1351             else
1352             {
1353                 /* Increment the lock count so the task that unlocks the queue
1354                  * knows that data was posted while it was locked. */
1355                 pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );
1356             }
1357 
1358             xReturn = pdPASS;
1359         }
1360         else
1361         {
1362             traceQUEUE_GIVE_FROM_ISR_FAILED( pxQueue );
1363             xReturn = errQUEUE_FULL;
1364         }
1365         taskEXIT_CRITICAL_ISR();
1366     }
1367     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1368 
1369     return xReturn;
1370 }
1371 /*-----------------------------------------------------------*/
1372 
xQueueReceive(QueueHandle_t xQueue,void * const pvBuffer,TickType_t xTicksToWait)1373 BaseType_t xQueueReceive( QueueHandle_t xQueue,
1374                           void * const pvBuffer,
1375                           TickType_t xTicksToWait )
1376 {
1377     BaseType_t xEntryTimeSet = pdFALSE;
1378     TimeOut_t xTimeOut;
1379     Queue_t * const pxQueue = xQueue;
1380 
1381     /* Check the pointer is not NULL. */
1382     configASSERT( ( pxQueue ) );
1383 
1384     /* The buffer into which data is received can only be NULL if the data size
1385      * is zero (so no data is copied into the buffer). */
1386     configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1387 
1388     /* Cannot block if the scheduler is suspended. */
1389     #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1390         {
1391             configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1392         }
1393     #endif
1394 
1395     /*lint -save -e904  This function relaxes the coding standard somewhat to
1396      * allow return statements within the function itself.  This is done in the
1397      * interest of execution time efficiency. */
1398     for( ; ; )
1399     {
1400         taskENTER_CRITICAL();
1401         {
1402             const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1403 
1404             /* Is there data in the queue now?  To be running the calling task
1405              * must be the highest priority task wanting to access the queue. */
1406             if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1407             {
1408                 /* Data available, remove one item. */
1409                 prvCopyDataFromQueue( pxQueue, pvBuffer );
1410                 traceQUEUE_RECEIVE( pxQueue );
1411                 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
1412 
1413                 /* There is now space in the queue, were any tasks waiting to
1414                  * post to the queue?  If so, unblock the highest priority waiting
1415                  * task. */
1416                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1417                 {
1418                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1419                     {
1420                         queueYIELD_IF_USING_PREEMPTION();
1421                     }
1422                     else
1423                     {
1424                         mtCOVERAGE_TEST_MARKER();
1425                     }
1426                 }
1427                 else
1428                 {
1429                     mtCOVERAGE_TEST_MARKER();
1430                 }
1431 
1432                 taskEXIT_CRITICAL();
1433                 return pdPASS;
1434             }
1435             else
1436             {
1437                 if( xTicksToWait == ( TickType_t ) 0 )
1438                 {
1439                     /* The queue was empty and no block time is specified (or
1440                      * the block time has expired) so leave now. */
1441                     taskEXIT_CRITICAL();
1442                     traceQUEUE_RECEIVE_FAILED( pxQueue );
1443                     return errQUEUE_EMPTY;
1444                 }
1445                 else if( xEntryTimeSet == pdFALSE )
1446                 {
1447                     /* The queue was empty and a block time was specified so
1448                      * configure the timeout structure. */
1449                     vTaskInternalSetTimeOutState( &xTimeOut );
1450                     xEntryTimeSet = pdTRUE;
1451                 }
1452                 else
1453                 {
1454                     /* Entry time was already set. */
1455                     mtCOVERAGE_TEST_MARKER();
1456                 }
1457             }
1458         }
1459         taskEXIT_CRITICAL();
1460 
1461         /* Interrupts and other tasks can send to and receive from the queue
1462          * now the critical section has been exited. */
1463 
1464 #ifdef ESP_PLATFORM // IDF-3755
1465         taskENTER_CRITICAL();
1466 #else
1467         vTaskSuspendAll();
1468 #endif // ESP_PLATFORM
1469         prvLockQueue( pxQueue );
1470 
1471         /* Update the timeout state to see if it has expired yet. */
1472         if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1473         {
1474             /* The timeout has not expired.  If the queue is still empty place
1475              * the task on the list of tasks waiting to receive from the queue. */
1476             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1477             {
1478                 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1479                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1480                 prvUnlockQueue( pxQueue );
1481 #ifdef ESP_PLATFORM // IDF-3755
1482                 taskEXIT_CRITICAL();
1483 #else
1484                 if( xTaskResumeAll() == pdFALSE )
1485 #endif // ESP_PLATFORM
1486                 {
1487                     portYIELD_WITHIN_API();
1488                 }
1489 #ifndef ESP_PLATFORM
1490                 else
1491                 {
1492                     mtCOVERAGE_TEST_MARKER();
1493                 }
1494 #endif // ESP_PLATFORM
1495             }
1496             else
1497             {
1498                 /* The queue contains data again.  Loop back to try and read the
1499                  * data. */
1500                 prvUnlockQueue( pxQueue );
1501 #ifdef ESP_PLATFORM // IDF-3755
1502                 taskEXIT_CRITICAL();
1503 #else
1504                 ( void ) xTaskResumeAll();
1505 #endif // ESP_PLATFORM
1506             }
1507         }
1508         else
1509         {
1510             /* Timed out.  If there is no data in the queue exit, otherwise loop
1511              * back and attempt to read the data. */
1512             prvUnlockQueue( pxQueue );
1513 #ifdef ESP_PLATFORM // IDF-3755
1514             taskEXIT_CRITICAL();
1515 #else
1516             ( void ) xTaskResumeAll();
1517 #endif // ESP_PLATFORM
1518 
1519             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1520             {
1521                 traceQUEUE_RECEIVE_FAILED( pxQueue );
1522                 return errQUEUE_EMPTY;
1523             }
1524             else
1525             {
1526                 mtCOVERAGE_TEST_MARKER();
1527             }
1528         }
1529     } /*lint -restore */
1530 }
1531 /*-----------------------------------------------------------*/
1532 
xQueueSemaphoreTake(QueueHandle_t xQueue,TickType_t xTicksToWait)1533 BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
1534                                 TickType_t xTicksToWait )
1535 {
1536     BaseType_t xEntryTimeSet = pdFALSE;
1537     TimeOut_t xTimeOut;
1538     Queue_t * const pxQueue = xQueue;
1539 
1540     #if ( configUSE_MUTEXES == 1 )
1541         BaseType_t xInheritanceOccurred = pdFALSE;
1542     #endif
1543 
1544     /* Check the queue pointer is not NULL. */
1545     configASSERT( ( pxQueue ) );
1546 
1547     /* Check this really is a semaphore, in which case the item size will be
1548      * 0. */
1549     configASSERT( pxQueue->uxItemSize == 0 );
1550 
1551     /* Cannot block if the scheduler is suspended. */
1552     #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1553         {
1554             configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1555         }
1556     #endif
1557 
1558     /*lint -save -e904 This function relaxes the coding standard somewhat to allow return
1559      * statements within the function itself.  This is done in the interest
1560      * of execution time efficiency. */
1561     for( ; ; )
1562     {
1563         taskENTER_CRITICAL();
1564         {
1565             /* Semaphores are queues with an item size of 0, and where the
1566              * number of messages in the queue is the semaphore's count value. */
1567             const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;
1568 
1569             /* Is there data in the queue now?  To be running the calling task
1570              * must be the highest priority task wanting to access the queue. */
1571             if( uxSemaphoreCount > ( UBaseType_t ) 0 )
1572             {
1573                 traceQUEUE_SEMAPHORE_RECEIVE( pxQueue );
1574 
1575                 /* Semaphores are queues with a data size of zero and where the
1576                  * messages waiting is the semaphore's count.  Reduce the count. */
1577                 pxQueue->uxMessagesWaiting = uxSemaphoreCount - ( UBaseType_t ) 1;
1578 
1579                 #if ( configUSE_MUTEXES == 1 )
1580                     {
1581                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1582                         {
1583                             /* Record the information required to implement
1584                              * priority inheritance should it become necessary. */
1585                             pxQueue->u.xSemaphore.xMutexHolder = pvTaskIncrementMutexHeldCount();
1586                         }
1587                         else
1588                         {
1589                             mtCOVERAGE_TEST_MARKER();
1590                         }
1591                     }
1592                 #endif /* configUSE_MUTEXES */
1593 
1594                 /* Check to see if other tasks are blocked waiting to give the
1595                  * semaphore, and if so, unblock the highest priority such task. */
1596                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1597                 {
1598                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1599                     {
1600                         queueYIELD_IF_USING_PREEMPTION();
1601                     }
1602                     else
1603                     {
1604                         mtCOVERAGE_TEST_MARKER();
1605                     }
1606                 }
1607                 else
1608                 {
1609                     mtCOVERAGE_TEST_MARKER();
1610                 }
1611 
1612                 taskEXIT_CRITICAL();
1613                 return pdPASS;
1614             }
1615             else
1616             {
1617                 if( xTicksToWait == ( TickType_t ) 0 )
1618                 {
1619                     /* For inheritance to have occurred there must have been an
1620                      * initial timeout, and an adjusted timeout cannot become 0, as
1621                      * if it were 0 the function would have exited. */
1622                     #if ( configUSE_MUTEXES == 1 )
1623                         {
1624                             configASSERT( xInheritanceOccurred == pdFALSE );
1625                         }
1626                     #endif /* configUSE_MUTEXES */
1627 
1628                     /* The semaphore count was 0 and no block time is specified
1629                      * (or the block time has expired) so exit now. */
1630                     taskEXIT_CRITICAL();
1631                     traceQUEUE_RECEIVE_FAILED( pxQueue );
1632                     return errQUEUE_EMPTY;
1633                 }
1634                 else if( xEntryTimeSet == pdFALSE )
1635                 {
1636                     /* The semaphore count was 0 and a block time was specified
1637                      * so configure the timeout structure ready to block. */
1638                     vTaskInternalSetTimeOutState( &xTimeOut );
1639                     xEntryTimeSet = pdTRUE;
1640                 }
1641                 else
1642                 {
1643                     /* Entry time was already set. */
1644                     mtCOVERAGE_TEST_MARKER();
1645                 }
1646             }
1647         }
1648         taskEXIT_CRITICAL();
1649 
1650         /* Interrupts and other tasks can give to and take from the semaphore
1651          * now the critical section has been exited. */
1652 
1653 #ifdef ESP_PLATFORM // IDF-3755
1654         taskENTER_CRITICAL();
1655 #else
1656         vTaskSuspendAll();
1657 #endif // ESP_PLATFORM
1658         prvLockQueue( pxQueue );
1659 
1660         /* Update the timeout state to see if it has expired yet. */
1661         if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1662         {
1663             /* A block time is specified and not expired.  If the semaphore
1664              * count is 0 then enter the Blocked state to wait for a semaphore to
1665              * become available.  As semaphores are implemented with queues the
1666              * queue being empty is equivalent to the semaphore count being 0. */
1667             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1668             {
1669                 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1670 
1671                 #if ( configUSE_MUTEXES == 1 )
1672                     {
1673                         if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1674                         {
1675                             taskENTER_CRITICAL();
1676                             {
1677                                 xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );
1678                             }
1679                             taskEXIT_CRITICAL();
1680                         }
1681                         else
1682                         {
1683                             mtCOVERAGE_TEST_MARKER();
1684                         }
1685                     }
1686                 #endif /* if ( configUSE_MUTEXES == 1 ) */
1687 
1688                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1689                 prvUnlockQueue( pxQueue );
1690 #ifdef ESP_PLATFORM // IDF-3755
1691                 taskEXIT_CRITICAL();
1692 #else
1693                 if( xTaskResumeAll() == pdFALSE )
1694 #endif // ESP_PLATFORM
1695                 {
1696                     portYIELD_WITHIN_API();
1697                 }
1698 #ifndef ESP_PLATFORM
1699                 else
1700                 {
1701                     mtCOVERAGE_TEST_MARKER();
1702                 }
1703 #endif // ESP_PLATFORM
1704             }
1705             else
1706             {
1707                 /* There was no timeout and the semaphore count was not 0, so
1708                  * attempt to take the semaphore again. */
1709                 prvUnlockQueue( pxQueue );
1710 #ifdef ESP_PLATFORM // IDF-3755
1711                 taskEXIT_CRITICAL();
1712 #else
1713                 ( void ) xTaskResumeAll();
1714 #endif // ESP_PLATFORM
1715             }
1716         }
1717         else
1718         {
1719             /* Timed out. */
1720             prvUnlockQueue( pxQueue );
1721 #ifdef ESP_PLATFORM // IDF-3755
1722             taskEXIT_CRITICAL();
1723 #else
1724             ( void ) xTaskResumeAll();
1725 #endif // ESP_PLATFORM
1726 
1727             /* If the semaphore count is 0 exit now as the timeout has
1728              * expired.  Otherwise return to attempt to take the semaphore that is
1729              * known to be available.  As semaphores are implemented by queues the
1730              * queue being empty is equivalent to the semaphore count being 0. */
1731             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1732             {
1733                 #if ( configUSE_MUTEXES == 1 )
1734                     {
1735                         /* xInheritanceOccurred could only have be set if
1736                          * pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
1737                          * test the mutex type again to check it is actually a mutex. */
1738                         if( xInheritanceOccurred != pdFALSE )
1739                         {
1740                             taskENTER_CRITICAL();
1741                             {
1742                                 UBaseType_t uxHighestWaitingPriority;
1743 
1744                                 /* This task blocking on the mutex caused another
1745                                  * task to inherit this task's priority.  Now this task
1746                                  * has timed out the priority should be disinherited
1747                                  * again, but only as low as the next highest priority
1748                                  * task that is waiting for the same mutex. */
1749                                 uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );
1750                                 vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );
1751                             }
1752                             taskEXIT_CRITICAL();
1753                         }
1754                     }
1755                 #endif /* configUSE_MUTEXES */
1756 
1757                 traceQUEUE_RECEIVE_FAILED( pxQueue );
1758                 return errQUEUE_EMPTY;
1759             }
1760             else
1761             {
1762                 mtCOVERAGE_TEST_MARKER();
1763             }
1764         }
1765     } /*lint -restore */
1766 }
1767 /*-----------------------------------------------------------*/
1768 
xQueuePeek(QueueHandle_t xQueue,void * const pvBuffer,TickType_t xTicksToWait)1769 BaseType_t xQueuePeek( QueueHandle_t xQueue,
1770                        void * const pvBuffer,
1771                        TickType_t xTicksToWait )
1772 {
1773     BaseType_t xEntryTimeSet = pdFALSE;
1774     TimeOut_t xTimeOut;
1775     int8_t * pcOriginalReadPosition;
1776     Queue_t * const pxQueue = xQueue;
1777 
1778     /* Check the pointer is not NULL. */
1779     configASSERT( ( pxQueue ) );
1780 
1781     /* The buffer into which data is received can only be NULL if the data size
1782      * is zero (so no data is copied into the buffer. */
1783     configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1784 
1785     /* Cannot block if the scheduler is suspended. */
1786     #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1787         {
1788             configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1789         }
1790     #endif
1791 
1792     /*lint -save -e904  This function relaxes the coding standard somewhat to
1793      * allow return statements within the function itself.  This is done in the
1794      * interest of execution time efficiency. */
1795     for( ; ; )
1796     {
1797         taskENTER_CRITICAL();
1798         {
1799             const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1800 
1801             /* Is there data in the queue now?  To be running the calling task
1802              * must be the highest priority task wanting to access the queue. */
1803             if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1804             {
1805                 /* Remember the read position so it can be reset after the data
1806                  * is read from the queue as this function is only peeking the
1807                  * data, not removing it. */
1808                 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
1809 
1810                 prvCopyDataFromQueue( pxQueue, pvBuffer );
1811                 traceQUEUE_PEEK( pxQueue );
1812 
1813                 /* The data is not being removed, so reset the read pointer. */
1814                 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
1815 
1816                 /* The data is being left in the queue, so see if there are
1817                  * any other tasks waiting for the data. */
1818                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1819                 {
1820                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1821                     {
1822                         /* The task waiting has a higher priority than this task. */
1823                         queueYIELD_IF_USING_PREEMPTION();
1824                     }
1825                     else
1826                     {
1827                         mtCOVERAGE_TEST_MARKER();
1828                     }
1829                 }
1830                 else
1831                 {
1832                     mtCOVERAGE_TEST_MARKER();
1833                 }
1834 
1835                 taskEXIT_CRITICAL();
1836                 return pdPASS;
1837             }
1838             else
1839             {
1840                 if( xTicksToWait == ( TickType_t ) 0 )
1841                 {
1842                     /* The queue was empty and no block time is specified (or
1843                      * the block time has expired) so leave now. */
1844                     taskEXIT_CRITICAL();
1845                     traceQUEUE_PEEK_FAILED( pxQueue );
1846                     return errQUEUE_EMPTY;
1847                 }
1848                 else if( xEntryTimeSet == pdFALSE )
1849                 {
1850                     /* The queue was empty and a block time was specified so
1851                      * configure the timeout structure ready to enter the blocked
1852                      * state. */
1853                     vTaskInternalSetTimeOutState( &xTimeOut );
1854                     xEntryTimeSet = pdTRUE;
1855                 }
1856                 else
1857                 {
1858                     /* Entry time was already set. */
1859                     mtCOVERAGE_TEST_MARKER();
1860                 }
1861             }
1862         }
1863         taskEXIT_CRITICAL();
1864 
1865         /* Interrupts and other tasks can send to and receive from the queue
1866          * now the critical section has been exited. */
1867 
1868 #ifdef ESP_PLATFORM // IDF-3755
1869         taskENTER_CRITICAL();
1870 #else
1871         vTaskSuspendAll();
1872 #endif // ESP_PLATFORM
1873         prvLockQueue( pxQueue );
1874 
1875         /* Update the timeout state to see if it has expired yet. */
1876         if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1877         {
1878             /* Timeout has not expired yet, check to see if there is data in the
1879             * queue now, and if not enter the Blocked state to wait for data. */
1880             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1881             {
1882                 traceBLOCKING_ON_QUEUE_PEEK( pxQueue );
1883                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1884                 prvUnlockQueue( pxQueue );
1885 #ifdef ESP_PLATFORM // IDF-3755
1886                 taskEXIT_CRITICAL();
1887 #else
1888                 if( xTaskResumeAll() == pdFALSE )
1889 #endif // ESP_PLATFORM
1890                 {
1891                     portYIELD_WITHIN_API();
1892                 }
1893 #ifndef ESP_PLATFORM
1894                 else
1895                 {
1896                     mtCOVERAGE_TEST_MARKER();
1897                 }
1898 #endif // ESP_PLATFORM
1899             }
1900             else
1901             {
1902                 /* There is data in the queue now, so don't enter the blocked
1903                  * state, instead return to try and obtain the data. */
1904                 prvUnlockQueue( pxQueue );
1905 #ifdef ESP_PLATFORM // IDF-3755
1906                 taskEXIT_CRITICAL();
1907 #else
1908                 ( void ) xTaskResumeAll();
1909 #endif // ESP_PLATFORM
1910             }
1911         }
1912         else
1913         {
1914             /* The timeout has expired.  If there is still no data in the queue
1915              * exit, otherwise go back and try to read the data again. */
1916             prvUnlockQueue( pxQueue );
1917 #ifdef ESP_PLATFORM // IDF-3755
1918             taskEXIT_CRITICAL();
1919 #else
1920             ( void ) xTaskResumeAll();
1921 #endif // ESP_PLATFORM
1922 
1923             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1924             {
1925                 traceQUEUE_PEEK_FAILED( pxQueue );
1926                 return errQUEUE_EMPTY;
1927             }
1928             else
1929             {
1930                 mtCOVERAGE_TEST_MARKER();
1931             }
1932         }
1933     } /*lint -restore */
1934 }
1935 /*-----------------------------------------------------------*/
1936 
xQueueReceiveFromISR(QueueHandle_t xQueue,void * const pvBuffer,BaseType_t * const pxHigherPriorityTaskWoken)1937 BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,
1938                                  void * const pvBuffer,
1939                                  BaseType_t * const pxHigherPriorityTaskWoken )
1940 {
1941     BaseType_t xReturn;
1942     UBaseType_t uxSavedInterruptStatus;
1943     Queue_t * const pxQueue = xQueue;
1944 
1945     configASSERT( pxQueue );
1946     configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1947 
1948     /* RTOS ports that support interrupt nesting have the concept of a maximum
1949      * system call (or maximum API call) interrupt priority.  Interrupts that are
1950      * above the maximum system call priority are kept permanently enabled, even
1951      * when the RTOS kernel is in a critical section, but cannot make any calls to
1952      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h
1953      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1954      * failure if a FreeRTOS API function is called from an interrupt that has been
1955      * assigned a priority above the configured maximum system call priority.
1956      * Only FreeRTOS functions that end in FromISR can be called from interrupts
1957      * that have been assigned a priority at or (logically) below the maximum
1958      * system call interrupt priority.  FreeRTOS maintains a separate interrupt
1959      * safe API to ensure interrupt entry is as fast and as simple as possible.
1960      * More information (albeit Cortex-M specific) is provided on the following
1961      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1962     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1963 
1964     uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1965     {
1966         taskENTER_CRITICAL_ISR();
1967 
1968         const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1969 
1970         /* Cannot block in an ISR, so check there is data available. */
1971         if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1972         {
1973             const int8_t cRxLock = pxQueue->cRxLock;
1974 
1975             traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
1976 
1977             prvCopyDataFromQueue( pxQueue, pvBuffer );
1978             pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
1979 
1980             /* If the queue is locked the event list will not be modified.
1981              * Instead update the lock count so the task that unlocks the queue
1982              * will know that an ISR has removed data while the queue was
1983              * locked. */
1984             if( cRxLock == queueUNLOCKED )
1985             {
1986                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1987                 {
1988                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1989                     {
1990                         /* The task waiting has a higher priority than us so
1991                          * force a context switch. */
1992                         if( pxHigherPriorityTaskWoken != NULL )
1993                         {
1994                             *pxHigherPriorityTaskWoken = pdTRUE;
1995                         }
1996                         else
1997                         {
1998                             mtCOVERAGE_TEST_MARKER();
1999                         }
2000                     }
2001                     else
2002                     {
2003                         mtCOVERAGE_TEST_MARKER();
2004                     }
2005                 }
2006                 else
2007                 {
2008                     mtCOVERAGE_TEST_MARKER();
2009                 }
2010             }
2011             else
2012             {
2013                 /* Increment the lock count so the task that unlocks the queue
2014                  * knows that data was removed while it was locked. */
2015                 pxQueue->cRxLock = ( int8_t ) ( cRxLock + 1 );
2016             }
2017 
2018             xReturn = pdPASS;
2019         }
2020         else
2021         {
2022             xReturn = pdFAIL;
2023             traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
2024         }
2025         taskEXIT_CRITICAL_ISR();
2026     }
2027     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2028 
2029     return xReturn;
2030 }
2031 /*-----------------------------------------------------------*/
2032 
xQueuePeekFromISR(QueueHandle_t xQueue,void * const pvBuffer)2033 BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,
2034                               void * const pvBuffer )
2035 {
2036     BaseType_t xReturn;
2037     UBaseType_t uxSavedInterruptStatus;
2038     int8_t * pcOriginalReadPosition;
2039     Queue_t * const pxQueue = xQueue;
2040 
2041     configASSERT( pxQueue );
2042     configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
2043     configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
2044 
2045     /* RTOS ports that support interrupt nesting have the concept of a maximum
2046      * system call (or maximum API call) interrupt priority.  Interrupts that are
2047      * above the maximum system call priority are kept permanently enabled, even
2048      * when the RTOS kernel is in a critical section, but cannot make any calls to
2049      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h
2050      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2051      * failure if a FreeRTOS API function is called from an interrupt that has been
2052      * assigned a priority above the configured maximum system call priority.
2053      * Only FreeRTOS functions that end in FromISR can be called from interrupts
2054      * that have been assigned a priority at or (logically) below the maximum
2055      * system call interrupt priority.  FreeRTOS maintains a separate interrupt
2056      * safe API to ensure interrupt entry is as fast and as simple as possible.
2057      * More information (albeit Cortex-M specific) is provided on the following
2058      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2059     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2060 
2061     uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2062     taskENTER_CRITICAL_ISR();
2063     {
2064         /* Cannot block in an ISR, so check there is data available. */
2065         if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2066         {
2067             traceQUEUE_PEEK_FROM_ISR( pxQueue );
2068 
2069             /* Remember the read position so it can be reset as nothing is
2070              * actually being removed from the queue. */
2071             pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
2072             prvCopyDataFromQueue( pxQueue, pvBuffer );
2073             pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
2074 
2075             xReturn = pdPASS;
2076         }
2077         else
2078         {
2079             xReturn = pdFAIL;
2080             traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
2081         }
2082     }
2083     taskEXIT_CRITICAL_ISR();
2084     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2085 
2086     return xReturn;
2087 }
2088 /*-----------------------------------------------------------*/
2089 
uxQueueMessagesWaiting(const QueueHandle_t xQueue)2090 UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
2091 {
2092     UBaseType_t uxReturn;
2093     Queue_t * const pxQueue = ( Queue_t * ) xQueue;
2094 
2095     configASSERT( xQueue );
2096 
2097     taskENTER_CRITICAL();
2098     {
2099         uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
2100     }
2101     taskEXIT_CRITICAL();
2102 
2103     return uxReturn;
2104 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2105 /*-----------------------------------------------------------*/
2106 
uxQueueSpacesAvailable(const QueueHandle_t xQueue)2107 UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
2108 {
2109     UBaseType_t uxReturn;
2110     Queue_t * const pxQueue = xQueue;
2111 
2112     configASSERT( pxQueue );
2113 
2114     taskENTER_CRITICAL();
2115     {
2116         uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;
2117     }
2118     taskEXIT_CRITICAL();
2119 
2120     return uxReturn;
2121 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2122 /*-----------------------------------------------------------*/
2123 
uxQueueMessagesWaitingFromISR(const QueueHandle_t xQueue)2124 UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
2125 {
2126     UBaseType_t uxReturn;
2127     Queue_t * const pxQueue = xQueue;
2128 
2129     configASSERT( pxQueue );
2130     uxReturn = pxQueue->uxMessagesWaiting;
2131 
2132     return uxReturn;
2133 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2134 /*-----------------------------------------------------------*/
2135 
vQueueDelete(QueueHandle_t xQueue)2136 void vQueueDelete( QueueHandle_t xQueue )
2137 {
2138     Queue_t * const pxQueue = xQueue;
2139 
2140     configASSERT( pxQueue );
2141     traceQUEUE_DELETE( pxQueue );
2142 
2143     #if ( configQUEUE_REGISTRY_SIZE > 0 )
2144         {
2145             vQueueUnregisterQueue( pxQueue );
2146         }
2147     #endif
2148 
2149     #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
2150         {
2151             /* The queue can only have been allocated dynamically - free it
2152              * again. */
2153             vPortFree( pxQueue );
2154         }
2155     #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
2156         {
2157             /* The queue could have been allocated statically or dynamically, so
2158              * check before attempting to free the memory. */
2159             if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
2160             {
2161                 vPortFree( pxQueue );
2162             }
2163             else
2164             {
2165                 mtCOVERAGE_TEST_MARKER();
2166             }
2167         }
2168     #else /* if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) ) */
2169         {
2170             /* The queue must have been statically allocated, so is not going to be
2171              * deleted.  Avoid compiler warnings about the unused parameter. */
2172             ( void ) pxQueue;
2173         }
2174     #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
2175 }
2176 /*-----------------------------------------------------------*/
2177 
2178 #if ( configUSE_TRACE_FACILITY == 1 )
2179 
uxQueueGetQueueNumber(QueueHandle_t xQueue)2180     UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
2181     {
2182         return ( ( Queue_t * ) xQueue )->uxQueueNumber;
2183     }
2184 
2185 #endif /* configUSE_TRACE_FACILITY */
2186 /*-----------------------------------------------------------*/
2187 
2188 #if ( configUSE_TRACE_FACILITY == 1 )
2189 
vQueueSetQueueNumber(QueueHandle_t xQueue,UBaseType_t uxQueueNumber)2190     void vQueueSetQueueNumber( QueueHandle_t xQueue,
2191                                UBaseType_t uxQueueNumber )
2192     {
2193         ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
2194     }
2195 
2196 #endif /* configUSE_TRACE_FACILITY */
2197 /*-----------------------------------------------------------*/
2198 
2199 #if ( configUSE_TRACE_FACILITY == 1 )
2200 
ucQueueGetQueueType(QueueHandle_t xQueue)2201     uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
2202     {
2203         return ( ( Queue_t * ) xQueue )->ucQueueType;
2204     }
2205 
2206 #endif /* configUSE_TRACE_FACILITY */
2207 /*-----------------------------------------------------------*/
2208 
2209 #if ( configUSE_MUTEXES == 1 )
2210 
prvGetDisinheritPriorityAfterTimeout(const Queue_t * const pxQueue)2211     static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue )
2212     {
2213         UBaseType_t uxHighestPriorityOfWaitingTasks;
2214 
2215         /* If a task waiting for a mutex causes the mutex holder to inherit a
2216          * priority, but the waiting task times out, then the holder should
2217          * disinherit the priority - but only down to the highest priority of any
2218          * other tasks that are waiting for the same mutex.  For this purpose,
2219          * return the priority of the highest priority task that is waiting for the
2220          * mutex. */
2221         if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0U )
2222         {
2223             uxHighestPriorityOfWaitingTasks = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) );
2224         }
2225         else
2226         {
2227             uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;
2228         }
2229 
2230         return uxHighestPriorityOfWaitingTasks;
2231     }
2232 
2233 #endif /* configUSE_MUTEXES */
2234 /*-----------------------------------------------------------*/
2235 
prvCopyDataToQueue(Queue_t * const pxQueue,const void * pvItemToQueue,const BaseType_t xPosition)2236 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
2237                                       const void * pvItemToQueue,
2238                                       const BaseType_t xPosition )
2239 {
2240     BaseType_t xReturn = pdFALSE;
2241     UBaseType_t uxMessagesWaiting;
2242 
2243     /* This function is called from a critical section. */
2244 
2245     uxMessagesWaiting = pxQueue->uxMessagesWaiting;
2246 
2247     if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
2248     {
2249         #if ( configUSE_MUTEXES == 1 )
2250             {
2251                 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
2252                 {
2253                     /* The mutex is no longer being held. */
2254                     xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
2255                     pxQueue->u.xSemaphore.xMutexHolder = NULL;
2256                 }
2257                 else
2258                 {
2259                     mtCOVERAGE_TEST_MARKER();
2260                 }
2261             }
2262         #endif /* configUSE_MUTEXES */
2263     }
2264     else if( xPosition == queueSEND_TO_BACK )
2265     {
2266         ( void ) memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports, plus previous logic ensures a null pointer can only be passed to memcpy() if the copy size is 0.  Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */
2267         pxQueue->pcWriteTo += pxQueue->uxItemSize;                                                       /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */
2268 
2269         if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail )                                             /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2270         {
2271             pxQueue->pcWriteTo = pxQueue->pcHead;
2272         }
2273         else
2274         {
2275             mtCOVERAGE_TEST_MARKER();
2276         }
2277     }
2278     else
2279     {
2280         ( void ) memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e9087 !e418 MISRA exception as the casts are only redundant for some ports.  Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes.  Assert checks null pointer only used when length is 0. */
2281         pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
2282 
2283         if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2284         {
2285             pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
2286         }
2287         else
2288         {
2289             mtCOVERAGE_TEST_MARKER();
2290         }
2291 
2292         if( xPosition == queueOVERWRITE )
2293         {
2294             if( uxMessagesWaiting > ( UBaseType_t ) 0 )
2295             {
2296                 /* An item is not being added but overwritten, so subtract
2297                  * one from the recorded number of items in the queue so when
2298                  * one is added again below the number of recorded items remains
2299                  * correct. */
2300                 --uxMessagesWaiting;
2301             }
2302             else
2303             {
2304                 mtCOVERAGE_TEST_MARKER();
2305             }
2306         }
2307         else
2308         {
2309             mtCOVERAGE_TEST_MARKER();
2310         }
2311     }
2312 
2313     pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
2314 
2315     return xReturn;
2316 }
2317 /*-----------------------------------------------------------*/
2318 
prvCopyDataFromQueue(Queue_t * const pxQueue,void * const pvBuffer)2319 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
2320                                   void * const pvBuffer )
2321 {
2322     if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
2323     {
2324         pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;           /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */
2325 
2326         if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
2327         {
2328             pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2329         }
2330         else
2331         {
2332             mtCOVERAGE_TEST_MARKER();
2333         }
2334 
2335         ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports.  Also previous logic ensures a null pointer can only be passed to memcpy() when the count is 0.  Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */
2336     }
2337 }
2338 /*-----------------------------------------------------------*/
2339 
prvUnlockQueue(Queue_t * const pxQueue)2340 static void prvUnlockQueue( Queue_t * const pxQueue )
2341 {
2342     /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
2343 
2344     /* The lock counts contains the number of extra data items placed or
2345      * removed from the queue while the queue was locked.  When a queue is
2346      * locked items can be added or removed, but the event lists cannot be
2347      * updated. */
2348     taskENTER_CRITICAL();
2349     {
2350         int8_t cTxLock = pxQueue->cTxLock;
2351 
2352         /* See if data was added to the queue while it was locked. */
2353         while( cTxLock > queueLOCKED_UNMODIFIED )
2354         {
2355             /* Data was posted while the queue was locked.  Are any tasks
2356              * blocked waiting for data to become available? */
2357             #if ( configUSE_QUEUE_SETS == 1 )
2358                 {
2359                     if( pxQueue->pxQueueSetContainer != NULL )
2360                     {
2361                         if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) != pdFALSE )
2362                         {
2363                             /* The queue is a member of a queue set, and posting to
2364                              * the queue set caused a higher priority task to unblock.
2365                              * A context switch is required. */
2366                             vTaskMissedYield();
2367                         }
2368                         else
2369                         {
2370                             mtCOVERAGE_TEST_MARKER();
2371                         }
2372                     }
2373                     else
2374                     {
2375                         /* Tasks that are removed from the event list will get
2376                          * added to the pending ready list as the scheduler is still
2377                          * suspended. */
2378                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2379                         {
2380                             if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2381                             {
2382                                 /* The task waiting has a higher priority so record that a
2383                                  * context switch is required. */
2384                                 vTaskMissedYield();
2385                             }
2386                             else
2387                             {
2388                                 mtCOVERAGE_TEST_MARKER();
2389                             }
2390                         }
2391                         else
2392                         {
2393                             break;
2394                         }
2395                     }
2396                 }
2397             #else /* configUSE_QUEUE_SETS */
2398                 {
2399                     /* Tasks that are removed from the event list will get added to
2400                      * the pending ready list as the scheduler is still suspended. */
2401                     if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2402                     {
2403                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2404                         {
2405                             /* The task waiting has a higher priority so record that
2406                              * a context switch is required. */
2407                             vTaskMissedYield();
2408                         }
2409                         else
2410                         {
2411                             mtCOVERAGE_TEST_MARKER();
2412                         }
2413                     }
2414                     else
2415                     {
2416                         break;
2417                     }
2418                 }
2419             #endif /* configUSE_QUEUE_SETS */
2420 
2421             --cTxLock;
2422         }
2423 
2424         pxQueue->cTxLock = queueUNLOCKED;
2425     }
2426     taskEXIT_CRITICAL();
2427 
2428     /* Do the same for the Rx lock. */
2429     taskENTER_CRITICAL();
2430     {
2431         int8_t cRxLock = pxQueue->cRxLock;
2432 
2433         while( cRxLock > queueLOCKED_UNMODIFIED )
2434         {
2435             if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2436             {
2437                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2438                 {
2439                     vTaskMissedYield();
2440                 }
2441                 else
2442                 {
2443                     mtCOVERAGE_TEST_MARKER();
2444                 }
2445 
2446                 --cRxLock;
2447             }
2448             else
2449             {
2450                 break;
2451             }
2452         }
2453 
2454         pxQueue->cRxLock = queueUNLOCKED;
2455     }
2456     taskEXIT_CRITICAL();
2457 }
2458 /*-----------------------------------------------------------*/
2459 
prvIsQueueEmpty(const Queue_t * pxQueue)2460 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue )
2461 {
2462     BaseType_t xReturn;
2463     taskENTER_CRITICAL();
2464     {
2465         if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2466         {
2467             xReturn = pdTRUE;
2468         }
2469         else
2470         {
2471             xReturn = pdFALSE;
2472         }
2473     }
2474     taskEXIT_CRITICAL();
2475 
2476     return xReturn;
2477 }
2478 /*-----------------------------------------------------------*/
2479 
xQueueIsQueueEmptyFromISR(const QueueHandle_t xQueue)2480 BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )
2481 {
2482     BaseType_t xReturn;
2483     Queue_t * const pxQueue = xQueue;
2484 
2485     configASSERT( pxQueue );
2486 
2487     if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2488     {
2489         xReturn = pdTRUE;
2490     }
2491     else
2492     {
2493         xReturn = pdFALSE;
2494     }
2495 
2496     return xReturn;
2497 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2498 /*-----------------------------------------------------------*/
2499 
prvIsQueueFull(const Queue_t * pxQueue)2500 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue )
2501 {
2502     BaseType_t xReturn;
2503 
2504     {
2505         if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2506         {
2507             xReturn = pdTRUE;
2508         }
2509         else
2510         {
2511             xReturn = pdFALSE;
2512         }
2513     }
2514 
2515     return xReturn;
2516 }
2517 /*-----------------------------------------------------------*/
2518 
xQueueIsQueueFullFromISR(const QueueHandle_t xQueue)2519 BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )
2520 {
2521     BaseType_t xReturn;
2522     Queue_t * const pxQueue = xQueue;
2523 
2524     configASSERT( pxQueue );
2525 
2526     if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2527     {
2528         xReturn = pdTRUE;
2529     }
2530     else
2531     {
2532         xReturn = pdFALSE;
2533     }
2534 
2535     return xReturn;
2536 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2537 /*-----------------------------------------------------------*/
2538 
2539 #if ( configUSE_CO_ROUTINES == 1 )
2540 
xQueueCRSend(QueueHandle_t xQueue,const void * pvItemToQueue,TickType_t xTicksToWait)2541     BaseType_t xQueueCRSend( QueueHandle_t xQueue,
2542                              const void * pvItemToQueue,
2543                              TickType_t xTicksToWait )
2544     {
2545         BaseType_t xReturn;
2546         Queue_t * const pxQueue = xQueue;
2547 
2548         /* If the queue is already full we may have to block.  A critical section
2549          * is required to prevent an interrupt removing something from the queue
2550          * between the check to see if the queue is full and blocking on the queue. */
2551         portDISABLE_INTERRUPTS();
2552         {
2553             if( prvIsQueueFull( pxQueue ) != pdFALSE )
2554             {
2555                 /* The queue is full - do we want to block or just leave without
2556                  * posting? */
2557                 if( xTicksToWait > ( TickType_t ) 0 )
2558                 {
2559                     /* As this is called from a coroutine we cannot block directly, but
2560                      * return indicating that we need to block. */
2561                     vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
2562                     portENABLE_INTERRUPTS();
2563                     return errQUEUE_BLOCKED;
2564                 }
2565                 else
2566                 {
2567                     portENABLE_INTERRUPTS();
2568                     return errQUEUE_FULL;
2569                 }
2570             }
2571         }
2572         portENABLE_INTERRUPTS();
2573 
2574         portDISABLE_INTERRUPTS();
2575         {
2576             if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2577             {
2578                 /* There is room in the queue, copy the data into the queue. */
2579                 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2580                 xReturn = pdPASS;
2581 
2582                 /* Were any co-routines waiting for data to become available? */
2583                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2584                 {
2585                     /* In this instance the co-routine could be placed directly
2586                      * into the ready list as we are within a critical section.
2587                      * Instead the same pending ready list mechanism is used as if
2588                      * the event were caused from within an interrupt. */
2589                     if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2590                     {
2591                         /* The co-routine waiting has a higher priority so record
2592                          * that a yield might be appropriate. */
2593                         xReturn = errQUEUE_YIELD;
2594                     }
2595                     else
2596                     {
2597                         mtCOVERAGE_TEST_MARKER();
2598                     }
2599                 }
2600                 else
2601                 {
2602                     mtCOVERAGE_TEST_MARKER();
2603                 }
2604             }
2605             else
2606             {
2607                 xReturn = errQUEUE_FULL;
2608             }
2609         }
2610         portENABLE_INTERRUPTS();
2611 
2612         return xReturn;
2613     }
2614 
2615 #endif /* configUSE_CO_ROUTINES */
2616 /*-----------------------------------------------------------*/
2617 
2618 #if ( configUSE_CO_ROUTINES == 1 )
2619 
xQueueCRReceive(QueueHandle_t xQueue,void * pvBuffer,TickType_t xTicksToWait)2620     BaseType_t xQueueCRReceive( QueueHandle_t xQueue,
2621                                 void * pvBuffer,
2622                                 TickType_t xTicksToWait )
2623     {
2624         BaseType_t xReturn;
2625         Queue_t * const pxQueue = xQueue;
2626 
2627         /* If the queue is already empty we may have to block.  A critical section
2628          * is required to prevent an interrupt adding something to the queue
2629          * between the check to see if the queue is empty and blocking on the queue. */
2630         portDISABLE_INTERRUPTS();
2631         {
2632             if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2633             {
2634                 /* There are no messages in the queue, do we want to block or just
2635                  * leave with nothing? */
2636                 if( xTicksToWait > ( TickType_t ) 0 )
2637                 {
2638                     /* As this is a co-routine we cannot block directly, but return
2639                      * indicating that we need to block. */
2640                     vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
2641                     portENABLE_INTERRUPTS();
2642                     return errQUEUE_BLOCKED;
2643                 }
2644                 else
2645                 {
2646                     portENABLE_INTERRUPTS();
2647                     return errQUEUE_FULL;
2648                 }
2649             }
2650             else
2651             {
2652                 mtCOVERAGE_TEST_MARKER();
2653             }
2654         }
2655         portENABLE_INTERRUPTS();
2656 
2657         portDISABLE_INTERRUPTS();
2658         {
2659             if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2660             {
2661                 /* Data is available from the queue. */
2662                 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2663 
2664                 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2665                 {
2666                     pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2667                 }
2668                 else
2669                 {
2670                     mtCOVERAGE_TEST_MARKER();
2671                 }
2672 
2673                 --( pxQueue->uxMessagesWaiting );
2674                 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2675 
2676                 xReturn = pdPASS;
2677 
2678                 /* Were any co-routines waiting for space to become available? */
2679                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2680                 {
2681                     /* In this instance the co-routine could be placed directly
2682                      * into the ready list as we are within a critical section.
2683                      * Instead the same pending ready list mechanism is used as if
2684                      * the event were caused from within an interrupt. */
2685                     if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2686                     {
2687                         xReturn = errQUEUE_YIELD;
2688                     }
2689                     else
2690                     {
2691                         mtCOVERAGE_TEST_MARKER();
2692                     }
2693                 }
2694                 else
2695                 {
2696                     mtCOVERAGE_TEST_MARKER();
2697                 }
2698             }
2699             else
2700             {
2701                 xReturn = pdFAIL;
2702             }
2703         }
2704         portENABLE_INTERRUPTS();
2705 
2706         return xReturn;
2707     }
2708 
2709 #endif /* configUSE_CO_ROUTINES */
2710 /*-----------------------------------------------------------*/
2711 
2712 #if ( configUSE_CO_ROUTINES == 1 )
2713 
xQueueCRSendFromISR(QueueHandle_t xQueue,const void * pvItemToQueue,BaseType_t xCoRoutinePreviouslyWoken)2714     BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue,
2715                                     const void * pvItemToQueue,
2716                                     BaseType_t xCoRoutinePreviouslyWoken )
2717     {
2718         Queue_t * const pxQueue = xQueue;
2719 
2720         /* Cannot block within an ISR so if there is no space on the queue then
2721          * exit without doing anything. */
2722         if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2723         {
2724             prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2725 
2726             /* We only want to wake one co-routine per ISR, so check that a
2727              * co-routine has not already been woken. */
2728             if( xCoRoutinePreviouslyWoken == pdFALSE )
2729             {
2730                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2731                 {
2732                     if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2733                     {
2734                         return pdTRUE;
2735                     }
2736                     else
2737                     {
2738                         mtCOVERAGE_TEST_MARKER();
2739                     }
2740                 }
2741                 else
2742                 {
2743                     mtCOVERAGE_TEST_MARKER();
2744                 }
2745             }
2746             else
2747             {
2748                 mtCOVERAGE_TEST_MARKER();
2749             }
2750         }
2751         else
2752         {
2753             mtCOVERAGE_TEST_MARKER();
2754         }
2755 
2756         return xCoRoutinePreviouslyWoken;
2757     }
2758 
2759 #endif /* configUSE_CO_ROUTINES */
2760 /*-----------------------------------------------------------*/
2761 
2762 #if ( configUSE_CO_ROUTINES == 1 )
2763 
xQueueCRReceiveFromISR(QueueHandle_t xQueue,void * pvBuffer,BaseType_t * pxCoRoutineWoken)2764     BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue,
2765                                        void * pvBuffer,
2766                                        BaseType_t * pxCoRoutineWoken )
2767     {
2768         BaseType_t xReturn;
2769         Queue_t * const pxQueue = xQueue;
2770 
2771         /* We cannot block from an ISR, so check there is data available. If
2772          * not then just leave without doing anything. */
2773         if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2774         {
2775             /* Copy the data from the queue. */
2776             pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2777 
2778             if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2779             {
2780                 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2781             }
2782             else
2783             {
2784                 mtCOVERAGE_TEST_MARKER();
2785             }
2786 
2787             --( pxQueue->uxMessagesWaiting );
2788             ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2789 
2790             if( ( *pxCoRoutineWoken ) == pdFALSE )
2791             {
2792                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2793                 {
2794                     if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2795                     {
2796                         *pxCoRoutineWoken = pdTRUE;
2797                     }
2798                     else
2799                     {
2800                         mtCOVERAGE_TEST_MARKER();
2801                     }
2802                 }
2803                 else
2804                 {
2805                     mtCOVERAGE_TEST_MARKER();
2806                 }
2807             }
2808             else
2809             {
2810                 mtCOVERAGE_TEST_MARKER();
2811             }
2812 
2813             xReturn = pdPASS;
2814         }
2815         else
2816         {
2817             xReturn = pdFAIL;
2818         }
2819 
2820         return xReturn;
2821     }
2822 
2823 #endif /* configUSE_CO_ROUTINES */
2824 /*-----------------------------------------------------------*/
2825 
2826 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2827 
vQueueAddToRegistry(QueueHandle_t xQueue,const char * pcQueueName)2828     void vQueueAddToRegistry( QueueHandle_t xQueue,
2829                               const char * pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2830     {
2831         UBaseType_t ux;
2832 
2833         portENTER_CRITICAL(&queue_registry_spinlock);
2834         /* See if there is an empty space in the registry.  A NULL name denotes
2835          * a free slot. */
2836         for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2837         {
2838             if( xQueueRegistry[ ux ].pcQueueName == NULL )
2839             {
2840                 /* Store the information on this queue. */
2841                 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
2842                 xQueueRegistry[ ux ].xHandle = xQueue;
2843 
2844                 traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
2845                 break;
2846             }
2847             else
2848             {
2849                 mtCOVERAGE_TEST_MARKER();
2850             }
2851         }
2852         portEXIT_CRITICAL(&queue_registry_spinlock);
2853     }
2854 
2855 #endif /* configQUEUE_REGISTRY_SIZE */
2856 /*-----------------------------------------------------------*/
2857 
2858 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2859 
pcQueueGetName(QueueHandle_t xQueue)2860     const char * pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2861     {
2862         UBaseType_t ux;
2863         const char * pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2864 
2865         portENTER_CRITICAL(&queue_registry_spinlock);
2866 
2867         /* Note there is nothing here to protect against another task adding or
2868          * removing entries from the registry while it is being searched. */
2869 
2870         for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2871         {
2872             if( xQueueRegistry[ ux ].xHandle == xQueue )
2873             {
2874                 pcReturn = xQueueRegistry[ ux ].pcQueueName;
2875                 break;
2876             }
2877             else
2878             {
2879                 mtCOVERAGE_TEST_MARKER();
2880             }
2881         }
2882         portEXIT_CRITICAL(&queue_registry_spinlock);
2883 
2884         return pcReturn;
2885     } /*lint !e818 xQueue cannot be a pointer to const because it is a typedef. */
2886 
2887 #endif /* configQUEUE_REGISTRY_SIZE */
2888 /*-----------------------------------------------------------*/
2889 
2890 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2891 
vQueueUnregisterQueue(QueueHandle_t xQueue)2892     void vQueueUnregisterQueue( QueueHandle_t xQueue )
2893     {
2894         UBaseType_t ux;
2895 
2896         portENTER_CRITICAL(&queue_registry_spinlock);
2897 
2898         /* See if the handle of the queue being unregistered in actually in the
2899          * registry. */
2900         for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2901         {
2902             if( xQueueRegistry[ ux ].xHandle == xQueue )
2903             {
2904                 /* Set the name to NULL to show that this slot if free again. */
2905                 xQueueRegistry[ ux ].pcQueueName = NULL;
2906 
2907                 /* Set the handle to NULL to ensure the same queue handle cannot
2908                  * appear in the registry twice if it is added, removed, then
2909                  * added again. */
2910                 xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;
2911                 break;
2912             }
2913             else
2914             {
2915                 mtCOVERAGE_TEST_MARKER();
2916             }
2917         }
2918         portEXIT_CRITICAL(&queue_registry_spinlock);
2919 
2920     } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2921 
2922 #endif /* configQUEUE_REGISTRY_SIZE */
2923 /*-----------------------------------------------------------*/
2924 
2925 #if ( configUSE_TIMERS == 1 )
2926 
vQueueWaitForMessageRestricted(QueueHandle_t xQueue,TickType_t xTicksToWait,const BaseType_t xWaitIndefinitely)2927     void vQueueWaitForMessageRestricted( QueueHandle_t xQueue,
2928                                          TickType_t xTicksToWait,
2929                                          const BaseType_t xWaitIndefinitely )
2930     {
2931         Queue_t * const pxQueue = xQueue;
2932 
2933         /* This function should not be called by application code hence the
2934          * 'Restricted' in its name.  It is not part of the public API.  It is
2935          * designed for use by kernel code, and has special calling requirements.
2936          * It can result in vListInsert() being called on a list that can only
2937          * possibly ever have one item in it, so the list will be fast, but even
2938          * so it should be called with the scheduler locked and not from a critical
2939          * section. */
2940 
2941         /* Only do anything if there are no messages in the queue.  This function
2942          *  will not actually cause the task to block, just place it on a blocked
2943          *  list.  It will not block until the scheduler is unlocked - at which
2944          *  time a yield will be performed.  If an item is added to the queue while
2945          *  the queue is locked, and the calling task blocks on the queue, then the
2946          *  calling task will be immediately unblocked when the queue is unlocked. */
2947         prvLockQueue( pxQueue );
2948 
2949         if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
2950         {
2951             /* There is nothing in the queue, block for the specified period. */
2952             vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );
2953         }
2954         else
2955         {
2956             mtCOVERAGE_TEST_MARKER();
2957         }
2958 
2959         prvUnlockQueue( pxQueue );
2960     }
2961 
2962 #endif /* configUSE_TIMERS */
2963 /*-----------------------------------------------------------*/
2964 
2965 #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
2966 
xQueueCreateSet(const UBaseType_t uxEventQueueLength)2967     QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
2968     {
2969         QueueSetHandle_t pxQueue;
2970 
2971         pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
2972 
2973         return pxQueue;
2974     }
2975 
2976 #endif /* configUSE_QUEUE_SETS */
2977 /*-----------------------------------------------------------*/
2978 
2979 #if ( configUSE_QUEUE_SETS == 1 )
2980 
xQueueAddToSet(QueueSetMemberHandle_t xQueueOrSemaphore,QueueSetHandle_t xQueueSet)2981     BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore,
2982                                QueueSetHandle_t xQueueSet )
2983     {
2984         BaseType_t xReturn;
2985 #ifdef ESP_PLATFORM
2986         Queue_t * pxQueue = (Queue_t * )xQueueOrSemaphore;
2987 #endif
2988 
2989         taskENTER_CRITICAL();
2990         {
2991             if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
2992             {
2993                 /* Cannot add a queue/semaphore to more than one queue set. */
2994                 xReturn = pdFAIL;
2995             }
2996             else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
2997             {
2998                 /* Cannot add a queue/semaphore to a queue set if there are already
2999                  * items in the queue/semaphore. */
3000                 xReturn = pdFAIL;
3001             }
3002             else
3003             {
3004                 ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
3005                 xReturn = pdPASS;
3006             }
3007         }
3008         taskEXIT_CRITICAL();
3009 
3010         return xReturn;
3011     }
3012 
3013 #endif /* configUSE_QUEUE_SETS */
3014 /*-----------------------------------------------------------*/
3015 
3016 #if ( configUSE_QUEUE_SETS == 1 )
3017 
xQueueRemoveFromSet(QueueSetMemberHandle_t xQueueOrSemaphore,QueueSetHandle_t xQueueSet)3018     BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore,
3019                                     QueueSetHandle_t xQueueSet )
3020     {
3021         BaseType_t xReturn;
3022         Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
3023 
3024         if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
3025         {
3026             /* The queue was not a member of the set. */
3027             xReturn = pdFAIL;
3028         }
3029         else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
3030         {
3031             /* It is dangerous to remove a queue from a set when the queue is
3032              * not empty because the queue set will still hold pending events for
3033              * the queue. */
3034             xReturn = pdFAIL;
3035         }
3036         else
3037         {
3038 #ifdef ESP_PLATFORM
3039             Queue_t* pxQueue = (Queue_t*)pxQueueOrSemaphore;
3040 #endif
3041             taskENTER_CRITICAL();
3042             {
3043                 /* The queue is no longer contained in the set. */
3044                 pxQueueOrSemaphore->pxQueueSetContainer = NULL;
3045             }
3046             taskEXIT_CRITICAL();
3047             xReturn = pdPASS;
3048         }
3049 
3050         return xReturn;
3051     } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
3052 
3053 #endif /* configUSE_QUEUE_SETS */
3054 /*-----------------------------------------------------------*/
3055 
3056 #if ( configUSE_QUEUE_SETS == 1 )
3057 
xQueueSelectFromSet(QueueSetHandle_t xQueueSet,TickType_t const xTicksToWait)3058     QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet,
3059                                                 TickType_t const xTicksToWait )
3060     {
3061         QueueSetMemberHandle_t xReturn = NULL;
3062 
3063         ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait ); /*lint !e961 Casting from one typedef to another is not redundant. */
3064         return xReturn;
3065     }
3066 
3067 #endif /* configUSE_QUEUE_SETS */
3068 /*-----------------------------------------------------------*/
3069 
3070 #if ( configUSE_QUEUE_SETS == 1 )
3071 
xQueueSelectFromSetFromISR(QueueSetHandle_t xQueueSet)3072     QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
3073     {
3074         QueueSetMemberHandle_t xReturn = NULL;
3075 
3076         ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
3077         return xReturn;
3078     }
3079 
3080 #endif /* configUSE_QUEUE_SETS */
3081 /*-----------------------------------------------------------*/
3082 
3083 #if ( configUSE_QUEUE_SETS == 1 )
3084 
prvNotifyQueueSetContainer(const Queue_t * const pxQueue,const BaseType_t xCopyPosition)3085     static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue,
3086                                                   const BaseType_t xCopyPosition )
3087     {
3088         Queue_t * pxQueueSetContainer = pxQueue->pxQueueSetContainer;
3089         BaseType_t xReturn = pdFALSE;
3090 
3091         /* This function must be called form a critical section. */
3092 
3093         /* The following line is not reachable in unit tests because every call
3094          * to prvNotifyQueueSetContainer is preceded by a check that
3095          * pxQueueSetContainer != NULL */
3096         configASSERT( pxQueueSetContainer ); /* LCOV_EXCL_BR_LINE */
3097 
3098         //Acquire the Queue set's spinlock
3099         portENTER_CRITICAL(&(pxQueueSetContainer->mux));
3100 
3101         configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
3102 
3103         if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
3104         {
3105             const int8_t cTxLock = pxQueueSetContainer->cTxLock;
3106 
3107             traceQUEUE_SEND( pxQueueSetContainer );
3108 
3109             /* The data copied is the handle of the queue that contains data. */
3110             xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, xCopyPosition );
3111 
3112             if( cTxLock == queueUNLOCKED )
3113             {
3114                 if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
3115                 {
3116                     if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
3117                     {
3118                         /* The task waiting has a higher priority. */
3119                         xReturn = pdTRUE;
3120                     }
3121                     else
3122                     {
3123                         mtCOVERAGE_TEST_MARKER();
3124                     }
3125                 }
3126                 else
3127                 {
3128                     mtCOVERAGE_TEST_MARKER();
3129                 }
3130             }
3131             else
3132             {
3133                 pxQueueSetContainer->cTxLock = ( int8_t ) ( cTxLock + 1 );
3134             }
3135         }
3136         else
3137         {
3138             mtCOVERAGE_TEST_MARKER();
3139         }
3140 
3141         //Release the Queue set's spinlock
3142         portEXIT_CRITICAL(&(pxQueueSetContainer->mux));
3143 
3144         return xReturn;
3145     }
3146 
3147 #endif /* configUSE_QUEUE_SETS */
3148