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 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
46 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
47 * for the header files above, but not in this file, in order to generate the
48 * correct privileged Vs unprivileged linkage and placement. */
49 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
52 /* Constants used with the cRxLock and cTxLock structure members. */
53 #define queueUNLOCKED ( ( int8_t ) -1 )
54 #define queueLOCKED_UNMODIFIED ( ( int8_t ) 0 )
55 #define queueINT8_MAX ( ( int8_t ) 127 )
57 /* When the Queue_t structure is used to represent a base queue its pcHead and
58 * pcTail members are used as pointers into the queue storage area. When the
59 * Queue_t structure is used to represent a mutex pcHead and pcTail pointers are
60 * not necessary, and the pcHead pointer is set to NULL to indicate that the
61 * structure instead holds a pointer to the mutex holder (if any). Map alternative
62 * names to the pcHead and structure member to ensure the readability of the code
63 * is maintained. The QueuePointers_t and SemaphoreData_t types are used to form
64 * a union as their usage is mutually exclusive dependent on what the queue is
66 #define uxQueueType pcHead
67 #define queueQUEUE_IS_MUTEX NULL
69 typedef struct QueuePointers
71 int8_t * pcTail; /**< Points to the byte at the end of the queue storage area. Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
72 int8_t * pcReadFrom; /**< Points to the last place that a queued item was read from when the structure is used as a queue. */
75 typedef struct SemaphoreData
77 TaskHandle_t xMutexHolder; /**< The handle of the task that holds the mutex. */
78 UBaseType_t uxRecursiveCallCount; /**< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */
81 /* Semaphores do not actually store or copy data, so have an item size of
83 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )
84 #define queueMUTEX_GIVE_BLOCK_TIME ( ( TickType_t ) 0U )
86 #if ( configUSE_PREEMPTION == 0 )
88 /* If the cooperative scheduler is being used then a yield should not be
89 * performed just because a higher priority task has been woken. */
90 #define queueYIELD_IF_USING_PREEMPTION()
92 #define queueYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
96 * Definition of the queue used by the scheduler.
97 * Items are queued by copy, not reference. See the following link for the
98 * rationale: https://www.FreeRTOS.org/Embedded-RTOS-Queues.html
100 typedef struct QueueDefinition /* The old naming convention is used to prevent breaking kernel aware debuggers. */
102 int8_t * pcHead; /**< Points to the beginning of the queue storage area. */
103 int8_t * pcWriteTo; /**< Points to the free next place in the storage area. */
107 QueuePointers_t xQueue; /**< Data required exclusively when this structure is used as a queue. */
108 SemaphoreData_t xSemaphore; /**< Data required exclusively when this structure is used as a semaphore. */
111 List_t xTasksWaitingToSend; /**< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
112 List_t xTasksWaitingToReceive; /**< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
114 volatile UBaseType_t uxMessagesWaiting; /**< The number of items currently in the queue. */
115 UBaseType_t uxLength; /**< The length of the queue defined as the number of items it will hold, not the number of bytes. */
116 UBaseType_t uxItemSize; /**< The size of each items that the queue will hold. */
118 volatile int8_t cRxLock; /**< Stores the number of items received from the queue (removed from the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
119 volatile int8_t cTxLock; /**< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
121 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
122 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */
125 #if ( configUSE_QUEUE_SETS == 1 )
126 struct QueueDefinition * pxQueueSetContainer;
129 #if ( configUSE_TRACE_FACILITY == 1 )
130 UBaseType_t uxQueueNumber;
135 /* The old xQUEUE name is maintained above then typedefed to the new Queue_t
136 * name below to enable the use of older kernel aware debuggers. */
137 typedef xQUEUE Queue_t;
139 /*-----------------------------------------------------------*/
142 * The queue registry is just a means for kernel aware debuggers to locate
143 * queue structures. It has no other purpose so is an optional component.
145 #if ( configQUEUE_REGISTRY_SIZE > 0 )
147 /* The type stored within the queue registry array. This allows a name
148 * to be assigned to each queue making kernel aware debugging a little
149 * more user friendly. */
150 typedef struct QUEUE_REGISTRY_ITEM
152 const char * pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
153 QueueHandle_t xHandle;
154 } xQueueRegistryItem;
156 /* The old xQueueRegistryItem name is maintained above then typedefed to the
157 * new xQueueRegistryItem name below to enable the use of older kernel aware
159 typedef xQueueRegistryItem QueueRegistryItem_t;
161 /* The queue registry is simply an array of QueueRegistryItem_t structures.
162 * The pcQueueName member of a structure being NULL is indicative of the
163 * array position being vacant. */
164 PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
166 #endif /* configQUEUE_REGISTRY_SIZE */
169 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
170 * prevent an ISR from adding or removing items to the queue, but does prevent
171 * an ISR from removing tasks from the queue event lists. If an ISR finds a
172 * queue is locked it will instead increment the appropriate queue lock count
173 * to indicate that a task may require unblocking. When the queue in unlocked
174 * these lock counts are inspected, and the appropriate action taken.
176 static void prvUnlockQueue( Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
179 * Uses a critical section to determine if there is any data in a queue.
181 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
183 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
186 * Uses a critical section to determine if there is any space in a queue.
188 * @return pdTRUE if there is no space, otherwise pdFALSE;
190 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
193 * Copies an item into the queue, either at the front of the queue or the
196 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
197 const void * pvItemToQueue,
198 const BaseType_t xPosition ) PRIVILEGED_FUNCTION;
201 * Copies an item out of a queue.
203 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
204 void * const pvBuffer ) PRIVILEGED_FUNCTION;
206 #if ( configUSE_QUEUE_SETS == 1 )
209 * Checks to see if a queue is a member of a queue set, and if so, notifies
210 * the queue set that the queue contains data.
212 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
216 * Called after a Queue_t structure has been allocated either statically or
217 * dynamically to fill in the structure's members.
219 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
220 const UBaseType_t uxItemSize,
221 uint8_t * pucQueueStorage,
222 const uint8_t ucQueueType,
223 Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
226 * Mutexes are a special type of queue. When a mutex is created, first the
227 * queue is created, then prvInitialiseMutex() is called to configure the queue
230 #if ( configUSE_MUTEXES == 1 )
231 static void prvInitialiseMutex( Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
234 #if ( configUSE_MUTEXES == 1 )
237 * If a task waiting for a mutex causes the mutex holder to inherit a
238 * priority, but the waiting task times out, then the holder should
239 * disinherit the priority - but only down to the highest priority of any
240 * other tasks that are waiting for the same mutex. This function returns
243 static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
245 /*-----------------------------------------------------------*/
248 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
249 * accessing the queue event lists.
251 #define prvLockQueue( pxQueue ) \
252 taskENTER_CRITICAL(); \
254 if( ( pxQueue )->cRxLock == queueUNLOCKED ) \
256 ( pxQueue )->cRxLock = queueLOCKED_UNMODIFIED; \
258 if( ( pxQueue )->cTxLock == queueUNLOCKED ) \
260 ( pxQueue )->cTxLock = queueLOCKED_UNMODIFIED; \
266 * Macro to increment cTxLock member of the queue data structure. It is
267 * capped at the number of tasks in the system as we cannot unblock more
268 * tasks than the number of tasks in the system.
270 #define prvIncrementQueueTxLock( pxQueue, cTxLock ) \
272 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
273 if( ( UBaseType_t ) ( cTxLock ) < uxNumberOfTasks ) \
275 configASSERT( ( cTxLock ) != queueINT8_MAX ); \
276 ( pxQueue )->cTxLock = ( int8_t ) ( ( cTxLock ) + ( int8_t ) 1 ); \
281 * Macro to increment cRxLock member of the queue data structure. It is
282 * capped at the number of tasks in the system as we cannot unblock more
283 * tasks than the number of tasks in the system.
285 #define prvIncrementQueueRxLock( pxQueue, cRxLock ) \
287 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
288 if( ( UBaseType_t ) ( cRxLock ) < uxNumberOfTasks ) \
290 configASSERT( ( cRxLock ) != queueINT8_MAX ); \
291 ( pxQueue )->cRxLock = ( int8_t ) ( ( cRxLock ) + ( int8_t ) 1 ); \
294 /*-----------------------------------------------------------*/
296 BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
297 BaseType_t xNewQueue )
299 BaseType_t xReturn = pdPASS;
300 Queue_t * const pxQueue = xQueue;
302 configASSERT( pxQueue );
304 if( ( pxQueue != NULL ) &&
305 ( pxQueue->uxLength >= 1U ) &&
306 /* Check for multiplication overflow. */
307 ( ( SIZE_MAX / pxQueue->uxLength ) >= pxQueue->uxItemSize ) )
309 taskENTER_CRITICAL();
311 pxQueue->u.xQueue.pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
312 pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
313 pxQueue->pcWriteTo = pxQueue->pcHead;
314 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - 1U ) * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
315 pxQueue->cRxLock = queueUNLOCKED;
316 pxQueue->cTxLock = queueUNLOCKED;
318 if( xNewQueue == pdFALSE )
320 /* If there are tasks blocked waiting to read from the queue, then
321 * the tasks will remain blocked as after this function exits the queue
322 * will still be empty. If there are tasks blocked waiting to write to
323 * the queue, then one should be unblocked as after this function exits
324 * it will be possible to write to it. */
325 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
327 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
329 queueYIELD_IF_USING_PREEMPTION();
333 mtCOVERAGE_TEST_MARKER();
338 mtCOVERAGE_TEST_MARKER();
343 /* Ensure the event queues start in the correct state. */
344 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
345 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
355 configASSERT( xReturn != pdFAIL );
357 /* A value is returned for calling semantic consistency with previous
361 /*-----------------------------------------------------------*/
363 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
365 QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,
366 const UBaseType_t uxItemSize,
367 uint8_t * pucQueueStorage,
368 StaticQueue_t * pxStaticQueue,
369 const uint8_t ucQueueType )
371 Queue_t * pxNewQueue = NULL;
373 /* The StaticQueue_t structure and the queue storage area must be
375 configASSERT( pxStaticQueue );
377 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
378 ( pxStaticQueue != NULL ) &&
380 /* A queue storage area should be provided if the item size is not 0, and
381 * should not be provided if the item size is 0. */
382 ( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) ) &&
383 ( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) ) )
385 #if ( configASSERT_DEFINED == 1 )
387 /* Sanity check that the size of the structure used to declare a
388 * variable of type StaticQueue_t or StaticSemaphore_t equals the size of
389 * the real queue and semaphore structures. */
390 volatile size_t xSize = sizeof( StaticQueue_t );
392 /* This assertion cannot be branch covered in unit tests */
393 configASSERT( xSize == sizeof( Queue_t ) ); /* LCOV_EXCL_BR_LINE */
394 ( void ) xSize; /* Keeps lint quiet when configASSERT() is not defined. */
396 #endif /* configASSERT_DEFINED */
398 /* The address of a statically allocated queue was passed in, use it.
399 * The address of a statically allocated storage area was also passed in
400 * but is already set. */
401 pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
403 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
405 /* Queues can be allocated wither statically or dynamically, so
406 * note this queue was allocated statically in case the queue is
408 pxNewQueue->ucStaticallyAllocated = pdTRUE;
410 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
412 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
416 configASSERT( pxNewQueue );
417 mtCOVERAGE_TEST_MARKER();
423 #endif /* configSUPPORT_STATIC_ALLOCATION */
424 /*-----------------------------------------------------------*/
426 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
428 BaseType_t xQueueGenericGetStaticBuffers( QueueHandle_t xQueue,
429 uint8_t ** ppucQueueStorage,
430 StaticQueue_t ** ppxStaticQueue )
433 Queue_t * const pxQueue = xQueue;
435 configASSERT( pxQueue );
436 configASSERT( ppxStaticQueue );
438 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
440 /* Check if the queue was statically allocated. */
441 if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdTRUE )
443 if( ppucQueueStorage != NULL )
445 *ppucQueueStorage = ( uint8_t * ) pxQueue->pcHead;
448 *ppxStaticQueue = ( StaticQueue_t * ) pxQueue;
456 #else /* configSUPPORT_DYNAMIC_ALLOCATION */
458 /* Queue must have been statically allocated. */
459 if( ppucQueueStorage != NULL )
461 *ppucQueueStorage = ( uint8_t * ) pxQueue->pcHead;
464 *ppxStaticQueue = ( StaticQueue_t * ) pxQueue;
467 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
472 #endif /* configSUPPORT_STATIC_ALLOCATION */
473 /*-----------------------------------------------------------*/
475 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
477 QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
478 const UBaseType_t uxItemSize,
479 const uint8_t ucQueueType )
481 Queue_t * pxNewQueue = NULL;
482 size_t xQueueSizeInBytes;
483 uint8_t * pucQueueStorage;
485 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
486 /* Check for multiplication overflow. */
487 ( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&
488 /* Check for addition overflow. */
489 ( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( uxQueueLength * uxItemSize ) ) )
491 /* Allocate enough space to hold the maximum number of items that
492 * can be in the queue at any time. It is valid for uxItemSize to be
493 * zero in the case the queue is used as a semaphore. */
494 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
496 /* Allocate the queue and storage area. Justification for MISRA
497 * deviation as follows: pvPortMalloc() always ensures returned memory
498 * blocks are aligned per the requirements of the MCU stack. In this case
499 * pvPortMalloc() must return a pointer that is guaranteed to meet the
500 * alignment requirements of the Queue_t structure - which in this case
501 * is an int8_t *. Therefore, whenever the stack alignment requirements
502 * are greater than or equal to the pointer to char requirements the cast
503 * is safe. In other cases alignment requirements are not strict (one or
505 pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */
507 if( pxNewQueue != NULL )
509 /* Jump past the queue structure to find the location of the queue
511 pucQueueStorage = ( uint8_t * ) pxNewQueue;
512 pucQueueStorage += sizeof( Queue_t ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
514 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
516 /* Queues can be created either statically or dynamically, so
517 * note this task was created dynamically in case it is later
519 pxNewQueue->ucStaticallyAllocated = pdFALSE;
521 #endif /* configSUPPORT_STATIC_ALLOCATION */
523 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
527 traceQUEUE_CREATE_FAILED( ucQueueType );
528 mtCOVERAGE_TEST_MARKER();
533 configASSERT( pxNewQueue );
534 mtCOVERAGE_TEST_MARKER();
540 #endif /* configSUPPORT_STATIC_ALLOCATION */
541 /*-----------------------------------------------------------*/
543 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
544 const UBaseType_t uxItemSize,
545 uint8_t * pucQueueStorage,
546 const uint8_t ucQueueType,
547 Queue_t * pxNewQueue )
549 /* Remove compiler warnings about unused parameters should
550 * configUSE_TRACE_FACILITY not be set to 1. */
551 ( void ) ucQueueType;
553 if( uxItemSize == ( UBaseType_t ) 0 )
555 /* No RAM was allocated for the queue storage area, but PC head cannot
556 * be set to NULL because NULL is used as a key to say the queue is used as
557 * a mutex. Therefore just set pcHead to point to the queue as a benign
558 * value that is known to be within the memory map. */
559 pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
563 /* Set the head to the start of the queue storage area. */
564 pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
567 /* Initialise the queue members as described where the queue type is
569 pxNewQueue->uxLength = uxQueueLength;
570 pxNewQueue->uxItemSize = uxItemSize;
571 ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
573 #if ( configUSE_TRACE_FACILITY == 1 )
575 pxNewQueue->ucQueueType = ucQueueType;
577 #endif /* configUSE_TRACE_FACILITY */
579 #if ( configUSE_QUEUE_SETS == 1 )
581 pxNewQueue->pxQueueSetContainer = NULL;
583 #endif /* configUSE_QUEUE_SETS */
585 traceQUEUE_CREATE( pxNewQueue );
587 /*-----------------------------------------------------------*/
589 #if ( configUSE_MUTEXES == 1 )
591 static void prvInitialiseMutex( Queue_t * pxNewQueue )
593 if( pxNewQueue != NULL )
595 /* The queue create function will set all the queue structure members
596 * correctly for a generic queue, but this function is creating a
597 * mutex. Overwrite those members that need to be set differently -
598 * in particular the information required for priority inheritance. */
599 pxNewQueue->u.xSemaphore.xMutexHolder = NULL;
600 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
602 /* In case this is a recursive mutex. */
603 pxNewQueue->u.xSemaphore.uxRecursiveCallCount = 0;
605 traceCREATE_MUTEX( pxNewQueue );
607 /* Start with the semaphore in the expected state. */
608 ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
612 traceCREATE_MUTEX_FAILED();
616 #endif /* configUSE_MUTEXES */
617 /*-----------------------------------------------------------*/
619 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
621 QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
623 QueueHandle_t xNewQueue;
624 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
626 xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
627 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
632 #endif /* configUSE_MUTEXES */
633 /*-----------------------------------------------------------*/
635 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
637 QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
638 StaticQueue_t * pxStaticQueue )
640 QueueHandle_t xNewQueue;
641 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
643 /* Prevent compiler warnings about unused parameters if
644 * configUSE_TRACE_FACILITY does not equal 1. */
645 ( void ) ucQueueType;
647 xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
648 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
653 #endif /* configUSE_MUTEXES */
654 /*-----------------------------------------------------------*/
656 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
658 TaskHandle_t xQueueGetMutexHolder( QueueHandle_t xSemaphore )
660 TaskHandle_t pxReturn;
661 Queue_t * const pxSemaphore = ( Queue_t * ) xSemaphore;
663 configASSERT( xSemaphore );
665 /* This function is called by xSemaphoreGetMutexHolder(), and should not
666 * be called directly. Note: This is a good way of determining if the
667 * calling task is the mutex holder, but not a good way of determining the
668 * identity of the mutex holder, as the holder may change between the
669 * following critical section exiting and the function returning. */
670 taskENTER_CRITICAL();
672 if( pxSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
674 pxReturn = pxSemaphore->u.xSemaphore.xMutexHolder;
684 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
686 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
687 /*-----------------------------------------------------------*/
689 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
691 TaskHandle_t xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )
693 TaskHandle_t pxReturn;
695 configASSERT( xSemaphore );
697 /* Mutexes cannot be used in interrupt service routines, so the mutex
698 * holder should not change in an ISR, and therefore a critical section is
699 * not required here. */
700 if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
702 pxReturn = ( ( Queue_t * ) xSemaphore )->u.xSemaphore.xMutexHolder;
710 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
712 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
713 /*-----------------------------------------------------------*/
715 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
717 BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
720 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
722 configASSERT( pxMutex );
724 /* If this is the task that holds the mutex then xMutexHolder will not
725 * change outside of this task. If this task does not hold the mutex then
726 * pxMutexHolder can never coincidentally equal the tasks handle, and as
727 * this is the only condition we are interested in it does not matter if
728 * pxMutexHolder is accessed simultaneously by another task. Therefore no
729 * mutual exclusion is required to test the pxMutexHolder variable. */
730 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
732 traceGIVE_MUTEX_RECURSIVE( pxMutex );
734 /* uxRecursiveCallCount cannot be zero if xMutexHolder is equal to
735 * the task handle, therefore no underflow check is required. Also,
736 * uxRecursiveCallCount is only modified by the mutex holder, and as
737 * there can only be one, no mutual exclusion is required to modify the
738 * uxRecursiveCallCount member. */
739 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )--;
741 /* Has the recursive call count unwound to 0? */
742 if( pxMutex->u.xSemaphore.uxRecursiveCallCount == ( UBaseType_t ) 0 )
744 /* Return the mutex. This will automatically unblock any other
745 * task that might be waiting to access the mutex. */
746 ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
750 mtCOVERAGE_TEST_MARKER();
757 /* The mutex cannot be given because the calling task is not the
761 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
767 #endif /* configUSE_RECURSIVE_MUTEXES */
768 /*-----------------------------------------------------------*/
770 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
772 BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
773 TickType_t xTicksToWait )
776 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
778 configASSERT( pxMutex );
780 /* Comments regarding mutual exclusion as per those within
781 * xQueueGiveMutexRecursive(). */
783 traceTAKE_MUTEX_RECURSIVE( pxMutex );
785 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
787 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
792 xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );
794 /* pdPASS will only be returned if the mutex was successfully
795 * obtained. The calling task may have entered the Blocked state
796 * before reaching here. */
797 if( xReturn != pdFAIL )
799 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
803 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
810 #endif /* configUSE_RECURSIVE_MUTEXES */
811 /*-----------------------------------------------------------*/
813 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
815 QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
816 const UBaseType_t uxInitialCount,
817 StaticQueue_t * pxStaticQueue )
819 QueueHandle_t xHandle = NULL;
821 if( ( uxMaxCount != 0 ) &&
822 ( uxInitialCount <= uxMaxCount ) )
824 xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
826 if( xHandle != NULL )
828 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
830 traceCREATE_COUNTING_SEMAPHORE();
834 traceCREATE_COUNTING_SEMAPHORE_FAILED();
839 configASSERT( xHandle );
840 mtCOVERAGE_TEST_MARKER();
846 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
847 /*-----------------------------------------------------------*/
849 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
851 QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
852 const UBaseType_t uxInitialCount )
854 QueueHandle_t xHandle = NULL;
856 if( ( uxMaxCount != 0 ) &&
857 ( uxInitialCount <= uxMaxCount ) )
859 xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
861 if( xHandle != NULL )
863 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
865 traceCREATE_COUNTING_SEMAPHORE();
869 traceCREATE_COUNTING_SEMAPHORE_FAILED();
874 configASSERT( xHandle );
875 mtCOVERAGE_TEST_MARKER();
881 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
882 /*-----------------------------------------------------------*/
884 BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
885 const void * const pvItemToQueue,
886 TickType_t xTicksToWait,
887 const BaseType_t xCopyPosition )
889 BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
891 Queue_t * const pxQueue = xQueue;
893 configASSERT( pxQueue );
894 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
895 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
896 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
898 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
902 /*lint -save -e904 This function relaxes the coding standard somewhat to
903 * allow return statements within the function itself. This is done in the
904 * interest of execution time efficiency. */
907 taskENTER_CRITICAL();
909 /* Is there room on the queue now? The running task must be the
910 * highest priority task wanting to access the queue. If the head item
911 * in the queue is to be overwritten then it does not matter if the
913 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
915 traceQUEUE_SEND( pxQueue );
917 #if ( configUSE_QUEUE_SETS == 1 )
919 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
921 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
923 if( pxQueue->pxQueueSetContainer != NULL )
925 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
927 /* Do not notify the queue set as an existing item
928 * was overwritten in the queue so the number of items
929 * in the queue has not changed. */
930 mtCOVERAGE_TEST_MARKER();
932 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
934 /* The queue is a member of a queue set, and posting
935 * to the queue set caused a higher priority task to
936 * unblock. A context switch is required. */
937 queueYIELD_IF_USING_PREEMPTION();
941 mtCOVERAGE_TEST_MARKER();
946 /* If there was a task waiting for data to arrive on the
947 * queue then unblock it now. */
948 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
950 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
952 /* The unblocked task has a priority higher than
953 * our own so yield immediately. Yes it is ok to
954 * do this from within the critical section - the
955 * kernel takes care of that. */
956 queueYIELD_IF_USING_PREEMPTION();
960 mtCOVERAGE_TEST_MARKER();
963 else if( xYieldRequired != pdFALSE )
965 /* This path is a special case that will only get
966 * executed if the task was holding multiple mutexes
967 * and the mutexes were given back in an order that is
968 * different to that in which they were taken. */
969 queueYIELD_IF_USING_PREEMPTION();
973 mtCOVERAGE_TEST_MARKER();
977 #else /* configUSE_QUEUE_SETS */
979 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
981 /* If there was a task waiting for data to arrive on the
982 * queue then unblock it now. */
983 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
985 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
987 /* The unblocked task has a priority higher than
988 * our own so yield immediately. Yes it is ok to do
989 * this from within the critical section - the kernel
990 * takes care of that. */
991 queueYIELD_IF_USING_PREEMPTION();
995 mtCOVERAGE_TEST_MARKER();
998 else if( xYieldRequired != pdFALSE )
1000 /* This path is a special case that will only get
1001 * executed if the task was holding multiple mutexes and
1002 * the mutexes were given back in an order that is
1003 * different to that in which they were taken. */
1004 queueYIELD_IF_USING_PREEMPTION();
1008 mtCOVERAGE_TEST_MARKER();
1011 #endif /* configUSE_QUEUE_SETS */
1013 taskEXIT_CRITICAL();
1018 if( xTicksToWait == ( TickType_t ) 0 )
1020 /* The queue was full and no block time is specified (or
1021 * the block time has expired) so leave now. */
1022 taskEXIT_CRITICAL();
1024 /* Return to the original privilege level before exiting
1026 traceQUEUE_SEND_FAILED( pxQueue );
1027 return errQUEUE_FULL;
1029 else if( xEntryTimeSet == pdFALSE )
1031 /* The queue was full and a block time was specified so
1032 * configure the timeout structure. */
1033 vTaskInternalSetTimeOutState( &xTimeOut );
1034 xEntryTimeSet = pdTRUE;
1038 /* Entry time was already set. */
1039 mtCOVERAGE_TEST_MARKER();
1043 taskEXIT_CRITICAL();
1045 /* Interrupts and other tasks can send to and receive from the queue
1046 * now the critical section has been exited. */
1049 prvLockQueue( pxQueue );
1051 /* Update the timeout state to see if it has expired yet. */
1052 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1054 if( prvIsQueueFull( pxQueue ) != pdFALSE )
1056 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
1057 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
1059 /* Unlocking the queue means queue events can effect the
1060 * event list. It is possible that interrupts occurring now
1061 * remove this task from the event list again - but as the
1062 * scheduler is suspended the task will go onto the pending
1063 * ready list instead of the actual ready list. */
1064 prvUnlockQueue( pxQueue );
1066 /* Resuming the scheduler will move tasks from the pending
1067 * ready list into the ready list - so it is feasible that this
1068 * task is already in the ready list before it yields - in which
1069 * case the yield will not cause a context switch unless there
1070 * is also a higher priority task in the pending ready list. */
1071 if( xTaskResumeAll() == pdFALSE )
1073 portYIELD_WITHIN_API();
1079 prvUnlockQueue( pxQueue );
1080 ( void ) xTaskResumeAll();
1085 /* The timeout has expired. */
1086 prvUnlockQueue( pxQueue );
1087 ( void ) xTaskResumeAll();
1089 traceQUEUE_SEND_FAILED( pxQueue );
1090 return errQUEUE_FULL;
1092 } /*lint -restore */
1094 /*-----------------------------------------------------------*/
1096 BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,
1097 const void * const pvItemToQueue,
1098 BaseType_t * const pxHigherPriorityTaskWoken,
1099 const BaseType_t xCopyPosition )
1102 UBaseType_t uxSavedInterruptStatus;
1103 Queue_t * const pxQueue = xQueue;
1105 configASSERT( pxQueue );
1106 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1107 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
1109 /* RTOS ports that support interrupt nesting have the concept of a maximum
1110 * system call (or maximum API call) interrupt priority. Interrupts that are
1111 * above the maximum system call priority are kept permanently enabled, even
1112 * when the RTOS kernel is in a critical section, but cannot make any calls to
1113 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1114 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1115 * failure if a FreeRTOS API function is called from an interrupt that has been
1116 * assigned a priority above the configured maximum system call priority.
1117 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1118 * that have been assigned a priority at or (logically) below the maximum
1119 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1120 * safe API to ensure interrupt entry is as fast and as simple as possible.
1121 * More information (albeit Cortex-M specific) is provided on the following
1122 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1123 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1125 /* Similar to xQueueGenericSend, except without blocking if there is no room
1126 * in the queue. Also don't directly wake a task that was blocked on a queue
1127 * read, instead return a flag to say whether a context switch is required or
1128 * not (i.e. has a task with a higher priority than us been woken by this
1130 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1132 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
1134 const int8_t cTxLock = pxQueue->cTxLock;
1135 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
1137 traceQUEUE_SEND_FROM_ISR( pxQueue );
1139 /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a
1140 * semaphore or mutex. That means prvCopyDataToQueue() cannot result
1141 * in a task disinheriting a priority and prvCopyDataToQueue() can be
1142 * called here even though the disinherit function does not check if
1143 * the scheduler is suspended before accessing the ready lists. */
1144 ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
1146 /* The event list is not altered if the queue is locked. This will
1147 * be done when the queue is unlocked later. */
1148 if( cTxLock == queueUNLOCKED )
1150 #if ( configUSE_QUEUE_SETS == 1 )
1152 if( pxQueue->pxQueueSetContainer != NULL )
1154 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
1156 /* Do not notify the queue set as an existing item
1157 * was overwritten in the queue so the number of items
1158 * in the queue has not changed. */
1159 mtCOVERAGE_TEST_MARKER();
1161 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1163 /* The queue is a member of a queue set, and posting
1164 * to the queue set caused a higher priority task to
1165 * unblock. A context switch is required. */
1166 if( pxHigherPriorityTaskWoken != NULL )
1168 *pxHigherPriorityTaskWoken = pdTRUE;
1172 mtCOVERAGE_TEST_MARKER();
1177 mtCOVERAGE_TEST_MARKER();
1182 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1184 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1186 /* The task waiting has a higher priority so
1187 * record that a context switch is required. */
1188 if( pxHigherPriorityTaskWoken != NULL )
1190 *pxHigherPriorityTaskWoken = pdTRUE;
1194 mtCOVERAGE_TEST_MARKER();
1199 mtCOVERAGE_TEST_MARKER();
1204 mtCOVERAGE_TEST_MARKER();
1208 #else /* configUSE_QUEUE_SETS */
1210 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1212 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1214 /* The task waiting has a higher priority so record that a
1215 * context switch is required. */
1216 if( pxHigherPriorityTaskWoken != NULL )
1218 *pxHigherPriorityTaskWoken = pdTRUE;
1222 mtCOVERAGE_TEST_MARKER();
1227 mtCOVERAGE_TEST_MARKER();
1232 mtCOVERAGE_TEST_MARKER();
1235 /* Not used in this path. */
1236 ( void ) uxPreviousMessagesWaiting;
1238 #endif /* configUSE_QUEUE_SETS */
1242 /* Increment the lock count so the task that unlocks the queue
1243 * knows that data was posted while it was locked. */
1244 prvIncrementQueueTxLock( pxQueue, cTxLock );
1251 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1252 xReturn = errQUEUE_FULL;
1255 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1259 /*-----------------------------------------------------------*/
1261 BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,
1262 BaseType_t * const pxHigherPriorityTaskWoken )
1265 UBaseType_t uxSavedInterruptStatus;
1266 Queue_t * const pxQueue = xQueue;
1268 /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
1269 * item size is 0. Don't directly wake a task that was blocked on a queue
1270 * read, instead return a flag to say whether a context switch is required or
1271 * not (i.e. has a task with a higher priority than us been woken by this
1274 configASSERT( pxQueue );
1276 /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()
1277 * if the item size is not 0. */
1278 configASSERT( pxQueue->uxItemSize == 0 );
1280 /* Normally a mutex would not be given from an interrupt, especially if
1281 * there is a mutex holder, as priority inheritance makes no sense for an
1282 * interrupts, only tasks. */
1283 configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->u.xSemaphore.xMutexHolder != NULL ) ) );
1285 /* RTOS ports that support interrupt nesting have the concept of a maximum
1286 * system call (or maximum API call) interrupt priority. Interrupts that are
1287 * above the maximum system call priority are kept permanently enabled, even
1288 * when the RTOS kernel is in a critical section, but cannot make any calls to
1289 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1290 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1291 * failure if a FreeRTOS API function is called from an interrupt that has been
1292 * assigned a priority above the configured maximum system call priority.
1293 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1294 * that have been assigned a priority at or (logically) below the maximum
1295 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1296 * safe API to ensure interrupt entry is as fast and as simple as possible.
1297 * More information (albeit Cortex-M specific) is provided on the following
1298 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1299 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1301 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1303 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1305 /* When the queue is used to implement a semaphore no data is ever
1306 * moved through the queue but it is still valid to see if the queue 'has
1308 if( uxMessagesWaiting < pxQueue->uxLength )
1310 const int8_t cTxLock = pxQueue->cTxLock;
1312 traceQUEUE_SEND_FROM_ISR( pxQueue );
1314 /* A task can only have an inherited priority if it is a mutex
1315 * holder - and if there is a mutex holder then the mutex cannot be
1316 * given from an ISR. As this is the ISR version of the function it
1317 * can be assumed there is no mutex holder and no need to determine if
1318 * priority disinheritance is needed. Simply increase the count of
1319 * messages (semaphores) available. */
1320 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
1322 /* The event list is not altered if the queue is locked. This will
1323 * be done when the queue is unlocked later. */
1324 if( cTxLock == queueUNLOCKED )
1326 #if ( configUSE_QUEUE_SETS == 1 )
1328 if( pxQueue->pxQueueSetContainer != NULL )
1330 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1332 /* The semaphore is a member of a queue set, and
1333 * posting to the queue set caused a higher priority
1334 * task to unblock. A context switch is required. */
1335 if( pxHigherPriorityTaskWoken != NULL )
1337 *pxHigherPriorityTaskWoken = pdTRUE;
1341 mtCOVERAGE_TEST_MARKER();
1346 mtCOVERAGE_TEST_MARKER();
1351 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1353 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1355 /* The task waiting has a higher priority so
1356 * record that a context switch is required. */
1357 if( pxHigherPriorityTaskWoken != NULL )
1359 *pxHigherPriorityTaskWoken = pdTRUE;
1363 mtCOVERAGE_TEST_MARKER();
1368 mtCOVERAGE_TEST_MARKER();
1373 mtCOVERAGE_TEST_MARKER();
1377 #else /* configUSE_QUEUE_SETS */
1379 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1381 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1383 /* The task waiting has a higher priority so record that a
1384 * context switch is required. */
1385 if( pxHigherPriorityTaskWoken != NULL )
1387 *pxHigherPriorityTaskWoken = pdTRUE;
1391 mtCOVERAGE_TEST_MARKER();
1396 mtCOVERAGE_TEST_MARKER();
1401 mtCOVERAGE_TEST_MARKER();
1404 #endif /* configUSE_QUEUE_SETS */
1408 /* Increment the lock count so the task that unlocks the queue
1409 * knows that data was posted while it was locked. */
1410 prvIncrementQueueTxLock( pxQueue, cTxLock );
1417 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1418 xReturn = errQUEUE_FULL;
1421 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1425 /*-----------------------------------------------------------*/
1427 BaseType_t xQueueReceive( QueueHandle_t xQueue,
1428 void * const pvBuffer,
1429 TickType_t xTicksToWait )
1431 BaseType_t xEntryTimeSet = pdFALSE;
1433 Queue_t * const pxQueue = xQueue;
1435 /* Check the pointer is not NULL. */
1436 configASSERT( ( pxQueue ) );
1438 /* The buffer into which data is received can only be NULL if the data size
1439 * is zero (so no data is copied into the buffer). */
1440 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1442 /* Cannot block if the scheduler is suspended. */
1443 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1445 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1449 /*lint -save -e904 This function relaxes the coding standard somewhat to
1450 * allow return statements within the function itself. This is done in the
1451 * interest of execution time efficiency. */
1454 taskENTER_CRITICAL();
1456 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1458 /* Is there data in the queue now? To be running the calling task
1459 * must be the highest priority task wanting to access the queue. */
1460 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1462 /* Data available, remove one item. */
1463 prvCopyDataFromQueue( pxQueue, pvBuffer );
1464 traceQUEUE_RECEIVE( pxQueue );
1465 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
1467 /* There is now space in the queue, were any tasks waiting to
1468 * post to the queue? If so, unblock the highest priority waiting
1470 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1472 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1474 queueYIELD_IF_USING_PREEMPTION();
1478 mtCOVERAGE_TEST_MARKER();
1483 mtCOVERAGE_TEST_MARKER();
1486 taskEXIT_CRITICAL();
1491 if( xTicksToWait == ( TickType_t ) 0 )
1493 /* The queue was empty and no block time is specified (or
1494 * the block time has expired) so leave now. */
1495 taskEXIT_CRITICAL();
1496 traceQUEUE_RECEIVE_FAILED( pxQueue );
1497 return errQUEUE_EMPTY;
1499 else if( xEntryTimeSet == pdFALSE )
1501 /* The queue was empty and a block time was specified so
1502 * configure the timeout structure. */
1503 vTaskInternalSetTimeOutState( &xTimeOut );
1504 xEntryTimeSet = pdTRUE;
1508 /* Entry time was already set. */
1509 mtCOVERAGE_TEST_MARKER();
1513 taskEXIT_CRITICAL();
1515 /* Interrupts and other tasks can send to and receive from the queue
1516 * now the critical section has been exited. */
1519 prvLockQueue( pxQueue );
1521 /* Update the timeout state to see if it has expired yet. */
1522 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1524 /* The timeout has not expired. If the queue is still empty place
1525 * the task on the list of tasks waiting to receive from the queue. */
1526 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1528 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1529 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1530 prvUnlockQueue( pxQueue );
1532 if( xTaskResumeAll() == pdFALSE )
1534 portYIELD_WITHIN_API();
1538 mtCOVERAGE_TEST_MARKER();
1543 /* The queue contains data again. Loop back to try and read the
1545 prvUnlockQueue( pxQueue );
1546 ( void ) xTaskResumeAll();
1551 /* Timed out. If there is no data in the queue exit, otherwise loop
1552 * back and attempt to read the data. */
1553 prvUnlockQueue( pxQueue );
1554 ( void ) xTaskResumeAll();
1556 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1558 traceQUEUE_RECEIVE_FAILED( pxQueue );
1559 return errQUEUE_EMPTY;
1563 mtCOVERAGE_TEST_MARKER();
1566 } /*lint -restore */
1568 /*-----------------------------------------------------------*/
1570 BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
1571 TickType_t xTicksToWait )
1573 BaseType_t xEntryTimeSet = pdFALSE;
1575 Queue_t * const pxQueue = xQueue;
1577 #if ( configUSE_MUTEXES == 1 )
1578 BaseType_t xInheritanceOccurred = pdFALSE;
1581 /* Check the queue pointer is not NULL. */
1582 configASSERT( ( pxQueue ) );
1584 /* Check this really is a semaphore, in which case the item size will be
1586 configASSERT( pxQueue->uxItemSize == 0 );
1588 /* Cannot block if the scheduler is suspended. */
1589 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1591 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1595 /*lint -save -e904 This function relaxes the coding standard somewhat to allow return
1596 * statements within the function itself. This is done in the interest
1597 * of execution time efficiency. */
1600 taskENTER_CRITICAL();
1602 /* Semaphores are queues with an item size of 0, and where the
1603 * number of messages in the queue is the semaphore's count value. */
1604 const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;
1606 /* Is there data in the queue now? To be running the calling task
1607 * must be the highest priority task wanting to access the queue. */
1608 if( uxSemaphoreCount > ( UBaseType_t ) 0 )
1610 traceQUEUE_RECEIVE( pxQueue );
1612 /* Semaphores are queues with a data size of zero and where the
1613 * messages waiting is the semaphore's count. Reduce the count. */
1614 pxQueue->uxMessagesWaiting = uxSemaphoreCount - ( UBaseType_t ) 1;
1616 #if ( configUSE_MUTEXES == 1 )
1618 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1620 /* Record the information required to implement
1621 * priority inheritance should it become necessary. */
1622 pxQueue->u.xSemaphore.xMutexHolder = pvTaskIncrementMutexHeldCount();
1626 mtCOVERAGE_TEST_MARKER();
1629 #endif /* configUSE_MUTEXES */
1631 /* Check to see if other tasks are blocked waiting to give the
1632 * semaphore, and if so, unblock the highest priority such task. */
1633 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1635 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1637 queueYIELD_IF_USING_PREEMPTION();
1641 mtCOVERAGE_TEST_MARKER();
1646 mtCOVERAGE_TEST_MARKER();
1649 taskEXIT_CRITICAL();
1654 if( xTicksToWait == ( TickType_t ) 0 )
1656 /* The semaphore count was 0 and no block time is specified
1657 * (or the block time has expired) so exit now. */
1658 taskEXIT_CRITICAL();
1659 traceQUEUE_RECEIVE_FAILED( pxQueue );
1660 return errQUEUE_EMPTY;
1662 else if( xEntryTimeSet == pdFALSE )
1664 /* The semaphore count was 0 and a block time was specified
1665 * so configure the timeout structure ready to block. */
1666 vTaskInternalSetTimeOutState( &xTimeOut );
1667 xEntryTimeSet = pdTRUE;
1671 /* Entry time was already set. */
1672 mtCOVERAGE_TEST_MARKER();
1676 taskEXIT_CRITICAL();
1678 /* Interrupts and other tasks can give to and take from the semaphore
1679 * now the critical section has been exited. */
1682 prvLockQueue( pxQueue );
1684 /* Update the timeout state to see if it has expired yet. */
1685 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1687 /* A block time is specified and not expired. If the semaphore
1688 * count is 0 then enter the Blocked state to wait for a semaphore to
1689 * become available. As semaphores are implemented with queues the
1690 * queue being empty is equivalent to the semaphore count being 0. */
1691 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1693 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1695 #if ( configUSE_MUTEXES == 1 )
1697 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1699 taskENTER_CRITICAL();
1701 xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );
1703 taskEXIT_CRITICAL();
1707 mtCOVERAGE_TEST_MARKER();
1710 #endif /* if ( configUSE_MUTEXES == 1 ) */
1712 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1713 prvUnlockQueue( pxQueue );
1715 if( xTaskResumeAll() == pdFALSE )
1717 portYIELD_WITHIN_API();
1721 mtCOVERAGE_TEST_MARKER();
1726 /* There was no timeout and the semaphore count was not 0, so
1727 * attempt to take the semaphore again. */
1728 prvUnlockQueue( pxQueue );
1729 ( void ) xTaskResumeAll();
1735 prvUnlockQueue( pxQueue );
1736 ( void ) xTaskResumeAll();
1738 /* If the semaphore count is 0 exit now as the timeout has
1739 * expired. Otherwise return to attempt to take the semaphore that is
1740 * known to be available. As semaphores are implemented by queues the
1741 * queue being empty is equivalent to the semaphore count being 0. */
1742 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1744 #if ( configUSE_MUTEXES == 1 )
1746 /* xInheritanceOccurred could only have be set if
1747 * pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
1748 * test the mutex type again to check it is actually a mutex. */
1749 if( xInheritanceOccurred != pdFALSE )
1751 taskENTER_CRITICAL();
1753 UBaseType_t uxHighestWaitingPriority;
1755 /* This task blocking on the mutex caused another
1756 * task to inherit this task's priority. Now this task
1757 * has timed out the priority should be disinherited
1758 * again, but only as low as the next highest priority
1759 * task that is waiting for the same mutex. */
1760 uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );
1761 vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );
1763 taskEXIT_CRITICAL();
1766 #endif /* configUSE_MUTEXES */
1768 traceQUEUE_RECEIVE_FAILED( pxQueue );
1769 return errQUEUE_EMPTY;
1773 mtCOVERAGE_TEST_MARKER();
1776 } /*lint -restore */
1778 /*-----------------------------------------------------------*/
1780 BaseType_t xQueuePeek( QueueHandle_t xQueue,
1781 void * const pvBuffer,
1782 TickType_t xTicksToWait )
1784 BaseType_t xEntryTimeSet = pdFALSE;
1786 int8_t * pcOriginalReadPosition;
1787 Queue_t * const pxQueue = xQueue;
1789 /* Check the pointer is not NULL. */
1790 configASSERT( ( pxQueue ) );
1792 /* The buffer into which data is received can only be NULL if the data size
1793 * is zero (so no data is copied into the buffer. */
1794 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
1796 /* Cannot block if the scheduler is suspended. */
1797 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1799 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1803 /*lint -save -e904 This function relaxes the coding standard somewhat to
1804 * allow return statements within the function itself. This is done in the
1805 * interest of execution time efficiency. */
1808 taskENTER_CRITICAL();
1810 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1812 /* Is there data in the queue now? To be running the calling task
1813 * must be the highest priority task wanting to access the queue. */
1814 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1816 /* Remember the read position so it can be reset after the data
1817 * is read from the queue as this function is only peeking the
1818 * data, not removing it. */
1819 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
1821 prvCopyDataFromQueue( pxQueue, pvBuffer );
1822 traceQUEUE_PEEK( pxQueue );
1824 /* The data is not being removed, so reset the read pointer. */
1825 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
1827 /* The data is being left in the queue, so see if there are
1828 * any other tasks waiting for the data. */
1829 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1831 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1833 /* The task waiting has a higher priority than this task. */
1834 queueYIELD_IF_USING_PREEMPTION();
1838 mtCOVERAGE_TEST_MARKER();
1843 mtCOVERAGE_TEST_MARKER();
1846 taskEXIT_CRITICAL();
1851 if( xTicksToWait == ( TickType_t ) 0 )
1853 /* The queue was empty and no block time is specified (or
1854 * the block time has expired) so leave now. */
1855 taskEXIT_CRITICAL();
1856 traceQUEUE_PEEK_FAILED( pxQueue );
1857 return errQUEUE_EMPTY;
1859 else if( xEntryTimeSet == pdFALSE )
1861 /* The queue was empty and a block time was specified so
1862 * configure the timeout structure ready to enter the blocked
1864 vTaskInternalSetTimeOutState( &xTimeOut );
1865 xEntryTimeSet = pdTRUE;
1869 /* Entry time was already set. */
1870 mtCOVERAGE_TEST_MARKER();
1874 taskEXIT_CRITICAL();
1876 /* Interrupts and other tasks can send to and receive from the queue
1877 * now that the critical section has been exited. */
1880 prvLockQueue( pxQueue );
1882 /* Update the timeout state to see if it has expired yet. */
1883 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1885 /* Timeout has not expired yet, check to see if there is data in the
1886 * queue now, and if not enter the Blocked state to wait for data. */
1887 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1889 traceBLOCKING_ON_QUEUE_PEEK( pxQueue );
1890 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1891 prvUnlockQueue( pxQueue );
1893 if( xTaskResumeAll() == pdFALSE )
1895 portYIELD_WITHIN_API();
1899 mtCOVERAGE_TEST_MARKER();
1904 /* There is data in the queue now, so don't enter the blocked
1905 * state, instead return to try and obtain the data. */
1906 prvUnlockQueue( pxQueue );
1907 ( void ) xTaskResumeAll();
1912 /* The timeout has expired. If there is still no data in the queue
1913 * exit, otherwise go back and try to read the data again. */
1914 prvUnlockQueue( pxQueue );
1915 ( void ) xTaskResumeAll();
1917 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1919 traceQUEUE_PEEK_FAILED( pxQueue );
1920 return errQUEUE_EMPTY;
1924 mtCOVERAGE_TEST_MARKER();
1927 } /*lint -restore */
1929 /*-----------------------------------------------------------*/
1931 BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,
1932 void * const pvBuffer,
1933 BaseType_t * const pxHigherPriorityTaskWoken )
1936 UBaseType_t uxSavedInterruptStatus;
1937 Queue_t * const pxQueue = xQueue;
1939 configASSERT( pxQueue );
1940 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1942 /* RTOS ports that support interrupt nesting have the concept of a maximum
1943 * system call (or maximum API call) interrupt priority. Interrupts that are
1944 * above the maximum system call priority are kept permanently enabled, even
1945 * when the RTOS kernel is in a critical section, but cannot make any calls to
1946 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1947 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1948 * failure if a FreeRTOS API function is called from an interrupt that has been
1949 * assigned a priority above the configured maximum system call priority.
1950 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1951 * that have been assigned a priority at or (logically) below the maximum
1952 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1953 * safe API to ensure interrupt entry is as fast and as simple as possible.
1954 * More information (albeit Cortex-M specific) is provided on the following
1955 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1956 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1958 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1960 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
1962 /* Cannot block in an ISR, so check there is data available. */
1963 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1965 const int8_t cRxLock = pxQueue->cRxLock;
1967 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
1969 prvCopyDataFromQueue( pxQueue, pvBuffer );
1970 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
1972 /* If the queue is locked the event list will not be modified.
1973 * Instead update the lock count so the task that unlocks the queue
1974 * will know that an ISR has removed data while the queue was
1976 if( cRxLock == queueUNLOCKED )
1978 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1980 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1982 /* The task waiting has a higher priority than us so
1983 * force a context switch. */
1984 if( pxHigherPriorityTaskWoken != NULL )
1986 *pxHigherPriorityTaskWoken = pdTRUE;
1990 mtCOVERAGE_TEST_MARKER();
1995 mtCOVERAGE_TEST_MARKER();
2000 mtCOVERAGE_TEST_MARKER();
2005 /* Increment the lock count so the task that unlocks the queue
2006 * knows that data was removed while it was locked. */
2007 prvIncrementQueueRxLock( pxQueue, cRxLock );
2015 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
2018 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2022 /*-----------------------------------------------------------*/
2024 BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,
2025 void * const pvBuffer )
2028 UBaseType_t uxSavedInterruptStatus;
2029 int8_t * pcOriginalReadPosition;
2030 Queue_t * const pxQueue = xQueue;
2032 configASSERT( pxQueue );
2033 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
2034 configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
2036 /* RTOS ports that support interrupt nesting have the concept of a maximum
2037 * system call (or maximum API call) interrupt priority. Interrupts that are
2038 * above the maximum system call priority are kept permanently enabled, even
2039 * when the RTOS kernel is in a critical section, but cannot make any calls to
2040 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
2041 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2042 * failure if a FreeRTOS API function is called from an interrupt that has been
2043 * assigned a priority above the configured maximum system call priority.
2044 * Only FreeRTOS functions that end in FromISR can be called from interrupts
2045 * that have been assigned a priority at or (logically) below the maximum
2046 * system call interrupt priority. FreeRTOS maintains a separate interrupt
2047 * safe API to ensure interrupt entry is as fast and as simple as possible.
2048 * More information (albeit Cortex-M specific) is provided on the following
2049 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2050 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2052 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2054 /* Cannot block in an ISR, so check there is data available. */
2055 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2057 traceQUEUE_PEEK_FROM_ISR( pxQueue );
2059 /* Remember the read position so it can be reset as nothing is
2060 * actually being removed from the queue. */
2061 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
2062 prvCopyDataFromQueue( pxQueue, pvBuffer );
2063 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
2070 traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
2073 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2077 /*-----------------------------------------------------------*/
2079 UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
2081 UBaseType_t uxReturn;
2083 configASSERT( xQueue );
2085 taskENTER_CRITICAL();
2087 uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
2089 taskEXIT_CRITICAL();
2092 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2093 /*-----------------------------------------------------------*/
2095 UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
2097 UBaseType_t uxReturn;
2098 Queue_t * const pxQueue = xQueue;
2100 configASSERT( pxQueue );
2102 taskENTER_CRITICAL();
2104 uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;
2106 taskEXIT_CRITICAL();
2109 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2110 /*-----------------------------------------------------------*/
2112 UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
2114 UBaseType_t uxReturn;
2115 Queue_t * const pxQueue = xQueue;
2117 configASSERT( pxQueue );
2118 uxReturn = pxQueue->uxMessagesWaiting;
2121 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2122 /*-----------------------------------------------------------*/
2124 void vQueueDelete( QueueHandle_t xQueue )
2126 Queue_t * const pxQueue = xQueue;
2128 configASSERT( pxQueue );
2129 traceQUEUE_DELETE( pxQueue );
2131 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2133 vQueueUnregisterQueue( pxQueue );
2137 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
2139 /* The queue can only have been allocated dynamically - free it
2141 vPortFree( pxQueue );
2143 #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
2145 /* The queue could have been allocated statically or dynamically, so
2146 * check before attempting to free the memory. */
2147 if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
2149 vPortFree( pxQueue );
2153 mtCOVERAGE_TEST_MARKER();
2156 #else /* if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) ) */
2158 /* The queue must have been statically allocated, so is not going to be
2159 * deleted. Avoid compiler warnings about the unused parameter. */
2162 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
2164 /*-----------------------------------------------------------*/
2166 #if ( configUSE_TRACE_FACILITY == 1 )
2168 UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
2170 return ( ( Queue_t * ) xQueue )->uxQueueNumber;
2173 #endif /* configUSE_TRACE_FACILITY */
2174 /*-----------------------------------------------------------*/
2176 #if ( configUSE_TRACE_FACILITY == 1 )
2178 void vQueueSetQueueNumber( QueueHandle_t xQueue,
2179 UBaseType_t uxQueueNumber )
2181 ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
2184 #endif /* configUSE_TRACE_FACILITY */
2185 /*-----------------------------------------------------------*/
2187 #if ( configUSE_TRACE_FACILITY == 1 )
2189 uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
2191 return ( ( Queue_t * ) xQueue )->ucQueueType;
2194 #endif /* configUSE_TRACE_FACILITY */
2195 /*-----------------------------------------------------------*/
2197 #if ( configUSE_MUTEXES == 1 )
2199 static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue )
2201 UBaseType_t uxHighestPriorityOfWaitingTasks;
2203 /* If a task waiting for a mutex causes the mutex holder to inherit a
2204 * priority, but the waiting task times out, then the holder should
2205 * disinherit the priority - but only down to the highest priority of any
2206 * other tasks that are waiting for the same mutex. For this purpose,
2207 * return the priority of the highest priority task that is waiting for the
2209 if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0U )
2211 uxHighestPriorityOfWaitingTasks = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) );
2215 uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;
2218 return uxHighestPriorityOfWaitingTasks;
2221 #endif /* configUSE_MUTEXES */
2222 /*-----------------------------------------------------------*/
2224 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
2225 const void * pvItemToQueue,
2226 const BaseType_t xPosition )
2228 BaseType_t xReturn = pdFALSE;
2229 UBaseType_t uxMessagesWaiting;
2231 /* This function is called from a critical section. */
2233 uxMessagesWaiting = pxQueue->uxMessagesWaiting;
2235 if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
2237 #if ( configUSE_MUTEXES == 1 )
2239 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
2241 /* The mutex is no longer being held. */
2242 xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
2243 pxQueue->u.xSemaphore.xMutexHolder = NULL;
2247 mtCOVERAGE_TEST_MARKER();
2250 #endif /* configUSE_MUTEXES */
2252 else if( xPosition == queueSEND_TO_BACK )
2254 ( void ) memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports, plus previous logic ensures a null pointer can only be passed to memcpy() if the copy size is 0. Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */
2255 pxQueue->pcWriteTo += pxQueue->uxItemSize; /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */
2257 if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2259 pxQueue->pcWriteTo = pxQueue->pcHead;
2263 mtCOVERAGE_TEST_MARKER();
2268 ( void ) memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e9087 !e418 MISRA exception as the casts are only redundant for some ports. Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. Assert checks null pointer only used when length is 0. */
2269 pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
2271 if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2273 pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
2277 mtCOVERAGE_TEST_MARKER();
2280 if( xPosition == queueOVERWRITE )
2282 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
2284 /* An item is not being added but overwritten, so subtract
2285 * one from the recorded number of items in the queue so when
2286 * one is added again below the number of recorded items remains
2288 --uxMessagesWaiting;
2292 mtCOVERAGE_TEST_MARKER();
2297 mtCOVERAGE_TEST_MARKER();
2301 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
2305 /*-----------------------------------------------------------*/
2307 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
2308 void * const pvBuffer )
2310 if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
2312 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize; /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */
2314 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
2316 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2320 mtCOVERAGE_TEST_MARKER();
2323 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports. Also previous logic ensures a null pointer can only be passed to memcpy() when the count is 0. Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */
2326 /*-----------------------------------------------------------*/
2328 static void prvUnlockQueue( Queue_t * const pxQueue )
2330 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
2332 /* The lock counts contains the number of extra data items placed or
2333 * removed from the queue while the queue was locked. When a queue is
2334 * locked items can be added or removed, but the event lists cannot be
2336 taskENTER_CRITICAL();
2338 int8_t cTxLock = pxQueue->cTxLock;
2340 /* See if data was added to the queue while it was locked. */
2341 while( cTxLock > queueLOCKED_UNMODIFIED )
2343 /* Data was posted while the queue was locked. Are any tasks
2344 * blocked waiting for data to become available? */
2345 #if ( configUSE_QUEUE_SETS == 1 )
2347 if( pxQueue->pxQueueSetContainer != NULL )
2349 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
2351 /* The queue is a member of a queue set, and posting to
2352 * the queue set caused a higher priority task to unblock.
2353 * A context switch is required. */
2358 mtCOVERAGE_TEST_MARKER();
2363 /* Tasks that are removed from the event list will get
2364 * added to the pending ready list as the scheduler is still
2366 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2368 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2370 /* The task waiting has a higher priority so record that a
2371 * context switch is required. */
2376 mtCOVERAGE_TEST_MARKER();
2385 #else /* configUSE_QUEUE_SETS */
2387 /* Tasks that are removed from the event list will get added to
2388 * the pending ready list as the scheduler is still suspended. */
2389 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2391 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2393 /* The task waiting has a higher priority so record that
2394 * a context switch is required. */
2399 mtCOVERAGE_TEST_MARKER();
2407 #endif /* configUSE_QUEUE_SETS */
2412 pxQueue->cTxLock = queueUNLOCKED;
2414 taskEXIT_CRITICAL();
2416 /* Do the same for the Rx lock. */
2417 taskENTER_CRITICAL();
2419 int8_t cRxLock = pxQueue->cRxLock;
2421 while( cRxLock > queueLOCKED_UNMODIFIED )
2423 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2425 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2431 mtCOVERAGE_TEST_MARKER();
2442 pxQueue->cRxLock = queueUNLOCKED;
2444 taskEXIT_CRITICAL();
2446 /*-----------------------------------------------------------*/
2448 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue )
2452 taskENTER_CRITICAL();
2454 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2463 taskEXIT_CRITICAL();
2467 /*-----------------------------------------------------------*/
2469 BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )
2472 Queue_t * const pxQueue = xQueue;
2474 configASSERT( pxQueue );
2476 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2486 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2487 /*-----------------------------------------------------------*/
2489 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue )
2493 taskENTER_CRITICAL();
2495 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2504 taskEXIT_CRITICAL();
2508 /*-----------------------------------------------------------*/
2510 BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )
2513 Queue_t * const pxQueue = xQueue;
2515 configASSERT( pxQueue );
2517 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2527 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2528 /*-----------------------------------------------------------*/
2530 #if ( configUSE_CO_ROUTINES == 1 )
2532 BaseType_t xQueueCRSend( QueueHandle_t xQueue,
2533 const void * pvItemToQueue,
2534 TickType_t xTicksToWait )
2537 Queue_t * const pxQueue = xQueue;
2539 /* If the queue is already full we may have to block. A critical section
2540 * is required to prevent an interrupt removing something from the queue
2541 * between the check to see if the queue is full and blocking on the queue. */
2542 portDISABLE_INTERRUPTS();
2544 if( prvIsQueueFull( pxQueue ) != pdFALSE )
2546 /* The queue is full - do we want to block or just leave without
2548 if( xTicksToWait > ( TickType_t ) 0 )
2550 /* As this is called from a coroutine we cannot block directly, but
2551 * return indicating that we need to block. */
2552 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
2553 portENABLE_INTERRUPTS();
2554 return errQUEUE_BLOCKED;
2558 portENABLE_INTERRUPTS();
2559 return errQUEUE_FULL;
2563 portENABLE_INTERRUPTS();
2565 portDISABLE_INTERRUPTS();
2567 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2569 /* There is room in the queue, copy the data into the queue. */
2570 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2573 /* Were any co-routines waiting for data to become available? */
2574 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2576 /* In this instance the co-routine could be placed directly
2577 * into the ready list as we are within a critical section.
2578 * Instead the same pending ready list mechanism is used as if
2579 * the event were caused from within an interrupt. */
2580 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2582 /* The co-routine waiting has a higher priority so record
2583 * that a yield might be appropriate. */
2584 xReturn = errQUEUE_YIELD;
2588 mtCOVERAGE_TEST_MARKER();
2593 mtCOVERAGE_TEST_MARKER();
2598 xReturn = errQUEUE_FULL;
2601 portENABLE_INTERRUPTS();
2606 #endif /* configUSE_CO_ROUTINES */
2607 /*-----------------------------------------------------------*/
2609 #if ( configUSE_CO_ROUTINES == 1 )
2611 BaseType_t xQueueCRReceive( QueueHandle_t xQueue,
2613 TickType_t xTicksToWait )
2616 Queue_t * const pxQueue = xQueue;
2618 /* If the queue is already empty we may have to block. A critical section
2619 * is required to prevent an interrupt adding something to the queue
2620 * between the check to see if the queue is empty and blocking on the queue. */
2621 portDISABLE_INTERRUPTS();
2623 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2625 /* There are no messages in the queue, do we want to block or just
2626 * leave with nothing? */
2627 if( xTicksToWait > ( TickType_t ) 0 )
2629 /* As this is a co-routine we cannot block directly, but return
2630 * indicating that we need to block. */
2631 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
2632 portENABLE_INTERRUPTS();
2633 return errQUEUE_BLOCKED;
2637 portENABLE_INTERRUPTS();
2638 return errQUEUE_FULL;
2643 mtCOVERAGE_TEST_MARKER();
2646 portENABLE_INTERRUPTS();
2648 portDISABLE_INTERRUPTS();
2650 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2652 /* Data is available from the queue. */
2653 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2655 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2657 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2661 mtCOVERAGE_TEST_MARKER();
2664 --( pxQueue->uxMessagesWaiting );
2665 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2669 /* Were any co-routines waiting for space to become available? */
2670 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2672 /* In this instance the co-routine could be placed directly
2673 * into the ready list as we are within a critical section.
2674 * Instead the same pending ready list mechanism is used as if
2675 * the event were caused from within an interrupt. */
2676 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2678 xReturn = errQUEUE_YIELD;
2682 mtCOVERAGE_TEST_MARKER();
2687 mtCOVERAGE_TEST_MARKER();
2695 portENABLE_INTERRUPTS();
2700 #endif /* configUSE_CO_ROUTINES */
2701 /*-----------------------------------------------------------*/
2703 #if ( configUSE_CO_ROUTINES == 1 )
2705 BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue,
2706 const void * pvItemToQueue,
2707 BaseType_t xCoRoutinePreviouslyWoken )
2709 Queue_t * const pxQueue = xQueue;
2711 /* Cannot block within an ISR so if there is no space on the queue then
2712 * exit without doing anything. */
2713 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2715 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2717 /* We only want to wake one co-routine per ISR, so check that a
2718 * co-routine has not already been woken. */
2719 if( xCoRoutinePreviouslyWoken == pdFALSE )
2721 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2723 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2729 mtCOVERAGE_TEST_MARKER();
2734 mtCOVERAGE_TEST_MARKER();
2739 mtCOVERAGE_TEST_MARKER();
2744 mtCOVERAGE_TEST_MARKER();
2747 return xCoRoutinePreviouslyWoken;
2750 #endif /* configUSE_CO_ROUTINES */
2751 /*-----------------------------------------------------------*/
2753 #if ( configUSE_CO_ROUTINES == 1 )
2755 BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue,
2757 BaseType_t * pxCoRoutineWoken )
2760 Queue_t * const pxQueue = xQueue;
2762 /* We cannot block from an ISR, so check there is data available. If
2763 * not then just leave without doing anything. */
2764 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2766 /* Copy the data from the queue. */
2767 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
2769 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2771 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2775 mtCOVERAGE_TEST_MARKER();
2778 --( pxQueue->uxMessagesWaiting );
2779 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
2781 if( ( *pxCoRoutineWoken ) == pdFALSE )
2783 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2785 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2787 *pxCoRoutineWoken = pdTRUE;
2791 mtCOVERAGE_TEST_MARKER();
2796 mtCOVERAGE_TEST_MARKER();
2801 mtCOVERAGE_TEST_MARKER();
2814 #endif /* configUSE_CO_ROUTINES */
2815 /*-----------------------------------------------------------*/
2817 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2819 void vQueueAddToRegistry( QueueHandle_t xQueue,
2820 const char * pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2823 QueueRegistryItem_t * pxEntryToWrite = NULL;
2825 configASSERT( xQueue );
2827 if( pcQueueName != NULL )
2829 /* See if there is an empty space in the registry. A NULL name denotes
2831 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2833 /* Replace an existing entry if the queue is already in the registry. */
2834 if( xQueue == xQueueRegistry[ ux ].xHandle )
2836 pxEntryToWrite = &( xQueueRegistry[ ux ] );
2839 /* Otherwise, store in the next empty location */
2840 else if( ( pxEntryToWrite == NULL ) && ( xQueueRegistry[ ux ].pcQueueName == NULL ) )
2842 pxEntryToWrite = &( xQueueRegistry[ ux ] );
2846 mtCOVERAGE_TEST_MARKER();
2851 if( pxEntryToWrite != NULL )
2853 /* Store the information on this queue. */
2854 pxEntryToWrite->pcQueueName = pcQueueName;
2855 pxEntryToWrite->xHandle = xQueue;
2857 traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
2861 #endif /* configQUEUE_REGISTRY_SIZE */
2862 /*-----------------------------------------------------------*/
2864 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2866 const char * pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2869 const char * pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2871 configASSERT( xQueue );
2873 /* Note there is nothing here to protect against another task adding or
2874 * removing entries from the registry while it is being searched. */
2876 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2878 if( xQueueRegistry[ ux ].xHandle == xQueue )
2880 pcReturn = xQueueRegistry[ ux ].pcQueueName;
2885 mtCOVERAGE_TEST_MARKER();
2890 } /*lint !e818 xQueue cannot be a pointer to const because it is a typedef. */
2892 #endif /* configQUEUE_REGISTRY_SIZE */
2893 /*-----------------------------------------------------------*/
2895 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2897 void vQueueUnregisterQueue( QueueHandle_t xQueue )
2901 configASSERT( xQueue );
2903 /* See if the handle of the queue being unregistered in actually in the
2905 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2907 if( xQueueRegistry[ ux ].xHandle == xQueue )
2909 /* Set the name to NULL to show that this slot if free again. */
2910 xQueueRegistry[ ux ].pcQueueName = NULL;
2912 /* Set the handle to NULL to ensure the same queue handle cannot
2913 * appear in the registry twice if it is added, removed, then
2915 xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;
2920 mtCOVERAGE_TEST_MARKER();
2923 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2925 #endif /* configQUEUE_REGISTRY_SIZE */
2926 /*-----------------------------------------------------------*/
2928 #if ( configUSE_TIMERS == 1 )
2930 void vQueueWaitForMessageRestricted( QueueHandle_t xQueue,
2931 TickType_t xTicksToWait,
2932 const BaseType_t xWaitIndefinitely )
2934 Queue_t * const pxQueue = xQueue;
2936 /* This function should not be called by application code hence the
2937 * 'Restricted' in its name. It is not part of the public API. It is
2938 * designed for use by kernel code, and has special calling requirements.
2939 * It can result in vListInsert() being called on a list that can only
2940 * possibly ever have one item in it, so the list will be fast, but even
2941 * so it should be called with the scheduler locked and not from a critical
2944 /* Only do anything if there are no messages in the queue. This function
2945 * will not actually cause the task to block, just place it on a blocked
2946 * list. It will not block until the scheduler is unlocked - at which
2947 * time a yield will be performed. If an item is added to the queue while
2948 * the queue is locked, and the calling task blocks on the queue, then the
2949 * calling task will be immediately unblocked when the queue is unlocked. */
2950 prvLockQueue( pxQueue );
2952 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
2954 /* There is nothing in the queue, block for the specified period. */
2955 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );
2959 mtCOVERAGE_TEST_MARKER();
2962 prvUnlockQueue( pxQueue );
2965 #endif /* configUSE_TIMERS */
2966 /*-----------------------------------------------------------*/
2968 #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
2970 QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
2972 QueueSetHandle_t pxQueue;
2974 pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
2979 #endif /* configUSE_QUEUE_SETS */
2980 /*-----------------------------------------------------------*/
2982 #if ( configUSE_QUEUE_SETS == 1 )
2984 BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore,
2985 QueueSetHandle_t xQueueSet )
2989 taskENTER_CRITICAL();
2991 if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
2993 /* Cannot add a queue/semaphore to more than one queue set. */
2996 else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
2998 /* Cannot add a queue/semaphore to a queue set if there are already
2999 * items in the queue/semaphore. */
3004 ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
3008 taskEXIT_CRITICAL();
3013 #endif /* configUSE_QUEUE_SETS */
3014 /*-----------------------------------------------------------*/
3016 #if ( configUSE_QUEUE_SETS == 1 )
3018 BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore,
3019 QueueSetHandle_t xQueueSet )
3022 Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
3024 if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
3026 /* The queue was not a member of the set. */
3029 else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
3031 /* It is dangerous to remove a queue from a set when the queue is
3032 * not empty because the queue set will still hold pending events for
3038 taskENTER_CRITICAL();
3040 /* The queue is no longer contained in the set. */
3041 pxQueueOrSemaphore->pxQueueSetContainer = NULL;
3043 taskEXIT_CRITICAL();
3048 } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
3050 #endif /* configUSE_QUEUE_SETS */
3051 /*-----------------------------------------------------------*/
3053 #if ( configUSE_QUEUE_SETS == 1 )
3055 QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet,
3056 TickType_t const xTicksToWait )
3058 QueueSetMemberHandle_t xReturn = NULL;
3060 ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait ); /*lint !e961 Casting from one typedef to another is not redundant. */
3064 #endif /* configUSE_QUEUE_SETS */
3065 /*-----------------------------------------------------------*/
3067 #if ( configUSE_QUEUE_SETS == 1 )
3069 QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
3071 QueueSetMemberHandle_t xReturn = NULL;
3073 ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
3077 #endif /* configUSE_QUEUE_SETS */
3078 /*-----------------------------------------------------------*/
3080 #if ( configUSE_QUEUE_SETS == 1 )
3082 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue )
3084 Queue_t * pxQueueSetContainer = pxQueue->pxQueueSetContainer;
3085 BaseType_t xReturn = pdFALSE;
3087 /* This function must be called form a critical section. */
3089 /* The following line is not reachable in unit tests because every call
3090 * to prvNotifyQueueSetContainer is preceded by a check that
3091 * pxQueueSetContainer != NULL */
3092 configASSERT( pxQueueSetContainer ); /* LCOV_EXCL_BR_LINE */
3093 configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
3095 if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
3097 const int8_t cTxLock = pxQueueSetContainer->cTxLock;
3099 traceQUEUE_SET_SEND( pxQueueSetContainer );
3101 /* The data copied is the handle of the queue that contains data. */
3102 xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, queueSEND_TO_BACK );
3104 if( cTxLock == queueUNLOCKED )
3106 if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
3108 if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
3110 /* The task waiting has a higher priority. */
3115 mtCOVERAGE_TEST_MARKER();
3120 mtCOVERAGE_TEST_MARKER();
3125 prvIncrementQueueTxLock( pxQueueSetContainer, cTxLock );
3130 mtCOVERAGE_TEST_MARKER();
3136 #endif /* configUSE_QUEUE_SETS */