2 * FreeRTOS Kernel V10.5.1
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; \
266 * Macro to increment cTxLock member of the queue data structure. It is
267 * capped at the number of tasks in the system as we cannot unblock more
268 * tasks than the number of tasks in the system.
270 #define prvIncrementQueueTxLock( pxQueue, cTxLock ) \
272 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
273 if( ( UBaseType_t ) ( cTxLock ) < uxNumberOfTasks ) \
275 configASSERT( ( cTxLock ) != queueINT8_MAX ); \
276 ( pxQueue )->cTxLock = ( int8_t ) ( ( cTxLock ) + ( int8_t ) 1 ); \
281 * Macro to increment cRxLock member of the queue data structure. It is
282 * capped at the number of tasks in the system as we cannot unblock more
283 * tasks than the number of tasks in the system.
285 #define prvIncrementQueueRxLock( pxQueue, cRxLock ) \
287 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
288 if( ( UBaseType_t ) ( cRxLock ) < uxNumberOfTasks ) \
290 configASSERT( ( cRxLock ) != queueINT8_MAX ); \
291 ( pxQueue )->cRxLock = ( int8_t ) ( ( cRxLock ) + ( int8_t ) 1 ); \
294 /*-----------------------------------------------------------*/
296 BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
297 BaseType_t xNewQueue )
299 BaseType_t xReturn = pdPASS;
300 Queue_t * const pxQueue = xQueue;
302 configASSERT( pxQueue );
304 if( ( pxQueue != NULL ) &&
305 ( pxQueue->uxLength >= 1U ) &&
306 /* Check for multiplication overflow. */
307 ( ( SIZE_MAX / pxQueue->uxLength ) >= pxQueue->uxItemSize ) )
309 taskENTER_CRITICAL();
311 pxQueue->u.xQueue.pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
312 pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
313 pxQueue->pcWriteTo = pxQueue->pcHead;
314 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - 1U ) * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
315 pxQueue->cRxLock = queueUNLOCKED;
316 pxQueue->cTxLock = queueUNLOCKED;
318 if( xNewQueue == pdFALSE )
320 /* If there are tasks blocked waiting to read from the queue, then
321 * the tasks will remain blocked as after this function exits the queue
322 * will still be empty. If there are tasks blocked waiting to write to
323 * the queue, then one should be unblocked as after this function exits
324 * it will be possible to write to it. */
325 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
327 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
329 queueYIELD_IF_USING_PREEMPTION();
333 mtCOVERAGE_TEST_MARKER();
338 mtCOVERAGE_TEST_MARKER();
343 /* Ensure the event queues start in the correct state. */
344 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
345 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
355 configASSERT( xReturn != pdFAIL );
357 /* A value is returned for calling semantic consistency with previous
361 /*-----------------------------------------------------------*/
363 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
365 QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,
366 const UBaseType_t uxItemSize,
367 uint8_t * pucQueueStorage,
368 StaticQueue_t * pxStaticQueue,
369 const uint8_t ucQueueType )
371 Queue_t * pxNewQueue = NULL;
373 /* The StaticQueue_t structure and the queue storage area must be
375 configASSERT( pxStaticQueue );
377 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
378 ( pxStaticQueue != NULL ) &&
380 /* A queue storage area should be provided if the item size is not 0, and
381 * should not be provided if the item size is 0. */
382 ( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) ) &&
383 ( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) ) )
385 #if ( configASSERT_DEFINED == 1 )
387 /* Sanity check that the size of the structure used to declare a
388 * variable of type StaticQueue_t or StaticSemaphore_t equals the size of
389 * the real queue and semaphore structures. */
390 volatile size_t xSize = sizeof( StaticQueue_t );
392 /* This assertion cannot be branch covered in unit tests */
393 configASSERT( xSize == sizeof( Queue_t ) ); /* LCOV_EXCL_BR_LINE */
394 ( void ) xSize; /* Keeps lint quiet when configASSERT() is not defined. */
396 #endif /* configASSERT_DEFINED */
398 /* The address of a statically allocated queue was passed in, use it.
399 * The address of a statically allocated storage area was also passed in
400 * but is already set. */
401 pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
403 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
405 /* Queues can be allocated wither statically or dynamically, so
406 * note this queue was allocated statically in case the queue is
408 pxNewQueue->ucStaticallyAllocated = pdTRUE;
410 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
412 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
416 configASSERT( pxNewQueue );
417 mtCOVERAGE_TEST_MARKER();
423 #endif /* configSUPPORT_STATIC_ALLOCATION */
424 /*-----------------------------------------------------------*/
426 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
428 QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
429 const UBaseType_t uxItemSize,
430 const uint8_t ucQueueType )
432 Queue_t * pxNewQueue = NULL;
433 size_t xQueueSizeInBytes;
434 uint8_t * pucQueueStorage;
436 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
437 /* Check for multiplication overflow. */
438 ( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&
439 /* Check for addition overflow. */
440 ( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( uxQueueLength * uxItemSize ) ) )
442 /* Allocate enough space to hold the maximum number of items that
443 * can be in the queue at any time. It is valid for uxItemSize to be
444 * zero in the case the queue is used as a semaphore. */
445 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
447 /* Allocate the queue and storage area. Justification for MISRA
448 * deviation as follows: pvPortMalloc() always ensures returned memory
449 * blocks are aligned per the requirements of the MCU stack. In this case
450 * pvPortMalloc() must return a pointer that is guaranteed to meet the
451 * alignment requirements of the Queue_t structure - which in this case
452 * is an int8_t *. Therefore, whenever the stack alignment requirements
453 * are greater than or equal to the pointer to char requirements the cast
454 * is safe. In other cases alignment requirements are not strict (one or
456 pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */
458 if( pxNewQueue != NULL )
460 /* Jump past the queue structure to find the location of the queue
462 pucQueueStorage = ( uint8_t * ) pxNewQueue;
463 pucQueueStorage += sizeof( Queue_t ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
465 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
467 /* Queues can be created either statically or dynamically, so
468 * note this task was created dynamically in case it is later
470 pxNewQueue->ucStaticallyAllocated = pdFALSE;
472 #endif /* configSUPPORT_STATIC_ALLOCATION */
474 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
478 traceQUEUE_CREATE_FAILED( ucQueueType );
479 mtCOVERAGE_TEST_MARKER();
484 configASSERT( pxNewQueue );
485 mtCOVERAGE_TEST_MARKER();
491 #endif /* configSUPPORT_STATIC_ALLOCATION */
492 /*-----------------------------------------------------------*/
494 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
495 const UBaseType_t uxItemSize,
496 uint8_t * pucQueueStorage,
497 const uint8_t ucQueueType,
498 Queue_t * pxNewQueue )
500 /* Remove compiler warnings about unused parameters should
501 * configUSE_TRACE_FACILITY not be set to 1. */
502 ( void ) ucQueueType;
504 if( uxItemSize == ( UBaseType_t ) 0 )
506 /* No RAM was allocated for the queue storage area, but PC head cannot
507 * be set to NULL because NULL is used as a key to say the queue is used as
508 * a mutex. Therefore just set pcHead to point to the queue as a benign
509 * value that is known to be within the memory map. */
510 pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
514 /* Set the head to the start of the queue storage area. */
515 pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
518 /* Initialise the queue members as described where the queue type is
520 pxNewQueue->uxLength = uxQueueLength;
521 pxNewQueue->uxItemSize = uxItemSize;
522 ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
524 #if ( configUSE_TRACE_FACILITY == 1 )
526 pxNewQueue->ucQueueType = ucQueueType;
528 #endif /* configUSE_TRACE_FACILITY */
530 #if ( configUSE_QUEUE_SETS == 1 )
532 pxNewQueue->pxQueueSetContainer = NULL;
534 #endif /* configUSE_QUEUE_SETS */
536 traceQUEUE_CREATE( pxNewQueue );
538 /*-----------------------------------------------------------*/
540 #if ( configUSE_MUTEXES == 1 )
542 static void prvInitialiseMutex( Queue_t * pxNewQueue )
544 if( pxNewQueue != NULL )
546 /* The queue create function will set all the queue structure members
547 * correctly for a generic queue, but this function is creating a
548 * mutex. Overwrite those members that need to be set differently -
549 * in particular the information required for priority inheritance. */
550 pxNewQueue->u.xSemaphore.xMutexHolder = NULL;
551 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
553 /* In case this is a recursive mutex. */
554 pxNewQueue->u.xSemaphore.uxRecursiveCallCount = 0;
556 traceCREATE_MUTEX( pxNewQueue );
558 /* Start with the semaphore in the expected state. */
559 ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
563 traceCREATE_MUTEX_FAILED();
567 #endif /* configUSE_MUTEXES */
568 /*-----------------------------------------------------------*/
570 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
572 QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
574 QueueHandle_t xNewQueue;
575 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
577 xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
578 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
583 #endif /* configUSE_MUTEXES */
584 /*-----------------------------------------------------------*/
586 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
588 QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
589 StaticQueue_t * pxStaticQueue )
591 QueueHandle_t xNewQueue;
592 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
594 /* Prevent compiler warnings about unused parameters if
595 * configUSE_TRACE_FACILITY does not equal 1. */
596 ( void ) ucQueueType;
598 xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
599 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
604 #endif /* configUSE_MUTEXES */
605 /*-----------------------------------------------------------*/
607 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
609 TaskHandle_t xQueueGetMutexHolder( QueueHandle_t xSemaphore )
611 TaskHandle_t pxReturn;
612 Queue_t * const pxSemaphore = ( Queue_t * ) xSemaphore;
614 configASSERT( xSemaphore );
616 /* This function is called by xSemaphoreGetMutexHolder(), and should not
617 * be called directly. Note: This is a good way of determining if the
618 * calling task is the mutex holder, but not a good way of determining the
619 * identity of the mutex holder, as the holder may change between the
620 * following critical section exiting and the function returning. */
621 taskENTER_CRITICAL();
623 if( pxSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
625 pxReturn = pxSemaphore->u.xSemaphore.xMutexHolder;
635 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
637 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
638 /*-----------------------------------------------------------*/
640 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
642 TaskHandle_t xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )
644 TaskHandle_t pxReturn;
646 configASSERT( xSemaphore );
648 /* Mutexes cannot be used in interrupt service routines, so the mutex
649 * holder should not change in an ISR, and therefore a critical section is
650 * not required here. */
651 if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
653 pxReturn = ( ( Queue_t * ) xSemaphore )->u.xSemaphore.xMutexHolder;
661 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
663 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
664 /*-----------------------------------------------------------*/
666 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
668 BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
671 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
673 configASSERT( pxMutex );
675 /* If this is the task that holds the mutex then xMutexHolder will not
676 * change outside of this task. If this task does not hold the mutex then
677 * pxMutexHolder can never coincidentally equal the tasks handle, and as
678 * this is the only condition we are interested in it does not matter if
679 * pxMutexHolder is accessed simultaneously by another task. Therefore no
680 * mutual exclusion is required to test the pxMutexHolder variable. */
681 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
683 traceGIVE_MUTEX_RECURSIVE( pxMutex );
685 /* uxRecursiveCallCount cannot be zero if xMutexHolder is equal to
686 * the task handle, therefore no underflow check is required. Also,
687 * uxRecursiveCallCount is only modified by the mutex holder, and as
688 * there can only be one, no mutual exclusion is required to modify the
689 * uxRecursiveCallCount member. */
690 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )--;
692 /* Has the recursive call count unwound to 0? */
693 if( pxMutex->u.xSemaphore.uxRecursiveCallCount == ( UBaseType_t ) 0 )
695 /* Return the mutex. This will automatically unblock any other
696 * task that might be waiting to access the mutex. */
697 ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
701 mtCOVERAGE_TEST_MARKER();
708 /* The mutex cannot be given because the calling task is not the
712 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
718 #endif /* configUSE_RECURSIVE_MUTEXES */
719 /*-----------------------------------------------------------*/
721 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
723 BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
724 TickType_t xTicksToWait )
727 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
729 configASSERT( pxMutex );
731 /* Comments regarding mutual exclusion as per those within
732 * xQueueGiveMutexRecursive(). */
734 traceTAKE_MUTEX_RECURSIVE( pxMutex );
736 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
738 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
743 xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );
745 /* pdPASS will only be returned if the mutex was successfully
746 * obtained. The calling task may have entered the Blocked state
747 * before reaching here. */
748 if( xReturn != pdFAIL )
750 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
754 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
761 #endif /* configUSE_RECURSIVE_MUTEXES */
762 /*-----------------------------------------------------------*/
764 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
766 QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
767 const UBaseType_t uxInitialCount,
768 StaticQueue_t * pxStaticQueue )
770 QueueHandle_t xHandle = NULL;
772 if( ( uxMaxCount != 0 ) &&
773 ( uxInitialCount <= uxMaxCount ) )
775 xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
777 if( xHandle != NULL )
779 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
781 traceCREATE_COUNTING_SEMAPHORE();
785 traceCREATE_COUNTING_SEMAPHORE_FAILED();
790 configASSERT( xHandle );
791 mtCOVERAGE_TEST_MARKER();
797 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
798 /*-----------------------------------------------------------*/
800 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
802 QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
803 const UBaseType_t uxInitialCount )
805 QueueHandle_t xHandle = NULL;
807 if( ( uxMaxCount != 0 ) &&
808 ( uxInitialCount <= uxMaxCount ) )
810 xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
812 if( xHandle != NULL )
814 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
816 traceCREATE_COUNTING_SEMAPHORE();
820 traceCREATE_COUNTING_SEMAPHORE_FAILED();
825 configASSERT( xHandle );
826 mtCOVERAGE_TEST_MARKER();
832 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
833 /*-----------------------------------------------------------*/
835 BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
836 const void * const pvItemToQueue,
837 TickType_t xTicksToWait,
838 const BaseType_t xCopyPosition )
840 BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
842 Queue_t * const pxQueue = xQueue;
844 configASSERT( pxQueue );
845 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
846 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
847 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
849 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
853 /*lint -save -e904 This function relaxes the coding standard somewhat to
854 * allow return statements within the function itself. This is done in the
855 * interest of execution time efficiency. */
858 taskENTER_CRITICAL();
860 /* Is there room on the queue now? The running task must be the
861 * highest priority task wanting to access the queue. If the head item
862 * in the queue is to be overwritten then it does not matter if the
864 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
866 traceQUEUE_SEND( pxQueue );
868 #if ( configUSE_QUEUE_SETS == 1 )
870 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
872 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
874 if( pxQueue->pxQueueSetContainer != NULL )
876 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
878 /* Do not notify the queue set as an existing item
879 * was overwritten in the queue so the number of items
880 * in the queue has not changed. */
881 mtCOVERAGE_TEST_MARKER();
883 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
885 /* The queue is a member of a queue set, and posting
886 * to the queue set caused a higher priority task to
887 * unblock. A context switch is required. */
888 queueYIELD_IF_USING_PREEMPTION();
892 mtCOVERAGE_TEST_MARKER();
897 /* If there was a task waiting for data to arrive on the
898 * queue then unblock it now. */
899 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
901 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
903 /* The unblocked task has a priority higher than
904 * our own so yield immediately. Yes it is ok to
905 * do this from within the critical section - the
906 * kernel takes care of that. */
907 queueYIELD_IF_USING_PREEMPTION();
911 mtCOVERAGE_TEST_MARKER();
914 else if( xYieldRequired != pdFALSE )
916 /* This path is a special case that will only get
917 * executed if the task was holding multiple mutexes
918 * and the mutexes were given back in an order that is
919 * different to that in which they were taken. */
920 queueYIELD_IF_USING_PREEMPTION();
924 mtCOVERAGE_TEST_MARKER();
928 #else /* configUSE_QUEUE_SETS */
930 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
932 /* If there was a task waiting for data to arrive on the
933 * queue then unblock it now. */
934 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
936 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
938 /* The unblocked task has a priority higher than
939 * our own so yield immediately. Yes it is ok to do
940 * this from within the critical section - the kernel
941 * takes care of that. */
942 queueYIELD_IF_USING_PREEMPTION();
946 mtCOVERAGE_TEST_MARKER();
949 else if( xYieldRequired != pdFALSE )
951 /* This path is a special case that will only get
952 * executed if the task was holding multiple mutexes and
953 * the mutexes were given back in an order that is
954 * different to that in which they were taken. */
955 queueYIELD_IF_USING_PREEMPTION();
959 mtCOVERAGE_TEST_MARKER();
962 #endif /* configUSE_QUEUE_SETS */
969 if( xTicksToWait == ( TickType_t ) 0 )
971 /* The queue was full and no block time is specified (or
972 * the block time has expired) so leave now. */
975 /* Return to the original privilege level before exiting
977 traceQUEUE_SEND_FAILED( pxQueue );
978 return errQUEUE_FULL;
980 else if( xEntryTimeSet == pdFALSE )
982 /* The queue was full and a block time was specified so
983 * configure the timeout structure. */
984 vTaskInternalSetTimeOutState( &xTimeOut );
985 xEntryTimeSet = pdTRUE;
989 /* Entry time was already set. */
990 mtCOVERAGE_TEST_MARKER();
996 /* Interrupts and other tasks can send to and receive from the queue
997 * now the critical section has been exited. */
1000 prvLockQueue( pxQueue );
1002 /* Update the timeout state to see if it has expired yet. */
1003 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1005 if( prvIsQueueFull( pxQueue ) != pdFALSE )
1007 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
1008 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
1010 /* Unlocking the queue means queue events can effect the
1011 * event list. It is possible that interrupts occurring now
1012 * remove this task from the event list again - but as the
1013 * scheduler is suspended the task will go onto the pending
1014 * ready list instead of the actual ready list. */
1015 prvUnlockQueue( pxQueue );
1017 /* Resuming the scheduler will move tasks from the pending
1018 * ready list into the ready list - so it is feasible that this
1019 * task is already in the ready list before it yields - in which
1020 * case the yield will not cause a context switch unless there
1021 * is also a higher priority task in the pending ready list. */
1022 if( xTaskResumeAll() == pdFALSE )
1024 portYIELD_WITHIN_API();
1030 prvUnlockQueue( pxQueue );
1031 ( void ) xTaskResumeAll();
1036 /* The timeout has expired. */
1037 prvUnlockQueue( pxQueue );
1038 ( void ) xTaskResumeAll();
1040 traceQUEUE_SEND_FAILED( pxQueue );
1041 return errQUEUE_FULL;
1043 } /*lint -restore */
1045 /*-----------------------------------------------------------*/
1047 BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,
1048 const void * const pvItemToQueue,
1049 BaseType_t * const pxHigherPriorityTaskWoken,
1050 const BaseType_t xCopyPosition )
1053 UBaseType_t uxSavedInterruptStatus;
1054 Queue_t * const pxQueue = xQueue;
1056 configASSERT( pxQueue );
1057 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1058 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
1060 /* RTOS ports that support interrupt nesting have the concept of a maximum
1061 * system call (or maximum API call) interrupt priority. Interrupts that are
1062 * above the maximum system call priority are kept permanently enabled, even
1063 * when the RTOS kernel is in a critical section, but cannot make any calls to
1064 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1065 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1066 * failure if a FreeRTOS API function is called from an interrupt that has been
1067 * assigned a priority above the configured maximum system call priority.
1068 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1069 * that have been assigned a priority at or (logically) below the maximum
1070 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1071 * safe API to ensure interrupt entry is as fast and as simple as possible.
1072 * More information (albeit Cortex-M specific) is provided on the following
1073 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1074 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1076 /* Similar to xQueueGenericSend, except without blocking if there is no room
1077 * in the queue. Also don't directly wake a task that was blocked on a queue
1078 * read, instead return a flag to say whether a context switch is required or
1079 * not (i.e. has a task with a higher priority than us been woken by this
1081 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1083 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
1085 const int8_t cTxLock = pxQueue->cTxLock;
1086 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
1088 traceQUEUE_SEND_FROM_ISR( pxQueue );
1090 /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a
1091 * semaphore or mutex. That means prvCopyDataToQueue() cannot result
1092 * in a task disinheriting a priority and prvCopyDataToQueue() can be
1093 * called here even though the disinherit function does not check if
1094 * the scheduler is suspended before accessing the ready lists. */
1095 ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
1097 /* The event list is not altered if the queue is locked. This will
1098 * be done when the queue is unlocked later. */
1099 if( cTxLock == queueUNLOCKED )
1101 #if ( configUSE_QUEUE_SETS == 1 )
1103 if( pxQueue->pxQueueSetContainer != NULL )
1105 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
1107 /* Do not notify the queue set as an existing item
1108 * was overwritten in the queue so the number of items
1109 * in the queue has not changed. */
1110 mtCOVERAGE_TEST_MARKER();
1112 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1114 /* The queue is a member of a queue set, and posting
1115 * to the queue set caused a higher priority task to
1116 * unblock. A context switch is required. */
1117 if( pxHigherPriorityTaskWoken != NULL )
1119 *pxHigherPriorityTaskWoken = pdTRUE;
1123 mtCOVERAGE_TEST_MARKER();
1128 mtCOVERAGE_TEST_MARKER();
1133 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1135 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1137 /* The task waiting has a higher priority so
1138 * record that a context switch is required. */
1139 if( pxHigherPriorityTaskWoken != NULL )
1141 *pxHigherPriorityTaskWoken = pdTRUE;
1145 mtCOVERAGE_TEST_MARKER();
1150 mtCOVERAGE_TEST_MARKER();
1155 mtCOVERAGE_TEST_MARKER();
1159 #else /* configUSE_QUEUE_SETS */
1161 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1163 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1165 /* The task waiting has a higher priority so record that a
1166 * context switch is required. */
1167 if( pxHigherPriorityTaskWoken != NULL )
1169 *pxHigherPriorityTaskWoken = pdTRUE;
1173 mtCOVERAGE_TEST_MARKER();
1178 mtCOVERAGE_TEST_MARKER();
1183 mtCOVERAGE_TEST_MARKER();
1186 /* Not used in this path. */
1187 ( void ) uxPreviousMessagesWaiting;
1189 #endif /* configUSE_QUEUE_SETS */
1193 /* Increment the lock count so the task that unlocks the queue
1194 * knows that data was posted while it was locked. */
1195 prvIncrementQueueTxLock( pxQueue, cTxLock );
1202 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1203 xReturn = errQUEUE_FULL;
1206 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1210 /*-----------------------------------------------------------*/
1212 BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,
1213 BaseType_t * const pxHigherPriorityTaskWoken )
1216 UBaseType_t uxSavedInterruptStatus;
1217 Queue_t * const pxQueue = xQueue;
1219 /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
1220 * item size is 0. Don't directly wake a task that was blocked on a queue
1221 * read, instead return a flag to say whether a context switch is required or
1222 * not (i.e. has a task with a higher priority than us been woken by this
1225 configASSERT( pxQueue );
1227 /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()
1228 * if the item size is not 0. */
1229 configASSERT( pxQueue->uxItemSize == 0 );
1231 /* Normally a mutex would not be given from an interrupt, especially if
1232 * there is a mutex holder, as priority inheritance makes no sense for an
1233 * interrupts, only tasks. */
1234 configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->u.xSemaphore.xMutexHolder != NULL ) ) );
1236 /* RTOS ports that support interrupt nesting have the concept of a maximum
1237 * system call (or maximum API call) interrupt priority. Interrupts that are
1238 * above the maximum system call priority are kept permanently enabled, even
1239 * when the RTOS kernel is in a critical section, but cannot make any calls to
1240 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1241 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1242 * failure if a FreeRTOS API function is called from an interrupt that has been
1243 * assigned a priority above the configured maximum system call priority.
1244 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1245 * that have been assigned a priority at or (logically) below the maximum
1246 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1247 * safe API to ensure interrupt entry is as fast and as simple as possible.
1248 * More information (albeit Cortex-M specific) is provided on the following
1249 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1250 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1252 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1254 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1256 /* When the queue is used to implement a semaphore no data is ever
1257 * moved through the queue but it is still valid to see if the queue 'has
1259 if( uxMessagesWaiting < pxQueue->uxLength )
1261 const int8_t cTxLock = pxQueue->cTxLock;
1263 traceQUEUE_SEND_FROM_ISR( pxQueue );
1265 /* A task can only have an inherited priority if it is a mutex
1266 * holder - and if there is a mutex holder then the mutex cannot be
1267 * given from an ISR. As this is the ISR version of the function it
1268 * can be assumed there is no mutex holder and no need to determine if
1269 * priority disinheritance is needed. Simply increase the count of
1270 * messages (semaphores) available. */
1271 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
1273 /* The event list is not altered if the queue is locked. This will
1274 * be done when the queue is unlocked later. */
1275 if( cTxLock == queueUNLOCKED )
1277 #if ( configUSE_QUEUE_SETS == 1 )
1279 if( pxQueue->pxQueueSetContainer != NULL )
1281 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1283 /* The semaphore is a member of a queue set, and
1284 * posting to the queue set caused a higher priority
1285 * task to unblock. A context switch is required. */
1286 if( pxHigherPriorityTaskWoken != NULL )
1288 *pxHigherPriorityTaskWoken = pdTRUE;
1292 mtCOVERAGE_TEST_MARKER();
1297 mtCOVERAGE_TEST_MARKER();
1302 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1304 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1306 /* The task waiting has a higher priority so
1307 * record that a context switch is required. */
1308 if( pxHigherPriorityTaskWoken != NULL )
1310 *pxHigherPriorityTaskWoken = pdTRUE;
1314 mtCOVERAGE_TEST_MARKER();
1319 mtCOVERAGE_TEST_MARKER();
1324 mtCOVERAGE_TEST_MARKER();
1328 #else /* configUSE_QUEUE_SETS */
1330 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1332 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1334 /* The task waiting has a higher priority so record that a
1335 * context switch is required. */
1336 if( pxHigherPriorityTaskWoken != NULL )
1338 *pxHigherPriorityTaskWoken = pdTRUE;
1342 mtCOVERAGE_TEST_MARKER();
1347 mtCOVERAGE_TEST_MARKER();
1352 mtCOVERAGE_TEST_MARKER();
1355 #endif /* configUSE_QUEUE_SETS */
1359 /* Increment the lock count so the task that unlocks the queue
1360 * knows that data was posted while it was locked. */
1361 prvIncrementQueueTxLock( pxQueue, cTxLock );
1368 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1369 xReturn = errQUEUE_FULL;
1372 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1376 /*-----------------------------------------------------------*/
1378 BaseType_t xQueueReceive( QueueHandle_t xQueue,
1379 void * const pvBuffer,
1380 TickType_t xTicksToWait )
1382 BaseType_t xEntryTimeSet = pdFALSE;
1384 Queue_t * const pxQueue = xQueue;
1386 /* Check the pointer is not NULL. */
1387 configASSERT( ( pxQueue ) );
1389 /* The buffer into which data is received can only be NULL if the data size
1390 * is zero (so no data is copied into the buffer). */
1391 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1393 /* Cannot block if the scheduler is suspended. */
1394 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1396 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1400 /*lint -save -e904 This function relaxes the coding standard somewhat to
1401 * allow return statements within the function itself. This is done in the
1402 * interest of execution time efficiency. */
1405 taskENTER_CRITICAL();
1407 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1409 /* Is there data in the queue now? To be running the calling task
1410 * must be the highest priority task wanting to access the queue. */
1411 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1413 /* Data available, remove one item. */
1414 prvCopyDataFromQueue( pxQueue, pvBuffer );
1415 traceQUEUE_RECEIVE( pxQueue );
1416 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
1418 /* There is now space in the queue, were any tasks waiting to
1419 * post to the queue? If so, unblock the highest priority waiting
1421 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1423 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1425 queueYIELD_IF_USING_PREEMPTION();
1429 mtCOVERAGE_TEST_MARKER();
1434 mtCOVERAGE_TEST_MARKER();
1437 taskEXIT_CRITICAL();
1442 if( xTicksToWait == ( TickType_t ) 0 )
1444 /* The queue was empty and no block time is specified (or
1445 * the block time has expired) so leave now. */
1446 taskEXIT_CRITICAL();
1447 traceQUEUE_RECEIVE_FAILED( pxQueue );
1448 return errQUEUE_EMPTY;
1450 else if( xEntryTimeSet == pdFALSE )
1452 /* The queue was empty and a block time was specified so
1453 * configure the timeout structure. */
1454 vTaskInternalSetTimeOutState( &xTimeOut );
1455 xEntryTimeSet = pdTRUE;
1459 /* Entry time was already set. */
1460 mtCOVERAGE_TEST_MARKER();
1464 taskEXIT_CRITICAL();
1466 /* Interrupts and other tasks can send to and receive from the queue
1467 * now the critical section has been exited. */
1470 prvLockQueue( pxQueue );
1472 /* Update the timeout state to see if it has expired yet. */
1473 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1475 /* The timeout has not expired. If the queue is still empty place
1476 * the task on the list of tasks waiting to receive from the queue. */
1477 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1479 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1480 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1481 prvUnlockQueue( pxQueue );
1483 if( xTaskResumeAll() == pdFALSE )
1485 portYIELD_WITHIN_API();
1489 mtCOVERAGE_TEST_MARKER();
1494 /* The queue contains data again. Loop back to try and read the
1496 prvUnlockQueue( pxQueue );
1497 ( void ) xTaskResumeAll();
1502 /* Timed out. If there is no data in the queue exit, otherwise loop
1503 * back and attempt to read the data. */
1504 prvUnlockQueue( pxQueue );
1505 ( void ) xTaskResumeAll();
1507 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1509 traceQUEUE_RECEIVE_FAILED( pxQueue );
1510 return errQUEUE_EMPTY;
1514 mtCOVERAGE_TEST_MARKER();
1517 } /*lint -restore */
1519 /*-----------------------------------------------------------*/
1521 BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
1522 TickType_t xTicksToWait )
1524 BaseType_t xEntryTimeSet = pdFALSE;
1526 Queue_t * const pxQueue = xQueue;
1528 #if ( configUSE_MUTEXES == 1 )
1529 BaseType_t xInheritanceOccurred = pdFALSE;
1532 /* Check the queue pointer is not NULL. */
1533 configASSERT( ( pxQueue ) );
1535 /* Check this really is a semaphore, in which case the item size will be
1537 configASSERT( pxQueue->uxItemSize == 0 );
1539 /* Cannot block if the scheduler is suspended. */
1540 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1542 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1546 /*lint -save -e904 This function relaxes the coding standard somewhat to allow return
1547 * statements within the function itself. This is done in the interest
1548 * of execution time efficiency. */
1551 taskENTER_CRITICAL();
1553 /* Semaphores are queues with an item size of 0, and where the
1554 * number of messages in the queue is the semaphore's count value. */
1555 const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;
1557 /* Is there data in the queue now? To be running the calling task
1558 * must be the highest priority task wanting to access the queue. */
1559 if( uxSemaphoreCount > ( UBaseType_t ) 0 )
1561 traceQUEUE_RECEIVE( pxQueue );
1563 /* Semaphores are queues with a data size of zero and where the
1564 * messages waiting is the semaphore's count. Reduce the count. */
1565 pxQueue->uxMessagesWaiting = uxSemaphoreCount - ( UBaseType_t ) 1;
1567 #if ( configUSE_MUTEXES == 1 )
1569 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1571 /* Record the information required to implement
1572 * priority inheritance should it become necessary. */
1573 pxQueue->u.xSemaphore.xMutexHolder = pvTaskIncrementMutexHeldCount();
1577 mtCOVERAGE_TEST_MARKER();
1580 #endif /* configUSE_MUTEXES */
1582 /* Check to see if other tasks are blocked waiting to give the
1583 * semaphore, and if so, unblock the highest priority such task. */
1584 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1586 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1588 queueYIELD_IF_USING_PREEMPTION();
1592 mtCOVERAGE_TEST_MARKER();
1597 mtCOVERAGE_TEST_MARKER();
1600 taskEXIT_CRITICAL();
1605 if( xTicksToWait == ( TickType_t ) 0 )
1607 /* The semaphore count was 0 and no block time is specified
1608 * (or the block time has expired) so exit now. */
1609 taskEXIT_CRITICAL();
1610 traceQUEUE_RECEIVE_FAILED( pxQueue );
1611 return errQUEUE_EMPTY;
1613 else if( xEntryTimeSet == pdFALSE )
1615 /* The semaphore count was 0 and a block time was specified
1616 * so configure the timeout structure ready to block. */
1617 vTaskInternalSetTimeOutState( &xTimeOut );
1618 xEntryTimeSet = pdTRUE;
1622 /* Entry time was already set. */
1623 mtCOVERAGE_TEST_MARKER();
1627 taskEXIT_CRITICAL();
1629 /* Interrupts and other tasks can give to and take from the semaphore
1630 * now the critical section has been exited. */
1633 prvLockQueue( pxQueue );
1635 /* Update the timeout state to see if it has expired yet. */
1636 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1638 /* A block time is specified and not expired. If the semaphore
1639 * count is 0 then enter the Blocked state to wait for a semaphore to
1640 * become available. As semaphores are implemented with queues the
1641 * queue being empty is equivalent to the semaphore count being 0. */
1642 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1644 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1646 #if ( configUSE_MUTEXES == 1 )
1648 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1650 taskENTER_CRITICAL();
1652 xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );
1654 taskEXIT_CRITICAL();
1658 mtCOVERAGE_TEST_MARKER();
1661 #endif /* if ( configUSE_MUTEXES == 1 ) */
1663 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1664 prvUnlockQueue( pxQueue );
1666 if( xTaskResumeAll() == pdFALSE )
1668 portYIELD_WITHIN_API();
1672 mtCOVERAGE_TEST_MARKER();
1677 /* There was no timeout and the semaphore count was not 0, so
1678 * attempt to take the semaphore again. */
1679 prvUnlockQueue( pxQueue );
1680 ( void ) xTaskResumeAll();
1686 prvUnlockQueue( pxQueue );
1687 ( void ) xTaskResumeAll();
1689 /* If the semaphore count is 0 exit now as the timeout has
1690 * expired. Otherwise return to attempt to take the semaphore that is
1691 * known to be available. As semaphores are implemented by queues the
1692 * queue being empty is equivalent to the semaphore count being 0. */
1693 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1695 #if ( configUSE_MUTEXES == 1 )
1697 /* xInheritanceOccurred could only have be set if
1698 * pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
1699 * test the mutex type again to check it is actually a mutex. */
1700 if( xInheritanceOccurred != pdFALSE )
1702 taskENTER_CRITICAL();
1704 UBaseType_t uxHighestWaitingPriority;
1706 /* This task blocking on the mutex caused another
1707 * task to inherit this task's priority. Now this task
1708 * has timed out the priority should be disinherited
1709 * again, but only as low as the next highest priority
1710 * task that is waiting for the same mutex. */
1711 uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );
1712 vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );
1714 taskEXIT_CRITICAL();
1717 #endif /* configUSE_MUTEXES */
1719 traceQUEUE_RECEIVE_FAILED( pxQueue );
1720 return errQUEUE_EMPTY;
1724 mtCOVERAGE_TEST_MARKER();
1727 } /*lint -restore */
1729 /*-----------------------------------------------------------*/
1731 BaseType_t xQueuePeek( QueueHandle_t xQueue,
1732 void * const pvBuffer,
1733 TickType_t xTicksToWait )
1735 BaseType_t xEntryTimeSet = pdFALSE;
1737 int8_t * pcOriginalReadPosition;
1738 Queue_t * const pxQueue = xQueue;
1740 /* Check the pointer is not NULL. */
1741 configASSERT( ( pxQueue ) );
1743 /* The buffer into which data is received can only be NULL if the data size
1744 * is zero (so no data is copied into the buffer. */
1745 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1747 /* Cannot block if the scheduler is suspended. */
1748 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1750 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1754 /*lint -save -e904 This function relaxes the coding standard somewhat to
1755 * allow return statements within the function itself. This is done in the
1756 * interest of execution time efficiency. */
1759 taskENTER_CRITICAL();
1761 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1763 /* Is there data in the queue now? To be running the calling task
1764 * must be the highest priority task wanting to access the queue. */
1765 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1767 /* Remember the read position so it can be reset after the data
1768 * is read from the queue as this function is only peeking the
1769 * data, not removing it. */
1770 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
1772 prvCopyDataFromQueue( pxQueue, pvBuffer );
1773 traceQUEUE_PEEK( pxQueue );
1775 /* The data is not being removed, so reset the read pointer. */
1776 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
1778 /* The data is being left in the queue, so see if there are
1779 * any other tasks waiting for the data. */
1780 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1782 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1784 /* The task waiting has a higher priority than this task. */
1785 queueYIELD_IF_USING_PREEMPTION();
1789 mtCOVERAGE_TEST_MARKER();
1794 mtCOVERAGE_TEST_MARKER();
1797 taskEXIT_CRITICAL();
1802 if( xTicksToWait == ( TickType_t ) 0 )
1804 /* The queue was empty and no block time is specified (or
1805 * the block time has expired) so leave now. */
1806 taskEXIT_CRITICAL();
1807 traceQUEUE_PEEK_FAILED( pxQueue );
1808 return errQUEUE_EMPTY;
1810 else if( xEntryTimeSet == pdFALSE )
1812 /* The queue was empty and a block time was specified so
1813 * configure the timeout structure ready to enter the blocked
1815 vTaskInternalSetTimeOutState( &xTimeOut );
1816 xEntryTimeSet = pdTRUE;
1820 /* Entry time was already set. */
1821 mtCOVERAGE_TEST_MARKER();
1825 taskEXIT_CRITICAL();
1827 /* Interrupts and other tasks can send to and receive from the queue
1828 * now that the critical section has been exited. */
1831 prvLockQueue( pxQueue );
1833 /* Update the timeout state to see if it has expired yet. */
1834 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1836 /* Timeout has not expired yet, check to see if there is data in the
1837 * queue now, and if not enter the Blocked state to wait for data. */
1838 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1840 traceBLOCKING_ON_QUEUE_PEEK( pxQueue );
1841 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1842 prvUnlockQueue( pxQueue );
1844 if( xTaskResumeAll() == pdFALSE )
1846 portYIELD_WITHIN_API();
1850 mtCOVERAGE_TEST_MARKER();
1855 /* There is data in the queue now, so don't enter the blocked
1856 * state, instead return to try and obtain the data. */
1857 prvUnlockQueue( pxQueue );
1858 ( void ) xTaskResumeAll();
1863 /* The timeout has expired. If there is still no data in the queue
1864 * exit, otherwise go back and try to read the data again. */
1865 prvUnlockQueue( pxQueue );
1866 ( void ) xTaskResumeAll();
1868 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1870 traceQUEUE_PEEK_FAILED( pxQueue );
1871 return errQUEUE_EMPTY;
1875 mtCOVERAGE_TEST_MARKER();
1878 } /*lint -restore */
1880 /*-----------------------------------------------------------*/
1882 BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,
1883 void * const pvBuffer,
1884 BaseType_t * const pxHigherPriorityTaskWoken )
1887 UBaseType_t uxSavedInterruptStatus;
1888 Queue_t * const pxQueue = xQueue;
1890 configASSERT( pxQueue );
1891 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1893 /* RTOS ports that support interrupt nesting have the concept of a maximum
1894 * system call (or maximum API call) interrupt priority. Interrupts that are
1895 * above the maximum system call priority are kept permanently enabled, even
1896 * when the RTOS kernel is in a critical section, but cannot make any calls to
1897 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1898 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1899 * failure if a FreeRTOS API function is called from an interrupt that has been
1900 * assigned a priority above the configured maximum system call priority.
1901 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1902 * that have been assigned a priority at or (logically) below the maximum
1903 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1904 * safe API to ensure interrupt entry is as fast and as simple as possible.
1905 * More information (albeit Cortex-M specific) is provided on the following
1906 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1907 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1909 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1911 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1913 /* Cannot block in an ISR, so check there is data available. */
1914 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1916 const int8_t cRxLock = pxQueue->cRxLock;
1918 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
1920 prvCopyDataFromQueue( pxQueue, pvBuffer );
1921 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
1923 /* If the queue is locked the event list will not be modified.
1924 * Instead update the lock count so the task that unlocks the queue
1925 * will know that an ISR has removed data while the queue was
1927 if( cRxLock == queueUNLOCKED )
1929 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1931 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1933 /* The task waiting has a higher priority than us so
1934 * force a context switch. */
1935 if( pxHigherPriorityTaskWoken != NULL )
1937 *pxHigherPriorityTaskWoken = pdTRUE;
1941 mtCOVERAGE_TEST_MARKER();
1946 mtCOVERAGE_TEST_MARKER();
1951 mtCOVERAGE_TEST_MARKER();
1956 /* Increment the lock count so the task that unlocks the queue
1957 * knows that data was removed while it was locked. */
1958 prvIncrementQueueRxLock( pxQueue, cRxLock );
1966 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
1969 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1973 /*-----------------------------------------------------------*/
1975 BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,
1976 void * const pvBuffer )
1979 UBaseType_t uxSavedInterruptStatus;
1980 int8_t * pcOriginalReadPosition;
1981 Queue_t * const pxQueue = xQueue;
1983 configASSERT( pxQueue );
1984 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1985 configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
1987 /* RTOS ports that support interrupt nesting have the concept of a maximum
1988 * system call (or maximum API call) interrupt priority. Interrupts that are
1989 * above the maximum system call priority are kept permanently enabled, even
1990 * when the RTOS kernel is in a critical section, but cannot make any calls to
1991 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1992 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1993 * failure if a FreeRTOS API function is called from an interrupt that has been
1994 * assigned a priority above the configured maximum system call priority.
1995 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1996 * that have been assigned a priority at or (logically) below the maximum
1997 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1998 * safe API to ensure interrupt entry is as fast and as simple as possible.
1999 * More information (albeit Cortex-M specific) is provided on the following
2000 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2001 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2003 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2005 /* Cannot block in an ISR, so check there is data available. */
2006 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2008 traceQUEUE_PEEK_FROM_ISR( pxQueue );
2010 /* Remember the read position so it can be reset as nothing is
2011 * actually being removed from the queue. */
2012 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
2013 prvCopyDataFromQueue( pxQueue, pvBuffer );
2014 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
2021 traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
2024 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2028 /*-----------------------------------------------------------*/
2030 UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
2032 UBaseType_t uxReturn;
2034 configASSERT( xQueue );
2036 taskENTER_CRITICAL();
2038 uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
2040 taskEXIT_CRITICAL();
2043 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2044 /*-----------------------------------------------------------*/
2046 UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
2048 UBaseType_t uxReturn;
2049 Queue_t * const pxQueue = xQueue;
2051 configASSERT( pxQueue );
2053 taskENTER_CRITICAL();
2055 uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;
2057 taskEXIT_CRITICAL();
2060 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2061 /*-----------------------------------------------------------*/
2063 UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
2065 UBaseType_t uxReturn;
2066 Queue_t * const pxQueue = xQueue;
2068 configASSERT( pxQueue );
2069 uxReturn = pxQueue->uxMessagesWaiting;
2072 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2073 /*-----------------------------------------------------------*/
2075 void vQueueDelete( QueueHandle_t xQueue )
2077 Queue_t * const pxQueue = xQueue;
2079 configASSERT( pxQueue );
2080 traceQUEUE_DELETE( pxQueue );
2082 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2084 vQueueUnregisterQueue( pxQueue );
2088 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
2090 /* The queue can only have been allocated dynamically - free it
2092 vPortFree( pxQueue );
2094 #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
2096 /* The queue could have been allocated statically or dynamically, so
2097 * check before attempting to free the memory. */
2098 if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
2100 vPortFree( pxQueue );
2104 mtCOVERAGE_TEST_MARKER();
2107 #else /* if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) ) */
2109 /* The queue must have been statically allocated, so is not going to be
2110 * deleted. Avoid compiler warnings about the unused parameter. */
2113 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
2115 /*-----------------------------------------------------------*/
2117 #if ( configUSE_TRACE_FACILITY == 1 )
2119 UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
2121 return ( ( Queue_t * ) xQueue )->uxQueueNumber;
2124 #endif /* configUSE_TRACE_FACILITY */
2125 /*-----------------------------------------------------------*/
2127 #if ( configUSE_TRACE_FACILITY == 1 )
2129 void vQueueSetQueueNumber( QueueHandle_t xQueue,
2130 UBaseType_t uxQueueNumber )
2132 ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
2135 #endif /* configUSE_TRACE_FACILITY */
2136 /*-----------------------------------------------------------*/
2138 #if ( configUSE_TRACE_FACILITY == 1 )
2140 uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
2142 return ( ( Queue_t * ) xQueue )->ucQueueType;
2145 #endif /* configUSE_TRACE_FACILITY */
2146 /*-----------------------------------------------------------*/
2148 #if ( configUSE_MUTEXES == 1 )
2150 static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue )
2152 UBaseType_t uxHighestPriorityOfWaitingTasks;
2154 /* If a task waiting for a mutex causes the mutex holder to inherit a
2155 * priority, but the waiting task times out, then the holder should
2156 * disinherit the priority - but only down to the highest priority of any
2157 * other tasks that are waiting for the same mutex. For this purpose,
2158 * return the priority of the highest priority task that is waiting for the
2160 if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0U )
2162 uxHighestPriorityOfWaitingTasks = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) );
2166 uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;
2169 return uxHighestPriorityOfWaitingTasks;
2172 #endif /* configUSE_MUTEXES */
2173 /*-----------------------------------------------------------*/
2175 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
2176 const void * pvItemToQueue,
2177 const BaseType_t xPosition )
2179 BaseType_t xReturn = pdFALSE;
2180 UBaseType_t uxMessagesWaiting;
2182 /* This function is called from a critical section. */
2184 uxMessagesWaiting = pxQueue->uxMessagesWaiting;
2186 if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
2188 #if ( configUSE_MUTEXES == 1 )
2190 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
2192 /* The mutex is no longer being held. */
2193 xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
2194 pxQueue->u.xSemaphore.xMutexHolder = NULL;
2198 mtCOVERAGE_TEST_MARKER();
2201 #endif /* configUSE_MUTEXES */
2203 else if( xPosition == queueSEND_TO_BACK )
2205 ( 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. */
2206 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. */
2208 if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2210 pxQueue->pcWriteTo = pxQueue->pcHead;
2214 mtCOVERAGE_TEST_MARKER();
2219 ( 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. */
2220 pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
2222 if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2224 pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
2228 mtCOVERAGE_TEST_MARKER();
2231 if( xPosition == queueOVERWRITE )
2233 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
2235 /* An item is not being added but overwritten, so subtract
2236 * one from the recorded number of items in the queue so when
2237 * one is added again below the number of recorded items remains
2239 --uxMessagesWaiting;
2243 mtCOVERAGE_TEST_MARKER();
2248 mtCOVERAGE_TEST_MARKER();
2252 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
2256 /*-----------------------------------------------------------*/
2258 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
2259 void * const pvBuffer )
2261 if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
2263 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. */
2265 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
2267 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2271 mtCOVERAGE_TEST_MARKER();
2274 ( 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. */
2277 /*-----------------------------------------------------------*/
2279 static void prvUnlockQueue( Queue_t * const pxQueue )
2281 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
2283 /* The lock counts contains the number of extra data items placed or
2284 * removed from the queue while the queue was locked. When a queue is
2285 * locked items can be added or removed, but the event lists cannot be
2287 taskENTER_CRITICAL();
2289 int8_t cTxLock = pxQueue->cTxLock;
2291 /* See if data was added to the queue while it was locked. */
2292 while( cTxLock > queueLOCKED_UNMODIFIED )
2294 /* Data was posted while the queue was locked. Are any tasks
2295 * blocked waiting for data to become available? */
2296 #if ( configUSE_QUEUE_SETS == 1 )
2298 if( pxQueue->pxQueueSetContainer != NULL )
2300 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
2302 /* The queue is a member of a queue set, and posting to
2303 * the queue set caused a higher priority task to unblock.
2304 * A context switch is required. */
2309 mtCOVERAGE_TEST_MARKER();
2314 /* Tasks that are removed from the event list will get
2315 * added to the pending ready list as the scheduler is still
2317 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2319 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2321 /* The task waiting has a higher priority so record that a
2322 * context switch is required. */
2327 mtCOVERAGE_TEST_MARKER();
2336 #else /* configUSE_QUEUE_SETS */
2338 /* Tasks that are removed from the event list will get added to
2339 * the pending ready list as the scheduler is still suspended. */
2340 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2342 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2344 /* The task waiting has a higher priority so record that
2345 * a context switch is required. */
2350 mtCOVERAGE_TEST_MARKER();
2358 #endif /* configUSE_QUEUE_SETS */
2363 pxQueue->cTxLock = queueUNLOCKED;
2365 taskEXIT_CRITICAL();
2367 /* Do the same for the Rx lock. */
2368 taskENTER_CRITICAL();
2370 int8_t cRxLock = pxQueue->cRxLock;
2372 while( cRxLock > queueLOCKED_UNMODIFIED )
2374 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2376 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2382 mtCOVERAGE_TEST_MARKER();
2393 pxQueue->cRxLock = queueUNLOCKED;
2395 taskEXIT_CRITICAL();
2397 /*-----------------------------------------------------------*/
2399 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue )
2403 taskENTER_CRITICAL();
2405 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2414 taskEXIT_CRITICAL();
2418 /*-----------------------------------------------------------*/
2420 BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )
2423 Queue_t * const pxQueue = xQueue;
2425 configASSERT( pxQueue );
2427 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2437 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2438 /*-----------------------------------------------------------*/
2440 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue )
2444 taskENTER_CRITICAL();
2446 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2455 taskEXIT_CRITICAL();
2459 /*-----------------------------------------------------------*/
2461 BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )
2464 Queue_t * const pxQueue = xQueue;
2466 configASSERT( pxQueue );
2468 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2478 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2479 /*-----------------------------------------------------------*/
2481 #if ( configUSE_CO_ROUTINES == 1 )
2483 BaseType_t xQueueCRSend( QueueHandle_t xQueue,
2484 const void * pvItemToQueue,
2485 TickType_t xTicksToWait )
2488 Queue_t * const pxQueue = xQueue;
2490 /* If the queue is already full we may have to block. A critical section
2491 * is required to prevent an interrupt removing something from the queue
2492 * between the check to see if the queue is full and blocking on the queue. */
2493 portDISABLE_INTERRUPTS();
2495 if( prvIsQueueFull( pxQueue ) != pdFALSE )
2497 /* The queue is full - do we want to block or just leave without
2499 if( xTicksToWait > ( TickType_t ) 0 )
2501 /* As this is called from a coroutine we cannot block directly, but
2502 * return indicating that we need to block. */
2503 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
2504 portENABLE_INTERRUPTS();
2505 return errQUEUE_BLOCKED;
2509 portENABLE_INTERRUPTS();
2510 return errQUEUE_FULL;
2514 portENABLE_INTERRUPTS();
2516 portDISABLE_INTERRUPTS();
2518 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2520 /* There is room in the queue, copy the data into the queue. */
2521 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2524 /* Were any co-routines waiting for data to become available? */
2525 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2527 /* In this instance the co-routine could be placed directly
2528 * into the ready list as we are within a critical section.
2529 * Instead the same pending ready list mechanism is used as if
2530 * the event were caused from within an interrupt. */
2531 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2533 /* The co-routine waiting has a higher priority so record
2534 * that a yield might be appropriate. */
2535 xReturn = errQUEUE_YIELD;
2539 mtCOVERAGE_TEST_MARKER();
2544 mtCOVERAGE_TEST_MARKER();
2549 xReturn = errQUEUE_FULL;
2552 portENABLE_INTERRUPTS();
2557 #endif /* configUSE_CO_ROUTINES */
2558 /*-----------------------------------------------------------*/
2560 #if ( configUSE_CO_ROUTINES == 1 )
2562 BaseType_t xQueueCRReceive( QueueHandle_t xQueue,
2564 TickType_t xTicksToWait )
2567 Queue_t * const pxQueue = xQueue;
2569 /* If the queue is already empty we may have to block. A critical section
2570 * is required to prevent an interrupt adding something to the queue
2571 * between the check to see if the queue is empty and blocking on the queue. */
2572 portDISABLE_INTERRUPTS();
2574 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2576 /* There are no messages in the queue, do we want to block or just
2577 * leave with nothing? */
2578 if( xTicksToWait > ( TickType_t ) 0 )
2580 /* As this is a co-routine we cannot block directly, but return
2581 * indicating that we need to block. */
2582 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
2583 portENABLE_INTERRUPTS();
2584 return errQUEUE_BLOCKED;
2588 portENABLE_INTERRUPTS();
2589 return errQUEUE_FULL;
2594 mtCOVERAGE_TEST_MARKER();
2597 portENABLE_INTERRUPTS();
2599 portDISABLE_INTERRUPTS();
2601 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2603 /* Data is available from the queue. */
2604 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2606 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2608 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2612 mtCOVERAGE_TEST_MARKER();
2615 --( pxQueue->uxMessagesWaiting );
2616 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2620 /* Were any co-routines waiting for space to become available? */
2621 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2623 /* In this instance the co-routine could be placed directly
2624 * into the ready list as we are within a critical section.
2625 * Instead the same pending ready list mechanism is used as if
2626 * the event were caused from within an interrupt. */
2627 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2629 xReturn = errQUEUE_YIELD;
2633 mtCOVERAGE_TEST_MARKER();
2638 mtCOVERAGE_TEST_MARKER();
2646 portENABLE_INTERRUPTS();
2651 #endif /* configUSE_CO_ROUTINES */
2652 /*-----------------------------------------------------------*/
2654 #if ( configUSE_CO_ROUTINES == 1 )
2656 BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue,
2657 const void * pvItemToQueue,
2658 BaseType_t xCoRoutinePreviouslyWoken )
2660 Queue_t * const pxQueue = xQueue;
2662 /* Cannot block within an ISR so if there is no space on the queue then
2663 * exit without doing anything. */
2664 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2666 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2668 /* We only want to wake one co-routine per ISR, so check that a
2669 * co-routine has not already been woken. */
2670 if( xCoRoutinePreviouslyWoken == pdFALSE )
2672 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2674 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2680 mtCOVERAGE_TEST_MARKER();
2685 mtCOVERAGE_TEST_MARKER();
2690 mtCOVERAGE_TEST_MARKER();
2695 mtCOVERAGE_TEST_MARKER();
2698 return xCoRoutinePreviouslyWoken;
2701 #endif /* configUSE_CO_ROUTINES */
2702 /*-----------------------------------------------------------*/
2704 #if ( configUSE_CO_ROUTINES == 1 )
2706 BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue,
2708 BaseType_t * pxCoRoutineWoken )
2711 Queue_t * const pxQueue = xQueue;
2713 /* We cannot block from an ISR, so check there is data available. If
2714 * not then just leave without doing anything. */
2715 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2717 /* Copy the data from the queue. */
2718 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2720 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2722 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2726 mtCOVERAGE_TEST_MARKER();
2729 --( pxQueue->uxMessagesWaiting );
2730 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2732 if( ( *pxCoRoutineWoken ) == pdFALSE )
2734 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2736 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2738 *pxCoRoutineWoken = pdTRUE;
2742 mtCOVERAGE_TEST_MARKER();
2747 mtCOVERAGE_TEST_MARKER();
2752 mtCOVERAGE_TEST_MARKER();
2765 #endif /* configUSE_CO_ROUTINES */
2766 /*-----------------------------------------------------------*/
2768 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2770 void vQueueAddToRegistry( QueueHandle_t xQueue,
2771 const char * pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2774 QueueRegistryItem_t * pxEntryToWrite = NULL;
2776 configASSERT( xQueue );
2778 if( pcQueueName != NULL )
2780 /* See if there is an empty space in the registry. A NULL name denotes
2782 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2784 /* Replace an existing entry if the queue is already in the registry. */
2785 if( xQueue == xQueueRegistry[ ux ].xHandle )
2787 pxEntryToWrite = &( xQueueRegistry[ ux ] );
2790 /* Otherwise, store in the next empty location */
2791 else if( ( pxEntryToWrite == NULL ) && ( xQueueRegistry[ ux ].pcQueueName == NULL ) )
2793 pxEntryToWrite = &( xQueueRegistry[ ux ] );
2797 mtCOVERAGE_TEST_MARKER();
2802 if( pxEntryToWrite != NULL )
2804 /* Store the information on this queue. */
2805 pxEntryToWrite->pcQueueName = pcQueueName;
2806 pxEntryToWrite->xHandle = xQueue;
2808 traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
2812 #endif /* configQUEUE_REGISTRY_SIZE */
2813 /*-----------------------------------------------------------*/
2815 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2817 const char * pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2820 const char * pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2822 configASSERT( xQueue );
2824 /* Note there is nothing here to protect against another task adding or
2825 * removing entries from the registry while it is being searched. */
2827 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2829 if( xQueueRegistry[ ux ].xHandle == xQueue )
2831 pcReturn = xQueueRegistry[ ux ].pcQueueName;
2836 mtCOVERAGE_TEST_MARKER();
2841 } /*lint !e818 xQueue cannot be a pointer to const because it is a typedef. */
2843 #endif /* configQUEUE_REGISTRY_SIZE */
2844 /*-----------------------------------------------------------*/
2846 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2848 void vQueueUnregisterQueue( QueueHandle_t xQueue )
2852 configASSERT( xQueue );
2854 /* See if the handle of the queue being unregistered in actually in the
2856 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2858 if( xQueueRegistry[ ux ].xHandle == xQueue )
2860 /* Set the name to NULL to show that this slot if free again. */
2861 xQueueRegistry[ ux ].pcQueueName = NULL;
2863 /* Set the handle to NULL to ensure the same queue handle cannot
2864 * appear in the registry twice if it is added, removed, then
2866 xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;
2871 mtCOVERAGE_TEST_MARKER();
2874 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2876 #endif /* configQUEUE_REGISTRY_SIZE */
2877 /*-----------------------------------------------------------*/
2879 #if ( configUSE_TIMERS == 1 )
2881 void vQueueWaitForMessageRestricted( QueueHandle_t xQueue,
2882 TickType_t xTicksToWait,
2883 const BaseType_t xWaitIndefinitely )
2885 Queue_t * const pxQueue = xQueue;
2887 /* This function should not be called by application code hence the
2888 * 'Restricted' in its name. It is not part of the public API. It is
2889 * designed for use by kernel code, and has special calling requirements.
2890 * It can result in vListInsert() being called on a list that can only
2891 * possibly ever have one item in it, so the list will be fast, but even
2892 * so it should be called with the scheduler locked and not from a critical
2895 /* Only do anything if there are no messages in the queue. This function
2896 * will not actually cause the task to block, just place it on a blocked
2897 * list. It will not block until the scheduler is unlocked - at which
2898 * time a yield will be performed. If an item is added to the queue while
2899 * the queue is locked, and the calling task blocks on the queue, then the
2900 * calling task will be immediately unblocked when the queue is unlocked. */
2901 prvLockQueue( pxQueue );
2903 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
2905 /* There is nothing in the queue, block for the specified period. */
2906 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );
2910 mtCOVERAGE_TEST_MARKER();
2913 prvUnlockQueue( pxQueue );
2916 #endif /* configUSE_TIMERS */
2917 /*-----------------------------------------------------------*/
2919 #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
2921 QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
2923 QueueSetHandle_t pxQueue;
2925 pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
2930 #endif /* configUSE_QUEUE_SETS */
2931 /*-----------------------------------------------------------*/
2933 #if ( configUSE_QUEUE_SETS == 1 )
2935 BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore,
2936 QueueSetHandle_t xQueueSet )
2940 taskENTER_CRITICAL();
2942 if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
2944 /* Cannot add a queue/semaphore to more than one queue set. */
2947 else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
2949 /* Cannot add a queue/semaphore to a queue set if there are already
2950 * items in the queue/semaphore. */
2955 ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
2959 taskEXIT_CRITICAL();
2964 #endif /* configUSE_QUEUE_SETS */
2965 /*-----------------------------------------------------------*/
2967 #if ( configUSE_QUEUE_SETS == 1 )
2969 BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore,
2970 QueueSetHandle_t xQueueSet )
2973 Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
2975 if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
2977 /* The queue was not a member of the set. */
2980 else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
2982 /* It is dangerous to remove a queue from a set when the queue is
2983 * not empty because the queue set will still hold pending events for
2989 taskENTER_CRITICAL();
2991 /* The queue is no longer contained in the set. */
2992 pxQueueOrSemaphore->pxQueueSetContainer = NULL;
2994 taskEXIT_CRITICAL();
2999 } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
3001 #endif /* configUSE_QUEUE_SETS */
3002 /*-----------------------------------------------------------*/
3004 #if ( configUSE_QUEUE_SETS == 1 )
3006 QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet,
3007 TickType_t const xTicksToWait )
3009 QueueSetMemberHandle_t xReturn = NULL;
3011 ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait ); /*lint !e961 Casting from one typedef to another is not redundant. */
3015 #endif /* configUSE_QUEUE_SETS */
3016 /*-----------------------------------------------------------*/
3018 #if ( configUSE_QUEUE_SETS == 1 )
3020 QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
3022 QueueSetMemberHandle_t xReturn = NULL;
3024 ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
3028 #endif /* configUSE_QUEUE_SETS */
3029 /*-----------------------------------------------------------*/
3031 #if ( configUSE_QUEUE_SETS == 1 )
3033 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue )
3035 Queue_t * pxQueueSetContainer = pxQueue->pxQueueSetContainer;
3036 BaseType_t xReturn = pdFALSE;
3038 /* This function must be called form a critical section. */
3040 /* The following line is not reachable in unit tests because every call
3041 * to prvNotifyQueueSetContainer is preceded by a check that
3042 * pxQueueSetContainer != NULL */
3043 configASSERT( pxQueueSetContainer ); /* LCOV_EXCL_BR_LINE */
3044 configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
3046 if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
3048 const int8_t cTxLock = pxQueueSetContainer->cTxLock;
3050 traceQUEUE_SET_SEND( pxQueueSetContainer );
3052 /* The data copied is the handle of the queue that contains data. */
3053 xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, queueSEND_TO_BACK );
3055 if( cTxLock == queueUNLOCKED )
3057 if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
3059 if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
3061 /* The task waiting has a higher priority. */
3066 mtCOVERAGE_TEST_MARKER();
3071 mtCOVERAGE_TEST_MARKER();
3076 prvIncrementQueueTxLock( pxQueueSetContainer, cTxLock );
3081 mtCOVERAGE_TEST_MARKER();
3087 #endif /* configUSE_QUEUE_SETS */