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