2 * FreeRTOS Kernel V10.4.4
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
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
41 #if ( configUSE_CO_ROUTINES == 1 )
45 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
46 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
47 * for the header files above, but not in this file, in order to generate the
48 * correct privileged Vs unprivileged linkage and placement. */
49 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
52 /* Constants used with the cRxLock and cTxLock structure members. */
53 #define queueUNLOCKED ( ( int8_t ) -1 )
54 #define queueLOCKED_UNMODIFIED ( ( int8_t ) 0 )
55 #define queueINT8_MAX ( ( int8_t ) 127 )
57 /* When the Queue_t structure is used to represent a base queue its pcHead and
58 * pcTail members are used as pointers into the queue storage area. When the
59 * Queue_t structure is used to represent a mutex pcHead and pcTail pointers are
60 * not necessary, and the pcHead pointer is set to NULL to indicate that the
61 * structure instead holds a pointer to the mutex holder (if any). Map alternative
62 * names to the pcHead and structure member to ensure the readability of the code
63 * is maintained. The QueuePointers_t and SemaphoreData_t types are used to form
64 * a union as their usage is mutually exclusive dependent on what the queue is
66 #define uxQueueType pcHead
67 #define queueQUEUE_IS_MUTEX NULL
69 typedef struct QueuePointers
71 int8_t * pcTail; /*< Points to the byte at the end of the queue storage area. Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
72 int8_t * pcReadFrom; /*< Points to the last place that a queued item was read from when the structure is used as a queue. */
75 typedef struct SemaphoreData
77 TaskHandle_t xMutexHolder; /*< The handle of the task that holds the mutex. */
78 UBaseType_t uxRecursiveCallCount; /*< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */
81 /* Semaphores do not actually store or copy data, so have an item size of
83 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )
84 #define queueMUTEX_GIVE_BLOCK_TIME ( ( TickType_t ) 0U )
86 #if ( configUSE_PREEMPTION == 0 )
88 /* If the cooperative scheduler is being used then a yield should not be
89 * performed just because a higher priority task has been woken. */
90 #define queueYIELD_IF_USING_PREEMPTION()
92 #define queueYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
96 * Definition of the queue used by the scheduler.
97 * Items are queued by copy, not reference. See the following link for the
98 * rationale: https://www.FreeRTOS.org/Embedded-RTOS-Queues.html
100 typedef struct QueueDefinition /* The old naming convention is used to prevent breaking kernel aware debuggers. */
102 int8_t * pcHead; /*< Points to the beginning of the queue storage area. */
103 int8_t * pcWriteTo; /*< Points to the free next place in the storage area. */
107 QueuePointers_t xQueue; /*< Data required exclusively when this structure is used as a queue. */
108 SemaphoreData_t xSemaphore; /*< Data required exclusively when this structure is used as a semaphore. */
111 List_t xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
112 List_t xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
114 volatile UBaseType_t uxMessagesWaiting; /*< The number of items currently in the queue. */
115 UBaseType_t uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
116 UBaseType_t uxItemSize; /*< The size of each items that the queue will hold. */
118 volatile int8_t cRxLock; /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
119 volatile int8_t cTxLock; /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
121 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
122 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */
125 #if ( configUSE_QUEUE_SETS == 1 )
126 struct QueueDefinition * pxQueueSetContainer;
129 #if ( configUSE_TRACE_FACILITY == 1 )
130 UBaseType_t uxQueueNumber;
135 /* The old xQUEUE name is maintained above then typedefed to the new Queue_t
136 * name below to enable the use of older kernel aware debuggers. */
137 typedef xQUEUE Queue_t;
139 /*-----------------------------------------------------------*/
142 * The queue registry is just a means for kernel aware debuggers to locate
143 * queue structures. It has no other purpose so is an optional component.
145 #if ( configQUEUE_REGISTRY_SIZE > 0 )
147 /* The type stored within the queue registry array. This allows a name
148 * to be assigned to each queue making kernel aware debugging a little
149 * more user friendly. */
150 typedef struct QUEUE_REGISTRY_ITEM
152 const char * pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
153 QueueHandle_t xHandle;
154 } xQueueRegistryItem;
156 /* The old xQueueRegistryItem name is maintained above then typedefed to the
157 * new xQueueRegistryItem name below to enable the use of older kernel aware
159 typedef xQueueRegistryItem QueueRegistryItem_t;
161 /* The queue registry is simply an array of QueueRegistryItem_t structures.
162 * The pcQueueName member of a structure being NULL is indicative of the
163 * array position being vacant. */
164 PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
166 #endif /* configQUEUE_REGISTRY_SIZE */
169 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
170 * prevent an ISR from adding or removing items to the queue, but does prevent
171 * an ISR from removing tasks from the queue event lists. If an ISR finds a
172 * queue is locked it will instead increment the appropriate queue lock count
173 * to indicate that a task may require unblocking. When the queue in unlocked
174 * these lock counts are inspected, and the appropriate action taken.
176 static void prvUnlockQueue( Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
179 * Uses a critical section to determine if there is any data in a queue.
181 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
183 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
186 * Uses a critical section to determine if there is any space in a queue.
188 * @return pdTRUE if there is no space, otherwise pdFALSE;
190 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
193 * Copies an item into the queue, either at the front of the queue or the
196 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
197 const void * pvItemToQueue,
198 const BaseType_t xPosition ) PRIVILEGED_FUNCTION;
201 * Copies an item out of a queue.
203 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
204 void * const pvBuffer ) PRIVILEGED_FUNCTION;
206 #if ( configUSE_QUEUE_SETS == 1 )
209 * Checks to see if a queue is a member of a queue set, and if so, notifies
210 * the queue set that the queue contains data.
212 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
216 * Called after a Queue_t structure has been allocated either statically or
217 * dynamically to fill in the structure's members.
219 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
220 const UBaseType_t uxItemSize,
221 uint8_t * pucQueueStorage,
222 const uint8_t ucQueueType,
223 Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
226 * Mutexes are a special type of queue. When a mutex is created, first the
227 * queue is created, then prvInitialiseMutex() is called to configure the queue
230 #if ( configUSE_MUTEXES == 1 )
231 static void prvInitialiseMutex( Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
234 #if ( configUSE_MUTEXES == 1 )
237 * If a task waiting for a mutex causes the mutex holder to inherit a
238 * priority, but the waiting task times out, then the holder should
239 * disinherit the priority - but only down to the highest priority of any
240 * other tasks that are waiting for the same mutex. This function returns
243 static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
245 /*-----------------------------------------------------------*/
248 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
249 * accessing the queue event lists.
251 #define prvLockQueue( pxQueue ) \
252 taskENTER_CRITICAL(); \
254 if( ( pxQueue )->cRxLock == queueUNLOCKED ) \
256 ( pxQueue )->cRxLock = queueLOCKED_UNMODIFIED; \
258 if( ( pxQueue )->cTxLock == queueUNLOCKED ) \
260 ( pxQueue )->cTxLock = queueLOCKED_UNMODIFIED; \
264 /*-----------------------------------------------------------*/
266 BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
267 BaseType_t xNewQueue )
269 BaseType_t xReturn = pdPASS;
270 Queue_t * const pxQueue = xQueue;
272 configASSERT( pxQueue );
274 if( ( pxQueue != NULL ) &&
275 ( pxQueue->uxLength >= 1U ) &&
276 /* Check for multiplication overflow. */
277 ( ( SIZE_MAX / pxQueue->uxLength ) >= pxQueue->uxItemSize ) )
279 taskENTER_CRITICAL();
281 pxQueue->u.xQueue.pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
282 pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
283 pxQueue->pcWriteTo = pxQueue->pcHead;
284 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. */
285 pxQueue->cRxLock = queueUNLOCKED;
286 pxQueue->cTxLock = queueUNLOCKED;
288 if( xNewQueue == pdFALSE )
290 /* If there are tasks blocked waiting to read from the queue, then
291 * the tasks will remain blocked as after this function exits the queue
292 * will still be empty. If there are tasks blocked waiting to write to
293 * the queue, then one should be unblocked as after this function exits
294 * it will be possible to write to it. */
295 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
297 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
299 queueYIELD_IF_USING_PREEMPTION();
303 mtCOVERAGE_TEST_MARKER();
308 mtCOVERAGE_TEST_MARKER();
313 /* Ensure the event queues start in the correct state. */
314 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
315 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
324 configASSERT( xReturn != pdFAIL );
326 /* A value is returned for calling semantic consistency with previous
330 /*-----------------------------------------------------------*/
332 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
334 QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,
335 const UBaseType_t uxItemSize,
336 uint8_t * pucQueueStorage,
337 StaticQueue_t * pxStaticQueue,
338 const uint8_t ucQueueType )
340 Queue_t * pxNewQueue = NULL;
342 /* The StaticQueue_t structure and the queue storage area must be
344 configASSERT( pxStaticQueue );
346 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
347 ( pxStaticQueue != NULL ) &&
348 /* A queue storage area should be provided if the item size is not 0, and
349 * should not be provided if the item size is 0. */
350 ( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) ) &&
351 ( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) ) )
354 #if ( configASSERT_DEFINED == 1 )
356 /* Sanity check that the size of the structure used to declare a
357 * variable of type StaticQueue_t or StaticSemaphore_t equals the size of
358 * the real queue and semaphore structures. */
359 volatile size_t xSize = sizeof( StaticQueue_t );
361 /* This assertion cannot be branch covered in unit tests */
362 configASSERT( xSize == sizeof( Queue_t ) ); /* LCOV_EXCL_BR_LINE */
363 ( void ) xSize; /* Keeps lint quiet when configASSERT() is not defined. */
365 #endif /* configASSERT_DEFINED */
367 /* The address of a statically allocated queue was passed in, use it.
368 * The address of a statically allocated storage area was also passed in
369 * but is already set. */
370 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. */
372 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
374 /* Queues can be allocated wither statically or dynamically, so
375 * note this queue was allocated statically in case the queue is
377 pxNewQueue->ucStaticallyAllocated = pdTRUE;
379 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
381 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
385 configASSERT( pxNewQueue );
386 mtCOVERAGE_TEST_MARKER();
392 #endif /* configSUPPORT_STATIC_ALLOCATION */
393 /*-----------------------------------------------------------*/
395 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
397 QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
398 const UBaseType_t uxItemSize,
399 const uint8_t ucQueueType )
401 Queue_t * pxNewQueue = NULL;
402 size_t xQueueSizeInBytes;
403 uint8_t * pucQueueStorage;
405 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
406 /* Check for multiplication overflow. */
407 ( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&
408 /* Check for addition overflow. */
409 ( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( uxQueueLength * uxItemSize ) ) )
411 /* Allocate enough space to hold the maximum number of items that
412 * can be in the queue at any time. It is valid for uxItemSize to be
413 * zero in the case the queue is used as a semaphore. */
414 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
416 /* Allocate the queue and storage area. Justification for MISRA
417 * deviation as follows: pvPortMalloc() always ensures returned memory
418 * blocks are aligned per the requirements of the MCU stack. In this case
419 * pvPortMalloc() must return a pointer that is guaranteed to meet the
420 * alignment requirements of the Queue_t structure - which in this case
421 * is an int8_t *. Therefore, whenever the stack alignment requirements
422 * are greater than or equal to the pointer to char requirements the cast
423 * is safe. In other cases alignment requirements are not strict (one or
425 pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */
427 if( pxNewQueue != NULL )
429 /* Jump past the queue structure to find the location of the queue
431 pucQueueStorage = ( uint8_t * ) pxNewQueue;
432 pucQueueStorage += sizeof( Queue_t ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
434 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
436 /* Queues can be created either statically or dynamically, so
437 * note this task was created dynamically in case it is later
439 pxNewQueue->ucStaticallyAllocated = pdFALSE;
441 #endif /* configSUPPORT_STATIC_ALLOCATION */
443 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
447 traceQUEUE_CREATE_FAILED( ucQueueType );
448 mtCOVERAGE_TEST_MARKER();
453 configASSERT( pxNewQueue );
454 mtCOVERAGE_TEST_MARKER();
460 #endif /* configSUPPORT_STATIC_ALLOCATION */
461 /*-----------------------------------------------------------*/
463 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
464 const UBaseType_t uxItemSize,
465 uint8_t * pucQueueStorage,
466 const uint8_t ucQueueType,
467 Queue_t * pxNewQueue )
469 /* Remove compiler warnings about unused parameters should
470 * configUSE_TRACE_FACILITY not be set to 1. */
471 ( void ) ucQueueType;
473 if( uxItemSize == ( UBaseType_t ) 0 )
475 /* No RAM was allocated for the queue storage area, but PC head cannot
476 * be set to NULL because NULL is used as a key to say the queue is used as
477 * a mutex. Therefore just set pcHead to point to the queue as a benign
478 * value that is known to be within the memory map. */
479 pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
483 /* Set the head to the start of the queue storage area. */
484 pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
487 /* Initialise the queue members as described where the queue type is
489 pxNewQueue->uxLength = uxQueueLength;
490 pxNewQueue->uxItemSize = uxItemSize;
491 ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
493 #if ( configUSE_TRACE_FACILITY == 1 )
495 pxNewQueue->ucQueueType = ucQueueType;
497 #endif /* configUSE_TRACE_FACILITY */
499 #if ( configUSE_QUEUE_SETS == 1 )
501 pxNewQueue->pxQueueSetContainer = NULL;
503 #endif /* configUSE_QUEUE_SETS */
505 traceQUEUE_CREATE( pxNewQueue );
507 /*-----------------------------------------------------------*/
509 #if ( configUSE_MUTEXES == 1 )
511 static void prvInitialiseMutex( Queue_t * pxNewQueue )
513 if( pxNewQueue != NULL )
515 /* The queue create function will set all the queue structure members
516 * correctly for a generic queue, but this function is creating a
517 * mutex. Overwrite those members that need to be set differently -
518 * in particular the information required for priority inheritance. */
519 pxNewQueue->u.xSemaphore.xMutexHolder = NULL;
520 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
522 /* In case this is a recursive mutex. */
523 pxNewQueue->u.xSemaphore.uxRecursiveCallCount = 0;
525 traceCREATE_MUTEX( pxNewQueue );
527 /* Start with the semaphore in the expected state. */
528 ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
532 traceCREATE_MUTEX_FAILED();
536 #endif /* configUSE_MUTEXES */
537 /*-----------------------------------------------------------*/
539 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
541 QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
543 QueueHandle_t xNewQueue;
544 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
546 xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
547 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
552 #endif /* configUSE_MUTEXES */
553 /*-----------------------------------------------------------*/
555 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
557 QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
558 StaticQueue_t * pxStaticQueue )
560 QueueHandle_t xNewQueue;
561 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
563 /* Prevent compiler warnings about unused parameters if
564 * configUSE_TRACE_FACILITY does not equal 1. */
565 ( void ) ucQueueType;
567 xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
568 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
573 #endif /* configUSE_MUTEXES */
574 /*-----------------------------------------------------------*/
576 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
578 TaskHandle_t xQueueGetMutexHolder( QueueHandle_t xSemaphore )
580 TaskHandle_t pxReturn;
581 Queue_t * const pxSemaphore = ( Queue_t * ) xSemaphore;
583 configASSERT( xSemaphore );
585 /* This function is called by xSemaphoreGetMutexHolder(), and should not
586 * be called directly. Note: This is a good way of determining if the
587 * calling task is the mutex holder, but not a good way of determining the
588 * identity of the mutex holder, as the holder may change between the
589 * following critical section exiting and the function returning. */
590 taskENTER_CRITICAL();
592 if( pxSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
594 pxReturn = pxSemaphore->u.xSemaphore.xMutexHolder;
604 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
606 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
607 /*-----------------------------------------------------------*/
609 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
611 TaskHandle_t xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )
613 TaskHandle_t pxReturn;
615 configASSERT( xSemaphore );
617 /* Mutexes cannot be used in interrupt service routines, so the mutex
618 * holder should not change in an ISR, and therefore a critical section is
619 * not required here. */
620 if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
622 pxReturn = ( ( Queue_t * ) xSemaphore )->u.xSemaphore.xMutexHolder;
630 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
632 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
633 /*-----------------------------------------------------------*/
635 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
637 BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
640 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
642 configASSERT( pxMutex );
644 /* If this is the task that holds the mutex then xMutexHolder will not
645 * change outside of this task. If this task does not hold the mutex then
646 * pxMutexHolder can never coincidentally equal the tasks handle, and as
647 * this is the only condition we are interested in it does not matter if
648 * pxMutexHolder is accessed simultaneously by another task. Therefore no
649 * mutual exclusion is required to test the pxMutexHolder variable. */
650 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
652 traceGIVE_MUTEX_RECURSIVE( pxMutex );
654 /* uxRecursiveCallCount cannot be zero if xMutexHolder is equal to
655 * the task handle, therefore no underflow check is required. Also,
656 * uxRecursiveCallCount is only modified by the mutex holder, and as
657 * there can only be one, no mutual exclusion is required to modify the
658 * uxRecursiveCallCount member. */
659 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )--;
661 /* Has the recursive call count unwound to 0? */
662 if( pxMutex->u.xSemaphore.uxRecursiveCallCount == ( UBaseType_t ) 0 )
664 /* Return the mutex. This will automatically unblock any other
665 * task that might be waiting to access the mutex. */
666 ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
670 mtCOVERAGE_TEST_MARKER();
677 /* The mutex cannot be given because the calling task is not the
681 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
687 #endif /* configUSE_RECURSIVE_MUTEXES */
688 /*-----------------------------------------------------------*/
690 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
692 BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
693 TickType_t xTicksToWait )
696 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
698 configASSERT( pxMutex );
700 /* Comments regarding mutual exclusion as per those within
701 * xQueueGiveMutexRecursive(). */
703 traceTAKE_MUTEX_RECURSIVE( pxMutex );
705 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
707 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
712 xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );
714 /* pdPASS will only be returned if the mutex was successfully
715 * obtained. The calling task may have entered the Blocked state
716 * before reaching here. */
717 if( xReturn != pdFAIL )
719 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
723 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
730 #endif /* configUSE_RECURSIVE_MUTEXES */
731 /*-----------------------------------------------------------*/
733 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
735 QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
736 const UBaseType_t uxInitialCount,
737 StaticQueue_t * pxStaticQueue )
739 QueueHandle_t xHandle = NULL;
741 if( ( uxMaxCount != 0 ) &&
742 ( uxInitialCount <= uxMaxCount ) )
744 xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
746 if( xHandle != NULL )
748 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
750 traceCREATE_COUNTING_SEMAPHORE();
754 traceCREATE_COUNTING_SEMAPHORE_FAILED();
759 configASSERT( xHandle );
760 mtCOVERAGE_TEST_MARKER();
766 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
767 /*-----------------------------------------------------------*/
769 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
771 QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
772 const UBaseType_t uxInitialCount )
774 QueueHandle_t xHandle = NULL;
776 if( ( uxMaxCount != 0 ) &&
777 ( uxInitialCount <= uxMaxCount ) )
779 xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
781 if( xHandle != NULL )
783 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
785 traceCREATE_COUNTING_SEMAPHORE();
789 traceCREATE_COUNTING_SEMAPHORE_FAILED();
794 configASSERT( xHandle );
795 mtCOVERAGE_TEST_MARKER();
801 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
802 /*-----------------------------------------------------------*/
804 BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
805 const void * const pvItemToQueue,
806 TickType_t xTicksToWait,
807 const BaseType_t xCopyPosition )
809 BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
811 Queue_t * const pxQueue = xQueue;
813 configASSERT( pxQueue );
814 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
815 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
816 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
818 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
822 /*lint -save -e904 This function relaxes the coding standard somewhat to
823 * allow return statements within the function itself. This is done in the
824 * interest of execution time efficiency. */
827 taskENTER_CRITICAL();
829 /* Is there room on the queue now? The running task must be the
830 * highest priority task wanting to access the queue. If the head item
831 * in the queue is to be overwritten then it does not matter if the
833 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
835 traceQUEUE_SEND( pxQueue );
837 #if ( configUSE_QUEUE_SETS == 1 )
839 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
841 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
843 if( pxQueue->pxQueueSetContainer != NULL )
845 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
847 /* Do not notify the queue set as an existing item
848 * was overwritten in the queue so the number of items
849 * in the queue has not changed. */
850 mtCOVERAGE_TEST_MARKER();
852 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
854 /* The queue is a member of a queue set, and posting
855 * to the queue set caused a higher priority task to
856 * unblock. A context switch is required. */
857 queueYIELD_IF_USING_PREEMPTION();
861 mtCOVERAGE_TEST_MARKER();
866 /* If there was a task waiting for data to arrive on the
867 * queue then unblock it now. */
868 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
870 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
872 /* The unblocked task has a priority higher than
873 * our own so yield immediately. Yes it is ok to
874 * do this from within the critical section - the
875 * kernel takes care of that. */
876 queueYIELD_IF_USING_PREEMPTION();
880 mtCOVERAGE_TEST_MARKER();
883 else if( xYieldRequired != pdFALSE )
885 /* This path is a special case that will only get
886 * executed if the task was holding multiple mutexes
887 * and the mutexes were given back in an order that is
888 * different to that in which they were taken. */
889 queueYIELD_IF_USING_PREEMPTION();
893 mtCOVERAGE_TEST_MARKER();
897 #else /* configUSE_QUEUE_SETS */
899 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
901 /* If there was a task waiting for data to arrive on the
902 * queue then unblock it now. */
903 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
905 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
907 /* The unblocked task has a priority higher than
908 * our own so yield immediately. Yes it is ok to do
909 * this from within the critical section - the kernel
910 * takes care of that. */
911 queueYIELD_IF_USING_PREEMPTION();
915 mtCOVERAGE_TEST_MARKER();
918 else if( xYieldRequired != pdFALSE )
920 /* This path is a special case that will only get
921 * executed if the task was holding multiple mutexes and
922 * the mutexes were given back in an order that is
923 * different to that in which they were taken. */
924 queueYIELD_IF_USING_PREEMPTION();
928 mtCOVERAGE_TEST_MARKER();
931 #endif /* configUSE_QUEUE_SETS */
938 if( xTicksToWait == ( TickType_t ) 0 )
940 /* The queue was full and no block time is specified (or
941 * the block time has expired) so leave now. */
944 /* Return to the original privilege level before exiting
946 traceQUEUE_SEND_FAILED( pxQueue );
947 return errQUEUE_FULL;
949 else if( xEntryTimeSet == pdFALSE )
951 /* The queue was full and a block time was specified so
952 * configure the timeout structure. */
953 vTaskInternalSetTimeOutState( &xTimeOut );
954 xEntryTimeSet = pdTRUE;
958 /* Entry time was already set. */
959 mtCOVERAGE_TEST_MARKER();
965 /* Interrupts and other tasks can send to and receive from the queue
966 * now the critical section has been exited. */
969 prvLockQueue( pxQueue );
971 /* Update the timeout state to see if it has expired yet. */
972 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
974 if( prvIsQueueFull( pxQueue ) != pdFALSE )
976 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
977 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
979 /* Unlocking the queue means queue events can effect the
980 * event list. It is possible that interrupts occurring now
981 * remove this task from the event list again - but as the
982 * scheduler is suspended the task will go onto the pending
983 * ready list instead of the actual ready list. */
984 prvUnlockQueue( pxQueue );
986 /* Resuming the scheduler will move tasks from the pending
987 * ready list into the ready list - so it is feasible that this
988 * task is already in the ready list before it yields - in which
989 * case the yield will not cause a context switch unless there
990 * is also a higher priority task in the pending ready list. */
991 if( xTaskResumeAll() == pdFALSE )
993 portYIELD_WITHIN_API();
999 prvUnlockQueue( pxQueue );
1000 ( void ) xTaskResumeAll();
1005 /* The timeout has expired. */
1006 prvUnlockQueue( pxQueue );
1007 ( void ) xTaskResumeAll();
1009 traceQUEUE_SEND_FAILED( pxQueue );
1010 return errQUEUE_FULL;
1012 } /*lint -restore */
1014 /*-----------------------------------------------------------*/
1016 BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,
1017 const void * const pvItemToQueue,
1018 BaseType_t * const pxHigherPriorityTaskWoken,
1019 const BaseType_t xCopyPosition )
1022 UBaseType_t uxSavedInterruptStatus;
1023 Queue_t * const pxQueue = xQueue;
1025 configASSERT( pxQueue );
1026 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1027 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
1029 /* RTOS ports that support interrupt nesting have the concept of a maximum
1030 * system call (or maximum API call) interrupt priority. Interrupts that are
1031 * above the maximum system call priority are kept permanently enabled, even
1032 * when the RTOS kernel is in a critical section, but cannot make any calls to
1033 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1034 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1035 * failure if a FreeRTOS API function is called from an interrupt that has been
1036 * assigned a priority above the configured maximum system call priority.
1037 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1038 * that have been assigned a priority at or (logically) below the maximum
1039 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1040 * safe API to ensure interrupt entry is as fast and as simple as possible.
1041 * More information (albeit Cortex-M specific) is provided on the following
1042 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1043 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1045 /* Similar to xQueueGenericSend, except without blocking if there is no room
1046 * in the queue. Also don't directly wake a task that was blocked on a queue
1047 * read, instead return a flag to say whether a context switch is required or
1048 * not (i.e. has a task with a higher priority than us been woken by this
1050 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1052 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
1054 const int8_t cTxLock = pxQueue->cTxLock;
1055 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
1057 traceQUEUE_SEND_FROM_ISR( pxQueue );
1059 /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a
1060 * semaphore or mutex. That means prvCopyDataToQueue() cannot result
1061 * in a task disinheriting a priority and prvCopyDataToQueue() can be
1062 * called here even though the disinherit function does not check if
1063 * the scheduler is suspended before accessing the ready lists. */
1064 ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
1066 /* The event list is not altered if the queue is locked. This will
1067 * be done when the queue is unlocked later. */
1068 if( cTxLock == queueUNLOCKED )
1070 #if ( configUSE_QUEUE_SETS == 1 )
1072 if( pxQueue->pxQueueSetContainer != NULL )
1074 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
1076 /* Do not notify the queue set as an existing item
1077 * was overwritten in the queue so the number of items
1078 * in the queue has not changed. */
1079 mtCOVERAGE_TEST_MARKER();
1081 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1083 /* The queue is a member of a queue set, and posting
1084 * to the queue set caused a higher priority task to
1085 * unblock. A context switch is required. */
1086 if( pxHigherPriorityTaskWoken != NULL )
1088 *pxHigherPriorityTaskWoken = pdTRUE;
1092 mtCOVERAGE_TEST_MARKER();
1097 mtCOVERAGE_TEST_MARKER();
1102 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1104 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1106 /* The task waiting has a higher priority so
1107 * record that a context switch is required. */
1108 if( pxHigherPriorityTaskWoken != NULL )
1110 *pxHigherPriorityTaskWoken = pdTRUE;
1114 mtCOVERAGE_TEST_MARKER();
1119 mtCOVERAGE_TEST_MARKER();
1124 mtCOVERAGE_TEST_MARKER();
1128 #else /* configUSE_QUEUE_SETS */
1130 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1132 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1134 /* The task waiting has a higher priority so record that a
1135 * context switch is required. */
1136 if( pxHigherPriorityTaskWoken != NULL )
1138 *pxHigherPriorityTaskWoken = pdTRUE;
1142 mtCOVERAGE_TEST_MARKER();
1147 mtCOVERAGE_TEST_MARKER();
1152 mtCOVERAGE_TEST_MARKER();
1155 /* Not used in this path. */
1156 ( void ) uxPreviousMessagesWaiting;
1158 #endif /* configUSE_QUEUE_SETS */
1162 /* Increment the lock count so the task that unlocks the queue
1163 * knows that data was posted while it was locked. */
1164 configASSERT( cTxLock != queueINT8_MAX );
1166 pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );
1173 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1174 xReturn = errQUEUE_FULL;
1177 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1181 /*-----------------------------------------------------------*/
1183 BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,
1184 BaseType_t * const pxHigherPriorityTaskWoken )
1187 UBaseType_t uxSavedInterruptStatus;
1188 Queue_t * const pxQueue = xQueue;
1190 /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
1191 * item size is 0. Don't directly wake a task that was blocked on a queue
1192 * read, instead return a flag to say whether a context switch is required or
1193 * not (i.e. has a task with a higher priority than us been woken by this
1196 configASSERT( pxQueue );
1198 /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()
1199 * if the item size is not 0. */
1200 configASSERT( pxQueue->uxItemSize == 0 );
1202 /* Normally a mutex would not be given from an interrupt, especially if
1203 * there is a mutex holder, as priority inheritance makes no sense for an
1204 * interrupts, only tasks. */
1205 configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->u.xSemaphore.xMutexHolder != NULL ) ) );
1207 /* RTOS ports that support interrupt nesting have the concept of a maximum
1208 * system call (or maximum API call) interrupt priority. Interrupts that are
1209 * above the maximum system call priority are kept permanently enabled, even
1210 * when the RTOS kernel is in a critical section, but cannot make any calls to
1211 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1212 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1213 * failure if a FreeRTOS API function is called from an interrupt that has been
1214 * assigned a priority above the configured maximum system call priority.
1215 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1216 * that have been assigned a priority at or (logically) below the maximum
1217 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1218 * safe API to ensure interrupt entry is as fast and as simple as possible.
1219 * More information (albeit Cortex-M specific) is provided on the following
1220 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1221 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1223 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1225 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1227 /* When the queue is used to implement a semaphore no data is ever
1228 * moved through the queue but it is still valid to see if the queue 'has
1230 if( uxMessagesWaiting < pxQueue->uxLength )
1232 const int8_t cTxLock = pxQueue->cTxLock;
1234 traceQUEUE_SEND_FROM_ISR( pxQueue );
1236 /* A task can only have an inherited priority if it is a mutex
1237 * holder - and if there is a mutex holder then the mutex cannot be
1238 * given from an ISR. As this is the ISR version of the function it
1239 * can be assumed there is no mutex holder and no need to determine if
1240 * priority disinheritance is needed. Simply increase the count of
1241 * messages (semaphores) available. */
1242 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
1244 /* The event list is not altered if the queue is locked. This will
1245 * be done when the queue is unlocked later. */
1246 if( cTxLock == queueUNLOCKED )
1248 #if ( configUSE_QUEUE_SETS == 1 )
1250 if( pxQueue->pxQueueSetContainer != NULL )
1252 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1254 /* The semaphore is a member of a queue set, and
1255 * posting to the queue set caused a higher priority
1256 * task to unblock. A context switch is required. */
1257 if( pxHigherPriorityTaskWoken != NULL )
1259 *pxHigherPriorityTaskWoken = pdTRUE;
1263 mtCOVERAGE_TEST_MARKER();
1268 mtCOVERAGE_TEST_MARKER();
1273 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1275 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1277 /* The task waiting has a higher priority so
1278 * record that a context switch is required. */
1279 if( pxHigherPriorityTaskWoken != NULL )
1281 *pxHigherPriorityTaskWoken = pdTRUE;
1285 mtCOVERAGE_TEST_MARKER();
1290 mtCOVERAGE_TEST_MARKER();
1295 mtCOVERAGE_TEST_MARKER();
1299 #else /* configUSE_QUEUE_SETS */
1301 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1303 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1305 /* The task waiting has a higher priority so record that a
1306 * context switch is required. */
1307 if( pxHigherPriorityTaskWoken != NULL )
1309 *pxHigherPriorityTaskWoken = pdTRUE;
1313 mtCOVERAGE_TEST_MARKER();
1318 mtCOVERAGE_TEST_MARKER();
1323 mtCOVERAGE_TEST_MARKER();
1326 #endif /* configUSE_QUEUE_SETS */
1330 /* Increment the lock count so the task that unlocks the queue
1331 * knows that data was posted while it was locked. */
1332 configASSERT( cTxLock != queueINT8_MAX );
1334 pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );
1341 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1342 xReturn = errQUEUE_FULL;
1345 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1349 /*-----------------------------------------------------------*/
1351 BaseType_t xQueueReceive( QueueHandle_t xQueue,
1352 void * const pvBuffer,
1353 TickType_t xTicksToWait )
1355 BaseType_t xEntryTimeSet = pdFALSE;
1357 Queue_t * const pxQueue = xQueue;
1359 /* Check the pointer is not NULL. */
1360 configASSERT( ( pxQueue ) );
1362 /* The buffer into which data is received can only be NULL if the data size
1363 * is zero (so no data is copied into the buffer). */
1364 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1366 /* Cannot block if the scheduler is suspended. */
1367 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1369 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1373 /*lint -save -e904 This function relaxes the coding standard somewhat to
1374 * allow return statements within the function itself. This is done in the
1375 * interest of execution time efficiency. */
1378 taskENTER_CRITICAL();
1380 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1382 /* Is there data in the queue now? To be running the calling task
1383 * must be the highest priority task wanting to access the queue. */
1384 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1386 /* Data available, remove one item. */
1387 prvCopyDataFromQueue( pxQueue, pvBuffer );
1388 traceQUEUE_RECEIVE( pxQueue );
1389 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
1391 /* There is now space in the queue, were any tasks waiting to
1392 * post to the queue? If so, unblock the highest priority waiting
1394 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1396 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1398 queueYIELD_IF_USING_PREEMPTION();
1402 mtCOVERAGE_TEST_MARKER();
1407 mtCOVERAGE_TEST_MARKER();
1410 taskEXIT_CRITICAL();
1415 if( xTicksToWait == ( TickType_t ) 0 )
1417 /* The queue was empty and no block time is specified (or
1418 * the block time has expired) so leave now. */
1419 taskEXIT_CRITICAL();
1420 traceQUEUE_RECEIVE_FAILED( pxQueue );
1421 return errQUEUE_EMPTY;
1423 else if( xEntryTimeSet == pdFALSE )
1425 /* The queue was empty and a block time was specified so
1426 * configure the timeout structure. */
1427 vTaskInternalSetTimeOutState( &xTimeOut );
1428 xEntryTimeSet = pdTRUE;
1432 /* Entry time was already set. */
1433 mtCOVERAGE_TEST_MARKER();
1437 taskEXIT_CRITICAL();
1439 /* Interrupts and other tasks can send to and receive from the queue
1440 * now the critical section has been exited. */
1443 prvLockQueue( pxQueue );
1445 /* Update the timeout state to see if it has expired yet. */
1446 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1448 /* The timeout has not expired. If the queue is still empty place
1449 * the task on the list of tasks waiting to receive from the queue. */
1450 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1452 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1453 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1454 prvUnlockQueue( pxQueue );
1456 if( xTaskResumeAll() == pdFALSE )
1458 portYIELD_WITHIN_API();
1462 mtCOVERAGE_TEST_MARKER();
1467 /* The queue contains data again. Loop back to try and read the
1469 prvUnlockQueue( pxQueue );
1470 ( void ) xTaskResumeAll();
1475 /* Timed out. If there is no data in the queue exit, otherwise loop
1476 * back and attempt to read the data. */
1477 prvUnlockQueue( pxQueue );
1478 ( void ) xTaskResumeAll();
1480 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1482 traceQUEUE_RECEIVE_FAILED( pxQueue );
1483 return errQUEUE_EMPTY;
1487 mtCOVERAGE_TEST_MARKER();
1490 } /*lint -restore */
1492 /*-----------------------------------------------------------*/
1494 BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
1495 TickType_t xTicksToWait )
1497 BaseType_t xEntryTimeSet = pdFALSE;
1499 Queue_t * const pxQueue = xQueue;
1501 #if ( configUSE_MUTEXES == 1 )
1502 BaseType_t xInheritanceOccurred = pdFALSE;
1505 /* Check the queue pointer is not NULL. */
1506 configASSERT( ( pxQueue ) );
1508 /* Check this really is a semaphore, in which case the item size will be
1510 configASSERT( pxQueue->uxItemSize == 0 );
1512 /* Cannot block if the scheduler is suspended. */
1513 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1515 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1519 /*lint -save -e904 This function relaxes the coding standard somewhat to allow return
1520 * statements within the function itself. This is done in the interest
1521 * of execution time efficiency. */
1524 taskENTER_CRITICAL();
1526 /* Semaphores are queues with an item size of 0, and where the
1527 * number of messages in the queue is the semaphore's count value. */
1528 const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;
1530 /* Is there data in the queue now? To be running the calling task
1531 * must be the highest priority task wanting to access the queue. */
1532 if( uxSemaphoreCount > ( UBaseType_t ) 0 )
1534 traceQUEUE_RECEIVE( pxQueue );
1536 /* Semaphores are queues with a data size of zero and where the
1537 * messages waiting is the semaphore's count. Reduce the count. */
1538 pxQueue->uxMessagesWaiting = uxSemaphoreCount - ( UBaseType_t ) 1;
1540 #if ( configUSE_MUTEXES == 1 )
1542 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1544 /* Record the information required to implement
1545 * priority inheritance should it become necessary. */
1546 pxQueue->u.xSemaphore.xMutexHolder = pvTaskIncrementMutexHeldCount();
1550 mtCOVERAGE_TEST_MARKER();
1553 #endif /* configUSE_MUTEXES */
1555 /* Check to see if other tasks are blocked waiting to give the
1556 * semaphore, and if so, unblock the highest priority such task. */
1557 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1559 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1561 queueYIELD_IF_USING_PREEMPTION();
1565 mtCOVERAGE_TEST_MARKER();
1570 mtCOVERAGE_TEST_MARKER();
1573 taskEXIT_CRITICAL();
1578 if( xTicksToWait == ( TickType_t ) 0 )
1580 /* For inheritance to have occurred there must have been an
1581 * initial timeout, and an adjusted timeout cannot become 0, as
1582 * if it were 0 the function would have exited. */
1583 #if ( configUSE_MUTEXES == 1 )
1585 configASSERT( xInheritanceOccurred == pdFALSE );
1587 #endif /* configUSE_MUTEXES */
1589 /* The semaphore count was 0 and no block time is specified
1590 * (or the block time has expired) so exit now. */
1591 taskEXIT_CRITICAL();
1592 traceQUEUE_RECEIVE_FAILED( pxQueue );
1593 return errQUEUE_EMPTY;
1595 else if( xEntryTimeSet == pdFALSE )
1597 /* The semaphore count was 0 and a block time was specified
1598 * so configure the timeout structure ready to block. */
1599 vTaskInternalSetTimeOutState( &xTimeOut );
1600 xEntryTimeSet = pdTRUE;
1604 /* Entry time was already set. */
1605 mtCOVERAGE_TEST_MARKER();
1609 taskEXIT_CRITICAL();
1611 /* Interrupts and other tasks can give to and take from the semaphore
1612 * now the critical section has been exited. */
1615 prvLockQueue( pxQueue );
1617 /* Update the timeout state to see if it has expired yet. */
1618 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1620 /* A block time is specified and not expired. If the semaphore
1621 * count is 0 then enter the Blocked state to wait for a semaphore to
1622 * become available. As semaphores are implemented with queues the
1623 * queue being empty is equivalent to the semaphore count being 0. */
1624 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1626 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1628 #if ( configUSE_MUTEXES == 1 )
1630 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1632 taskENTER_CRITICAL();
1634 xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );
1636 taskEXIT_CRITICAL();
1640 mtCOVERAGE_TEST_MARKER();
1643 #endif /* if ( configUSE_MUTEXES == 1 ) */
1645 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1646 prvUnlockQueue( pxQueue );
1648 if( xTaskResumeAll() == pdFALSE )
1650 portYIELD_WITHIN_API();
1654 mtCOVERAGE_TEST_MARKER();
1659 /* There was no timeout and the semaphore count was not 0, so
1660 * attempt to take the semaphore again. */
1661 prvUnlockQueue( pxQueue );
1662 ( void ) xTaskResumeAll();
1668 prvUnlockQueue( pxQueue );
1669 ( void ) xTaskResumeAll();
1671 /* If the semaphore count is 0 exit now as the timeout has
1672 * expired. Otherwise return to attempt to take the semaphore that is
1673 * known to be available. As semaphores are implemented by queues the
1674 * queue being empty is equivalent to the semaphore count being 0. */
1675 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1677 #if ( configUSE_MUTEXES == 1 )
1679 /* xInheritanceOccurred could only have be set if
1680 * pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
1681 * test the mutex type again to check it is actually a mutex. */
1682 if( xInheritanceOccurred != pdFALSE )
1684 taskENTER_CRITICAL();
1686 UBaseType_t uxHighestWaitingPriority;
1688 /* This task blocking on the mutex caused another
1689 * task to inherit this task's priority. Now this task
1690 * has timed out the priority should be disinherited
1691 * again, but only as low as the next highest priority
1692 * task that is waiting for the same mutex. */
1693 uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );
1694 vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );
1696 taskEXIT_CRITICAL();
1699 #endif /* configUSE_MUTEXES */
1701 traceQUEUE_RECEIVE_FAILED( pxQueue );
1702 return errQUEUE_EMPTY;
1706 mtCOVERAGE_TEST_MARKER();
1709 } /*lint -restore */
1711 /*-----------------------------------------------------------*/
1713 BaseType_t xQueuePeek( QueueHandle_t xQueue,
1714 void * const pvBuffer,
1715 TickType_t xTicksToWait )
1717 BaseType_t xEntryTimeSet = pdFALSE;
1719 int8_t * pcOriginalReadPosition;
1720 Queue_t * const pxQueue = xQueue;
1722 /* Check the pointer is not NULL. */
1723 configASSERT( ( pxQueue ) );
1725 /* The buffer into which data is received can only be NULL if the data size
1726 * is zero (so no data is copied into the buffer. */
1727 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1729 /* Cannot block if the scheduler is suspended. */
1730 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1732 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1736 /*lint -save -e904 This function relaxes the coding standard somewhat to
1737 * allow return statements within the function itself. This is done in the
1738 * interest of execution time efficiency. */
1741 taskENTER_CRITICAL();
1743 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1745 /* Is there data in the queue now? To be running the calling task
1746 * must be the highest priority task wanting to access the queue. */
1747 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1749 /* Remember the read position so it can be reset after the data
1750 * is read from the queue as this function is only peeking the
1751 * data, not removing it. */
1752 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
1754 prvCopyDataFromQueue( pxQueue, pvBuffer );
1755 traceQUEUE_PEEK( pxQueue );
1757 /* The data is not being removed, so reset the read pointer. */
1758 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
1760 /* The data is being left in the queue, so see if there are
1761 * any other tasks waiting for the data. */
1762 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1764 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1766 /* The task waiting has a higher priority than this task. */
1767 queueYIELD_IF_USING_PREEMPTION();
1771 mtCOVERAGE_TEST_MARKER();
1776 mtCOVERAGE_TEST_MARKER();
1779 taskEXIT_CRITICAL();
1784 if( xTicksToWait == ( TickType_t ) 0 )
1786 /* The queue was empty and no block time is specified (or
1787 * the block time has expired) so leave now. */
1788 taskEXIT_CRITICAL();
1789 traceQUEUE_PEEK_FAILED( pxQueue );
1790 return errQUEUE_EMPTY;
1792 else if( xEntryTimeSet == pdFALSE )
1794 /* The queue was empty and a block time was specified so
1795 * configure the timeout structure ready to enter the blocked
1797 vTaskInternalSetTimeOutState( &xTimeOut );
1798 xEntryTimeSet = pdTRUE;
1802 /* Entry time was already set. */
1803 mtCOVERAGE_TEST_MARKER();
1807 taskEXIT_CRITICAL();
1809 /* Interrupts and other tasks can send to and receive from the queue
1810 * now that the critical section has been exited. */
1813 prvLockQueue( pxQueue );
1815 /* Update the timeout state to see if it has expired yet. */
1816 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1818 /* Timeout has not expired yet, check to see if there is data in the
1819 * queue now, and if not enter the Blocked state to wait for data. */
1820 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1822 traceBLOCKING_ON_QUEUE_PEEK( pxQueue );
1823 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1824 prvUnlockQueue( pxQueue );
1826 if( xTaskResumeAll() == pdFALSE )
1828 portYIELD_WITHIN_API();
1832 mtCOVERAGE_TEST_MARKER();
1837 /* There is data in the queue now, so don't enter the blocked
1838 * state, instead return to try and obtain the data. */
1839 prvUnlockQueue( pxQueue );
1840 ( void ) xTaskResumeAll();
1845 /* The timeout has expired. If there is still no data in the queue
1846 * exit, otherwise go back and try to read the data again. */
1847 prvUnlockQueue( pxQueue );
1848 ( void ) xTaskResumeAll();
1850 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1852 traceQUEUE_PEEK_FAILED( pxQueue );
1853 return errQUEUE_EMPTY;
1857 mtCOVERAGE_TEST_MARKER();
1860 } /*lint -restore */
1862 /*-----------------------------------------------------------*/
1864 BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,
1865 void * const pvBuffer,
1866 BaseType_t * const pxHigherPriorityTaskWoken )
1869 UBaseType_t uxSavedInterruptStatus;
1870 Queue_t * const pxQueue = xQueue;
1872 configASSERT( pxQueue );
1873 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1875 /* RTOS ports that support interrupt nesting have the concept of a maximum
1876 * system call (or maximum API call) interrupt priority. Interrupts that are
1877 * above the maximum system call priority are kept permanently enabled, even
1878 * when the RTOS kernel is in a critical section, but cannot make any calls to
1879 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1880 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1881 * failure if a FreeRTOS API function is called from an interrupt that has been
1882 * assigned a priority above the configured maximum system call priority.
1883 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1884 * that have been assigned a priority at or (logically) below the maximum
1885 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1886 * safe API to ensure interrupt entry is as fast and as simple as possible.
1887 * More information (albeit Cortex-M specific) is provided on the following
1888 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1889 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1891 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1893 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1895 /* Cannot block in an ISR, so check there is data available. */
1896 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1898 const int8_t cRxLock = pxQueue->cRxLock;
1900 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
1902 prvCopyDataFromQueue( pxQueue, pvBuffer );
1903 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
1905 /* If the queue is locked the event list will not be modified.
1906 * Instead update the lock count so the task that unlocks the queue
1907 * will know that an ISR has removed data while the queue was
1909 if( cRxLock == queueUNLOCKED )
1911 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1913 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1915 /* The task waiting has a higher priority than us so
1916 * force a context switch. */
1917 if( pxHigherPriorityTaskWoken != NULL )
1919 *pxHigherPriorityTaskWoken = pdTRUE;
1923 mtCOVERAGE_TEST_MARKER();
1928 mtCOVERAGE_TEST_MARKER();
1933 mtCOVERAGE_TEST_MARKER();
1938 /* Increment the lock count so the task that unlocks the queue
1939 * knows that data was removed while it was locked. */
1940 configASSERT( cRxLock != queueINT8_MAX );
1942 pxQueue->cRxLock = ( int8_t ) ( cRxLock + 1 );
1950 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
1953 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1957 /*-----------------------------------------------------------*/
1959 BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,
1960 void * const pvBuffer )
1963 UBaseType_t uxSavedInterruptStatus;
1964 int8_t * pcOriginalReadPosition;
1965 Queue_t * const pxQueue = xQueue;
1967 configASSERT( pxQueue );
1968 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1969 configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
1971 /* RTOS ports that support interrupt nesting have the concept of a maximum
1972 * system call (or maximum API call) interrupt priority. Interrupts that are
1973 * above the maximum system call priority are kept permanently enabled, even
1974 * when the RTOS kernel is in a critical section, but cannot make any calls to
1975 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1976 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1977 * failure if a FreeRTOS API function is called from an interrupt that has been
1978 * assigned a priority above the configured maximum system call priority.
1979 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1980 * that have been assigned a priority at or (logically) below the maximum
1981 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1982 * safe API to ensure interrupt entry is as fast and as simple as possible.
1983 * More information (albeit Cortex-M specific) is provided on the following
1984 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1985 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1987 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1989 /* Cannot block in an ISR, so check there is data available. */
1990 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
1992 traceQUEUE_PEEK_FROM_ISR( pxQueue );
1994 /* Remember the read position so it can be reset as nothing is
1995 * actually being removed from the queue. */
1996 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
1997 prvCopyDataFromQueue( pxQueue, pvBuffer );
1998 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
2005 traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
2008 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2012 /*-----------------------------------------------------------*/
2014 UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
2016 UBaseType_t uxReturn;
2018 configASSERT( xQueue );
2020 taskENTER_CRITICAL();
2022 uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
2024 taskEXIT_CRITICAL();
2027 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2028 /*-----------------------------------------------------------*/
2030 UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
2032 UBaseType_t uxReturn;
2033 Queue_t * const pxQueue = xQueue;
2035 configASSERT( pxQueue );
2037 taskENTER_CRITICAL();
2039 uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;
2041 taskEXIT_CRITICAL();
2044 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2045 /*-----------------------------------------------------------*/
2047 UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
2049 UBaseType_t uxReturn;
2050 Queue_t * const pxQueue = xQueue;
2052 configASSERT( pxQueue );
2053 uxReturn = pxQueue->uxMessagesWaiting;
2056 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2057 /*-----------------------------------------------------------*/
2059 void vQueueDelete( QueueHandle_t xQueue )
2061 Queue_t * const pxQueue = xQueue;
2063 configASSERT( pxQueue );
2064 traceQUEUE_DELETE( pxQueue );
2066 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2068 vQueueUnregisterQueue( pxQueue );
2072 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
2074 /* The queue can only have been allocated dynamically - free it
2076 vPortFree( pxQueue );
2078 #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
2080 /* The queue could have been allocated statically or dynamically, so
2081 * check before attempting to free the memory. */
2082 if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
2084 vPortFree( pxQueue );
2088 mtCOVERAGE_TEST_MARKER();
2091 #else /* if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) ) */
2093 /* The queue must have been statically allocated, so is not going to be
2094 * deleted. Avoid compiler warnings about the unused parameter. */
2097 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
2099 /*-----------------------------------------------------------*/
2101 #if ( configUSE_TRACE_FACILITY == 1 )
2103 UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
2105 return ( ( Queue_t * ) xQueue )->uxQueueNumber;
2108 #endif /* configUSE_TRACE_FACILITY */
2109 /*-----------------------------------------------------------*/
2111 #if ( configUSE_TRACE_FACILITY == 1 )
2113 void vQueueSetQueueNumber( QueueHandle_t xQueue,
2114 UBaseType_t uxQueueNumber )
2116 ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
2119 #endif /* configUSE_TRACE_FACILITY */
2120 /*-----------------------------------------------------------*/
2122 #if ( configUSE_TRACE_FACILITY == 1 )
2124 uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
2126 return ( ( Queue_t * ) xQueue )->ucQueueType;
2129 #endif /* configUSE_TRACE_FACILITY */
2130 /*-----------------------------------------------------------*/
2132 #if ( configUSE_MUTEXES == 1 )
2134 static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue )
2136 UBaseType_t uxHighestPriorityOfWaitingTasks;
2138 /* If a task waiting for a mutex causes the mutex holder to inherit a
2139 * priority, but the waiting task times out, then the holder should
2140 * disinherit the priority - but only down to the highest priority of any
2141 * other tasks that are waiting for the same mutex. For this purpose,
2142 * return the priority of the highest priority task that is waiting for the
2144 if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0U )
2146 uxHighestPriorityOfWaitingTasks = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) );
2150 uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;
2153 return uxHighestPriorityOfWaitingTasks;
2156 #endif /* configUSE_MUTEXES */
2157 /*-----------------------------------------------------------*/
2159 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
2160 const void * pvItemToQueue,
2161 const BaseType_t xPosition )
2163 BaseType_t xReturn = pdFALSE;
2164 UBaseType_t uxMessagesWaiting;
2166 /* This function is called from a critical section. */
2168 uxMessagesWaiting = pxQueue->uxMessagesWaiting;
2170 if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
2172 #if ( configUSE_MUTEXES == 1 )
2174 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
2176 /* The mutex is no longer being held. */
2177 xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
2178 pxQueue->u.xSemaphore.xMutexHolder = NULL;
2182 mtCOVERAGE_TEST_MARKER();
2185 #endif /* configUSE_MUTEXES */
2187 else if( xPosition == queueSEND_TO_BACK )
2189 ( 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. */
2190 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. */
2192 if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2194 pxQueue->pcWriteTo = pxQueue->pcHead;
2198 mtCOVERAGE_TEST_MARKER();
2203 ( 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. */
2204 pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
2206 if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2208 pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
2212 mtCOVERAGE_TEST_MARKER();
2215 if( xPosition == queueOVERWRITE )
2217 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
2219 /* An item is not being added but overwritten, so subtract
2220 * one from the recorded number of items in the queue so when
2221 * one is added again below the number of recorded items remains
2223 --uxMessagesWaiting;
2227 mtCOVERAGE_TEST_MARKER();
2232 mtCOVERAGE_TEST_MARKER();
2236 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
2240 /*-----------------------------------------------------------*/
2242 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
2243 void * const pvBuffer )
2245 if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
2247 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. */
2249 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
2251 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2255 mtCOVERAGE_TEST_MARKER();
2258 ( 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. */
2261 /*-----------------------------------------------------------*/
2263 static void prvUnlockQueue( Queue_t * const pxQueue )
2265 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
2267 /* The lock counts contains the number of extra data items placed or
2268 * removed from the queue while the queue was locked. When a queue is
2269 * locked items can be added or removed, but the event lists cannot be
2271 taskENTER_CRITICAL();
2273 int8_t cTxLock = pxQueue->cTxLock;
2275 /* See if data was added to the queue while it was locked. */
2276 while( cTxLock > queueLOCKED_UNMODIFIED )
2278 /* Data was posted while the queue was locked. Are any tasks
2279 * blocked waiting for data to become available? */
2280 #if ( configUSE_QUEUE_SETS == 1 )
2282 if( pxQueue->pxQueueSetContainer != NULL )
2284 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
2286 /* The queue is a member of a queue set, and posting to
2287 * the queue set caused a higher priority task to unblock.
2288 * A context switch is required. */
2293 mtCOVERAGE_TEST_MARKER();
2298 /* Tasks that are removed from the event list will get
2299 * added to the pending ready list as the scheduler is still
2301 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2303 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2305 /* The task waiting has a higher priority so record that a
2306 * context switch is required. */
2311 mtCOVERAGE_TEST_MARKER();
2320 #else /* configUSE_QUEUE_SETS */
2322 /* Tasks that are removed from the event list will get added to
2323 * the pending ready list as the scheduler is still suspended. */
2324 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2326 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2328 /* The task waiting has a higher priority so record that
2329 * a context switch is required. */
2334 mtCOVERAGE_TEST_MARKER();
2342 #endif /* configUSE_QUEUE_SETS */
2347 pxQueue->cTxLock = queueUNLOCKED;
2349 taskEXIT_CRITICAL();
2351 /* Do the same for the Rx lock. */
2352 taskENTER_CRITICAL();
2354 int8_t cRxLock = pxQueue->cRxLock;
2356 while( cRxLock > queueLOCKED_UNMODIFIED )
2358 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2360 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2366 mtCOVERAGE_TEST_MARKER();
2377 pxQueue->cRxLock = queueUNLOCKED;
2379 taskEXIT_CRITICAL();
2381 /*-----------------------------------------------------------*/
2383 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue )
2387 taskENTER_CRITICAL();
2389 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2398 taskEXIT_CRITICAL();
2402 /*-----------------------------------------------------------*/
2404 BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )
2407 Queue_t * const pxQueue = xQueue;
2409 configASSERT( pxQueue );
2411 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2421 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2422 /*-----------------------------------------------------------*/
2424 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue )
2428 taskENTER_CRITICAL();
2430 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2439 taskEXIT_CRITICAL();
2443 /*-----------------------------------------------------------*/
2445 BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )
2448 Queue_t * const pxQueue = xQueue;
2450 configASSERT( pxQueue );
2452 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2462 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2463 /*-----------------------------------------------------------*/
2465 #if ( configUSE_CO_ROUTINES == 1 )
2467 BaseType_t xQueueCRSend( QueueHandle_t xQueue,
2468 const void * pvItemToQueue,
2469 TickType_t xTicksToWait )
2472 Queue_t * const pxQueue = xQueue;
2474 /* If the queue is already full we may have to block. A critical section
2475 * is required to prevent an interrupt removing something from the queue
2476 * between the check to see if the queue is full and blocking on the queue. */
2477 portDISABLE_INTERRUPTS();
2479 if( prvIsQueueFull( pxQueue ) != pdFALSE )
2481 /* The queue is full - do we want to block or just leave without
2483 if( xTicksToWait > ( TickType_t ) 0 )
2485 /* As this is called from a coroutine we cannot block directly, but
2486 * return indicating that we need to block. */
2487 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
2488 portENABLE_INTERRUPTS();
2489 return errQUEUE_BLOCKED;
2493 portENABLE_INTERRUPTS();
2494 return errQUEUE_FULL;
2498 portENABLE_INTERRUPTS();
2500 portDISABLE_INTERRUPTS();
2502 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2504 /* There is room in the queue, copy the data into the queue. */
2505 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2508 /* Were any co-routines waiting for data to become available? */
2509 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2511 /* In this instance the co-routine could be placed directly
2512 * into the ready list as we are within a critical section.
2513 * Instead the same pending ready list mechanism is used as if
2514 * the event were caused from within an interrupt. */
2515 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2517 /* The co-routine waiting has a higher priority so record
2518 * that a yield might be appropriate. */
2519 xReturn = errQUEUE_YIELD;
2523 mtCOVERAGE_TEST_MARKER();
2528 mtCOVERAGE_TEST_MARKER();
2533 xReturn = errQUEUE_FULL;
2536 portENABLE_INTERRUPTS();
2541 #endif /* configUSE_CO_ROUTINES */
2542 /*-----------------------------------------------------------*/
2544 #if ( configUSE_CO_ROUTINES == 1 )
2546 BaseType_t xQueueCRReceive( QueueHandle_t xQueue,
2548 TickType_t xTicksToWait )
2551 Queue_t * const pxQueue = xQueue;
2553 /* If the queue is already empty we may have to block. A critical section
2554 * is required to prevent an interrupt adding something to the queue
2555 * between the check to see if the queue is empty and blocking on the queue. */
2556 portDISABLE_INTERRUPTS();
2558 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2560 /* There are no messages in the queue, do we want to block or just
2561 * leave with nothing? */
2562 if( xTicksToWait > ( TickType_t ) 0 )
2564 /* As this is a co-routine we cannot block directly, but return
2565 * indicating that we need to block. */
2566 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
2567 portENABLE_INTERRUPTS();
2568 return errQUEUE_BLOCKED;
2572 portENABLE_INTERRUPTS();
2573 return errQUEUE_FULL;
2578 mtCOVERAGE_TEST_MARKER();
2581 portENABLE_INTERRUPTS();
2583 portDISABLE_INTERRUPTS();
2585 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2587 /* Data is available from the queue. */
2588 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2590 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2592 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2596 mtCOVERAGE_TEST_MARKER();
2599 --( pxQueue->uxMessagesWaiting );
2600 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2604 /* Were any co-routines waiting for space to become available? */
2605 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2607 /* In this instance the co-routine could be placed directly
2608 * into the ready list as we are within a critical section.
2609 * Instead the same pending ready list mechanism is used as if
2610 * the event were caused from within an interrupt. */
2611 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2613 xReturn = errQUEUE_YIELD;
2617 mtCOVERAGE_TEST_MARKER();
2622 mtCOVERAGE_TEST_MARKER();
2630 portENABLE_INTERRUPTS();
2635 #endif /* configUSE_CO_ROUTINES */
2636 /*-----------------------------------------------------------*/
2638 #if ( configUSE_CO_ROUTINES == 1 )
2640 BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue,
2641 const void * pvItemToQueue,
2642 BaseType_t xCoRoutinePreviouslyWoken )
2644 Queue_t * const pxQueue = xQueue;
2646 /* Cannot block within an ISR so if there is no space on the queue then
2647 * exit without doing anything. */
2648 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2650 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2652 /* We only want to wake one co-routine per ISR, so check that a
2653 * co-routine has not already been woken. */
2654 if( xCoRoutinePreviouslyWoken == pdFALSE )
2656 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2658 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2664 mtCOVERAGE_TEST_MARKER();
2669 mtCOVERAGE_TEST_MARKER();
2674 mtCOVERAGE_TEST_MARKER();
2679 mtCOVERAGE_TEST_MARKER();
2682 return xCoRoutinePreviouslyWoken;
2685 #endif /* configUSE_CO_ROUTINES */
2686 /*-----------------------------------------------------------*/
2688 #if ( configUSE_CO_ROUTINES == 1 )
2690 BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue,
2692 BaseType_t * pxCoRoutineWoken )
2695 Queue_t * const pxQueue = xQueue;
2697 /* We cannot block from an ISR, so check there is data available. If
2698 * not then just leave without doing anything. */
2699 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2701 /* Copy the data from the queue. */
2702 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2704 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2706 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2710 mtCOVERAGE_TEST_MARKER();
2713 --( pxQueue->uxMessagesWaiting );
2714 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2716 if( ( *pxCoRoutineWoken ) == pdFALSE )
2718 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2720 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2722 *pxCoRoutineWoken = pdTRUE;
2726 mtCOVERAGE_TEST_MARKER();
2731 mtCOVERAGE_TEST_MARKER();
2736 mtCOVERAGE_TEST_MARKER();
2749 #endif /* configUSE_CO_ROUTINES */
2750 /*-----------------------------------------------------------*/
2752 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2754 void vQueueAddToRegistry( QueueHandle_t xQueue,
2755 const char * pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2759 configASSERT( xQueue );
2761 QueueRegistryItem_t * pxEntryToWrite = NULL;
2763 if( pcQueueName != NULL )
2765 /* See if there is an empty space in the registry. A NULL name denotes
2767 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2769 /* Replace an existing entry if the queue is already in the registry. */
2770 if( xQueue == xQueueRegistry[ ux ].xHandle )
2772 pxEntryToWrite = &( xQueueRegistry[ ux ] );
2775 /* Otherwise, store in the next empty location */
2776 else if( ( pxEntryToWrite == NULL ) && ( xQueueRegistry[ ux ].pcQueueName == NULL ) )
2778 pxEntryToWrite = &( xQueueRegistry[ ux ] );
2782 mtCOVERAGE_TEST_MARKER();
2787 if( pxEntryToWrite != NULL )
2789 /* Store the information on this queue. */
2790 pxEntryToWrite->pcQueueName = pcQueueName;
2791 pxEntryToWrite->xHandle = xQueue;
2793 traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
2797 #endif /* configQUEUE_REGISTRY_SIZE */
2798 /*-----------------------------------------------------------*/
2800 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2802 const char * pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2805 const char * pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2807 configASSERT( xQueue );
2809 /* Note there is nothing here to protect against another task adding or
2810 * removing entries from the registry while it is being searched. */
2812 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2814 if( xQueueRegistry[ ux ].xHandle == xQueue )
2816 pcReturn = xQueueRegistry[ ux ].pcQueueName;
2821 mtCOVERAGE_TEST_MARKER();
2826 } /*lint !e818 xQueue cannot be a pointer to const because it is a typedef. */
2828 #endif /* configQUEUE_REGISTRY_SIZE */
2829 /*-----------------------------------------------------------*/
2831 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2833 void vQueueUnregisterQueue( QueueHandle_t xQueue )
2837 configASSERT( xQueue );
2839 /* See if the handle of the queue being unregistered in actually in the
2841 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2843 if( xQueueRegistry[ ux ].xHandle == xQueue )
2845 /* Set the name to NULL to show that this slot if free again. */
2846 xQueueRegistry[ ux ].pcQueueName = NULL;
2848 /* Set the handle to NULL to ensure the same queue handle cannot
2849 * appear in the registry twice if it is added, removed, then
2851 xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;
2856 mtCOVERAGE_TEST_MARKER();
2859 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2861 #endif /* configQUEUE_REGISTRY_SIZE */
2862 /*-----------------------------------------------------------*/
2864 #if ( configUSE_TIMERS == 1 )
2866 void vQueueWaitForMessageRestricted( QueueHandle_t xQueue,
2867 TickType_t xTicksToWait,
2868 const BaseType_t xWaitIndefinitely )
2870 Queue_t * const pxQueue = xQueue;
2872 /* This function should not be called by application code hence the
2873 * 'Restricted' in its name. It is not part of the public API. It is
2874 * designed for use by kernel code, and has special calling requirements.
2875 * It can result in vListInsert() being called on a list that can only
2876 * possibly ever have one item in it, so the list will be fast, but even
2877 * so it should be called with the scheduler locked and not from a critical
2880 /* Only do anything if there are no messages in the queue. This function
2881 * will not actually cause the task to block, just place it on a blocked
2882 * list. It will not block until the scheduler is unlocked - at which
2883 * time a yield will be performed. If an item is added to the queue while
2884 * the queue is locked, and the calling task blocks on the queue, then the
2885 * calling task will be immediately unblocked when the queue is unlocked. */
2886 prvLockQueue( pxQueue );
2888 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
2890 /* There is nothing in the queue, block for the specified period. */
2891 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );
2895 mtCOVERAGE_TEST_MARKER();
2898 prvUnlockQueue( pxQueue );
2901 #endif /* configUSE_TIMERS */
2902 /*-----------------------------------------------------------*/
2904 #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
2906 QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
2908 QueueSetHandle_t pxQueue;
2910 pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
2915 #endif /* configUSE_QUEUE_SETS */
2916 /*-----------------------------------------------------------*/
2918 #if ( configUSE_QUEUE_SETS == 1 )
2920 BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore,
2921 QueueSetHandle_t xQueueSet )
2925 taskENTER_CRITICAL();
2927 if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
2929 /* Cannot add a queue/semaphore to more than one queue set. */
2932 else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
2934 /* Cannot add a queue/semaphore to a queue set if there are already
2935 * items in the queue/semaphore. */
2940 ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
2944 taskEXIT_CRITICAL();
2949 #endif /* configUSE_QUEUE_SETS */
2950 /*-----------------------------------------------------------*/
2952 #if ( configUSE_QUEUE_SETS == 1 )
2954 BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore,
2955 QueueSetHandle_t xQueueSet )
2958 Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
2960 if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
2962 /* The queue was not a member of the set. */
2965 else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
2967 /* It is dangerous to remove a queue from a set when the queue is
2968 * not empty because the queue set will still hold pending events for
2974 taskENTER_CRITICAL();
2976 /* The queue is no longer contained in the set. */
2977 pxQueueOrSemaphore->pxQueueSetContainer = NULL;
2979 taskEXIT_CRITICAL();
2984 } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
2986 #endif /* configUSE_QUEUE_SETS */
2987 /*-----------------------------------------------------------*/
2989 #if ( configUSE_QUEUE_SETS == 1 )
2991 QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet,
2992 TickType_t const xTicksToWait )
2994 QueueSetMemberHandle_t xReturn = NULL;
2996 ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait ); /*lint !e961 Casting from one typedef to another is not redundant. */
3000 #endif /* configUSE_QUEUE_SETS */
3001 /*-----------------------------------------------------------*/
3003 #if ( configUSE_QUEUE_SETS == 1 )
3005 QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
3007 QueueSetMemberHandle_t xReturn = NULL;
3009 ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
3013 #endif /* configUSE_QUEUE_SETS */
3014 /*-----------------------------------------------------------*/
3016 #if ( configUSE_QUEUE_SETS == 1 )
3018 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue )
3020 Queue_t * pxQueueSetContainer = pxQueue->pxQueueSetContainer;
3021 BaseType_t xReturn = pdFALSE;
3023 /* This function must be called form a critical section. */
3025 /* The following line is not reachable in unit tests because every call
3026 * to prvNotifyQueueSetContainer is preceded by a check that
3027 * pxQueueSetContainer != NULL */
3028 configASSERT( pxQueueSetContainer ); /* LCOV_EXCL_BR_LINE */
3029 configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
3031 if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
3033 const int8_t cTxLock = pxQueueSetContainer->cTxLock;
3035 traceQUEUE_SET_SEND( pxQueueSetContainer );
3037 /* The data copied is the handle of the queue that contains data. */
3038 xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, queueSEND_TO_BACK );
3040 if( cTxLock == queueUNLOCKED )
3042 if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
3044 if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
3046 /* The task waiting has a higher priority. */
3051 mtCOVERAGE_TEST_MARKER();
3056 mtCOVERAGE_TEST_MARKER();
3061 configASSERT( cTxLock != queueINT8_MAX );
3063 pxQueueSetContainer->cTxLock = ( int8_t ) ( cTxLock + 1 );
3068 mtCOVERAGE_TEST_MARKER();
3074 #endif /* configUSE_QUEUE_SETS */