2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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 /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46 * for the header files above, but not in this file, in order to generate the
47 * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
51 /* Constants used with the cRxLock and cTxLock structure members. */
52 #define queueUNLOCKED ( ( int8_t ) -1 )
53 #define queueLOCKED_UNMODIFIED ( ( int8_t ) 0 )
54 #define queueINT8_MAX ( ( int8_t ) 127 )
56 /* When the Queue_t structure is used to represent a base queue its pcHead and
57 * pcTail members are used as pointers into the queue storage area. When the
58 * Queue_t structure is used to represent a mutex pcHead and pcTail pointers are
59 * not necessary, and the pcHead pointer is set to NULL to indicate that the
60 * structure instead holds a pointer to the mutex holder (if any). Map alternative
61 * names to the pcHead and structure member to ensure the readability of the code
62 * is maintained. The QueuePointers_t and SemaphoreData_t types are used to form
63 * a union as their usage is mutually exclusive dependent on what the queue is
65 #define uxQueueType pcHead
66 #define queueQUEUE_IS_MUTEX NULL
68 typedef struct QueuePointers
70 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. */
71 int8_t * pcReadFrom; /**< Points to the last place that a queued item was read from when the structure is used as a queue. */
74 typedef struct SemaphoreData
76 TaskHandle_t xMutexHolder; /**< The handle of the task that holds the mutex. */
77 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. */
80 /* Semaphores do not actually store or copy data, so have an item size of
82 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )
83 #define queueMUTEX_GIVE_BLOCK_TIME ( ( TickType_t ) 0U )
85 #if ( configUSE_PREEMPTION == 0 )
87 /* If the cooperative scheduler is being used then a yield should not be
88 * performed just because a higher priority task has been woken. */
89 #define queueYIELD_IF_USING_PREEMPTION()
91 #if ( configNUMBER_OF_CORES == 1 )
92 #define queueYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
93 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
94 #define queueYIELD_IF_USING_PREEMPTION() vTaskYieldWithinAPI()
95 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
99 * Definition of the queue used by the scheduler.
100 * Items are queued by copy, not reference. See the following link for the
101 * rationale: https://www.FreeRTOS.org/Embedded-RTOS-Queues.html
103 typedef struct QueueDefinition /* The old naming convention is used to prevent breaking kernel aware debuggers. */
105 int8_t * pcHead; /**< Points to the beginning of the queue storage area. */
106 int8_t * pcWriteTo; /**< Points to the free next place in the storage area. */
110 QueuePointers_t xQueue; /**< Data required exclusively when this structure is used as a queue. */
111 SemaphoreData_t xSemaphore; /**< Data required exclusively when this structure is used as a semaphore. */
114 List_t xTasksWaitingToSend; /**< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
115 List_t xTasksWaitingToReceive; /**< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
117 volatile UBaseType_t uxMessagesWaiting; /**< The number of items currently in the queue. */
118 UBaseType_t uxLength; /**< The length of the queue defined as the number of items it will hold, not the number of bytes. */
119 UBaseType_t uxItemSize; /**< The size of each items that the queue will hold. */
121 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. */
122 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. */
124 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
125 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. */
128 #if ( configUSE_QUEUE_SETS == 1 )
129 struct QueueDefinition * pxQueueSetContainer;
132 #if ( configUSE_TRACE_FACILITY == 1 )
133 UBaseType_t uxQueueNumber;
138 /* The old xQUEUE name is maintained above then typedefed to the new Queue_t
139 * name below to enable the use of older kernel aware debuggers. */
140 typedef xQUEUE Queue_t;
142 /*-----------------------------------------------------------*/
145 * The queue registry is just a means for kernel aware debuggers to locate
146 * queue structures. It has no other purpose so is an optional component.
148 #if ( configQUEUE_REGISTRY_SIZE > 0 )
150 /* The type stored within the queue registry array. This allows a name
151 * to be assigned to each queue making kernel aware debugging a little
152 * more user friendly. */
153 typedef struct QUEUE_REGISTRY_ITEM
155 const char * pcQueueName;
156 QueueHandle_t xHandle;
157 } xQueueRegistryItem;
159 /* The old xQueueRegistryItem name is maintained above then typedefed to the
160 * new xQueueRegistryItem name below to enable the use of older kernel aware
162 typedef xQueueRegistryItem QueueRegistryItem_t;
164 /* The queue registry is simply an array of QueueRegistryItem_t structures.
165 * The pcQueueName member of a structure being NULL is indicative of the
166 * array position being vacant. */
168 /* MISRA Ref 8.4.2 [Declaration shall be visible] */
169 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
170 /* coverity[misra_c_2012_rule_8_4_violation] */
171 PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
173 #endif /* configQUEUE_REGISTRY_SIZE */
176 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
177 * prevent an ISR from adding or removing items to the queue, but does prevent
178 * an ISR from removing tasks from the queue event lists. If an ISR finds a
179 * queue is locked it will instead increment the appropriate queue lock count
180 * to indicate that a task may require unblocking. When the queue in unlocked
181 * these lock counts are inspected, and the appropriate action taken.
183 static void prvUnlockQueue( Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
186 * Uses a critical section to determine if there is any data in a queue.
188 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
190 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
193 * Uses a critical section to determine if there is any space in a queue.
195 * @return pdTRUE if there is no space, otherwise pdFALSE;
197 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
200 * Copies an item into the queue, either at the front of the queue or the
203 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
204 const void * pvItemToQueue,
205 const BaseType_t xPosition ) PRIVILEGED_FUNCTION;
208 * Copies an item out of a queue.
210 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
211 void * const pvBuffer ) PRIVILEGED_FUNCTION;
213 #if ( configUSE_QUEUE_SETS == 1 )
216 * Checks to see if a queue is a member of a queue set, and if so, notifies
217 * the queue set that the queue contains data.
219 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
223 * Called after a Queue_t structure has been allocated either statically or
224 * dynamically to fill in the structure's members.
226 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
227 const UBaseType_t uxItemSize,
228 uint8_t * pucQueueStorage,
229 const uint8_t ucQueueType,
230 Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
233 * Mutexes are a special type of queue. When a mutex is created, first the
234 * queue is created, then prvInitialiseMutex() is called to configure the queue
237 #if ( configUSE_MUTEXES == 1 )
238 static void prvInitialiseMutex( Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
241 #if ( configUSE_MUTEXES == 1 )
244 * If a task waiting for a mutex causes the mutex holder to inherit a
245 * priority, but the waiting task times out, then the holder should
246 * disinherit the priority - but only down to the highest priority of any
247 * other tasks that are waiting for the same mutex. This function returns
250 static UBaseType_t prvGetHighestPriorityOfWaitToReceiveList( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
252 /*-----------------------------------------------------------*/
255 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
256 * accessing the queue event lists.
258 #define prvLockQueue( pxQueue ) \
259 taskENTER_CRITICAL(); \
261 if( ( pxQueue )->cRxLock == queueUNLOCKED ) \
263 ( pxQueue )->cRxLock = queueLOCKED_UNMODIFIED; \
265 if( ( pxQueue )->cTxLock == queueUNLOCKED ) \
267 ( pxQueue )->cTxLock = queueLOCKED_UNMODIFIED; \
273 * Macro to increment cTxLock member of the queue data structure. It is
274 * capped at the number of tasks in the system as we cannot unblock more
275 * tasks than the number of tasks in the system.
277 #define prvIncrementQueueTxLock( pxQueue, cTxLock ) \
279 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
280 if( ( UBaseType_t ) ( cTxLock ) < uxNumberOfTasks ) \
282 configASSERT( ( cTxLock ) != queueINT8_MAX ); \
283 ( pxQueue )->cTxLock = ( int8_t ) ( ( cTxLock ) + ( int8_t ) 1 ); \
288 * Macro to increment cRxLock member of the queue data structure. It is
289 * capped at the number of tasks in the system as we cannot unblock more
290 * tasks than the number of tasks in the system.
292 #define prvIncrementQueueRxLock( pxQueue, cRxLock ) \
294 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
295 if( ( UBaseType_t ) ( cRxLock ) < uxNumberOfTasks ) \
297 configASSERT( ( cRxLock ) != queueINT8_MAX ); \
298 ( pxQueue )->cRxLock = ( int8_t ) ( ( cRxLock ) + ( int8_t ) 1 ); \
301 /*-----------------------------------------------------------*/
303 BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
304 BaseType_t xNewQueue )
306 BaseType_t xReturn = pdPASS;
307 Queue_t * const pxQueue = xQueue;
309 traceENTER_xQueueGenericReset( xQueue, xNewQueue );
311 configASSERT( pxQueue );
313 if( ( pxQueue != NULL ) &&
314 ( pxQueue->uxLength >= 1U ) &&
315 /* Check for multiplication overflow. */
316 ( ( SIZE_MAX / pxQueue->uxLength ) >= pxQueue->uxItemSize ) )
318 taskENTER_CRITICAL();
320 pxQueue->u.xQueue.pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
321 pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
322 pxQueue->pcWriteTo = pxQueue->pcHead;
323 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - 1U ) * pxQueue->uxItemSize );
324 pxQueue->cRxLock = queueUNLOCKED;
325 pxQueue->cTxLock = queueUNLOCKED;
327 if( xNewQueue == pdFALSE )
329 /* If there are tasks blocked waiting to read from the queue, then
330 * the tasks will remain blocked as after this function exits the queue
331 * will still be empty. If there are tasks blocked waiting to write to
332 * the queue, then one should be unblocked as after this function exits
333 * it will be possible to write to it. */
334 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
336 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
338 queueYIELD_IF_USING_PREEMPTION();
342 mtCOVERAGE_TEST_MARKER();
347 mtCOVERAGE_TEST_MARKER();
352 /* Ensure the event queues start in the correct state. */
353 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
354 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
364 configASSERT( xReturn != pdFAIL );
366 /* A value is returned for calling semantic consistency with previous
368 traceRETURN_xQueueGenericReset( xReturn );
372 /*-----------------------------------------------------------*/
374 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
376 QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,
377 const UBaseType_t uxItemSize,
378 uint8_t * pucQueueStorage,
379 StaticQueue_t * pxStaticQueue,
380 const uint8_t ucQueueType )
382 Queue_t * pxNewQueue = NULL;
384 traceENTER_xQueueGenericCreateStatic( uxQueueLength, uxItemSize, pucQueueStorage, pxStaticQueue, ucQueueType );
386 /* The StaticQueue_t structure and the queue storage area must be
388 configASSERT( pxStaticQueue );
390 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
391 ( pxStaticQueue != NULL ) &&
393 /* A queue storage area should be provided if the item size is not 0, and
394 * should not be provided if the item size is 0. */
395 ( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0U ) ) ) &&
396 ( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0U ) ) ) )
398 #if ( configASSERT_DEFINED == 1 )
400 /* Sanity check that the size of the structure used to declare a
401 * variable of type StaticQueue_t or StaticSemaphore_t equals the size of
402 * the real queue and semaphore structures. */
403 volatile size_t xSize = sizeof( StaticQueue_t );
405 /* This assertion cannot be branch covered in unit tests */
406 configASSERT( xSize == sizeof( Queue_t ) ); /* LCOV_EXCL_BR_LINE */
407 ( void ) xSize; /* Prevent unused variable warning when configASSERT() is not defined. */
409 #endif /* configASSERT_DEFINED */
411 /* The address of a statically allocated queue was passed in, use it.
412 * The address of a statically allocated storage area was also passed in
413 * but is already set. */
414 /* MISRA Ref 11.3.1 [Misaligned access] */
415 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
416 /* coverity[misra_c_2012_rule_11_3_violation] */
417 pxNewQueue = ( Queue_t * ) pxStaticQueue;
419 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
421 /* Queues can be allocated either statically or dynamically, so
422 * note this queue was allocated statically in case the queue is
424 pxNewQueue->ucStaticallyAllocated = pdTRUE;
426 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
428 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
432 configASSERT( pxNewQueue );
433 mtCOVERAGE_TEST_MARKER();
436 traceRETURN_xQueueGenericCreateStatic( pxNewQueue );
441 #endif /* configSUPPORT_STATIC_ALLOCATION */
442 /*-----------------------------------------------------------*/
444 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
446 BaseType_t xQueueGenericGetStaticBuffers( QueueHandle_t xQueue,
447 uint8_t ** ppucQueueStorage,
448 StaticQueue_t ** ppxStaticQueue )
451 Queue_t * const pxQueue = xQueue;
453 traceENTER_xQueueGenericGetStaticBuffers( xQueue, ppucQueueStorage, ppxStaticQueue );
455 configASSERT( pxQueue );
456 configASSERT( ppxStaticQueue );
458 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
460 /* Check if the queue was statically allocated. */
461 if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdTRUE )
463 if( ppucQueueStorage != NULL )
465 *ppucQueueStorage = ( uint8_t * ) pxQueue->pcHead;
468 /* MISRA Ref 11.3.1 [Misaligned access] */
469 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
470 /* coverity[misra_c_2012_rule_11_3_violation] */
471 *ppxStaticQueue = ( StaticQueue_t * ) pxQueue;
479 #else /* configSUPPORT_DYNAMIC_ALLOCATION */
481 /* Queue must have been statically allocated. */
482 if( ppucQueueStorage != NULL )
484 *ppucQueueStorage = ( uint8_t * ) pxQueue->pcHead;
487 *ppxStaticQueue = ( StaticQueue_t * ) pxQueue;
490 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
492 traceRETURN_xQueueGenericGetStaticBuffers( xReturn );
497 #endif /* configSUPPORT_STATIC_ALLOCATION */
498 /*-----------------------------------------------------------*/
500 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
502 QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
503 const UBaseType_t uxItemSize,
504 const uint8_t ucQueueType )
506 Queue_t * pxNewQueue = NULL;
507 size_t xQueueSizeInBytes;
508 uint8_t * pucQueueStorage;
510 traceENTER_xQueueGenericCreate( uxQueueLength, uxItemSize, ucQueueType );
512 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
513 /* Check for multiplication overflow. */
514 ( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&
515 /* Check for addition overflow. */
516 /* MISRA Ref 14.3.1 [Configuration dependent invariant] */
517 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-143. */
518 /* coverity[misra_c_2012_rule_14_3_violation] */
519 ( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( size_t ) ( ( size_t ) uxQueueLength * ( size_t ) uxItemSize ) ) )
521 /* Allocate enough space to hold the maximum number of items that
522 * can be in the queue at any time. It is valid for uxItemSize to be
523 * zero in the case the queue is used as a semaphore. */
524 xQueueSizeInBytes = ( size_t ) ( ( size_t ) uxQueueLength * ( size_t ) uxItemSize );
526 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
527 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
528 /* coverity[misra_c_2012_rule_11_5_violation] */
529 pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes );
531 if( pxNewQueue != NULL )
533 /* Jump past the queue structure to find the location of the queue
535 pucQueueStorage = ( uint8_t * ) pxNewQueue;
536 pucQueueStorage += sizeof( Queue_t );
538 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
540 /* Queues can be created either statically or dynamically, so
541 * note this task was created dynamically in case it is later
543 pxNewQueue->ucStaticallyAllocated = pdFALSE;
545 #endif /* configSUPPORT_STATIC_ALLOCATION */
547 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
551 traceQUEUE_CREATE_FAILED( ucQueueType );
552 mtCOVERAGE_TEST_MARKER();
557 configASSERT( pxNewQueue );
558 mtCOVERAGE_TEST_MARKER();
561 traceRETURN_xQueueGenericCreate( pxNewQueue );
566 #endif /* configSUPPORT_STATIC_ALLOCATION */
567 /*-----------------------------------------------------------*/
569 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
570 const UBaseType_t uxItemSize,
571 uint8_t * pucQueueStorage,
572 const uint8_t ucQueueType,
573 Queue_t * pxNewQueue )
575 /* Remove compiler warnings about unused parameters should
576 * configUSE_TRACE_FACILITY not be set to 1. */
577 ( void ) ucQueueType;
579 if( uxItemSize == ( UBaseType_t ) 0 )
581 /* No RAM was allocated for the queue storage area, but PC head cannot
582 * be set to NULL because NULL is used as a key to say the queue is used as
583 * a mutex. Therefore just set pcHead to point to the queue as a benign
584 * value that is known to be within the memory map. */
585 pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
589 /* Set the head to the start of the queue storage area. */
590 pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
593 /* Initialise the queue members as described where the queue type is
595 pxNewQueue->uxLength = uxQueueLength;
596 pxNewQueue->uxItemSize = uxItemSize;
597 ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
599 #if ( configUSE_TRACE_FACILITY == 1 )
601 pxNewQueue->ucQueueType = ucQueueType;
603 #endif /* configUSE_TRACE_FACILITY */
605 #if ( configUSE_QUEUE_SETS == 1 )
607 pxNewQueue->pxQueueSetContainer = NULL;
609 #endif /* configUSE_QUEUE_SETS */
611 traceQUEUE_CREATE( pxNewQueue );
613 /*-----------------------------------------------------------*/
615 #if ( configUSE_MUTEXES == 1 )
617 static void prvInitialiseMutex( Queue_t * pxNewQueue )
619 if( pxNewQueue != NULL )
621 /* The queue create function will set all the queue structure members
622 * correctly for a generic queue, but this function is creating a
623 * mutex. Overwrite those members that need to be set differently -
624 * in particular the information required for priority inheritance. */
625 pxNewQueue->u.xSemaphore.xMutexHolder = NULL;
626 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
628 /* In case this is a recursive mutex. */
629 pxNewQueue->u.xSemaphore.uxRecursiveCallCount = 0;
631 traceCREATE_MUTEX( pxNewQueue );
633 /* Start with the semaphore in the expected state. */
634 ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
638 traceCREATE_MUTEX_FAILED();
642 #endif /* configUSE_MUTEXES */
643 /*-----------------------------------------------------------*/
645 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
647 QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
649 QueueHandle_t xNewQueue;
650 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
652 traceENTER_xQueueCreateMutex( ucQueueType );
654 xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
655 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
657 traceRETURN_xQueueCreateMutex( xNewQueue );
662 #endif /* configUSE_MUTEXES */
663 /*-----------------------------------------------------------*/
665 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
667 QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
668 StaticQueue_t * pxStaticQueue )
670 QueueHandle_t xNewQueue;
671 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
673 traceENTER_xQueueCreateMutexStatic( ucQueueType, pxStaticQueue );
675 /* Prevent compiler warnings about unused parameters if
676 * configUSE_TRACE_FACILITY does not equal 1. */
677 ( void ) ucQueueType;
679 xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
680 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
682 traceRETURN_xQueueCreateMutexStatic( xNewQueue );
687 #endif /* configUSE_MUTEXES */
688 /*-----------------------------------------------------------*/
690 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
692 TaskHandle_t xQueueGetMutexHolder( QueueHandle_t xSemaphore )
694 TaskHandle_t pxReturn;
695 Queue_t * const pxSemaphore = ( Queue_t * ) xSemaphore;
697 traceENTER_xQueueGetMutexHolder( xSemaphore );
699 configASSERT( xSemaphore );
701 /* This function is called by xSemaphoreGetMutexHolder(), and should not
702 * be called directly. Note: This is a good way of determining if the
703 * calling task is the mutex holder, but not a good way of determining the
704 * identity of the mutex holder, as the holder may change between the
705 * following critical section exiting and the function returning. */
706 taskENTER_CRITICAL();
708 if( pxSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
710 pxReturn = pxSemaphore->u.xSemaphore.xMutexHolder;
719 traceRETURN_xQueueGetMutexHolder( pxReturn );
724 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
725 /*-----------------------------------------------------------*/
727 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
729 TaskHandle_t xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )
731 TaskHandle_t pxReturn;
733 traceENTER_xQueueGetMutexHolderFromISR( xSemaphore );
735 configASSERT( xSemaphore );
737 /* Mutexes cannot be used in interrupt service routines, so the mutex
738 * holder should not change in an ISR, and therefore a critical section is
739 * not required here. */
740 if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
742 pxReturn = ( ( Queue_t * ) xSemaphore )->u.xSemaphore.xMutexHolder;
749 traceRETURN_xQueueGetMutexHolderFromISR( pxReturn );
754 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
755 /*-----------------------------------------------------------*/
757 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
759 BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
762 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
764 traceENTER_xQueueGiveMutexRecursive( xMutex );
766 configASSERT( pxMutex );
768 /* If this is the task that holds the mutex then xMutexHolder will not
769 * change outside of this task. If this task does not hold the mutex then
770 * pxMutexHolder can never coincidentally equal the tasks handle, and as
771 * this is the only condition we are interested in it does not matter if
772 * pxMutexHolder is accessed simultaneously by another task. Therefore no
773 * mutual exclusion is required to test the pxMutexHolder variable. */
774 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
776 traceGIVE_MUTEX_RECURSIVE( pxMutex );
778 /* uxRecursiveCallCount cannot be zero if xMutexHolder is equal to
779 * the task handle, therefore no underflow check is required. Also,
780 * uxRecursiveCallCount is only modified by the mutex holder, and as
781 * there can only be one, no mutual exclusion is required to modify the
782 * uxRecursiveCallCount member. */
783 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )--;
785 /* Has the recursive call count unwound to 0? */
786 if( pxMutex->u.xSemaphore.uxRecursiveCallCount == ( UBaseType_t ) 0 )
788 /* Return the mutex. This will automatically unblock any other
789 * task that might be waiting to access the mutex. */
790 ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
794 mtCOVERAGE_TEST_MARKER();
801 /* The mutex cannot be given because the calling task is not the
805 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
808 traceRETURN_xQueueGiveMutexRecursive( xReturn );
813 #endif /* configUSE_RECURSIVE_MUTEXES */
814 /*-----------------------------------------------------------*/
816 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
818 BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
819 TickType_t xTicksToWait )
822 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
824 traceENTER_xQueueTakeMutexRecursive( xMutex, xTicksToWait );
826 configASSERT( pxMutex );
828 /* Comments regarding mutual exclusion as per those within
829 * xQueueGiveMutexRecursive(). */
831 traceTAKE_MUTEX_RECURSIVE( pxMutex );
833 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
835 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
837 /* Check if an overflow occurred. */
838 configASSERT( pxMutex->u.xSemaphore.uxRecursiveCallCount );
844 xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );
846 /* pdPASS will only be returned if the mutex was successfully
847 * obtained. The calling task may have entered the Blocked state
848 * before reaching here. */
849 if( xReturn != pdFAIL )
851 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
853 /* Check if an overflow occurred. */
854 configASSERT( pxMutex->u.xSemaphore.uxRecursiveCallCount );
858 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
862 traceRETURN_xQueueTakeMutexRecursive( xReturn );
867 #endif /* configUSE_RECURSIVE_MUTEXES */
868 /*-----------------------------------------------------------*/
870 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
872 QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
873 const UBaseType_t uxInitialCount,
874 StaticQueue_t * pxStaticQueue )
876 QueueHandle_t xHandle = NULL;
878 traceENTER_xQueueCreateCountingSemaphoreStatic( uxMaxCount, uxInitialCount, pxStaticQueue );
880 if( ( uxMaxCount != 0U ) &&
881 ( uxInitialCount <= uxMaxCount ) )
883 xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
885 if( xHandle != NULL )
887 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
889 traceCREATE_COUNTING_SEMAPHORE();
893 traceCREATE_COUNTING_SEMAPHORE_FAILED();
898 configASSERT( xHandle );
899 mtCOVERAGE_TEST_MARKER();
902 traceRETURN_xQueueCreateCountingSemaphoreStatic( xHandle );
907 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
908 /*-----------------------------------------------------------*/
910 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
912 QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
913 const UBaseType_t uxInitialCount )
915 QueueHandle_t xHandle = NULL;
917 traceENTER_xQueueCreateCountingSemaphore( uxMaxCount, uxInitialCount );
919 if( ( uxMaxCount != 0U ) &&
920 ( uxInitialCount <= uxMaxCount ) )
922 xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
924 if( xHandle != NULL )
926 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
928 traceCREATE_COUNTING_SEMAPHORE();
932 traceCREATE_COUNTING_SEMAPHORE_FAILED();
937 configASSERT( xHandle );
938 mtCOVERAGE_TEST_MARKER();
941 traceRETURN_xQueueCreateCountingSemaphore( xHandle );
946 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
947 /*-----------------------------------------------------------*/
949 BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
950 const void * const pvItemToQueue,
951 TickType_t xTicksToWait,
952 const BaseType_t xCopyPosition )
954 BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
956 Queue_t * const pxQueue = xQueue;
958 traceENTER_xQueueGenericSend( xQueue, pvItemToQueue, xTicksToWait, xCopyPosition );
960 configASSERT( pxQueue );
961 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
962 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
963 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
965 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
971 taskENTER_CRITICAL();
973 /* Is there room on the queue now? The running task must be the
974 * highest priority task wanting to access the queue. If the head item
975 * in the queue is to be overwritten then it does not matter if the
977 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
979 traceQUEUE_SEND( pxQueue );
981 #if ( configUSE_QUEUE_SETS == 1 )
983 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
985 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
987 if( pxQueue->pxQueueSetContainer != NULL )
989 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
991 /* Do not notify the queue set as an existing item
992 * was overwritten in the queue so the number of items
993 * in the queue has not changed. */
994 mtCOVERAGE_TEST_MARKER();
996 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
998 /* The queue is a member of a queue set, and posting
999 * to the queue set caused a higher priority task to
1000 * unblock. A context switch is required. */
1001 queueYIELD_IF_USING_PREEMPTION();
1005 mtCOVERAGE_TEST_MARKER();
1010 /* If there was a task waiting for data to arrive on the
1011 * queue then unblock it now. */
1012 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1014 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1016 /* The unblocked task has a priority higher than
1017 * our own so yield immediately. Yes it is ok to
1018 * do this from within the critical section - the
1019 * kernel takes care of that. */
1020 queueYIELD_IF_USING_PREEMPTION();
1024 mtCOVERAGE_TEST_MARKER();
1027 else if( xYieldRequired != pdFALSE )
1029 /* This path is a special case that will only get
1030 * executed if the task was holding multiple mutexes
1031 * and the mutexes were given back in an order that is
1032 * different to that in which they were taken. */
1033 queueYIELD_IF_USING_PREEMPTION();
1037 mtCOVERAGE_TEST_MARKER();
1041 #else /* configUSE_QUEUE_SETS */
1043 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
1045 /* If there was a task waiting for data to arrive on the
1046 * queue then unblock it now. */
1047 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1049 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1051 /* The unblocked task has a priority higher than
1052 * our own so yield immediately. Yes it is ok to do
1053 * this from within the critical section - the kernel
1054 * takes care of that. */
1055 queueYIELD_IF_USING_PREEMPTION();
1059 mtCOVERAGE_TEST_MARKER();
1062 else if( xYieldRequired != pdFALSE )
1064 /* This path is a special case that will only get
1065 * executed if the task was holding multiple mutexes and
1066 * the mutexes were given back in an order that is
1067 * different to that in which they were taken. */
1068 queueYIELD_IF_USING_PREEMPTION();
1072 mtCOVERAGE_TEST_MARKER();
1075 #endif /* configUSE_QUEUE_SETS */
1077 taskEXIT_CRITICAL();
1079 traceRETURN_xQueueGenericSend( pdPASS );
1085 if( xTicksToWait == ( TickType_t ) 0 )
1087 /* The queue was full and no block time is specified (or
1088 * the block time has expired) so leave now. */
1089 taskEXIT_CRITICAL();
1091 /* Return to the original privilege level before exiting
1093 traceQUEUE_SEND_FAILED( pxQueue );
1094 traceRETURN_xQueueGenericSend( errQUEUE_FULL );
1096 return errQUEUE_FULL;
1098 else if( xEntryTimeSet == pdFALSE )
1100 /* The queue was full and a block time was specified so
1101 * configure the timeout structure. */
1102 vTaskInternalSetTimeOutState( &xTimeOut );
1103 xEntryTimeSet = pdTRUE;
1107 /* Entry time was already set. */
1108 mtCOVERAGE_TEST_MARKER();
1112 taskEXIT_CRITICAL();
1114 /* Interrupts and other tasks can send to and receive from the queue
1115 * now the critical section has been exited. */
1118 prvLockQueue( pxQueue );
1120 /* Update the timeout state to see if it has expired yet. */
1121 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1123 if( prvIsQueueFull( pxQueue ) != pdFALSE )
1125 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
1126 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
1128 /* Unlocking the queue means queue events can effect the
1129 * event list. It is possible that interrupts occurring now
1130 * remove this task from the event list again - but as the
1131 * scheduler is suspended the task will go onto the pending
1132 * ready list instead of the actual ready list. */
1133 prvUnlockQueue( pxQueue );
1135 /* Resuming the scheduler will move tasks from the pending
1136 * ready list into the ready list - so it is feasible that this
1137 * task is already in the ready list before it yields - in which
1138 * case the yield will not cause a context switch unless there
1139 * is also a higher priority task in the pending ready list. */
1140 if( xTaskResumeAll() == pdFALSE )
1142 taskYIELD_WITHIN_API();
1148 prvUnlockQueue( pxQueue );
1149 ( void ) xTaskResumeAll();
1154 /* The timeout has expired. */
1155 prvUnlockQueue( pxQueue );
1156 ( void ) xTaskResumeAll();
1158 traceQUEUE_SEND_FAILED( pxQueue );
1159 traceRETURN_xQueueGenericSend( errQUEUE_FULL );
1161 return errQUEUE_FULL;
1165 /*-----------------------------------------------------------*/
1167 BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,
1168 const void * const pvItemToQueue,
1169 BaseType_t * const pxHigherPriorityTaskWoken,
1170 const BaseType_t xCopyPosition )
1173 UBaseType_t uxSavedInterruptStatus;
1174 Queue_t * const pxQueue = xQueue;
1176 traceENTER_xQueueGenericSendFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken, xCopyPosition );
1178 configASSERT( pxQueue );
1179 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1180 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
1182 /* RTOS ports that support interrupt nesting have the concept of a maximum
1183 * system call (or maximum API call) interrupt priority. Interrupts that are
1184 * above the maximum system call priority are kept permanently enabled, even
1185 * when the RTOS kernel is in a critical section, but cannot make any calls to
1186 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1187 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1188 * failure if a FreeRTOS API function is called from an interrupt that has been
1189 * assigned a priority above the configured maximum system call priority.
1190 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1191 * that have been assigned a priority at or (logically) below the maximum
1192 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1193 * safe API to ensure interrupt entry is as fast and as simple as possible.
1194 * More information (albeit Cortex-M specific) is provided on the following
1195 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1196 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1198 /* Similar to xQueueGenericSend, except without blocking if there is no room
1199 * in the queue. Also don't directly wake a task that was blocked on a queue
1200 * read, instead return a flag to say whether a context switch is required or
1201 * not (i.e. has a task with a higher priority than us been woken by this
1203 /* MISRA Ref 4.7.1 [Return value shall be checked] */
1204 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
1205 /* coverity[misra_c_2012_directive_4_7_violation] */
1206 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
1208 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
1210 const int8_t cTxLock = pxQueue->cTxLock;
1211 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
1213 traceQUEUE_SEND_FROM_ISR( pxQueue );
1215 /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a
1216 * semaphore or mutex. That means prvCopyDataToQueue() cannot result
1217 * in a task disinheriting a priority and prvCopyDataToQueue() can be
1218 * called here even though the disinherit function does not check if
1219 * the scheduler is suspended before accessing the ready lists. */
1220 ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
1222 /* The event list is not altered if the queue is locked. This will
1223 * be done when the queue is unlocked later. */
1224 if( cTxLock == queueUNLOCKED )
1226 #if ( configUSE_QUEUE_SETS == 1 )
1228 if( pxQueue->pxQueueSetContainer != NULL )
1230 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
1232 /* Do not notify the queue set as an existing item
1233 * was overwritten in the queue so the number of items
1234 * in the queue has not changed. */
1235 mtCOVERAGE_TEST_MARKER();
1237 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1239 /* The queue is a member of a queue set, and posting
1240 * to the queue set caused a higher priority task to
1241 * unblock. A context switch is required. */
1242 if( pxHigherPriorityTaskWoken != NULL )
1244 *pxHigherPriorityTaskWoken = pdTRUE;
1248 mtCOVERAGE_TEST_MARKER();
1253 mtCOVERAGE_TEST_MARKER();
1258 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1260 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1262 /* The task waiting has a higher priority so
1263 * record that a context switch is required. */
1264 if( pxHigherPriorityTaskWoken != NULL )
1266 *pxHigherPriorityTaskWoken = pdTRUE;
1270 mtCOVERAGE_TEST_MARKER();
1275 mtCOVERAGE_TEST_MARKER();
1280 mtCOVERAGE_TEST_MARKER();
1284 #else /* configUSE_QUEUE_SETS */
1286 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1288 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1290 /* The task waiting has a higher priority so record that a
1291 * context switch is required. */
1292 if( pxHigherPriorityTaskWoken != NULL )
1294 *pxHigherPriorityTaskWoken = pdTRUE;
1298 mtCOVERAGE_TEST_MARKER();
1303 mtCOVERAGE_TEST_MARKER();
1308 mtCOVERAGE_TEST_MARKER();
1311 /* Not used in this path. */
1312 ( void ) uxPreviousMessagesWaiting;
1314 #endif /* configUSE_QUEUE_SETS */
1318 /* Increment the lock count so the task that unlocks the queue
1319 * knows that data was posted while it was locked. */
1320 prvIncrementQueueTxLock( pxQueue, cTxLock );
1327 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1328 xReturn = errQUEUE_FULL;
1331 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1333 traceRETURN_xQueueGenericSendFromISR( xReturn );
1337 /*-----------------------------------------------------------*/
1339 BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,
1340 BaseType_t * const pxHigherPriorityTaskWoken )
1343 UBaseType_t uxSavedInterruptStatus;
1344 Queue_t * const pxQueue = xQueue;
1346 traceENTER_xQueueGiveFromISR( xQueue, pxHigherPriorityTaskWoken );
1348 /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
1349 * item size is 0. Don't directly wake a task that was blocked on a queue
1350 * read, instead return a flag to say whether a context switch is required or
1351 * not (i.e. has a task with a higher priority than us been woken by this
1354 configASSERT( pxQueue );
1356 /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()
1357 * if the item size is not 0. */
1358 configASSERT( pxQueue->uxItemSize == 0 );
1360 /* Normally a mutex would not be given from an interrupt, especially if
1361 * there is a mutex holder, as priority inheritance makes no sense for an
1362 * interrupts, only tasks. */
1363 configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->u.xSemaphore.xMutexHolder != NULL ) ) );
1365 /* RTOS ports that support interrupt nesting have the concept of a maximum
1366 * system call (or maximum API call) interrupt priority. Interrupts that are
1367 * above the maximum system call priority are kept permanently enabled, even
1368 * when the RTOS kernel is in a critical section, but cannot make any calls to
1369 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1370 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1371 * failure if a FreeRTOS API function is called from an interrupt that has been
1372 * assigned a priority above the configured maximum system call priority.
1373 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1374 * that have been assigned a priority at or (logically) below the maximum
1375 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1376 * safe API to ensure interrupt entry is as fast and as simple as possible.
1377 * More information (albeit Cortex-M specific) is provided on the following
1378 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1379 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1381 /* MISRA Ref 4.7.1 [Return value shall be checked] */
1382 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
1383 /* coverity[misra_c_2012_directive_4_7_violation] */
1384 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
1386 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1388 /* When the queue is used to implement a semaphore no data is ever
1389 * moved through the queue but it is still valid to see if the queue 'has
1391 if( uxMessagesWaiting < pxQueue->uxLength )
1393 const int8_t cTxLock = pxQueue->cTxLock;
1395 traceQUEUE_SEND_FROM_ISR( pxQueue );
1397 /* A task can only have an inherited priority if it is a mutex
1398 * holder - and if there is a mutex holder then the mutex cannot be
1399 * given from an ISR. As this is the ISR version of the function it
1400 * can be assumed there is no mutex holder and no need to determine if
1401 * priority disinheritance is needed. Simply increase the count of
1402 * messages (semaphores) available. */
1403 pxQueue->uxMessagesWaiting = ( UBaseType_t ) ( uxMessagesWaiting + ( UBaseType_t ) 1 );
1405 /* The event list is not altered if the queue is locked. This will
1406 * be done when the queue is unlocked later. */
1407 if( cTxLock == queueUNLOCKED )
1409 #if ( configUSE_QUEUE_SETS == 1 )
1411 if( pxQueue->pxQueueSetContainer != NULL )
1413 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1415 /* The semaphore is a member of a queue set, and
1416 * posting to the queue set caused a higher priority
1417 * task to unblock. A context switch is required. */
1418 if( pxHigherPriorityTaskWoken != NULL )
1420 *pxHigherPriorityTaskWoken = pdTRUE;
1424 mtCOVERAGE_TEST_MARKER();
1429 mtCOVERAGE_TEST_MARKER();
1434 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1436 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1438 /* The task waiting has a higher priority so
1439 * record that a context switch is required. */
1440 if( pxHigherPriorityTaskWoken != NULL )
1442 *pxHigherPriorityTaskWoken = pdTRUE;
1446 mtCOVERAGE_TEST_MARKER();
1451 mtCOVERAGE_TEST_MARKER();
1456 mtCOVERAGE_TEST_MARKER();
1460 #else /* configUSE_QUEUE_SETS */
1462 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1464 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1466 /* The task waiting has a higher priority so record that a
1467 * context switch is required. */
1468 if( pxHigherPriorityTaskWoken != NULL )
1470 *pxHigherPriorityTaskWoken = pdTRUE;
1474 mtCOVERAGE_TEST_MARKER();
1479 mtCOVERAGE_TEST_MARKER();
1484 mtCOVERAGE_TEST_MARKER();
1487 #endif /* configUSE_QUEUE_SETS */
1491 /* Increment the lock count so the task that unlocks the queue
1492 * knows that data was posted while it was locked. */
1493 prvIncrementQueueTxLock( pxQueue, cTxLock );
1500 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1501 xReturn = errQUEUE_FULL;
1504 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1506 traceRETURN_xQueueGiveFromISR( xReturn );
1510 /*-----------------------------------------------------------*/
1512 BaseType_t xQueueReceive( QueueHandle_t xQueue,
1513 void * const pvBuffer,
1514 TickType_t xTicksToWait )
1516 BaseType_t xEntryTimeSet = pdFALSE;
1518 Queue_t * const pxQueue = xQueue;
1520 traceENTER_xQueueReceive( xQueue, pvBuffer, xTicksToWait );
1522 /* Check the pointer is not NULL. */
1523 configASSERT( ( pxQueue ) );
1525 /* The buffer into which data is received can only be NULL if the data size
1526 * is zero (so no data is copied into the buffer). */
1527 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1529 /* Cannot block if the scheduler is suspended. */
1530 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1532 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1538 taskENTER_CRITICAL();
1540 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1542 /* Is there data in the queue now? To be running the calling task
1543 * must be the highest priority task wanting to access the queue. */
1544 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1546 /* Data available, remove one item. */
1547 prvCopyDataFromQueue( pxQueue, pvBuffer );
1548 traceQUEUE_RECEIVE( pxQueue );
1549 pxQueue->uxMessagesWaiting = ( UBaseType_t ) ( uxMessagesWaiting - ( UBaseType_t ) 1 );
1551 /* There is now space in the queue, were any tasks waiting to
1552 * post to the queue? If so, unblock the highest priority waiting
1554 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1556 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1558 queueYIELD_IF_USING_PREEMPTION();
1562 mtCOVERAGE_TEST_MARKER();
1567 mtCOVERAGE_TEST_MARKER();
1570 taskEXIT_CRITICAL();
1572 traceRETURN_xQueueReceive( pdPASS );
1578 if( xTicksToWait == ( TickType_t ) 0 )
1580 /* The queue was empty and no block time is specified (or
1581 * the block time has expired) so leave now. */
1582 taskEXIT_CRITICAL();
1584 traceQUEUE_RECEIVE_FAILED( pxQueue );
1585 traceRETURN_xQueueReceive( errQUEUE_EMPTY );
1587 return errQUEUE_EMPTY;
1589 else if( xEntryTimeSet == pdFALSE )
1591 /* The queue was empty and a block time was specified so
1592 * configure the timeout structure. */
1593 vTaskInternalSetTimeOutState( &xTimeOut );
1594 xEntryTimeSet = pdTRUE;
1598 /* Entry time was already set. */
1599 mtCOVERAGE_TEST_MARKER();
1603 taskEXIT_CRITICAL();
1605 /* Interrupts and other tasks can send to and receive from the queue
1606 * now the critical section has been exited. */
1609 prvLockQueue( pxQueue );
1611 /* Update the timeout state to see if it has expired yet. */
1612 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1614 /* The timeout has not expired. If the queue is still empty place
1615 * the task on the list of tasks waiting to receive from the queue. */
1616 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1618 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1619 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1620 prvUnlockQueue( pxQueue );
1622 if( xTaskResumeAll() == pdFALSE )
1624 taskYIELD_WITHIN_API();
1628 mtCOVERAGE_TEST_MARKER();
1633 /* The queue contains data again. Loop back to try and read the
1635 prvUnlockQueue( pxQueue );
1636 ( void ) xTaskResumeAll();
1641 /* Timed out. If there is no data in the queue exit, otherwise loop
1642 * back and attempt to read the data. */
1643 prvUnlockQueue( pxQueue );
1644 ( void ) xTaskResumeAll();
1646 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1648 traceQUEUE_RECEIVE_FAILED( pxQueue );
1649 traceRETURN_xQueueReceive( errQUEUE_EMPTY );
1651 return errQUEUE_EMPTY;
1655 mtCOVERAGE_TEST_MARKER();
1660 /*-----------------------------------------------------------*/
1662 BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
1663 TickType_t xTicksToWait )
1665 BaseType_t xEntryTimeSet = pdFALSE;
1667 Queue_t * const pxQueue = xQueue;
1669 #if ( configUSE_MUTEXES == 1 )
1670 BaseType_t xInheritanceOccurred = pdFALSE;
1673 traceENTER_xQueueSemaphoreTake( xQueue, xTicksToWait );
1675 /* Check the queue pointer is not NULL. */
1676 configASSERT( ( pxQueue ) );
1678 /* Check this really is a semaphore, in which case the item size will be
1680 configASSERT( pxQueue->uxItemSize == 0 );
1682 /* Cannot block if the scheduler is suspended. */
1683 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1685 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1691 taskENTER_CRITICAL();
1693 /* Semaphores are queues with an item size of 0, and where the
1694 * number of messages in the queue is the semaphore's count value. */
1695 const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;
1697 /* Is there data in the queue now? To be running the calling task
1698 * must be the highest priority task wanting to access the queue. */
1699 if( uxSemaphoreCount > ( UBaseType_t ) 0 )
1701 traceQUEUE_RECEIVE( pxQueue );
1703 /* Semaphores are queues with a data size of zero and where the
1704 * messages waiting is the semaphore's count. Reduce the count. */
1705 pxQueue->uxMessagesWaiting = ( UBaseType_t ) ( uxSemaphoreCount - ( UBaseType_t ) 1 );
1707 #if ( configUSE_MUTEXES == 1 )
1709 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1711 /* Record the information required to implement
1712 * priority inheritance should it become necessary. */
1713 pxQueue->u.xSemaphore.xMutexHolder = pvTaskIncrementMutexHeldCount();
1717 mtCOVERAGE_TEST_MARKER();
1720 #endif /* configUSE_MUTEXES */
1722 /* Check to see if other tasks are blocked waiting to give the
1723 * semaphore, and if so, unblock the highest priority such task. */
1724 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1726 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1728 queueYIELD_IF_USING_PREEMPTION();
1732 mtCOVERAGE_TEST_MARKER();
1737 mtCOVERAGE_TEST_MARKER();
1740 taskEXIT_CRITICAL();
1742 traceRETURN_xQueueSemaphoreTake( pdPASS );
1748 if( xTicksToWait == ( TickType_t ) 0 )
1750 /* The semaphore count was 0 and no block time is specified
1751 * (or the block time has expired) so exit now. */
1752 taskEXIT_CRITICAL();
1754 traceQUEUE_RECEIVE_FAILED( pxQueue );
1755 traceRETURN_xQueueSemaphoreTake( errQUEUE_EMPTY );
1757 return errQUEUE_EMPTY;
1759 else if( xEntryTimeSet == pdFALSE )
1761 /* The semaphore count was 0 and a block time was specified
1762 * so configure the timeout structure ready to block. */
1763 vTaskInternalSetTimeOutState( &xTimeOut );
1764 xEntryTimeSet = pdTRUE;
1768 /* Entry time was already set. */
1769 mtCOVERAGE_TEST_MARKER();
1773 taskEXIT_CRITICAL();
1775 /* Interrupts and other tasks can give to and take from the semaphore
1776 * now the critical section has been exited. */
1779 prvLockQueue( pxQueue );
1781 /* Update the timeout state to see if it has expired yet. */
1782 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1784 /* A block time is specified and not expired. If the semaphore
1785 * count is 0 then enter the Blocked state to wait for a semaphore to
1786 * become available. As semaphores are implemented with queues the
1787 * queue being empty is equivalent to the semaphore count being 0. */
1788 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1790 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1792 #if ( configUSE_MUTEXES == 1 )
1794 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1796 taskENTER_CRITICAL();
1798 xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );
1800 taskEXIT_CRITICAL();
1804 mtCOVERAGE_TEST_MARKER();
1807 #endif /* if ( configUSE_MUTEXES == 1 ) */
1809 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1810 prvUnlockQueue( pxQueue );
1812 if( xTaskResumeAll() == pdFALSE )
1814 taskYIELD_WITHIN_API();
1818 mtCOVERAGE_TEST_MARKER();
1823 /* There was no timeout and the semaphore count was not 0, so
1824 * attempt to take the semaphore again. */
1825 prvUnlockQueue( pxQueue );
1826 ( void ) xTaskResumeAll();
1832 prvUnlockQueue( pxQueue );
1833 ( void ) xTaskResumeAll();
1835 /* If the semaphore count is 0 exit now as the timeout has
1836 * expired. Otherwise return to attempt to take the semaphore that is
1837 * known to be available. As semaphores are implemented by queues the
1838 * queue being empty is equivalent to the semaphore count being 0. */
1839 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1841 #if ( configUSE_MUTEXES == 1 )
1843 /* xInheritanceOccurred could only have be set if
1844 * pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
1845 * test the mutex type again to check it is actually a mutex. */
1846 if( xInheritanceOccurred != pdFALSE )
1848 taskENTER_CRITICAL();
1850 UBaseType_t uxHighestWaitingPriority;
1852 /* This task blocking on the mutex caused another
1853 * task to inherit this task's priority. Now this task
1854 * has timed out the priority should be disinherited
1855 * again, but only as low as the next highest priority
1856 * task that is waiting for the same mutex. */
1857 uxHighestWaitingPriority = prvGetHighestPriorityOfWaitToReceiveList( pxQueue );
1859 /* vTaskPriorityDisinheritAfterTimeout uses the uxHighestWaitingPriority
1860 * parameter to index pxReadyTasksLists when adding the task holding
1861 * mutex to the ready list for its new priority. Coverity thinks that
1862 * it can result in out-of-bounds access which is not true because
1863 * uxHighestWaitingPriority, as returned by prvGetHighestPriorityOfWaitToReceiveList,
1864 * is capped at ( configMAX_PRIORITIES - 1 ). */
1865 /* coverity[overrun] */
1866 vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );
1868 taskEXIT_CRITICAL();
1871 #endif /* configUSE_MUTEXES */
1873 traceQUEUE_RECEIVE_FAILED( pxQueue );
1874 traceRETURN_xQueueSemaphoreTake( errQUEUE_EMPTY );
1876 return errQUEUE_EMPTY;
1880 mtCOVERAGE_TEST_MARKER();
1885 /*-----------------------------------------------------------*/
1887 BaseType_t xQueuePeek( QueueHandle_t xQueue,
1888 void * const pvBuffer,
1889 TickType_t xTicksToWait )
1891 BaseType_t xEntryTimeSet = pdFALSE;
1893 int8_t * pcOriginalReadPosition;
1894 Queue_t * const pxQueue = xQueue;
1896 traceENTER_xQueuePeek( xQueue, pvBuffer, xTicksToWait );
1898 /* Check the pointer is not NULL. */
1899 configASSERT( ( pxQueue ) );
1901 /* The buffer into which data is received can only be NULL if the data size
1902 * is zero (so no data is copied into the buffer. */
1903 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1905 /* Cannot block if the scheduler is suspended. */
1906 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1908 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1914 taskENTER_CRITICAL();
1916 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1918 /* Is there data in the queue now? To be running the calling task
1919 * must be the highest priority task wanting to access the queue. */
1920 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1922 /* Remember the read position so it can be reset after the data
1923 * is read from the queue as this function is only peeking the
1924 * data, not removing it. */
1925 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
1927 prvCopyDataFromQueue( pxQueue, pvBuffer );
1928 traceQUEUE_PEEK( pxQueue );
1930 /* The data is not being removed, so reset the read pointer. */
1931 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
1933 /* The data is being left in the queue, so see if there are
1934 * any other tasks waiting for the data. */
1935 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1937 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1939 /* The task waiting has a higher priority than this task. */
1940 queueYIELD_IF_USING_PREEMPTION();
1944 mtCOVERAGE_TEST_MARKER();
1949 mtCOVERAGE_TEST_MARKER();
1952 taskEXIT_CRITICAL();
1954 traceRETURN_xQueuePeek( pdPASS );
1960 if( xTicksToWait == ( TickType_t ) 0 )
1962 /* The queue was empty and no block time is specified (or
1963 * the block time has expired) so leave now. */
1964 taskEXIT_CRITICAL();
1966 traceQUEUE_PEEK_FAILED( pxQueue );
1967 traceRETURN_xQueuePeek( errQUEUE_EMPTY );
1969 return errQUEUE_EMPTY;
1971 else if( xEntryTimeSet == pdFALSE )
1973 /* The queue was empty and a block time was specified so
1974 * configure the timeout structure ready to enter the blocked
1976 vTaskInternalSetTimeOutState( &xTimeOut );
1977 xEntryTimeSet = pdTRUE;
1981 /* Entry time was already set. */
1982 mtCOVERAGE_TEST_MARKER();
1986 taskEXIT_CRITICAL();
1988 /* Interrupts and other tasks can send to and receive from the queue
1989 * now that the critical section has been exited. */
1992 prvLockQueue( pxQueue );
1994 /* Update the timeout state to see if it has expired yet. */
1995 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1997 /* Timeout has not expired yet, check to see if there is data in the
1998 * queue now, and if not enter the Blocked state to wait for data. */
1999 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
2001 traceBLOCKING_ON_QUEUE_PEEK( pxQueue );
2002 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
2003 prvUnlockQueue( pxQueue );
2005 if( xTaskResumeAll() == pdFALSE )
2007 taskYIELD_WITHIN_API();
2011 mtCOVERAGE_TEST_MARKER();
2016 /* There is data in the queue now, so don't enter the blocked
2017 * state, instead return to try and obtain the data. */
2018 prvUnlockQueue( pxQueue );
2019 ( void ) xTaskResumeAll();
2024 /* The timeout has expired. If there is still no data in the queue
2025 * exit, otherwise go back and try to read the data again. */
2026 prvUnlockQueue( pxQueue );
2027 ( void ) xTaskResumeAll();
2029 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
2031 traceQUEUE_PEEK_FAILED( pxQueue );
2032 traceRETURN_xQueuePeek( errQUEUE_EMPTY );
2034 return errQUEUE_EMPTY;
2038 mtCOVERAGE_TEST_MARKER();
2043 /*-----------------------------------------------------------*/
2045 BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,
2046 void * const pvBuffer,
2047 BaseType_t * const pxHigherPriorityTaskWoken )
2050 UBaseType_t uxSavedInterruptStatus;
2051 Queue_t * const pxQueue = xQueue;
2053 traceENTER_xQueueReceiveFromISR( xQueue, pvBuffer, pxHigherPriorityTaskWoken );
2055 configASSERT( pxQueue );
2056 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
2058 /* RTOS ports that support interrupt nesting have the concept of a maximum
2059 * system call (or maximum API call) interrupt priority. Interrupts that are
2060 * above the maximum system call priority are kept permanently enabled, even
2061 * when the RTOS kernel is in a critical section, but cannot make any calls to
2062 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
2063 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2064 * failure if a FreeRTOS API function is called from an interrupt that has been
2065 * assigned a priority above the configured maximum system call priority.
2066 * Only FreeRTOS functions that end in FromISR can be called from interrupts
2067 * that have been assigned a priority at or (logically) below the maximum
2068 * system call interrupt priority. FreeRTOS maintains a separate interrupt
2069 * safe API to ensure interrupt entry is as fast and as simple as possible.
2070 * More information (albeit Cortex-M specific) is provided on the following
2071 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2072 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2074 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2075 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2076 /* coverity[misra_c_2012_directive_4_7_violation] */
2077 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2079 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
2081 /* Cannot block in an ISR, so check there is data available. */
2082 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
2084 const int8_t cRxLock = pxQueue->cRxLock;
2086 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
2088 prvCopyDataFromQueue( pxQueue, pvBuffer );
2089 pxQueue->uxMessagesWaiting = ( UBaseType_t ) ( uxMessagesWaiting - ( UBaseType_t ) 1 );
2091 /* If the queue is locked the event list will not be modified.
2092 * Instead update the lock count so the task that unlocks the queue
2093 * will know that an ISR has removed data while the queue was
2095 if( cRxLock == queueUNLOCKED )
2097 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2099 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2101 /* The task waiting has a higher priority than us so
2102 * force a context switch. */
2103 if( pxHigherPriorityTaskWoken != NULL )
2105 *pxHigherPriorityTaskWoken = pdTRUE;
2109 mtCOVERAGE_TEST_MARKER();
2114 mtCOVERAGE_TEST_MARKER();
2119 mtCOVERAGE_TEST_MARKER();
2124 /* Increment the lock count so the task that unlocks the queue
2125 * knows that data was removed while it was locked. */
2126 prvIncrementQueueRxLock( pxQueue, cRxLock );
2134 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
2137 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2139 traceRETURN_xQueueReceiveFromISR( xReturn );
2143 /*-----------------------------------------------------------*/
2145 BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,
2146 void * const pvBuffer )
2149 UBaseType_t uxSavedInterruptStatus;
2150 int8_t * pcOriginalReadPosition;
2151 Queue_t * const pxQueue = xQueue;
2153 traceENTER_xQueuePeekFromISR( xQueue, pvBuffer );
2155 configASSERT( pxQueue );
2156 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
2157 configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
2159 /* RTOS ports that support interrupt nesting have the concept of a maximum
2160 * system call (or maximum API call) interrupt priority. Interrupts that are
2161 * above the maximum system call priority are kept permanently enabled, even
2162 * when the RTOS kernel is in a critical section, but cannot make any calls to
2163 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
2164 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2165 * failure if a FreeRTOS API function is called from an interrupt that has been
2166 * assigned a priority above the configured maximum system call priority.
2167 * Only FreeRTOS functions that end in FromISR can be called from interrupts
2168 * that have been assigned a priority at or (logically) below the maximum
2169 * system call interrupt priority. FreeRTOS maintains a separate interrupt
2170 * safe API to ensure interrupt entry is as fast and as simple as possible.
2171 * More information (albeit Cortex-M specific) is provided on the following
2172 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2173 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2175 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2176 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2177 /* coverity[misra_c_2012_directive_4_7_violation] */
2178 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2180 /* Cannot block in an ISR, so check there is data available. */
2181 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2183 traceQUEUE_PEEK_FROM_ISR( pxQueue );
2185 /* Remember the read position so it can be reset as nothing is
2186 * actually being removed from the queue. */
2187 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
2188 prvCopyDataFromQueue( pxQueue, pvBuffer );
2189 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
2196 traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
2199 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2201 traceRETURN_xQueuePeekFromISR( xReturn );
2205 /*-----------------------------------------------------------*/
2207 UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
2209 UBaseType_t uxReturn;
2211 traceENTER_uxQueueMessagesWaiting( xQueue );
2213 configASSERT( xQueue );
2215 portBASE_TYPE_ENTER_CRITICAL();
2217 uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
2219 portBASE_TYPE_EXIT_CRITICAL();
2221 traceRETURN_uxQueueMessagesWaiting( uxReturn );
2225 /*-----------------------------------------------------------*/
2227 UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
2229 UBaseType_t uxReturn;
2230 Queue_t * const pxQueue = xQueue;
2232 traceENTER_uxQueueSpacesAvailable( xQueue );
2234 configASSERT( pxQueue );
2236 portBASE_TYPE_ENTER_CRITICAL();
2238 uxReturn = ( UBaseType_t ) ( pxQueue->uxLength - pxQueue->uxMessagesWaiting );
2240 portBASE_TYPE_EXIT_CRITICAL();
2242 traceRETURN_uxQueueSpacesAvailable( uxReturn );
2246 /*-----------------------------------------------------------*/
2248 UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
2250 UBaseType_t uxReturn;
2251 Queue_t * const pxQueue = xQueue;
2253 traceENTER_uxQueueMessagesWaitingFromISR( xQueue );
2255 configASSERT( pxQueue );
2256 uxReturn = pxQueue->uxMessagesWaiting;
2258 traceRETURN_uxQueueMessagesWaitingFromISR( uxReturn );
2262 /*-----------------------------------------------------------*/
2264 void vQueueDelete( QueueHandle_t xQueue )
2266 Queue_t * const pxQueue = xQueue;
2268 traceENTER_vQueueDelete( xQueue );
2270 configASSERT( pxQueue );
2271 traceQUEUE_DELETE( pxQueue );
2273 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2275 vQueueUnregisterQueue( pxQueue );
2279 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
2281 /* The queue can only have been allocated dynamically - free it
2283 vPortFree( pxQueue );
2285 #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
2287 /* The queue could have been allocated statically or dynamically, so
2288 * check before attempting to free the memory. */
2289 if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
2291 vPortFree( pxQueue );
2295 mtCOVERAGE_TEST_MARKER();
2298 #else /* if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) ) */
2300 /* The queue must have been statically allocated, so is not going to be
2301 * deleted. Avoid compiler warnings about the unused parameter. */
2304 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
2306 traceRETURN_vQueueDelete();
2308 /*-----------------------------------------------------------*/
2310 #if ( configUSE_TRACE_FACILITY == 1 )
2312 UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
2314 traceENTER_uxQueueGetQueueNumber( xQueue );
2316 traceRETURN_uxQueueGetQueueNumber( ( ( Queue_t * ) xQueue )->uxQueueNumber );
2318 return ( ( Queue_t * ) xQueue )->uxQueueNumber;
2321 #endif /* configUSE_TRACE_FACILITY */
2322 /*-----------------------------------------------------------*/
2324 #if ( configUSE_TRACE_FACILITY == 1 )
2326 void vQueueSetQueueNumber( QueueHandle_t xQueue,
2327 UBaseType_t uxQueueNumber )
2329 traceENTER_vQueueSetQueueNumber( xQueue, uxQueueNumber );
2331 ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
2333 traceRETURN_vQueueSetQueueNumber();
2336 #endif /* configUSE_TRACE_FACILITY */
2337 /*-----------------------------------------------------------*/
2339 #if ( configUSE_TRACE_FACILITY == 1 )
2341 uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
2343 traceENTER_ucQueueGetQueueType( xQueue );
2345 traceRETURN_ucQueueGetQueueType( ( ( Queue_t * ) xQueue )->ucQueueType );
2347 return ( ( Queue_t * ) xQueue )->ucQueueType;
2350 #endif /* configUSE_TRACE_FACILITY */
2351 /*-----------------------------------------------------------*/
2353 UBaseType_t uxQueueGetQueueItemSize( QueueHandle_t xQueue ) /* PRIVILEGED_FUNCTION */
2355 traceENTER_uxQueueGetQueueItemSize( xQueue );
2357 traceRETURN_uxQueueGetQueueItemSize( ( ( Queue_t * ) xQueue )->uxItemSize );
2359 return ( ( Queue_t * ) xQueue )->uxItemSize;
2361 /*-----------------------------------------------------------*/
2363 UBaseType_t uxQueueGetQueueLength( QueueHandle_t xQueue ) /* PRIVILEGED_FUNCTION */
2365 traceENTER_uxQueueGetQueueLength( xQueue );
2367 traceRETURN_uxQueueGetQueueLength( ( ( Queue_t * ) xQueue )->uxLength );
2369 return ( ( Queue_t * ) xQueue )->uxLength;
2371 /*-----------------------------------------------------------*/
2373 #if ( configUSE_MUTEXES == 1 )
2375 static UBaseType_t prvGetHighestPriorityOfWaitToReceiveList( const Queue_t * const pxQueue )
2377 UBaseType_t uxHighestPriorityOfWaitingTasks;
2379 /* If a task waiting for a mutex causes the mutex holder to inherit a
2380 * priority, but the waiting task times out, then the holder should
2381 * disinherit the priority - but only down to the highest priority of any
2382 * other tasks that are waiting for the same mutex. For this purpose,
2383 * return the priority of the highest priority task that is waiting for the
2385 if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0U )
2387 uxHighestPriorityOfWaitingTasks = ( UBaseType_t ) ( ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) ) );
2391 uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;
2394 return uxHighestPriorityOfWaitingTasks;
2397 #endif /* configUSE_MUTEXES */
2398 /*-----------------------------------------------------------*/
2400 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
2401 const void * pvItemToQueue,
2402 const BaseType_t xPosition )
2404 BaseType_t xReturn = pdFALSE;
2405 UBaseType_t uxMessagesWaiting;
2407 /* This function is called from a critical section. */
2409 uxMessagesWaiting = pxQueue->uxMessagesWaiting;
2411 if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
2413 #if ( configUSE_MUTEXES == 1 )
2415 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
2417 /* The mutex is no longer being held. */
2418 xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
2419 pxQueue->u.xSemaphore.xMutexHolder = NULL;
2423 mtCOVERAGE_TEST_MARKER();
2426 #endif /* configUSE_MUTEXES */
2428 else if( xPosition == queueSEND_TO_BACK )
2430 ( void ) memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );
2431 pxQueue->pcWriteTo += pxQueue->uxItemSize;
2433 if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail )
2435 pxQueue->pcWriteTo = pxQueue->pcHead;
2439 mtCOVERAGE_TEST_MARKER();
2444 ( void ) memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize );
2445 pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
2447 if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead )
2449 pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
2453 mtCOVERAGE_TEST_MARKER();
2456 if( xPosition == queueOVERWRITE )
2458 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
2460 /* An item is not being added but overwritten, so subtract
2461 * one from the recorded number of items in the queue so when
2462 * one is added again below the number of recorded items remains
2464 --uxMessagesWaiting;
2468 mtCOVERAGE_TEST_MARKER();
2473 mtCOVERAGE_TEST_MARKER();
2477 pxQueue->uxMessagesWaiting = ( UBaseType_t ) ( uxMessagesWaiting + ( UBaseType_t ) 1 );
2481 /*-----------------------------------------------------------*/
2483 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
2484 void * const pvBuffer )
2486 if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
2488 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2490 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2492 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2496 mtCOVERAGE_TEST_MARKER();
2499 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( size_t ) pxQueue->uxItemSize );
2502 /*-----------------------------------------------------------*/
2504 static void prvUnlockQueue( Queue_t * const pxQueue )
2506 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
2508 /* The lock counts contains the number of extra data items placed or
2509 * removed from the queue while the queue was locked. When a queue is
2510 * locked items can be added or removed, but the event lists cannot be
2512 taskENTER_CRITICAL();
2514 int8_t cTxLock = pxQueue->cTxLock;
2516 /* See if data was added to the queue while it was locked. */
2517 while( cTxLock > queueLOCKED_UNMODIFIED )
2519 /* Data was posted while the queue was locked. Are any tasks
2520 * blocked waiting for data to become available? */
2521 #if ( configUSE_QUEUE_SETS == 1 )
2523 if( pxQueue->pxQueueSetContainer != NULL )
2525 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
2527 /* The queue is a member of a queue set, and posting to
2528 * the queue set caused a higher priority task to unblock.
2529 * A context switch is required. */
2534 mtCOVERAGE_TEST_MARKER();
2539 /* Tasks that are removed from the event list will get
2540 * added to the pending ready list as the scheduler is still
2542 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2544 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2546 /* The task waiting has a higher priority so record that a
2547 * context switch is required. */
2552 mtCOVERAGE_TEST_MARKER();
2561 #else /* configUSE_QUEUE_SETS */
2563 /* Tasks that are removed from the event list will get added to
2564 * the pending ready list as the scheduler is still suspended. */
2565 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2567 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2569 /* The task waiting has a higher priority so record that
2570 * a context switch is required. */
2575 mtCOVERAGE_TEST_MARKER();
2583 #endif /* configUSE_QUEUE_SETS */
2588 pxQueue->cTxLock = queueUNLOCKED;
2590 taskEXIT_CRITICAL();
2592 /* Do the same for the Rx lock. */
2593 taskENTER_CRITICAL();
2595 int8_t cRxLock = pxQueue->cRxLock;
2597 while( cRxLock > queueLOCKED_UNMODIFIED )
2599 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2601 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2607 mtCOVERAGE_TEST_MARKER();
2618 pxQueue->cRxLock = queueUNLOCKED;
2620 taskEXIT_CRITICAL();
2622 /*-----------------------------------------------------------*/
2624 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue )
2628 taskENTER_CRITICAL();
2630 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2639 taskEXIT_CRITICAL();
2643 /*-----------------------------------------------------------*/
2645 BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )
2648 Queue_t * const pxQueue = xQueue;
2650 traceENTER_xQueueIsQueueEmptyFromISR( xQueue );
2652 configASSERT( pxQueue );
2654 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2663 traceRETURN_xQueueIsQueueEmptyFromISR( xReturn );
2667 /*-----------------------------------------------------------*/
2669 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue )
2673 taskENTER_CRITICAL();
2675 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2684 taskEXIT_CRITICAL();
2688 /*-----------------------------------------------------------*/
2690 BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )
2693 Queue_t * const pxQueue = xQueue;
2695 traceENTER_xQueueIsQueueFullFromISR( xQueue );
2697 configASSERT( pxQueue );
2699 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2708 traceRETURN_xQueueIsQueueFullFromISR( xReturn );
2712 /*-----------------------------------------------------------*/
2714 #if ( configUSE_CO_ROUTINES == 1 )
2716 BaseType_t xQueueCRSend( QueueHandle_t xQueue,
2717 const void * pvItemToQueue,
2718 TickType_t xTicksToWait )
2721 Queue_t * const pxQueue = xQueue;
2723 traceENTER_xQueueCRSend( xQueue, pvItemToQueue, xTicksToWait );
2725 /* If the queue is already full we may have to block. A critical section
2726 * is required to prevent an interrupt removing something from the queue
2727 * between the check to see if the queue is full and blocking on the queue. */
2728 portDISABLE_INTERRUPTS();
2730 if( prvIsQueueFull( pxQueue ) != pdFALSE )
2732 /* The queue is full - do we want to block or just leave without
2734 if( xTicksToWait > ( TickType_t ) 0 )
2736 /* As this is called from a coroutine we cannot block directly, but
2737 * return indicating that we need to block. */
2738 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
2739 portENABLE_INTERRUPTS();
2740 return errQUEUE_BLOCKED;
2744 portENABLE_INTERRUPTS();
2745 return errQUEUE_FULL;
2749 portENABLE_INTERRUPTS();
2751 portDISABLE_INTERRUPTS();
2753 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2755 /* There is room in the queue, copy the data into the queue. */
2756 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2759 /* Were any co-routines waiting for data to become available? */
2760 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2762 /* In this instance the co-routine could be placed directly
2763 * into the ready list as we are within a critical section.
2764 * Instead the same pending ready list mechanism is used as if
2765 * the event were caused from within an interrupt. */
2766 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2768 /* The co-routine waiting has a higher priority so record
2769 * that a yield might be appropriate. */
2770 xReturn = errQUEUE_YIELD;
2774 mtCOVERAGE_TEST_MARKER();
2779 mtCOVERAGE_TEST_MARKER();
2784 xReturn = errQUEUE_FULL;
2787 portENABLE_INTERRUPTS();
2789 traceRETURN_xQueueCRSend( xReturn );
2794 #endif /* configUSE_CO_ROUTINES */
2795 /*-----------------------------------------------------------*/
2797 #if ( configUSE_CO_ROUTINES == 1 )
2799 BaseType_t xQueueCRReceive( QueueHandle_t xQueue,
2801 TickType_t xTicksToWait )
2804 Queue_t * const pxQueue = xQueue;
2806 traceENTER_xQueueCRReceive( xQueue, pvBuffer, xTicksToWait );
2808 /* If the queue is already empty we may have to block. A critical section
2809 * is required to prevent an interrupt adding something to the queue
2810 * between the check to see if the queue is empty and blocking on the queue. */
2811 portDISABLE_INTERRUPTS();
2813 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2815 /* There are no messages in the queue, do we want to block or just
2816 * leave with nothing? */
2817 if( xTicksToWait > ( TickType_t ) 0 )
2819 /* As this is a co-routine we cannot block directly, but return
2820 * indicating that we need to block. */
2821 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
2822 portENABLE_INTERRUPTS();
2823 return errQUEUE_BLOCKED;
2827 portENABLE_INTERRUPTS();
2828 return errQUEUE_FULL;
2833 mtCOVERAGE_TEST_MARKER();
2836 portENABLE_INTERRUPTS();
2838 portDISABLE_INTERRUPTS();
2840 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2842 /* Data is available from the queue. */
2843 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2845 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2847 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2851 mtCOVERAGE_TEST_MARKER();
2854 --( pxQueue->uxMessagesWaiting );
2855 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2859 /* Were any co-routines waiting for space to become available? */
2860 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2862 /* In this instance the co-routine could be placed directly
2863 * into the ready list as we are within a critical section.
2864 * Instead the same pending ready list mechanism is used as if
2865 * the event were caused from within an interrupt. */
2866 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2868 xReturn = errQUEUE_YIELD;
2872 mtCOVERAGE_TEST_MARKER();
2877 mtCOVERAGE_TEST_MARKER();
2885 portENABLE_INTERRUPTS();
2887 traceRETURN_xQueueCRReceive( xReturn );
2892 #endif /* configUSE_CO_ROUTINES */
2893 /*-----------------------------------------------------------*/
2895 #if ( configUSE_CO_ROUTINES == 1 )
2897 BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue,
2898 const void * pvItemToQueue,
2899 BaseType_t xCoRoutinePreviouslyWoken )
2901 Queue_t * const pxQueue = xQueue;
2903 traceENTER_xQueueCRSendFromISR( xQueue, pvItemToQueue, xCoRoutinePreviouslyWoken );
2905 /* Cannot block within an ISR so if there is no space on the queue then
2906 * exit without doing anything. */
2907 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2909 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2911 /* We only want to wake one co-routine per ISR, so check that a
2912 * co-routine has not already been woken. */
2913 if( xCoRoutinePreviouslyWoken == pdFALSE )
2915 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2917 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2923 mtCOVERAGE_TEST_MARKER();
2928 mtCOVERAGE_TEST_MARKER();
2933 mtCOVERAGE_TEST_MARKER();
2938 mtCOVERAGE_TEST_MARKER();
2941 traceRETURN_xQueueCRSendFromISR( xCoRoutinePreviouslyWoken );
2943 return xCoRoutinePreviouslyWoken;
2946 #endif /* configUSE_CO_ROUTINES */
2947 /*-----------------------------------------------------------*/
2949 #if ( configUSE_CO_ROUTINES == 1 )
2951 BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue,
2953 BaseType_t * pxCoRoutineWoken )
2956 Queue_t * const pxQueue = xQueue;
2958 traceENTER_xQueueCRReceiveFromISR( xQueue, pvBuffer, pxCoRoutineWoken );
2960 /* We cannot block from an ISR, so check there is data available. If
2961 * not then just leave without doing anything. */
2962 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2964 /* Copy the data from the queue. */
2965 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2967 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2969 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2973 mtCOVERAGE_TEST_MARKER();
2976 --( pxQueue->uxMessagesWaiting );
2977 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2979 if( ( *pxCoRoutineWoken ) == pdFALSE )
2981 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2983 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2985 *pxCoRoutineWoken = pdTRUE;
2989 mtCOVERAGE_TEST_MARKER();
2994 mtCOVERAGE_TEST_MARKER();
2999 mtCOVERAGE_TEST_MARKER();
3009 traceRETURN_xQueueCRReceiveFromISR( xReturn );
3014 #endif /* configUSE_CO_ROUTINES */
3015 /*-----------------------------------------------------------*/
3017 #if ( configQUEUE_REGISTRY_SIZE > 0 )
3019 void vQueueAddToRegistry( QueueHandle_t xQueue,
3020 const char * pcQueueName )
3023 QueueRegistryItem_t * pxEntryToWrite = NULL;
3025 traceENTER_vQueueAddToRegistry( xQueue, pcQueueName );
3027 configASSERT( xQueue );
3029 if( pcQueueName != NULL )
3031 /* See if there is an empty space in the registry. A NULL name denotes
3033 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
3035 /* Replace an existing entry if the queue is already in the registry. */
3036 if( xQueue == xQueueRegistry[ ux ].xHandle )
3038 pxEntryToWrite = &( xQueueRegistry[ ux ] );
3041 /* Otherwise, store in the next empty location */
3042 else if( ( pxEntryToWrite == NULL ) && ( xQueueRegistry[ ux ].pcQueueName == NULL ) )
3044 pxEntryToWrite = &( xQueueRegistry[ ux ] );
3048 mtCOVERAGE_TEST_MARKER();
3053 if( pxEntryToWrite != NULL )
3055 /* Store the information on this queue. */
3056 pxEntryToWrite->pcQueueName = pcQueueName;
3057 pxEntryToWrite->xHandle = xQueue;
3059 traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
3062 traceRETURN_vQueueAddToRegistry();
3065 #endif /* configQUEUE_REGISTRY_SIZE */
3066 /*-----------------------------------------------------------*/
3068 #if ( configQUEUE_REGISTRY_SIZE > 0 )
3070 const char * pcQueueGetName( QueueHandle_t xQueue )
3073 const char * pcReturn = NULL;
3075 traceENTER_pcQueueGetName( xQueue );
3077 configASSERT( xQueue );
3079 /* Note there is nothing here to protect against another task adding or
3080 * removing entries from the registry while it is being searched. */
3082 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
3084 if( xQueueRegistry[ ux ].xHandle == xQueue )
3086 pcReturn = xQueueRegistry[ ux ].pcQueueName;
3091 mtCOVERAGE_TEST_MARKER();
3095 traceRETURN_pcQueueGetName( pcReturn );
3100 #endif /* configQUEUE_REGISTRY_SIZE */
3101 /*-----------------------------------------------------------*/
3103 #if ( configQUEUE_REGISTRY_SIZE > 0 )
3105 void vQueueUnregisterQueue( QueueHandle_t xQueue )
3109 traceENTER_vQueueUnregisterQueue( xQueue );
3111 configASSERT( xQueue );
3113 /* See if the handle of the queue being unregistered in actually in the
3115 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
3117 if( xQueueRegistry[ ux ].xHandle == xQueue )
3119 /* Set the name to NULL to show that this slot if free again. */
3120 xQueueRegistry[ ux ].pcQueueName = NULL;
3122 /* Set the handle to NULL to ensure the same queue handle cannot
3123 * appear in the registry twice if it is added, removed, then
3125 xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;
3130 mtCOVERAGE_TEST_MARKER();
3134 traceRETURN_vQueueUnregisterQueue();
3137 #endif /* configQUEUE_REGISTRY_SIZE */
3138 /*-----------------------------------------------------------*/
3140 #if ( configUSE_TIMERS == 1 )
3142 void vQueueWaitForMessageRestricted( QueueHandle_t xQueue,
3143 TickType_t xTicksToWait,
3144 const BaseType_t xWaitIndefinitely )
3146 Queue_t * const pxQueue = xQueue;
3148 traceENTER_vQueueWaitForMessageRestricted( xQueue, xTicksToWait, xWaitIndefinitely );
3150 /* This function should not be called by application code hence the
3151 * 'Restricted' in its name. It is not part of the public API. It is
3152 * designed for use by kernel code, and has special calling requirements.
3153 * It can result in vListInsert() being called on a list that can only
3154 * possibly ever have one item in it, so the list will be fast, but even
3155 * so it should be called with the scheduler locked and not from a critical
3158 /* Only do anything if there are no messages in the queue. This function
3159 * will not actually cause the task to block, just place it on a blocked
3160 * list. It will not block until the scheduler is unlocked - at which
3161 * time a yield will be performed. If an item is added to the queue while
3162 * the queue is locked, and the calling task blocks on the queue, then the
3163 * calling task will be immediately unblocked when the queue is unlocked. */
3164 prvLockQueue( pxQueue );
3166 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
3168 /* There is nothing in the queue, block for the specified period. */
3169 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );
3173 mtCOVERAGE_TEST_MARKER();
3176 prvUnlockQueue( pxQueue );
3178 traceRETURN_vQueueWaitForMessageRestricted();
3181 #endif /* configUSE_TIMERS */
3182 /*-----------------------------------------------------------*/
3184 #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
3186 QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
3188 QueueSetHandle_t pxQueue;
3190 traceENTER_xQueueCreateSet( uxEventQueueLength );
3192 pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
3194 traceRETURN_xQueueCreateSet( pxQueue );
3199 #endif /* #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
3200 /*-----------------------------------------------------------*/
3202 #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
3204 QueueSetHandle_t xQueueCreateSetStatic( const UBaseType_t uxEventQueueLength,
3205 uint8_t * pucQueueStorage,
3206 StaticQueue_t * pxStaticQueue )
3208 QueueSetHandle_t pxQueue;
3210 traceENTER_xQueueCreateSetStatic( uxEventQueueLength );
3212 pxQueue = xQueueGenericCreateStatic( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), pucQueueStorage, pxStaticQueue, queueQUEUE_TYPE_SET );
3214 traceRETURN_xQueueCreateSetStatic( pxQueue );
3219 #endif /* #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
3220 /*-----------------------------------------------------------*/
3222 #if ( configUSE_QUEUE_SETS == 1 )
3224 BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore,
3225 QueueSetHandle_t xQueueSet )
3229 traceENTER_xQueueAddToSet( xQueueOrSemaphore, xQueueSet );
3231 taskENTER_CRITICAL();
3233 if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
3235 /* Cannot add a queue/semaphore to more than one queue set. */
3238 else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
3240 /* Cannot add a queue/semaphore to a queue set if there are already
3241 * items in the queue/semaphore. */
3246 ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
3250 taskEXIT_CRITICAL();
3252 traceRETURN_xQueueAddToSet( xReturn );
3257 #endif /* configUSE_QUEUE_SETS */
3258 /*-----------------------------------------------------------*/
3260 #if ( configUSE_QUEUE_SETS == 1 )
3262 BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore,
3263 QueueSetHandle_t xQueueSet )
3266 Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
3268 traceENTER_xQueueRemoveFromSet( xQueueOrSemaphore, xQueueSet );
3270 if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
3272 /* The queue was not a member of the set. */
3275 else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
3277 /* It is dangerous to remove a queue from a set when the queue is
3278 * not empty because the queue set will still hold pending events for
3284 taskENTER_CRITICAL();
3286 /* The queue is no longer contained in the set. */
3287 pxQueueOrSemaphore->pxQueueSetContainer = NULL;
3289 taskEXIT_CRITICAL();
3293 traceRETURN_xQueueRemoveFromSet( xReturn );
3298 #endif /* configUSE_QUEUE_SETS */
3299 /*-----------------------------------------------------------*/
3301 #if ( configUSE_QUEUE_SETS == 1 )
3303 QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet,
3304 TickType_t const xTicksToWait )
3306 QueueSetMemberHandle_t xReturn = NULL;
3308 traceENTER_xQueueSelectFromSet( xQueueSet, xTicksToWait );
3310 ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait );
3312 traceRETURN_xQueueSelectFromSet( xReturn );
3317 #endif /* configUSE_QUEUE_SETS */
3318 /*-----------------------------------------------------------*/
3320 #if ( configUSE_QUEUE_SETS == 1 )
3322 QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
3324 QueueSetMemberHandle_t xReturn = NULL;
3326 traceENTER_xQueueSelectFromSetFromISR( xQueueSet );
3328 ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL );
3330 traceRETURN_xQueueSelectFromSetFromISR( xReturn );
3335 #endif /* configUSE_QUEUE_SETS */
3336 /*-----------------------------------------------------------*/
3338 #if ( configUSE_QUEUE_SETS == 1 )
3340 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue )
3342 Queue_t * pxQueueSetContainer = pxQueue->pxQueueSetContainer;
3343 BaseType_t xReturn = pdFALSE;
3345 /* This function must be called form a critical section. */
3347 /* The following line is not reachable in unit tests because every call
3348 * to prvNotifyQueueSetContainer is preceded by a check that
3349 * pxQueueSetContainer != NULL */
3350 configASSERT( pxQueueSetContainer ); /* LCOV_EXCL_BR_LINE */
3351 configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
3353 if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
3355 const int8_t cTxLock = pxQueueSetContainer->cTxLock;
3357 traceQUEUE_SET_SEND( pxQueueSetContainer );
3359 /* The data copied is the handle of the queue that contains data. */
3360 xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, queueSEND_TO_BACK );
3362 if( cTxLock == queueUNLOCKED )
3364 if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
3366 if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
3368 /* The task waiting has a higher priority. */
3373 mtCOVERAGE_TEST_MARKER();
3378 mtCOVERAGE_TEST_MARKER();
3383 prvIncrementQueueTxLock( pxQueueSetContainer, cTxLock );
3388 mtCOVERAGE_TEST_MARKER();
3394 #endif /* configUSE_QUEUE_SETS */