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