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