2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
\r
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
32 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
33 * all the API functions to use the MPU wrappers. That should only be done when
\r
34 * task.h is included from an application file. */
\r
35 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
37 #include "FreeRTOS.h"
\r
41 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
\r
42 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
\r
43 * for the header files above, but not in this file, in order to generate the
\r
44 * correct privileged Vs unprivileged linkage and placement. */
\r
45 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
\r
48 /* Constants used with the cRxLock and cTxLock structure members. */
\r
49 #define queueUNLOCKED ( ( int8_t ) -1 )
\r
50 #define queueLOCKED_UNMODIFIED ( ( int8_t ) 0 )
\r
51 #define queueINT8_MAX ( ( int8_t ) 127 )
\r
53 /* When the Queue_t structure is used to represent a base queue its pcHead and
\r
54 * pcTail members are used as pointers into the queue storage area. When the
\r
55 * Queue_t structure is used to represent a mutex pcHead and pcTail pointers are
\r
56 * not necessary, and the pcHead pointer is set to NULL to indicate that the
\r
57 * structure instead holds a pointer to the mutex holder (if any). Map alternative
\r
58 * names to the pcHead and structure member to ensure the readability of the code
\r
59 * is maintained. The QueuePointers_t and SemaphoreData_t types are used to form
\r
60 * a union as their usage is mutually exclusive dependent on what the queue is
\r
61 * being used for. */
\r
62 #define uxQueueType pcHead
\r
63 #define queueQUEUE_IS_MUTEX NULL
\r
65 typedef struct QueuePointers
\r
67 int8_t * pcTail; /*< Points to the byte at the end of the queue storage area. Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
\r
68 int8_t * pcReadFrom; /*< Points to the last place that a queued item was read from when the structure is used as a queue. */
\r
71 typedef struct SemaphoreData
\r
73 TaskHandle_t xMutexHolder; /*< The handle of the task that holds the mutex. */
\r
74 UBaseType_t uxRecursiveCallCount; /*< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */
\r
77 /* Semaphores do not actually store or copy data, so have an item size of
\r
79 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )
\r
80 #define queueMUTEX_GIVE_BLOCK_TIME ( ( TickType_t ) 0U )
\r
82 #if ( configUSE_PREEMPTION == 0 )
\r
84 /* If the cooperative scheduler is being used then a yield should not be
\r
85 * performed just because a higher priority task has been woken. */
\r
86 #define queueYIELD_IF_USING_PREEMPTION()
\r
88 #define queueYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
\r
92 * Definition of the queue used by the scheduler.
\r
93 * Items are queued by copy, not reference. See the following link for the
\r
94 * rationale: https://www.FreeRTOS.org/Embedded-RTOS-Queues.html
\r
96 typedef struct QueueDefinition /* The old naming convention is used to prevent breaking kernel aware debuggers. */
\r
98 int8_t * pcHead; /*< Points to the beginning of the queue storage area. */
\r
99 int8_t * pcWriteTo; /*< Points to the free next place in the storage area. */
\r
103 QueuePointers_t xQueue; /*< Data required exclusively when this structure is used as a queue. */
\r
104 SemaphoreData_t xSemaphore; /*< Data required exclusively when this structure is used as a semaphore. */
\r
107 List_t xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
108 List_t xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
110 volatile UBaseType_t uxMessagesWaiting; /*< The number of items currently in the queue. */
\r
111 UBaseType_t uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
112 UBaseType_t uxItemSize; /*< The size of each items that the queue will hold. */
\r
114 volatile int8_t cRxLock; /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
\r
115 volatile int8_t cTxLock; /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
\r
117 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
\r
118 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */
\r
121 #if ( configUSE_QUEUE_SETS == 1 )
\r
122 struct QueueDefinition * pxQueueSetContainer;
\r
125 #if ( configUSE_TRACE_FACILITY == 1 )
\r
126 UBaseType_t uxQueueNumber;
\r
127 uint8_t ucQueueType;
\r
131 /* The old xQUEUE name is maintained above then typedefed to the new Queue_t
\r
132 * name below to enable the use of older kernel aware debuggers. */
\r
133 typedef xQUEUE Queue_t;
\r
135 /*-----------------------------------------------------------*/
\r
138 * The queue registry is just a means for kernel aware debuggers to locate
\r
139 * queue structures. It has no other purpose so is an optional component.
\r
141 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
143 /* The type stored within the queue registry array. This allows a name
\r
144 * to be assigned to each queue making kernel aware debugging a little
\r
145 * more user friendly. */
\r
146 typedef struct QUEUE_REGISTRY_ITEM
\r
148 const char * pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
149 QueueHandle_t xHandle;
\r
150 } xQueueRegistryItem;
\r
152 /* The old xQueueRegistryItem name is maintained above then typedefed to the
\r
153 * new xQueueRegistryItem name below to enable the use of older kernel aware
\r
155 typedef xQueueRegistryItem QueueRegistryItem_t;
\r
157 /* The queue registry is simply an array of QueueRegistryItem_t structures.
\r
158 * The pcQueueName member of a structure being NULL is indicative of the
\r
159 * array position being vacant. */
\r
160 PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
162 #endif /* configQUEUE_REGISTRY_SIZE */
\r
165 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
166 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
167 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
168 * queue is locked it will instead increment the appropriate queue lock count
\r
169 * to indicate that a task may require unblocking. When the queue in unlocked
\r
170 * these lock counts are inspected, and the appropriate action taken.
\r
172 static void prvUnlockQueue( Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
\r
175 * Uses a critical section to determine if there is any data in a queue.
\r
177 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
179 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
\r
182 * Uses a critical section to determine if there is any space in a queue.
\r
184 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
186 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
\r
189 * Copies an item into the queue, either at the front of the queue or the
\r
190 * back of the queue.
\r
192 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
\r
193 const void * pvItemToQueue,
\r
194 const BaseType_t xPosition ) PRIVILEGED_FUNCTION;
\r
197 * Copies an item out of a queue.
\r
199 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
\r
200 void * const pvBuffer ) PRIVILEGED_FUNCTION;
\r
202 #if ( configUSE_QUEUE_SETS == 1 )
\r
205 * Checks to see if a queue is a member of a queue set, and if so, notifies
\r
206 * the queue set that the queue contains data.
\r
208 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
\r
212 * Called after a Queue_t structure has been allocated either statically or
\r
213 * dynamically to fill in the structure's members.
\r
215 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
\r
216 const UBaseType_t uxItemSize,
\r
217 uint8_t * pucQueueStorage,
\r
218 const uint8_t ucQueueType,
\r
219 Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
\r
222 * Mutexes are a special type of queue. When a mutex is created, first the
\r
223 * queue is created, then prvInitialiseMutex() is called to configure the queue
\r
226 #if ( configUSE_MUTEXES == 1 )
\r
227 static void prvInitialiseMutex( Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
\r
230 #if ( configUSE_MUTEXES == 1 )
\r
233 * If a task waiting for a mutex causes the mutex holder to inherit a
\r
234 * priority, but the waiting task times out, then the holder should
\r
235 * disinherit the priority - but only down to the highest priority of any
\r
236 * other tasks that are waiting for the same mutex. This function returns
\r
239 static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
\r
241 /*-----------------------------------------------------------*/
\r
244 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
245 * accessing the queue event lists.
\r
247 #define prvLockQueue( pxQueue ) \
\r
248 taskENTER_CRITICAL(); \
\r
250 if( ( pxQueue )->cRxLock == queueUNLOCKED ) \
\r
252 ( pxQueue )->cRxLock = queueLOCKED_UNMODIFIED; \
\r
254 if( ( pxQueue )->cTxLock == queueUNLOCKED ) \
\r
256 ( pxQueue )->cTxLock = queueLOCKED_UNMODIFIED; \
\r
259 taskEXIT_CRITICAL()
\r
262 * Macro to increment cTxLock member of the queue data structure. It is
\r
263 * capped at the number of tasks in the system as we cannot unblock more
\r
264 * tasks than the number of tasks in the system.
\r
266 #define prvIncrementQueueTxLock( pxQueue, cTxLock ) \
\r
268 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
\r
269 if( ( UBaseType_t ) ( cTxLock ) < uxNumberOfTasks ) \
\r
271 configASSERT( ( cTxLock ) != queueINT8_MAX ); \
\r
272 ( pxQueue )->cTxLock = ( int8_t ) ( ( cTxLock ) + ( int8_t ) 1 ); \
\r
277 * Macro to increment cRxLock member of the queue data structure. It is
\r
278 * capped at the number of tasks in the system as we cannot unblock more
\r
279 * tasks than the number of tasks in the system.
\r
281 #define prvIncrementQueueRxLock( pxQueue, cRxLock ) \
\r
283 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
\r
284 if( ( UBaseType_t ) ( cRxLock ) < uxNumberOfTasks ) \
\r
286 configASSERT( ( cRxLock ) != queueINT8_MAX ); \
\r
287 ( pxQueue )->cRxLock = ( int8_t ) ( ( cRxLock ) + ( int8_t ) 1 ); \
\r
290 /*-----------------------------------------------------------*/
\r
292 BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
\r
293 BaseType_t xNewQueue )
\r
295 BaseType_t xReturn = pdPASS;
\r
296 Queue_t * const pxQueue = xQueue;
\r
298 configASSERT( pxQueue );
\r
300 if( ( pxQueue != NULL ) &&
\r
301 ( pxQueue->uxLength >= 1U ) &&
\r
302 /* Check for multiplication overflow. */
\r
303 ( ( SIZE_MAX / pxQueue->uxLength ) >= pxQueue->uxItemSize ) )
\r
305 taskENTER_CRITICAL();
\r
307 pxQueue->u.xQueue.pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
\r
308 pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
\r
309 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
310 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - 1U ) * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
\r
311 pxQueue->cRxLock = queueUNLOCKED;
\r
312 pxQueue->cTxLock = queueUNLOCKED;
\r
314 if( xNewQueue == pdFALSE )
\r
316 /* If there are tasks blocked waiting to read from the queue, then
\r
317 * the tasks will remain blocked as after this function exits the queue
\r
318 * will still be empty. If there are tasks blocked waiting to write to
\r
319 * the queue, then one should be unblocked as after this function exits
\r
320 * it will be possible to write to it. */
\r
321 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
323 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
325 queueYIELD_IF_USING_PREEMPTION();
\r
329 mtCOVERAGE_TEST_MARKER();
\r
334 mtCOVERAGE_TEST_MARKER();
\r
339 /* Ensure the event queues start in the correct state. */
\r
340 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
\r
341 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
\r
344 taskEXIT_CRITICAL();
\r
351 configASSERT( xReturn != pdFAIL );
\r
353 /* A value is returned for calling semantic consistency with previous
\r
357 /*-----------------------------------------------------------*/
\r
359 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
361 QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,
\r
362 const UBaseType_t uxItemSize,
\r
363 uint8_t * pucQueueStorage,
\r
364 StaticQueue_t * pxStaticQueue,
\r
365 const uint8_t ucQueueType )
\r
367 Queue_t * pxNewQueue = NULL;
\r
369 /* The StaticQueue_t structure and the queue storage area must be
\r
371 configASSERT( pxStaticQueue );
\r
373 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
\r
374 ( pxStaticQueue != NULL ) &&
\r
376 /* A queue storage area should be provided if the item size is not 0, and
\r
377 * should not be provided if the item size is 0. */
\r
378 ( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) ) &&
\r
379 ( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) ) )
\r
381 #if ( configASSERT_DEFINED == 1 )
\r
383 /* Sanity check that the size of the structure used to declare a
\r
384 * variable of type StaticQueue_t or StaticSemaphore_t equals the size of
\r
385 * the real queue and semaphore structures. */
\r
386 volatile size_t xSize = sizeof( StaticQueue_t );
\r
388 /* This assertion cannot be branch covered in unit tests */
\r
389 configASSERT( xSize == sizeof( Queue_t ) ); /* LCOV_EXCL_BR_LINE */
\r
390 ( void ) xSize; /* Keeps lint quiet when configASSERT() is not defined. */
\r
392 #endif /* configASSERT_DEFINED */
\r
394 /* The address of a statically allocated queue was passed in, use it.
\r
395 * The address of a statically allocated storage area was also passed in
\r
396 * but is already set. */
\r
397 pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
\r
399 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
401 /* Queues can be allocated wither statically or dynamically, so
\r
402 * note this queue was allocated statically in case the queue is
\r
403 * later deleted. */
\r
404 pxNewQueue->ucStaticallyAllocated = pdTRUE;
\r
406 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
\r
408 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
\r
412 configASSERT( pxNewQueue );
\r
413 mtCOVERAGE_TEST_MARKER();
\r
419 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
420 /*-----------------------------------------------------------*/
\r
422 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
424 QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
\r
425 const UBaseType_t uxItemSize,
\r
426 const uint8_t ucQueueType )
\r
428 Queue_t * pxNewQueue = NULL;
\r
429 size_t xQueueSizeInBytes;
\r
430 uint8_t * pucQueueStorage;
\r
432 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
\r
433 /* Check for multiplication overflow. */
\r
434 ( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&
\r
435 /* Check for addition overflow. */
\r
436 ( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( uxQueueLength * uxItemSize ) ) )
\r
438 /* Allocate enough space to hold the maximum number of items that
\r
439 * can be in the queue at any time. It is valid for uxItemSize to be
\r
440 * zero in the case the queue is used as a semaphore. */
\r
441 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
\r
443 /* Allocate the queue and storage area. Justification for MISRA
\r
444 * deviation as follows: pvPortMalloc() always ensures returned memory
\r
445 * blocks are aligned per the requirements of the MCU stack. In this case
\r
446 * pvPortMalloc() must return a pointer that is guaranteed to meet the
\r
447 * alignment requirements of the Queue_t structure - which in this case
\r
448 * is an int8_t *. Therefore, whenever the stack alignment requirements
\r
449 * are greater than or equal to the pointer to char requirements the cast
\r
450 * is safe. In other cases alignment requirements are not strict (one or
\r
452 pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */
\r
454 if( pxNewQueue != NULL )
\r
456 /* Jump past the queue structure to find the location of the queue
\r
458 pucQueueStorage = ( uint8_t * ) pxNewQueue;
\r
459 pucQueueStorage += sizeof( Queue_t ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
\r
461 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
463 /* Queues can be created either statically or dynamically, so
\r
464 * note this task was created dynamically in case it is later
\r
466 pxNewQueue->ucStaticallyAllocated = pdFALSE;
\r
468 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
470 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
\r
474 traceQUEUE_CREATE_FAILED( ucQueueType );
\r
475 mtCOVERAGE_TEST_MARKER();
\r
480 configASSERT( pxNewQueue );
\r
481 mtCOVERAGE_TEST_MARKER();
\r
487 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
488 /*-----------------------------------------------------------*/
\r
490 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
\r
491 const UBaseType_t uxItemSize,
\r
492 uint8_t * pucQueueStorage,
\r
493 const uint8_t ucQueueType,
\r
494 Queue_t * pxNewQueue )
\r
496 /* Remove compiler warnings about unused parameters should
\r
497 * configUSE_TRACE_FACILITY not be set to 1. */
\r
498 ( void ) ucQueueType;
\r
500 if( uxItemSize == ( UBaseType_t ) 0 )
\r
502 /* No RAM was allocated for the queue storage area, but PC head cannot
\r
503 * be set to NULL because NULL is used as a key to say the queue is used as
\r
504 * a mutex. Therefore just set pcHead to point to the queue as a benign
\r
505 * value that is known to be within the memory map. */
\r
506 pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
\r
510 /* Set the head to the start of the queue storage area. */
\r
511 pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
\r
514 /* Initialise the queue members as described where the queue type is
\r
516 pxNewQueue->uxLength = uxQueueLength;
\r
517 pxNewQueue->uxItemSize = uxItemSize;
\r
518 ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
\r
520 #if ( configUSE_TRACE_FACILITY == 1 )
\r
522 pxNewQueue->ucQueueType = ucQueueType;
\r
524 #endif /* configUSE_TRACE_FACILITY */
\r
526 #if ( configUSE_QUEUE_SETS == 1 )
\r
528 pxNewQueue->pxQueueSetContainer = NULL;
\r
530 #endif /* configUSE_QUEUE_SETS */
\r
532 traceQUEUE_CREATE( pxNewQueue );
\r
534 /*-----------------------------------------------------------*/
\r
536 #if ( configUSE_MUTEXES == 1 )
\r
538 static void prvInitialiseMutex( Queue_t * pxNewQueue )
\r
540 if( pxNewQueue != NULL )
\r
542 /* The queue create function will set all the queue structure members
\r
543 * correctly for a generic queue, but this function is creating a
\r
544 * mutex. Overwrite those members that need to be set differently -
\r
545 * in particular the information required for priority inheritance. */
\r
546 pxNewQueue->u.xSemaphore.xMutexHolder = NULL;
\r
547 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
549 /* In case this is a recursive mutex. */
\r
550 pxNewQueue->u.xSemaphore.uxRecursiveCallCount = 0;
\r
552 traceCREATE_MUTEX( pxNewQueue );
\r
554 /* Start with the semaphore in the expected state. */
\r
555 ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
\r
559 traceCREATE_MUTEX_FAILED();
\r
563 #endif /* configUSE_MUTEXES */
\r
564 /*-----------------------------------------------------------*/
\r
566 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
\r
568 QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
\r
570 QueueHandle_t xNewQueue;
\r
571 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
\r
573 xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
\r
574 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
\r
579 #endif /* configUSE_MUTEXES */
\r
580 /*-----------------------------------------------------------*/
\r
582 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
\r
584 QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
\r
585 StaticQueue_t * pxStaticQueue )
\r
587 QueueHandle_t xNewQueue;
\r
588 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
\r
590 /* Prevent compiler warnings about unused parameters if
\r
591 * configUSE_TRACE_FACILITY does not equal 1. */
\r
592 ( void ) ucQueueType;
\r
594 xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
\r
595 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
\r
600 #endif /* configUSE_MUTEXES */
\r
601 /*-----------------------------------------------------------*/
\r
603 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
\r
605 TaskHandle_t xQueueGetMutexHolder( QueueHandle_t xSemaphore )
\r
607 TaskHandle_t pxReturn;
\r
608 Queue_t * const pxSemaphore = ( Queue_t * ) xSemaphore;
\r
610 configASSERT( xSemaphore );
\r
612 /* This function is called by xSemaphoreGetMutexHolder(), and should not
\r
613 * be called directly. Note: This is a good way of determining if the
\r
614 * calling task is the mutex holder, but not a good way of determining the
\r
615 * identity of the mutex holder, as the holder may change between the
\r
616 * following critical section exiting and the function returning. */
\r
617 taskENTER_CRITICAL();
\r
619 if( pxSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
\r
621 pxReturn = pxSemaphore->u.xSemaphore.xMutexHolder;
\r
628 taskEXIT_CRITICAL();
\r
631 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
\r
633 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
\r
634 /*-----------------------------------------------------------*/
\r
636 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
\r
638 TaskHandle_t xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )
\r
640 TaskHandle_t pxReturn;
\r
642 configASSERT( xSemaphore );
\r
644 /* Mutexes cannot be used in interrupt service routines, so the mutex
\r
645 * holder should not change in an ISR, and therefore a critical section is
\r
646 * not required here. */
\r
647 if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
\r
649 pxReturn = ( ( Queue_t * ) xSemaphore )->u.xSemaphore.xMutexHolder;
\r
657 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
\r
659 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
\r
660 /*-----------------------------------------------------------*/
\r
662 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
\r
664 BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
\r
666 BaseType_t xReturn;
\r
667 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
\r
669 configASSERT( pxMutex );
\r
671 /* If this is the task that holds the mutex then xMutexHolder will not
\r
672 * change outside of this task. If this task does not hold the mutex then
\r
673 * pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
674 * this is the only condition we are interested in it does not matter if
\r
675 * pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
676 * mutual exclusion is required to test the pxMutexHolder variable. */
\r
677 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
\r
679 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
681 /* uxRecursiveCallCount cannot be zero if xMutexHolder is equal to
\r
682 * the task handle, therefore no underflow check is required. Also,
\r
683 * uxRecursiveCallCount is only modified by the mutex holder, and as
\r
684 * there can only be one, no mutual exclusion is required to modify the
\r
685 * uxRecursiveCallCount member. */
\r
686 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )--;
\r
688 /* Has the recursive call count unwound to 0? */
\r
689 if( pxMutex->u.xSemaphore.uxRecursiveCallCount == ( UBaseType_t ) 0 )
\r
691 /* Return the mutex. This will automatically unblock any other
\r
692 * task that might be waiting to access the mutex. */
\r
693 ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
697 mtCOVERAGE_TEST_MARKER();
\r
704 /* The mutex cannot be given because the calling task is not the
\r
708 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
714 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
715 /*-----------------------------------------------------------*/
\r
717 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
\r
719 BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
\r
720 TickType_t xTicksToWait )
\r
722 BaseType_t xReturn;
\r
723 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
\r
725 configASSERT( pxMutex );
\r
727 /* Comments regarding mutual exclusion as per those within
\r
728 * xQueueGiveMutexRecursive(). */
\r
730 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
732 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
\r
734 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
\r
739 xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );
\r
741 /* pdPASS will only be returned if the mutex was successfully
\r
742 * obtained. The calling task may have entered the Blocked state
\r
743 * before reaching here. */
\r
744 if( xReturn != pdFAIL )
\r
746 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
\r
750 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
757 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
758 /*-----------------------------------------------------------*/
\r
760 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
\r
762 QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
\r
763 const UBaseType_t uxInitialCount,
\r
764 StaticQueue_t * pxStaticQueue )
\r
766 QueueHandle_t xHandle = NULL;
\r
768 if( ( uxMaxCount != 0 ) &&
\r
769 ( uxInitialCount <= uxMaxCount ) )
\r
771 xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
\r
773 if( xHandle != NULL )
\r
775 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
\r
777 traceCREATE_COUNTING_SEMAPHORE();
\r
781 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
786 configASSERT( xHandle );
\r
787 mtCOVERAGE_TEST_MARKER();
\r
793 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
\r
794 /*-----------------------------------------------------------*/
\r
796 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
\r
798 QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
\r
799 const UBaseType_t uxInitialCount )
\r
801 QueueHandle_t xHandle = NULL;
\r
803 if( ( uxMaxCount != 0 ) &&
\r
804 ( uxInitialCount <= uxMaxCount ) )
\r
806 xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
\r
808 if( xHandle != NULL )
\r
810 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
\r
812 traceCREATE_COUNTING_SEMAPHORE();
\r
816 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
821 configASSERT( xHandle );
\r
822 mtCOVERAGE_TEST_MARKER();
\r
828 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
\r
829 /*-----------------------------------------------------------*/
\r
831 BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
\r
832 const void * const pvItemToQueue,
\r
833 TickType_t xTicksToWait,
\r
834 const BaseType_t xCopyPosition )
\r
836 BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
\r
837 TimeOut_t xTimeOut;
\r
838 Queue_t * const pxQueue = xQueue;
\r
840 configASSERT( pxQueue );
\r
841 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
\r
842 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
\r
843 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
\r
845 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
\r
849 /*lint -save -e904 This function relaxes the coding standard somewhat to
\r
850 * allow return statements within the function itself. This is done in the
\r
851 * interest of execution time efficiency. */
\r
854 taskENTER_CRITICAL();
\r
856 /* Is there room on the queue now? The running task must be the
\r
857 * highest priority task wanting to access the queue. If the head item
\r
858 * in the queue is to be overwritten then it does not matter if the
\r
859 * queue is full. */
\r
860 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
\r
862 traceQUEUE_SEND( pxQueue );
\r
864 #if ( configUSE_QUEUE_SETS == 1 )
\r
866 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
\r
868 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
870 if( pxQueue->pxQueueSetContainer != NULL )
\r
872 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
\r
874 /* Do not notify the queue set as an existing item
\r
875 * was overwritten in the queue so the number of items
\r
876 * in the queue has not changed. */
\r
877 mtCOVERAGE_TEST_MARKER();
\r
879 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
\r
881 /* The queue is a member of a queue set, and posting
\r
882 * to the queue set caused a higher priority task to
\r
883 * unblock. A context switch is required. */
\r
884 queueYIELD_IF_USING_PREEMPTION();
\r
888 mtCOVERAGE_TEST_MARKER();
\r
893 /* If there was a task waiting for data to arrive on the
\r
894 * queue then unblock it now. */
\r
895 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
897 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
899 /* The unblocked task has a priority higher than
\r
900 * our own so yield immediately. Yes it is ok to
\r
901 * do this from within the critical section - the
\r
902 * kernel takes care of that. */
\r
903 queueYIELD_IF_USING_PREEMPTION();
\r
907 mtCOVERAGE_TEST_MARKER();
\r
910 else if( xYieldRequired != pdFALSE )
\r
912 /* This path is a special case that will only get
\r
913 * executed if the task was holding multiple mutexes
\r
914 * and the mutexes were given back in an order that is
\r
915 * different to that in which they were taken. */
\r
916 queueYIELD_IF_USING_PREEMPTION();
\r
920 mtCOVERAGE_TEST_MARKER();
\r
924 #else /* configUSE_QUEUE_SETS */
\r
926 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
928 /* If there was a task waiting for data to arrive on the
\r
929 * queue then unblock it now. */
\r
930 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
932 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
934 /* The unblocked task has a priority higher than
\r
935 * our own so yield immediately. Yes it is ok to do
\r
936 * this from within the critical section - the kernel
\r
937 * takes care of that. */
\r
938 queueYIELD_IF_USING_PREEMPTION();
\r
942 mtCOVERAGE_TEST_MARKER();
\r
945 else if( xYieldRequired != pdFALSE )
\r
947 /* This path is a special case that will only get
\r
948 * executed if the task was holding multiple mutexes and
\r
949 * the mutexes were given back in an order that is
\r
950 * different to that in which they were taken. */
\r
951 queueYIELD_IF_USING_PREEMPTION();
\r
955 mtCOVERAGE_TEST_MARKER();
\r
958 #endif /* configUSE_QUEUE_SETS */
\r
960 taskEXIT_CRITICAL();
\r
965 if( xTicksToWait == ( TickType_t ) 0 )
\r
967 /* The queue was full and no block time is specified (or
\r
968 * the block time has expired) so leave now. */
\r
969 taskEXIT_CRITICAL();
\r
971 /* Return to the original privilege level before exiting
\r
973 traceQUEUE_SEND_FAILED( pxQueue );
\r
974 return errQUEUE_FULL;
\r
976 else if( xEntryTimeSet == pdFALSE )
\r
978 /* The queue was full and a block time was specified so
\r
979 * configure the timeout structure. */
\r
980 vTaskInternalSetTimeOutState( &xTimeOut );
\r
981 xEntryTimeSet = pdTRUE;
\r
985 /* Entry time was already set. */
\r
986 mtCOVERAGE_TEST_MARKER();
\r
990 taskEXIT_CRITICAL();
\r
992 /* Interrupts and other tasks can send to and receive from the queue
\r
993 * now the critical section has been exited. */
\r
996 prvLockQueue( pxQueue );
\r
998 /* Update the timeout state to see if it has expired yet. */
\r
999 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1001 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
1003 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
1004 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
1006 /* Unlocking the queue means queue events can effect the
\r
1007 * event list. It is possible that interrupts occurring now
\r
1008 * remove this task from the event list again - but as the
\r
1009 * scheduler is suspended the task will go onto the pending
\r
1010 * ready list instead of the actual ready list. */
\r
1011 prvUnlockQueue( pxQueue );
\r
1013 /* Resuming the scheduler will move tasks from the pending
\r
1014 * ready list into the ready list - so it is feasible that this
\r
1015 * task is already in the ready list before it yields - in which
\r
1016 * case the yield will not cause a context switch unless there
\r
1017 * is also a higher priority task in the pending ready list. */
\r
1018 if( xTaskResumeAll() == pdFALSE )
\r
1020 portYIELD_WITHIN_API();
\r
1026 prvUnlockQueue( pxQueue );
\r
1027 ( void ) xTaskResumeAll();
\r
1032 /* The timeout has expired. */
\r
1033 prvUnlockQueue( pxQueue );
\r
1034 ( void ) xTaskResumeAll();
\r
1036 traceQUEUE_SEND_FAILED( pxQueue );
\r
1037 return errQUEUE_FULL;
\r
1039 } /*lint -restore */
\r
1041 /*-----------------------------------------------------------*/
\r
1043 BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,
\r
1044 const void * const pvItemToQueue,
\r
1045 BaseType_t * const pxHigherPriorityTaskWoken,
\r
1046 const BaseType_t xCopyPosition )
\r
1048 BaseType_t xReturn;
\r
1049 UBaseType_t uxSavedInterruptStatus;
\r
1050 Queue_t * const pxQueue = xQueue;
\r
1052 configASSERT( pxQueue );
\r
1053 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
\r
1054 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
\r
1056 /* RTOS ports that support interrupt nesting have the concept of a maximum
\r
1057 * system call (or maximum API call) interrupt priority. Interrupts that are
\r
1058 * above the maximum system call priority are kept permanently enabled, even
\r
1059 * when the RTOS kernel is in a critical section, but cannot make any calls to
\r
1060 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
\r
1061 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
\r
1062 * failure if a FreeRTOS API function is called from an interrupt that has been
\r
1063 * assigned a priority above the configured maximum system call priority.
\r
1064 * Only FreeRTOS functions that end in FromISR can be called from interrupts
\r
1065 * that have been assigned a priority at or (logically) below the maximum
\r
1066 * system call interrupt priority. FreeRTOS maintains a separate interrupt
\r
1067 * safe API to ensure interrupt entry is as fast and as simple as possible.
\r
1068 * More information (albeit Cortex-M specific) is provided on the following
\r
1069 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
\r
1070 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
\r
1072 /* Similar to xQueueGenericSend, except without blocking if there is no room
\r
1073 * in the queue. Also don't directly wake a task that was blocked on a queue
\r
1074 * read, instead return a flag to say whether a context switch is required or
\r
1075 * not (i.e. has a task with a higher priority than us been woken by this
\r
1077 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1079 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
\r
1081 const int8_t cTxLock = pxQueue->cTxLock;
\r
1082 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
\r
1084 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
1086 /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a
\r
1087 * semaphore or mutex. That means prvCopyDataToQueue() cannot result
\r
1088 * in a task disinheriting a priority and prvCopyDataToQueue() can be
\r
1089 * called here even though the disinherit function does not check if
\r
1090 * the scheduler is suspended before accessing the ready lists. */
\r
1091 ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
1093 /* The event list is not altered if the queue is locked. This will
\r
1094 * be done when the queue is unlocked later. */
\r
1095 if( cTxLock == queueUNLOCKED )
\r
1097 #if ( configUSE_QUEUE_SETS == 1 )
\r
1099 if( pxQueue->pxQueueSetContainer != NULL )
\r
1101 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
\r
1103 /* Do not notify the queue set as an existing item
\r
1104 * was overwritten in the queue so the number of items
\r
1105 * in the queue has not changed. */
\r
1106 mtCOVERAGE_TEST_MARKER();
\r
1108 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
\r
1110 /* The queue is a member of a queue set, and posting
\r
1111 * to the queue set caused a higher priority task to
\r
1112 * unblock. A context switch is required. */
\r
1113 if( pxHigherPriorityTaskWoken != NULL )
\r
1115 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1119 mtCOVERAGE_TEST_MARKER();
\r
1124 mtCOVERAGE_TEST_MARKER();
\r
1129 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1131 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1133 /* The task waiting has a higher priority so
\r
1134 * record that a context switch is required. */
\r
1135 if( pxHigherPriorityTaskWoken != NULL )
\r
1137 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1141 mtCOVERAGE_TEST_MARKER();
\r
1146 mtCOVERAGE_TEST_MARKER();
\r
1151 mtCOVERAGE_TEST_MARKER();
\r
1155 #else /* configUSE_QUEUE_SETS */
\r
1157 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1159 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1161 /* The task waiting has a higher priority so record that a
\r
1162 * context switch is required. */
\r
1163 if( pxHigherPriorityTaskWoken != NULL )
\r
1165 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1169 mtCOVERAGE_TEST_MARKER();
\r
1174 mtCOVERAGE_TEST_MARKER();
\r
1179 mtCOVERAGE_TEST_MARKER();
\r
1182 /* Not used in this path. */
\r
1183 ( void ) uxPreviousMessagesWaiting;
\r
1185 #endif /* configUSE_QUEUE_SETS */
\r
1189 /* Increment the lock count so the task that unlocks the queue
\r
1190 * knows that data was posted while it was locked. */
\r
1191 prvIncrementQueueTxLock( pxQueue, cTxLock );
\r
1198 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
1199 xReturn = errQUEUE_FULL;
\r
1202 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1206 /*-----------------------------------------------------------*/
\r
1208 BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,
\r
1209 BaseType_t * const pxHigherPriorityTaskWoken )
\r
1211 BaseType_t xReturn;
\r
1212 UBaseType_t uxSavedInterruptStatus;
\r
1213 Queue_t * const pxQueue = xQueue;
\r
1215 /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
\r
1216 * item size is 0. Don't directly wake a task that was blocked on a queue
\r
1217 * read, instead return a flag to say whether a context switch is required or
\r
1218 * not (i.e. has a task with a higher priority than us been woken by this
\r
1221 configASSERT( pxQueue );
\r
1223 /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()
\r
1224 * if the item size is not 0. */
\r
1225 configASSERT( pxQueue->uxItemSize == 0 );
\r
1227 /* Normally a mutex would not be given from an interrupt, especially if
\r
1228 * there is a mutex holder, as priority inheritance makes no sense for an
\r
1229 * interrupts, only tasks. */
\r
1230 configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->u.xSemaphore.xMutexHolder != NULL ) ) );
\r
1232 /* RTOS ports that support interrupt nesting have the concept of a maximum
\r
1233 * system call (or maximum API call) interrupt priority. Interrupts that are
\r
1234 * above the maximum system call priority are kept permanently enabled, even
\r
1235 * when the RTOS kernel is in a critical section, but cannot make any calls to
\r
1236 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
\r
1237 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
\r
1238 * failure if a FreeRTOS API function is called from an interrupt that has been
\r
1239 * assigned a priority above the configured maximum system call priority.
\r
1240 * Only FreeRTOS functions that end in FromISR can be called from interrupts
\r
1241 * that have been assigned a priority at or (logically) below the maximum
\r
1242 * system call interrupt priority. FreeRTOS maintains a separate interrupt
\r
1243 * safe API to ensure interrupt entry is as fast and as simple as possible.
\r
1244 * More information (albeit Cortex-M specific) is provided on the following
\r
1245 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
\r
1246 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
\r
1248 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1250 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
\r
1252 /* When the queue is used to implement a semaphore no data is ever
\r
1253 * moved through the queue but it is still valid to see if the queue 'has
\r
1255 if( uxMessagesWaiting < pxQueue->uxLength )
\r
1257 const int8_t cTxLock = pxQueue->cTxLock;
\r
1259 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
1261 /* A task can only have an inherited priority if it is a mutex
\r
1262 * holder - and if there is a mutex holder then the mutex cannot be
\r
1263 * given from an ISR. As this is the ISR version of the function it
\r
1264 * can be assumed there is no mutex holder and no need to determine if
\r
1265 * priority disinheritance is needed. Simply increase the count of
\r
1266 * messages (semaphores) available. */
\r
1267 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
\r
1269 /* The event list is not altered if the queue is locked. This will
\r
1270 * be done when the queue is unlocked later. */
\r
1271 if( cTxLock == queueUNLOCKED )
\r
1273 #if ( configUSE_QUEUE_SETS == 1 )
\r
1275 if( pxQueue->pxQueueSetContainer != NULL )
\r
1277 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
\r
1279 /* The semaphore is a member of a queue set, and
\r
1280 * posting to the queue set caused a higher priority
\r
1281 * task to unblock. A context switch is required. */
\r
1282 if( pxHigherPriorityTaskWoken != NULL )
\r
1284 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1288 mtCOVERAGE_TEST_MARKER();
\r
1293 mtCOVERAGE_TEST_MARKER();
\r
1298 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1300 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1302 /* The task waiting has a higher priority so
\r
1303 * record that a context switch is required. */
\r
1304 if( pxHigherPriorityTaskWoken != NULL )
\r
1306 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1310 mtCOVERAGE_TEST_MARKER();
\r
1315 mtCOVERAGE_TEST_MARKER();
\r
1320 mtCOVERAGE_TEST_MARKER();
\r
1324 #else /* configUSE_QUEUE_SETS */
\r
1326 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1328 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1330 /* The task waiting has a higher priority so record that a
\r
1331 * context switch is required. */
\r
1332 if( pxHigherPriorityTaskWoken != NULL )
\r
1334 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1338 mtCOVERAGE_TEST_MARKER();
\r
1343 mtCOVERAGE_TEST_MARKER();
\r
1348 mtCOVERAGE_TEST_MARKER();
\r
1351 #endif /* configUSE_QUEUE_SETS */
\r
1355 /* Increment the lock count so the task that unlocks the queue
\r
1356 * knows that data was posted while it was locked. */
\r
1357 prvIncrementQueueTxLock( pxQueue, cTxLock );
\r
1364 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
1365 xReturn = errQUEUE_FULL;
\r
1368 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1372 /*-----------------------------------------------------------*/
\r
1374 BaseType_t xQueueReceive( QueueHandle_t xQueue,
\r
1375 void * const pvBuffer,
\r
1376 TickType_t xTicksToWait )
\r
1378 BaseType_t xEntryTimeSet = pdFALSE;
\r
1379 TimeOut_t xTimeOut;
\r
1380 Queue_t * const pxQueue = xQueue;
\r
1382 /* Check the pointer is not NULL. */
\r
1383 configASSERT( ( pxQueue ) );
\r
1385 /* The buffer into which data is received can only be NULL if the data size
\r
1386 * is zero (so no data is copied into the buffer). */
\r
1387 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
\r
1389 /* Cannot block if the scheduler is suspended. */
\r
1390 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
\r
1392 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
\r
1396 /*lint -save -e904 This function relaxes the coding standard somewhat to
\r
1397 * allow return statements within the function itself. This is done in the
\r
1398 * interest of execution time efficiency. */
\r
1401 taskENTER_CRITICAL();
\r
1403 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
\r
1405 /* Is there data in the queue now? To be running the calling task
\r
1406 * must be the highest priority task wanting to access the queue. */
\r
1407 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
\r
1409 /* Data available, remove one item. */
\r
1410 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1411 traceQUEUE_RECEIVE( pxQueue );
\r
1412 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
\r
1414 /* There is now space in the queue, were any tasks waiting to
\r
1415 * post to the queue? If so, unblock the highest priority waiting
\r
1417 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1419 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1421 queueYIELD_IF_USING_PREEMPTION();
\r
1425 mtCOVERAGE_TEST_MARKER();
\r
1430 mtCOVERAGE_TEST_MARKER();
\r
1433 taskEXIT_CRITICAL();
\r
1438 if( xTicksToWait == ( TickType_t ) 0 )
\r
1440 /* The queue was empty and no block time is specified (or
\r
1441 * the block time has expired) so leave now. */
\r
1442 taskEXIT_CRITICAL();
\r
1443 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1444 return errQUEUE_EMPTY;
\r
1446 else if( xEntryTimeSet == pdFALSE )
\r
1448 /* The queue was empty and a block time was specified so
\r
1449 * configure the timeout structure. */
\r
1450 vTaskInternalSetTimeOutState( &xTimeOut );
\r
1451 xEntryTimeSet = pdTRUE;
\r
1455 /* Entry time was already set. */
\r
1456 mtCOVERAGE_TEST_MARKER();
\r
1460 taskEXIT_CRITICAL();
\r
1462 /* Interrupts and other tasks can send to and receive from the queue
\r
1463 * now the critical section has been exited. */
\r
1465 vTaskSuspendAll();
\r
1466 prvLockQueue( pxQueue );
\r
1468 /* Update the timeout state to see if it has expired yet. */
\r
1469 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1471 /* The timeout has not expired. If the queue is still empty place
\r
1472 * the task on the list of tasks waiting to receive from the queue. */
\r
1473 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1475 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
1476 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1477 prvUnlockQueue( pxQueue );
\r
1479 if( xTaskResumeAll() == pdFALSE )
\r
1481 portYIELD_WITHIN_API();
\r
1485 mtCOVERAGE_TEST_MARKER();
\r
1490 /* The queue contains data again. Loop back to try and read the
\r
1492 prvUnlockQueue( pxQueue );
\r
1493 ( void ) xTaskResumeAll();
\r
1498 /* Timed out. If there is no data in the queue exit, otherwise loop
\r
1499 * back and attempt to read the data. */
\r
1500 prvUnlockQueue( pxQueue );
\r
1501 ( void ) xTaskResumeAll();
\r
1503 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1505 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1506 return errQUEUE_EMPTY;
\r
1510 mtCOVERAGE_TEST_MARKER();
\r
1513 } /*lint -restore */
\r
1515 /*-----------------------------------------------------------*/
\r
1517 BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
\r
1518 TickType_t xTicksToWait )
\r
1520 BaseType_t xEntryTimeSet = pdFALSE;
\r
1521 TimeOut_t xTimeOut;
\r
1522 Queue_t * const pxQueue = xQueue;
\r
1524 #if ( configUSE_MUTEXES == 1 )
\r
1525 BaseType_t xInheritanceOccurred = pdFALSE;
\r
1528 /* Check the queue pointer is not NULL. */
\r
1529 configASSERT( ( pxQueue ) );
\r
1531 /* Check this really is a semaphore, in which case the item size will be
\r
1533 configASSERT( pxQueue->uxItemSize == 0 );
\r
1535 /* Cannot block if the scheduler is suspended. */
\r
1536 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
\r
1538 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
\r
1542 /*lint -save -e904 This function relaxes the coding standard somewhat to allow return
\r
1543 * statements within the function itself. This is done in the interest
\r
1544 * of execution time efficiency. */
\r
1547 taskENTER_CRITICAL();
\r
1549 /* Semaphores are queues with an item size of 0, and where the
\r
1550 * number of messages in the queue is the semaphore's count value. */
\r
1551 const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;
\r
1553 /* Is there data in the queue now? To be running the calling task
\r
1554 * must be the highest priority task wanting to access the queue. */
\r
1555 if( uxSemaphoreCount > ( UBaseType_t ) 0 )
\r
1557 traceQUEUE_RECEIVE( pxQueue );
\r
1559 /* Semaphores are queues with a data size of zero and where the
\r
1560 * messages waiting is the semaphore's count. Reduce the count. */
\r
1561 pxQueue->uxMessagesWaiting = uxSemaphoreCount - ( UBaseType_t ) 1;
\r
1563 #if ( configUSE_MUTEXES == 1 )
\r
1565 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1567 /* Record the information required to implement
\r
1568 * priority inheritance should it become necessary. */
\r
1569 pxQueue->u.xSemaphore.xMutexHolder = pvTaskIncrementMutexHeldCount();
\r
1573 mtCOVERAGE_TEST_MARKER();
\r
1576 #endif /* configUSE_MUTEXES */
\r
1578 /* Check to see if other tasks are blocked waiting to give the
\r
1579 * semaphore, and if so, unblock the highest priority such task. */
\r
1580 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1582 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1584 queueYIELD_IF_USING_PREEMPTION();
\r
1588 mtCOVERAGE_TEST_MARKER();
\r
1593 mtCOVERAGE_TEST_MARKER();
\r
1596 taskEXIT_CRITICAL();
\r
1601 if( xTicksToWait == ( TickType_t ) 0 )
\r
1603 /* The semaphore count was 0 and no block time is specified
\r
1604 * (or the block time has expired) so exit now. */
\r
1605 taskEXIT_CRITICAL();
\r
1606 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1607 return errQUEUE_EMPTY;
\r
1609 else if( xEntryTimeSet == pdFALSE )
\r
1611 /* The semaphore count was 0 and a block time was specified
\r
1612 * so configure the timeout structure ready to block. */
\r
1613 vTaskInternalSetTimeOutState( &xTimeOut );
\r
1614 xEntryTimeSet = pdTRUE;
\r
1618 /* Entry time was already set. */
\r
1619 mtCOVERAGE_TEST_MARKER();
\r
1623 taskEXIT_CRITICAL();
\r
1625 /* Interrupts and other tasks can give to and take from the semaphore
\r
1626 * now the critical section has been exited. */
\r
1628 vTaskSuspendAll();
\r
1629 prvLockQueue( pxQueue );
\r
1631 /* Update the timeout state to see if it has expired yet. */
\r
1632 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1634 /* A block time is specified and not expired. If the semaphore
\r
1635 * count is 0 then enter the Blocked state to wait for a semaphore to
\r
1636 * become available. As semaphores are implemented with queues the
\r
1637 * queue being empty is equivalent to the semaphore count being 0. */
\r
1638 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1640 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
1642 #if ( configUSE_MUTEXES == 1 )
\r
1644 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1646 taskENTER_CRITICAL();
\r
1648 xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );
\r
1650 taskEXIT_CRITICAL();
\r
1654 mtCOVERAGE_TEST_MARKER();
\r
1657 #endif /* if ( configUSE_MUTEXES == 1 ) */
\r
1659 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1660 prvUnlockQueue( pxQueue );
\r
1662 if( xTaskResumeAll() == pdFALSE )
\r
1664 portYIELD_WITHIN_API();
\r
1668 mtCOVERAGE_TEST_MARKER();
\r
1673 /* There was no timeout and the semaphore count was not 0, so
\r
1674 * attempt to take the semaphore again. */
\r
1675 prvUnlockQueue( pxQueue );
\r
1676 ( void ) xTaskResumeAll();
\r
1682 prvUnlockQueue( pxQueue );
\r
1683 ( void ) xTaskResumeAll();
\r
1685 /* If the semaphore count is 0 exit now as the timeout has
\r
1686 * expired. Otherwise return to attempt to take the semaphore that is
\r
1687 * known to be available. As semaphores are implemented by queues the
\r
1688 * queue being empty is equivalent to the semaphore count being 0. */
\r
1689 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1691 #if ( configUSE_MUTEXES == 1 )
\r
1693 /* xInheritanceOccurred could only have be set if
\r
1694 * pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
\r
1695 * test the mutex type again to check it is actually a mutex. */
\r
1696 if( xInheritanceOccurred != pdFALSE )
\r
1698 taskENTER_CRITICAL();
\r
1700 UBaseType_t uxHighestWaitingPriority;
\r
1702 /* This task blocking on the mutex caused another
\r
1703 * task to inherit this task's priority. Now this task
\r
1704 * has timed out the priority should be disinherited
\r
1705 * again, but only as low as the next highest priority
\r
1706 * task that is waiting for the same mutex. */
\r
1707 uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );
\r
1708 vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );
\r
1710 taskEXIT_CRITICAL();
\r
1713 #endif /* configUSE_MUTEXES */
\r
1715 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1716 return errQUEUE_EMPTY;
\r
1720 mtCOVERAGE_TEST_MARKER();
\r
1723 } /*lint -restore */
\r
1725 /*-----------------------------------------------------------*/
\r
1727 BaseType_t xQueuePeek( QueueHandle_t xQueue,
\r
1728 void * const pvBuffer,
\r
1729 TickType_t xTicksToWait )
\r
1731 BaseType_t xEntryTimeSet = pdFALSE;
\r
1732 TimeOut_t xTimeOut;
\r
1733 int8_t * pcOriginalReadPosition;
\r
1734 Queue_t * const pxQueue = xQueue;
\r
1736 /* Check the pointer is not NULL. */
\r
1737 configASSERT( ( pxQueue ) );
\r
1739 /* The buffer into which data is received can only be NULL if the data size
\r
1740 * is zero (so no data is copied into the buffer. */
\r
1741 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
\r
1743 /* Cannot block if the scheduler is suspended. */
\r
1744 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
\r
1746 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
\r
1750 /*lint -save -e904 This function relaxes the coding standard somewhat to
\r
1751 * allow return statements within the function itself. This is done in the
\r
1752 * interest of execution time efficiency. */
\r
1755 taskENTER_CRITICAL();
\r
1757 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
\r
1759 /* Is there data in the queue now? To be running the calling task
\r
1760 * must be the highest priority task wanting to access the queue. */
\r
1761 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
\r
1763 /* Remember the read position so it can be reset after the data
\r
1764 * is read from the queue as this function is only peeking the
\r
1765 * data, not removing it. */
\r
1766 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
\r
1768 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1769 traceQUEUE_PEEK( pxQueue );
\r
1771 /* The data is not being removed, so reset the read pointer. */
\r
1772 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
\r
1774 /* The data is being left in the queue, so see if there are
\r
1775 * any other tasks waiting for the data. */
\r
1776 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1778 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1780 /* The task waiting has a higher priority than this task. */
\r
1781 queueYIELD_IF_USING_PREEMPTION();
\r
1785 mtCOVERAGE_TEST_MARKER();
\r
1790 mtCOVERAGE_TEST_MARKER();
\r
1793 taskEXIT_CRITICAL();
\r
1798 if( xTicksToWait == ( TickType_t ) 0 )
\r
1800 /* The queue was empty and no block time is specified (or
\r
1801 * the block time has expired) so leave now. */
\r
1802 taskEXIT_CRITICAL();
\r
1803 traceQUEUE_PEEK_FAILED( pxQueue );
\r
1804 return errQUEUE_EMPTY;
\r
1806 else if( xEntryTimeSet == pdFALSE )
\r
1808 /* The queue was empty and a block time was specified so
\r
1809 * configure the timeout structure ready to enter the blocked
\r
1811 vTaskInternalSetTimeOutState( &xTimeOut );
\r
1812 xEntryTimeSet = pdTRUE;
\r
1816 /* Entry time was already set. */
\r
1817 mtCOVERAGE_TEST_MARKER();
\r
1821 taskEXIT_CRITICAL();
\r
1823 /* Interrupts and other tasks can send to and receive from the queue
\r
1824 * now that the critical section has been exited. */
\r
1826 vTaskSuspendAll();
\r
1827 prvLockQueue( pxQueue );
\r
1829 /* Update the timeout state to see if it has expired yet. */
\r
1830 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1832 /* Timeout has not expired yet, check to see if there is data in the
\r
1833 * queue now, and if not enter the Blocked state to wait for data. */
\r
1834 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1836 traceBLOCKING_ON_QUEUE_PEEK( pxQueue );
\r
1837 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1838 prvUnlockQueue( pxQueue );
\r
1840 if( xTaskResumeAll() == pdFALSE )
\r
1842 portYIELD_WITHIN_API();
\r
1846 mtCOVERAGE_TEST_MARKER();
\r
1851 /* There is data in the queue now, so don't enter the blocked
\r
1852 * state, instead return to try and obtain the data. */
\r
1853 prvUnlockQueue( pxQueue );
\r
1854 ( void ) xTaskResumeAll();
\r
1859 /* The timeout has expired. If there is still no data in the queue
\r
1860 * exit, otherwise go back and try to read the data again. */
\r
1861 prvUnlockQueue( pxQueue );
\r
1862 ( void ) xTaskResumeAll();
\r
1864 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1866 traceQUEUE_PEEK_FAILED( pxQueue );
\r
1867 return errQUEUE_EMPTY;
\r
1871 mtCOVERAGE_TEST_MARKER();
\r
1874 } /*lint -restore */
\r
1876 /*-----------------------------------------------------------*/
\r
1878 BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,
\r
1879 void * const pvBuffer,
\r
1880 BaseType_t * const pxHigherPriorityTaskWoken )
\r
1882 BaseType_t xReturn;
\r
1883 UBaseType_t uxSavedInterruptStatus;
\r
1884 Queue_t * const pxQueue = xQueue;
\r
1886 configASSERT( pxQueue );
\r
1887 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
\r
1889 /* RTOS ports that support interrupt nesting have the concept of a maximum
\r
1890 * system call (or maximum API call) interrupt priority. Interrupts that are
\r
1891 * above the maximum system call priority are kept permanently enabled, even
\r
1892 * when the RTOS kernel is in a critical section, but cannot make any calls to
\r
1893 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
\r
1894 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
\r
1895 * failure if a FreeRTOS API function is called from an interrupt that has been
\r
1896 * assigned a priority above the configured maximum system call priority.
\r
1897 * Only FreeRTOS functions that end in FromISR can be called from interrupts
\r
1898 * that have been assigned a priority at or (logically) below the maximum
\r
1899 * system call interrupt priority. FreeRTOS maintains a separate interrupt
\r
1900 * safe API to ensure interrupt entry is as fast and as simple as possible.
\r
1901 * More information (albeit Cortex-M specific) is provided on the following
\r
1902 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
\r
1903 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
\r
1905 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1907 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
\r
1909 /* Cannot block in an ISR, so check there is data available. */
\r
1910 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
\r
1912 const int8_t cRxLock = pxQueue->cRxLock;
\r
1914 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1916 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1917 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
\r
1919 /* If the queue is locked the event list will not be modified.
\r
1920 * Instead update the lock count so the task that unlocks the queue
\r
1921 * will know that an ISR has removed data while the queue was
\r
1923 if( cRxLock == queueUNLOCKED )
\r
1925 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1927 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1929 /* The task waiting has a higher priority than us so
\r
1930 * force a context switch. */
\r
1931 if( pxHigherPriorityTaskWoken != NULL )
\r
1933 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1937 mtCOVERAGE_TEST_MARKER();
\r
1942 mtCOVERAGE_TEST_MARKER();
\r
1947 mtCOVERAGE_TEST_MARKER();
\r
1952 /* Increment the lock count so the task that unlocks the queue
\r
1953 * knows that data was removed while it was locked. */
\r
1954 prvIncrementQueueRxLock( pxQueue, cRxLock );
\r
1962 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1965 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1969 /*-----------------------------------------------------------*/
\r
1971 BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,
\r
1972 void * const pvBuffer )
\r
1974 BaseType_t xReturn;
\r
1975 UBaseType_t uxSavedInterruptStatus;
\r
1976 int8_t * pcOriginalReadPosition;
\r
1977 Queue_t * const pxQueue = xQueue;
\r
1979 configASSERT( pxQueue );
\r
1980 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
\r
1981 configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
\r
1983 /* RTOS ports that support interrupt nesting have the concept of a maximum
\r
1984 * system call (or maximum API call) interrupt priority. Interrupts that are
\r
1985 * above the maximum system call priority are kept permanently enabled, even
\r
1986 * when the RTOS kernel is in a critical section, but cannot make any calls to
\r
1987 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
\r
1988 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
\r
1989 * failure if a FreeRTOS API function is called from an interrupt that has been
\r
1990 * assigned a priority above the configured maximum system call priority.
\r
1991 * Only FreeRTOS functions that end in FromISR can be called from interrupts
\r
1992 * that have been assigned a priority at or (logically) below the maximum
\r
1993 * system call interrupt priority. FreeRTOS maintains a separate interrupt
\r
1994 * safe API to ensure interrupt entry is as fast and as simple as possible.
\r
1995 * More information (albeit Cortex-M specific) is provided on the following
\r
1996 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
\r
1997 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
\r
1999 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
2001 /* Cannot block in an ISR, so check there is data available. */
\r
2002 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
\r
2004 traceQUEUE_PEEK_FROM_ISR( pxQueue );
\r
2006 /* Remember the read position so it can be reset as nothing is
\r
2007 * actually being removed from the queue. */
\r
2008 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
\r
2009 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
2010 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
\r
2017 traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
\r
2020 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
2024 /*-----------------------------------------------------------*/
\r
2026 UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
\r
2028 UBaseType_t uxReturn;
\r
2030 configASSERT( xQueue );
\r
2032 taskENTER_CRITICAL();
\r
2034 uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
\r
2036 taskEXIT_CRITICAL();
\r
2039 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
\r
2040 /*-----------------------------------------------------------*/
\r
2042 UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
\r
2044 UBaseType_t uxReturn;
\r
2045 Queue_t * const pxQueue = xQueue;
\r
2047 configASSERT( pxQueue );
\r
2049 taskENTER_CRITICAL();
\r
2051 uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;
\r
2053 taskEXIT_CRITICAL();
\r
2056 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
\r
2057 /*-----------------------------------------------------------*/
\r
2059 UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
\r
2061 UBaseType_t uxReturn;
\r
2062 Queue_t * const pxQueue = xQueue;
\r
2064 configASSERT( pxQueue );
\r
2065 uxReturn = pxQueue->uxMessagesWaiting;
\r
2068 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
\r
2069 /*-----------------------------------------------------------*/
\r
2071 void vQueueDelete( QueueHandle_t xQueue )
\r
2073 Queue_t * const pxQueue = xQueue;
\r
2075 configASSERT( pxQueue );
\r
2076 traceQUEUE_DELETE( pxQueue );
\r
2078 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
2080 vQueueUnregisterQueue( pxQueue );
\r
2084 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
\r
2086 /* The queue can only have been allocated dynamically - free it
\r
2088 vPortFree( pxQueue );
\r
2090 #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
\r
2092 /* The queue could have been allocated statically or dynamically, so
\r
2093 * check before attempting to free the memory. */
\r
2094 if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
\r
2096 vPortFree( pxQueue );
\r
2100 mtCOVERAGE_TEST_MARKER();
\r
2103 #else /* if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) ) */
\r
2105 /* The queue must have been statically allocated, so is not going to be
\r
2106 * deleted. Avoid compiler warnings about the unused parameter. */
\r
2109 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
\r
2111 /*-----------------------------------------------------------*/
\r
2113 #if ( configUSE_TRACE_FACILITY == 1 )
\r
2115 UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
\r
2117 return ( ( Queue_t * ) xQueue )->uxQueueNumber;
\r
2120 #endif /* configUSE_TRACE_FACILITY */
\r
2121 /*-----------------------------------------------------------*/
\r
2123 #if ( configUSE_TRACE_FACILITY == 1 )
\r
2125 void vQueueSetQueueNumber( QueueHandle_t xQueue,
\r
2126 UBaseType_t uxQueueNumber )
\r
2128 ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
\r
2131 #endif /* configUSE_TRACE_FACILITY */
\r
2132 /*-----------------------------------------------------------*/
\r
2134 #if ( configUSE_TRACE_FACILITY == 1 )
\r
2136 uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
\r
2138 return ( ( Queue_t * ) xQueue )->ucQueueType;
\r
2141 #endif /* configUSE_TRACE_FACILITY */
\r
2142 /*-----------------------------------------------------------*/
\r
2144 #if ( configUSE_MUTEXES == 1 )
\r
2146 static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue )
\r
2148 UBaseType_t uxHighestPriorityOfWaitingTasks;
\r
2150 /* If a task waiting for a mutex causes the mutex holder to inherit a
\r
2151 * priority, but the waiting task times out, then the holder should
\r
2152 * disinherit the priority - but only down to the highest priority of any
\r
2153 * other tasks that are waiting for the same mutex. For this purpose,
\r
2154 * return the priority of the highest priority task that is waiting for the
\r
2156 if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0U )
\r
2158 uxHighestPriorityOfWaitingTasks = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) );
\r
2162 uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;
\r
2165 return uxHighestPriorityOfWaitingTasks;
\r
2168 #endif /* configUSE_MUTEXES */
\r
2169 /*-----------------------------------------------------------*/
\r
2171 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
\r
2172 const void * pvItemToQueue,
\r
2173 const BaseType_t xPosition )
\r
2175 BaseType_t xReturn = pdFALSE;
\r
2176 UBaseType_t uxMessagesWaiting;
\r
2178 /* This function is called from a critical section. */
\r
2180 uxMessagesWaiting = pxQueue->uxMessagesWaiting;
\r
2182 if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
\r
2184 #if ( configUSE_MUTEXES == 1 )
\r
2186 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
2188 /* The mutex is no longer being held. */
\r
2189 xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
\r
2190 pxQueue->u.xSemaphore.xMutexHolder = NULL;
\r
2194 mtCOVERAGE_TEST_MARKER();
\r
2197 #endif /* configUSE_MUTEXES */
\r
2199 else if( xPosition == queueSEND_TO_BACK )
\r
2201 ( 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. */
\r
2202 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. */
\r
2204 if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
\r
2206 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
2210 mtCOVERAGE_TEST_MARKER();
\r
2215 ( 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. */
\r
2216 pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
\r
2218 if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
\r
2220 pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
\r
2224 mtCOVERAGE_TEST_MARKER();
\r
2227 if( xPosition == queueOVERWRITE )
\r
2229 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
\r
2231 /* An item is not being added but overwritten, so subtract
\r
2232 * one from the recorded number of items in the queue so when
\r
2233 * one is added again below the number of recorded items remains
\r
2235 --uxMessagesWaiting;
\r
2239 mtCOVERAGE_TEST_MARKER();
\r
2244 mtCOVERAGE_TEST_MARKER();
\r
2248 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
\r
2252 /*-----------------------------------------------------------*/
\r
2254 static void prvCopyDataFromQueue( Queue_t * const pxQueue,
\r
2255 void * const pvBuffer )
\r
2257 if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
\r
2259 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. */
\r
2261 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
\r
2263 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
\r
2267 mtCOVERAGE_TEST_MARKER();
\r
2270 ( 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. */
\r
2273 /*-----------------------------------------------------------*/
\r
2275 static void prvUnlockQueue( Queue_t * const pxQueue )
\r
2277 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
2279 /* The lock counts contains the number of extra data items placed or
\r
2280 * removed from the queue while the queue was locked. When a queue is
\r
2281 * locked items can be added or removed, but the event lists cannot be
\r
2283 taskENTER_CRITICAL();
\r
2285 int8_t cTxLock = pxQueue->cTxLock;
\r
2287 /* See if data was added to the queue while it was locked. */
\r
2288 while( cTxLock > queueLOCKED_UNMODIFIED )
\r
2290 /* Data was posted while the queue was locked. Are any tasks
\r
2291 * blocked waiting for data to become available? */
\r
2292 #if ( configUSE_QUEUE_SETS == 1 )
\r
2294 if( pxQueue->pxQueueSetContainer != NULL )
\r
2296 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
\r
2298 /* The queue is a member of a queue set, and posting to
\r
2299 * the queue set caused a higher priority task to unblock.
\r
2300 * A context switch is required. */
\r
2301 vTaskMissedYield();
\r
2305 mtCOVERAGE_TEST_MARKER();
\r
2310 /* Tasks that are removed from the event list will get
\r
2311 * added to the pending ready list as the scheduler is still
\r
2313 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
2315 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
2317 /* The task waiting has a higher priority so record that a
\r
2318 * context switch is required. */
\r
2319 vTaskMissedYield();
\r
2323 mtCOVERAGE_TEST_MARKER();
\r
2332 #else /* configUSE_QUEUE_SETS */
\r
2334 /* Tasks that are removed from the event list will get added to
\r
2335 * the pending ready list as the scheduler is still suspended. */
\r
2336 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
2338 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
2340 /* The task waiting has a higher priority so record that
\r
2341 * a context switch is required. */
\r
2342 vTaskMissedYield();
\r
2346 mtCOVERAGE_TEST_MARKER();
\r
2354 #endif /* configUSE_QUEUE_SETS */
\r
2359 pxQueue->cTxLock = queueUNLOCKED;
\r
2361 taskEXIT_CRITICAL();
\r
2363 /* Do the same for the Rx lock. */
\r
2364 taskENTER_CRITICAL();
\r
2366 int8_t cRxLock = pxQueue->cRxLock;
\r
2368 while( cRxLock > queueLOCKED_UNMODIFIED )
\r
2370 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
2372 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
2374 vTaskMissedYield();
\r
2378 mtCOVERAGE_TEST_MARKER();
\r
2389 pxQueue->cRxLock = queueUNLOCKED;
\r
2391 taskEXIT_CRITICAL();
\r
2393 /*-----------------------------------------------------------*/
\r
2395 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue )
\r
2397 BaseType_t xReturn;
\r
2399 taskENTER_CRITICAL();
\r
2401 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
\r
2407 xReturn = pdFALSE;
\r
2410 taskEXIT_CRITICAL();
\r
2414 /*-----------------------------------------------------------*/
\r
2416 BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )
\r
2418 BaseType_t xReturn;
\r
2419 Queue_t * const pxQueue = xQueue;
\r
2421 configASSERT( pxQueue );
\r
2423 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
\r
2429 xReturn = pdFALSE;
\r
2433 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
\r
2434 /*-----------------------------------------------------------*/
\r
2436 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue )
\r
2438 BaseType_t xReturn;
\r
2440 taskENTER_CRITICAL();
\r
2442 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
\r
2448 xReturn = pdFALSE;
\r
2451 taskEXIT_CRITICAL();
\r
2455 /*-----------------------------------------------------------*/
\r
2457 BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )
\r
2459 BaseType_t xReturn;
\r
2460 Queue_t * const pxQueue = xQueue;
\r
2462 configASSERT( pxQueue );
\r
2464 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
\r
2470 xReturn = pdFALSE;
\r
2474 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
\r
2475 /*-----------------------------------------------------------*/
\r
2477 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
2479 void vQueueAddToRegistry( QueueHandle_t xQueue,
\r
2480 const char * pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
2483 QueueRegistryItem_t * pxEntryToWrite = NULL;
\r
2485 configASSERT( xQueue );
\r
2487 if( pcQueueName != NULL )
\r
2489 /* See if there is an empty space in the registry. A NULL name denotes
\r
2491 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
2493 /* Replace an existing entry if the queue is already in the registry. */
\r
2494 if( xQueue == xQueueRegistry[ ux ].xHandle )
\r
2496 pxEntryToWrite = &( xQueueRegistry[ ux ] );
\r
2499 /* Otherwise, store in the next empty location */
\r
2500 else if( ( pxEntryToWrite == NULL ) && ( xQueueRegistry[ ux ].pcQueueName == NULL ) )
\r
2502 pxEntryToWrite = &( xQueueRegistry[ ux ] );
\r
2506 mtCOVERAGE_TEST_MARKER();
\r
2511 if( pxEntryToWrite != NULL )
\r
2513 /* Store the information on this queue. */
\r
2514 pxEntryToWrite->pcQueueName = pcQueueName;
\r
2515 pxEntryToWrite->xHandle = xQueue;
\r
2517 traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
\r
2521 #endif /* configQUEUE_REGISTRY_SIZE */
\r
2522 /*-----------------------------------------------------------*/
\r
2524 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
2526 const char * pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
2529 const char * pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
2531 configASSERT( xQueue );
\r
2533 /* Note there is nothing here to protect against another task adding or
\r
2534 * removing entries from the registry while it is being searched. */
\r
2536 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
2538 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
2540 pcReturn = xQueueRegistry[ ux ].pcQueueName;
\r
2545 mtCOVERAGE_TEST_MARKER();
\r
2550 } /*lint !e818 xQueue cannot be a pointer to const because it is a typedef. */
\r
2552 #endif /* configQUEUE_REGISTRY_SIZE */
\r
2553 /*-----------------------------------------------------------*/
\r
2555 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
2557 void vQueueUnregisterQueue( QueueHandle_t xQueue )
\r
2561 configASSERT( xQueue );
\r
2563 /* See if the handle of the queue being unregistered in actually in the
\r
2565 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
2567 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
2569 /* Set the name to NULL to show that this slot if free again. */
\r
2570 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
2572 /* Set the handle to NULL to ensure the same queue handle cannot
\r
2573 * appear in the registry twice if it is added, removed, then
\r
2575 xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;
\r
2580 mtCOVERAGE_TEST_MARKER();
\r
2583 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
\r
2585 #endif /* configQUEUE_REGISTRY_SIZE */
\r
2586 /*-----------------------------------------------------------*/
\r
2588 #if ( configUSE_TIMERS == 1 )
\r
2590 void vQueueWaitForMessageRestricted( QueueHandle_t xQueue,
\r
2591 TickType_t xTicksToWait,
\r
2592 const BaseType_t xWaitIndefinitely )
\r
2594 Queue_t * const pxQueue = xQueue;
\r
2596 /* This function should not be called by application code hence the
\r
2597 * 'Restricted' in its name. It is not part of the public API. It is
\r
2598 * designed for use by kernel code, and has special calling requirements.
\r
2599 * It can result in vListInsert() being called on a list that can only
\r
2600 * possibly ever have one item in it, so the list will be fast, but even
\r
2601 * so it should be called with the scheduler locked and not from a critical
\r
2604 /* Only do anything if there are no messages in the queue. This function
\r
2605 * will not actually cause the task to block, just place it on a blocked
\r
2606 * list. It will not block until the scheduler is unlocked - at which
\r
2607 * time a yield will be performed. If an item is added to the queue while
\r
2608 * the queue is locked, and the calling task blocks on the queue, then the
\r
2609 * calling task will be immediately unblocked when the queue is unlocked. */
\r
2610 prvLockQueue( pxQueue );
\r
2612 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
\r
2614 /* There is nothing in the queue, block for the specified period. */
\r
2615 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );
\r
2619 mtCOVERAGE_TEST_MARKER();
\r
2622 prvUnlockQueue( pxQueue );
\r
2625 #endif /* configUSE_TIMERS */
\r
2626 /*-----------------------------------------------------------*/
\r
2628 #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
\r
2630 QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
\r
2632 QueueSetHandle_t pxQueue;
\r
2634 pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
\r
2639 #endif /* configUSE_QUEUE_SETS */
\r
2640 /*-----------------------------------------------------------*/
\r
2642 #if ( configUSE_QUEUE_SETS == 1 )
\r
2644 BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore,
\r
2645 QueueSetHandle_t xQueueSet )
\r
2647 BaseType_t xReturn;
\r
2649 taskENTER_CRITICAL();
\r
2651 if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
\r
2653 /* Cannot add a queue/semaphore to more than one queue set. */
\r
2656 else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
\r
2658 /* Cannot add a queue/semaphore to a queue set if there are already
\r
2659 * items in the queue/semaphore. */
\r
2664 ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
\r
2668 taskEXIT_CRITICAL();
\r
2673 #endif /* configUSE_QUEUE_SETS */
\r
2674 /*-----------------------------------------------------------*/
\r
2676 #if ( configUSE_QUEUE_SETS == 1 )
\r
2678 BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore,
\r
2679 QueueSetHandle_t xQueueSet )
\r
2681 BaseType_t xReturn;
\r
2682 Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
\r
2684 if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
\r
2686 /* The queue was not a member of the set. */
\r
2689 else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
\r
2691 /* It is dangerous to remove a queue from a set when the queue is
\r
2692 * not empty because the queue set will still hold pending events for
\r
2698 taskENTER_CRITICAL();
\r
2700 /* The queue is no longer contained in the set. */
\r
2701 pxQueueOrSemaphore->pxQueueSetContainer = NULL;
\r
2703 taskEXIT_CRITICAL();
\r
2708 } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
\r
2710 #endif /* configUSE_QUEUE_SETS */
\r
2711 /*-----------------------------------------------------------*/
\r
2713 #if ( configUSE_QUEUE_SETS == 1 )
\r
2715 QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet,
\r
2716 TickType_t const xTicksToWait )
\r
2718 QueueSetMemberHandle_t xReturn = NULL;
\r
2720 ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait ); /*lint !e961 Casting from one typedef to another is not redundant. */
\r
2724 #endif /* configUSE_QUEUE_SETS */
\r
2725 /*-----------------------------------------------------------*/
\r
2727 #if ( configUSE_QUEUE_SETS == 1 )
\r
2729 QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
\r
2731 QueueSetMemberHandle_t xReturn = NULL;
\r
2733 ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
\r
2737 #endif /* configUSE_QUEUE_SETS */
\r
2738 /*-----------------------------------------------------------*/
\r
2740 #if ( configUSE_QUEUE_SETS == 1 )
\r
2742 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue )
\r
2744 Queue_t * pxQueueSetContainer = pxQueue->pxQueueSetContainer;
\r
2745 BaseType_t xReturn = pdFALSE;
\r
2747 /* This function must be called form a critical section. */
\r
2749 /* The following line is not reachable in unit tests because every call
\r
2750 * to prvNotifyQueueSetContainer is preceded by a check that
\r
2751 * pxQueueSetContainer != NULL */
\r
2752 configASSERT( pxQueueSetContainer ); /* LCOV_EXCL_BR_LINE */
\r
2753 configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
\r
2755 if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
\r
2757 const int8_t cTxLock = pxQueueSetContainer->cTxLock;
\r
2759 traceQUEUE_SET_SEND( pxQueueSetContainer );
\r
2761 /* The data copied is the handle of the queue that contains data. */
\r
2762 xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, queueSEND_TO_BACK );
\r
2764 if( cTxLock == queueUNLOCKED )
\r
2766 if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
\r
2768 if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
\r
2770 /* The task waiting has a higher priority. */
\r
2775 mtCOVERAGE_TEST_MARKER();
\r
2780 mtCOVERAGE_TEST_MARKER();
\r
2785 prvIncrementQueueTxLock( pxQueueSetContainer, cTxLock );
\r
2790 mtCOVERAGE_TEST_MARKER();
\r
2796 #endif /* configUSE_QUEUE_SETS */
\r