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