blob: e76e7ff25014d9ede37b5351c541b65052c682c1 [file] [log] [blame]
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001/*
xiaohu.huang58292b32024-01-03 14:09:51 +08002 * FreeRTOS Kernel V10.5.1
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08006 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
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.
23 *
xiaohu.huang58292b32024-01-03 14:09:51 +080024 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080026 *
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080027 */
28
29#include <stdlib.h>
30#include <string.h>
31
32/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
xiaohu.huang58292b32024-01-03 14:09:51 +080033 * all the API functions to use the MPU wrappers. That should only be done when
34 * task.h is included from an application file. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080035#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
36
37#include "FreeRTOS.h"
38#include "task.h"
39#include "queue.h"
40
41#if ( configUSE_CO_ROUTINES == 1 )
xiaohu.huang58292b32024-01-03 14:09:51 +080042 #include "croutine.h"
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080043#endif
44
45/* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
xiaohu.huang58292b32024-01-03 14:09:51 +080046 * 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080049#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
50
51
52/* Constants used with the cRxLock and cTxLock structure members. */
xiaohu.huang58292b32024-01-03 14:09:51 +080053#define queueUNLOCKED ( ( int8_t ) -1 )
54#define queueLOCKED_UNMODIFIED ( ( int8_t ) 0 )
55#define queueINT8_MAX ( ( int8_t ) 127 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080056
57/* When the Queue_t structure is used to represent a base queue its pcHead and
xiaohu.huang58292b32024-01-03 14:09:51 +080058 * 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
65 * being used for. */
66#define uxQueueType pcHead
67#define queueQUEUE_IS_MUTEX NULL
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080068
69typedef struct QueuePointers
70{
xiaohu.huang58292b32024-01-03 14:09:51 +080071 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080073} QueuePointers_t;
74
75typedef struct SemaphoreData
76{
xiaohu.huang58292b32024-01-03 14:09:51 +080077 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080079} SemaphoreData_t;
80
81/* Semaphores do not actually store or copy data, so have an item size of
xiaohu.huang58292b32024-01-03 14:09:51 +080082 * zero. */
83#define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )
84#define queueMUTEX_GIVE_BLOCK_TIME ( ( TickType_t ) 0U )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080085
xiaohu.huang58292b32024-01-03 14:09:51 +080086#if ( configUSE_PREEMPTION == 0 )
87
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()
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080091#else
xiaohu.huang58292b32024-01-03 14:09:51 +080092 #define queueYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080093#endif
94
95/*
96 * Definition of the queue used by the scheduler.
97 * Items are queued by copy, not reference. See the following link for the
xiaohu.huang58292b32024-01-03 14:09:51 +080098 * rationale: https://www.FreeRTOS.org/Embedded-RTOS-Queues.html
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080099 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800100typedef struct QueueDefinition /* The old naming convention is used to prevent breaking kernel aware debuggers. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800101{
xiaohu.huang58292b32024-01-03 14:09:51 +0800102 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800104
xiaohu.huang58292b32024-01-03 14:09:51 +0800105 union
106 {
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. */
109 } u;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800110
xiaohu.huang58292b32024-01-03 14:09:51 +0800111 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800113
xiaohu.huang58292b32024-01-03 14:09:51 +0800114 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800117
xiaohu.huang58292b32024-01-03 14:09:51 +0800118 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800120
xiaohu.huang58292b32024-01-03 14:09:51 +0800121 #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. */
123 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800124
xiaohu.huang58292b32024-01-03 14:09:51 +0800125 #if ( configUSE_QUEUE_SETS == 1 )
126 struct QueueDefinition * pxQueueSetContainer;
127 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800128
xiaohu.huang58292b32024-01-03 14:09:51 +0800129 #if ( configUSE_TRACE_FACILITY == 1 )
130 UBaseType_t uxQueueNumber;
131 uint8_t ucQueueType;
132 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800133} xQUEUE;
134
135/* The old xQUEUE name is maintained above then typedefed to the new Queue_t
xiaohu.huang58292b32024-01-03 14:09:51 +0800136 * name below to enable the use of older kernel aware debuggers. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800137typedef xQUEUE Queue_t;
138
139/*-----------------------------------------------------------*/
140
141/*
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.
144 */
145#if ( configQUEUE_REGISTRY_SIZE > 0 )
146
xiaohu.huang58292b32024-01-03 14:09:51 +0800147/* 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
151 {
152 const char * pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
153 QueueHandle_t xHandle;
154 } xQueueRegistryItem;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800155
xiaohu.huang58292b32024-01-03 14:09:51 +0800156/* The old xQueueRegistryItem name is maintained above then typedefed to the
157 * new xQueueRegistryItem name below to enable the use of older kernel aware
158 * debuggers. */
159 typedef xQueueRegistryItem QueueRegistryItem_t;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800160
xiaohu.huang58292b32024-01-03 14:09:51 +0800161/* 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 ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800165
166#endif /* configQUEUE_REGISTRY_SIZE */
167
168/*
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.
175 */
176static void prvUnlockQueue( Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
177
178/*
179 * Uses a critical section to determine if there is any data in a queue.
180 *
181 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
182 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800183static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800184
185/*
186 * Uses a critical section to determine if there is any space in a queue.
187 *
188 * @return pdTRUE if there is no space, otherwise pdFALSE;
189 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800190static BaseType_t prvIsQueueFull( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800191
192/*
193 * Copies an item into the queue, either at the front of the queue or the
194 * back of the queue.
195 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800196static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
197 const void * pvItemToQueue,
198 const BaseType_t xPosition ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800199
200/*
201 * Copies an item out of a queue.
202 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800203static void prvCopyDataFromQueue( Queue_t * const pxQueue,
204 void * const pvBuffer ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800205
206#if ( configUSE_QUEUE_SETS == 1 )
xiaohu.huang58292b32024-01-03 14:09:51 +0800207
208/*
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.
211 */
212 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800213#endif
214
215/*
216 * Called after a Queue_t structure has been allocated either statically or
217 * dynamically to fill in the structure's members.
218 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800219static 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;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800224
225/*
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
228 * as a mutex.
229 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800230#if ( configUSE_MUTEXES == 1 )
231 static void prvInitialiseMutex( Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800232#endif
233
xiaohu.huang58292b32024-01-03 14:09:51 +0800234#if ( configUSE_MUTEXES == 1 )
235
236/*
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
241 * that priority.
242 */
243 static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800244#endif
245/*-----------------------------------------------------------*/
246
247/*
248 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
249 * accessing the queue event lists.
250 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800251#define prvLockQueue( pxQueue ) \
252 taskENTER_CRITICAL(); \
253 { \
254 if( ( pxQueue )->cRxLock == queueUNLOCKED ) \
255 { \
256 ( pxQueue )->cRxLock = queueLOCKED_UNMODIFIED; \
257 } \
258 if( ( pxQueue )->cTxLock == queueUNLOCKED ) \
259 { \
260 ( pxQueue )->cTxLock = queueLOCKED_UNMODIFIED; \
261 } \
262 } \
263 taskEXIT_CRITICAL()
264
265/*
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.
269 */
270#define prvIncrementQueueTxLock( pxQueue, cTxLock ) \
271 { \
272 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
273 if( ( UBaseType_t ) ( cTxLock ) < uxNumberOfTasks ) \
274 { \
275 configASSERT( ( cTxLock ) != queueINT8_MAX ); \
276 ( pxQueue )->cTxLock = ( int8_t ) ( ( cTxLock ) + ( int8_t ) 1 ); \
277 } \
278 }
279
280/*
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.
284 */
285#define prvIncrementQueueRxLock( pxQueue, cRxLock ) \
286 { \
287 const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks(); \
288 if( ( UBaseType_t ) ( cRxLock ) < uxNumberOfTasks ) \
289 { \
290 configASSERT( ( cRxLock ) != queueINT8_MAX ); \
291 ( pxQueue )->cRxLock = ( int8_t ) ( ( cRxLock ) + ( int8_t ) 1 ); \
292 } \
293 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800294/*-----------------------------------------------------------*/
295
xiaohu.huang58292b32024-01-03 14:09:51 +0800296BaseType_t xQueueGenericReset( QueueHandle_t xQueue,
297 BaseType_t xNewQueue )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800298{
xiaohu.huang58292b32024-01-03 14:09:51 +0800299 BaseType_t xReturn = pdPASS;
300 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800301
xiaohu.huang58292b32024-01-03 14:09:51 +0800302 configASSERT( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800303
xiaohu.huang58292b32024-01-03 14:09:51 +0800304 if( ( pxQueue != NULL ) &&
305 ( pxQueue->uxLength >= 1U ) &&
306 /* Check for multiplication overflow. */
307 ( ( SIZE_MAX / pxQueue->uxLength ) >= pxQueue->uxItemSize ) )
308 {
309 taskENTER_CRITICAL();
310 {
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;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800317
xiaohu.huang58292b32024-01-03 14:09:51 +0800318 if( xNewQueue == pdFALSE )
319 {
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 )
326 {
327 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
328 {
329 queueYIELD_IF_USING_PREEMPTION();
330 }
331 else
332 {
333 mtCOVERAGE_TEST_MARKER();
334 }
335 }
336 else
337 {
338 mtCOVERAGE_TEST_MARKER();
339 }
340 }
341 else
342 {
343 /* Ensure the event queues start in the correct state. */
344 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
345 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
346 }
347 }
348 taskEXIT_CRITICAL();
349 }
350 else
351 {
352 xReturn = pdFAIL;
353 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800354
xiaohu.huang58292b32024-01-03 14:09:51 +0800355 configASSERT( xReturn != pdFAIL );
356
357 /* A value is returned for calling semantic consistency with previous
358 * versions. */
359 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800360}
361/*-----------------------------------------------------------*/
362
xiaohu.huang58292b32024-01-03 14:09:51 +0800363#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800364
xiaohu.huang58292b32024-01-03 14:09:51 +0800365 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 )
370 {
371 Queue_t * pxNewQueue = NULL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800372
xiaohu.huang58292b32024-01-03 14:09:51 +0800373 /* The StaticQueue_t structure and the queue storage area must be
374 * supplied. */
375 configASSERT( pxStaticQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800376
xiaohu.huang58292b32024-01-03 14:09:51 +0800377 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
378 ( pxStaticQueue != NULL ) &&
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800379
xiaohu.huang58292b32024-01-03 14:09:51 +0800380 /* 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 ) ) ) )
384 {
385 #if ( configASSERT_DEFINED == 1 )
386 {
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 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800391
xiaohu.huang58292b32024-01-03 14:09:51 +0800392 /* 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. */
395 }
396 #endif /* configASSERT_DEFINED */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800397
xiaohu.huang58292b32024-01-03 14:09:51 +0800398 /* 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800402
xiaohu.huang58292b32024-01-03 14:09:51 +0800403 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
404 {
405 /* Queues can be allocated wither statically or dynamically, so
406 * note this queue was allocated statically in case the queue is
407 * later deleted. */
408 pxNewQueue->ucStaticallyAllocated = pdTRUE;
409 }
410 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800411
xiaohu.huang58292b32024-01-03 14:09:51 +0800412 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
413 }
414 else
415 {
416 configASSERT( pxNewQueue );
417 mtCOVERAGE_TEST_MARKER();
418 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800419
xiaohu.huang58292b32024-01-03 14:09:51 +0800420 return pxNewQueue;
421 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800422
423#endif /* configSUPPORT_STATIC_ALLOCATION */
424/*-----------------------------------------------------------*/
425
xiaohu.huang58292b32024-01-03 14:09:51 +0800426#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800427
xiaohu.huang58292b32024-01-03 14:09:51 +0800428 QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,
429 const UBaseType_t uxItemSize,
430 const uint8_t ucQueueType )
431 {
432 Queue_t * pxNewQueue = NULL;
433 size_t xQueueSizeInBytes;
434 uint8_t * pucQueueStorage;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800435
xiaohu.huang58292b32024-01-03 14:09:51 +0800436 if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&
437 /* Check for multiplication overflow. */
438 ( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&
439 /* Check for addition overflow. */
440 ( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( uxQueueLength * uxItemSize ) ) )
441 {
442 /* Allocate enough space to hold the maximum number of items that
443 * can be in the queue at any time. It is valid for uxItemSize to be
444 * zero in the case the queue is used as a semaphore. */
445 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800446
xiaohu.huang58292b32024-01-03 14:09:51 +0800447 /* Allocate the queue and storage area. Justification for MISRA
448 * deviation as follows: pvPortMalloc() always ensures returned memory
449 * blocks are aligned per the requirements of the MCU stack. In this case
450 * pvPortMalloc() must return a pointer that is guaranteed to meet the
451 * alignment requirements of the Queue_t structure - which in this case
452 * is an int8_t *. Therefore, whenever the stack alignment requirements
453 * are greater than or equal to the pointer to char requirements the cast
454 * is safe. In other cases alignment requirements are not strict (one or
455 * two bytes). */
456 pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800457
xiaohu.huang58292b32024-01-03 14:09:51 +0800458 if( pxNewQueue != NULL )
459 {
460 /* Jump past the queue structure to find the location of the queue
461 * storage area. */
462 pucQueueStorage = ( uint8_t * ) pxNewQueue;
463 pucQueueStorage += sizeof( Queue_t ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800464
xiaohu.huang58292b32024-01-03 14:09:51 +0800465 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
466 {
467 /* Queues can be created either statically or dynamically, so
468 * note this task was created dynamically in case it is later
469 * deleted. */
470 pxNewQueue->ucStaticallyAllocated = pdFALSE;
471 }
472 #endif /* configSUPPORT_STATIC_ALLOCATION */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800473
xiaohu.huang58292b32024-01-03 14:09:51 +0800474 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
475 }
476 else
477 {
478 traceQUEUE_CREATE_FAILED( ucQueueType );
479 mtCOVERAGE_TEST_MARKER();
480 }
481 }
482 else
483 {
484 configASSERT( pxNewQueue );
485 mtCOVERAGE_TEST_MARKER();
486 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800487
xiaohu.huang58292b32024-01-03 14:09:51 +0800488 return pxNewQueue;
489 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800490
491#endif /* configSUPPORT_STATIC_ALLOCATION */
492/*-----------------------------------------------------------*/
493
xiaohu.huang58292b32024-01-03 14:09:51 +0800494static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,
495 const UBaseType_t uxItemSize,
496 uint8_t * pucQueueStorage,
497 const uint8_t ucQueueType,
498 Queue_t * pxNewQueue )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800499{
xiaohu.huang58292b32024-01-03 14:09:51 +0800500 /* Remove compiler warnings about unused parameters should
501 * configUSE_TRACE_FACILITY not be set to 1. */
502 ( void ) ucQueueType;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800503
xiaohu.huang58292b32024-01-03 14:09:51 +0800504 if( uxItemSize == ( UBaseType_t ) 0 )
505 {
506 /* No RAM was allocated for the queue storage area, but PC head cannot
507 * be set to NULL because NULL is used as a key to say the queue is used as
508 * a mutex. Therefore just set pcHead to point to the queue as a benign
509 * value that is known to be within the memory map. */
510 pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
511 }
512 else
513 {
514 /* Set the head to the start of the queue storage area. */
515 pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
516 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800517
xiaohu.huang58292b32024-01-03 14:09:51 +0800518 /* Initialise the queue members as described where the queue type is
519 * defined. */
520 pxNewQueue->uxLength = uxQueueLength;
521 pxNewQueue->uxItemSize = uxItemSize;
522 ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800523
xiaohu.huang58292b32024-01-03 14:09:51 +0800524 #if ( configUSE_TRACE_FACILITY == 1 )
525 {
526 pxNewQueue->ucQueueType = ucQueueType;
527 }
528 #endif /* configUSE_TRACE_FACILITY */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800529
xiaohu.huang58292b32024-01-03 14:09:51 +0800530 #if ( configUSE_QUEUE_SETS == 1 )
531 {
532 pxNewQueue->pxQueueSetContainer = NULL;
533 }
534 #endif /* configUSE_QUEUE_SETS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800535
xiaohu.huang58292b32024-01-03 14:09:51 +0800536 traceQUEUE_CREATE( pxNewQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800537}
538/*-----------------------------------------------------------*/
539
xiaohu.huang58292b32024-01-03 14:09:51 +0800540#if ( configUSE_MUTEXES == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800541
xiaohu.huang58292b32024-01-03 14:09:51 +0800542 static void prvInitialiseMutex( Queue_t * pxNewQueue )
543 {
544 if( pxNewQueue != NULL )
545 {
546 /* The queue create function will set all the queue structure members
547 * correctly for a generic queue, but this function is creating a
548 * mutex. Overwrite those members that need to be set differently -
549 * in particular the information required for priority inheritance. */
550 pxNewQueue->u.xSemaphore.xMutexHolder = NULL;
551 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800552
xiaohu.huang58292b32024-01-03 14:09:51 +0800553 /* In case this is a recursive mutex. */
554 pxNewQueue->u.xSemaphore.uxRecursiveCallCount = 0;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800555
xiaohu.huang58292b32024-01-03 14:09:51 +0800556 traceCREATE_MUTEX( pxNewQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800557
xiaohu.huang58292b32024-01-03 14:09:51 +0800558 /* Start with the semaphore in the expected state. */
559 ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );
560 }
561 else
562 {
563 traceCREATE_MUTEX_FAILED();
564 }
565 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800566
567#endif /* configUSE_MUTEXES */
568/*-----------------------------------------------------------*/
569
xiaohu.huang58292b32024-01-03 14:09:51 +0800570#if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800571
xiaohu.huang58292b32024-01-03 14:09:51 +0800572 QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
573 {
574 QueueHandle_t xNewQueue;
575 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800576
xiaohu.huang58292b32024-01-03 14:09:51 +0800577 xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
578 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800579
xiaohu.huang58292b32024-01-03 14:09:51 +0800580 return xNewQueue;
581 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800582
583#endif /* configUSE_MUTEXES */
584/*-----------------------------------------------------------*/
585
xiaohu.huang58292b32024-01-03 14:09:51 +0800586#if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800587
xiaohu.huang58292b32024-01-03 14:09:51 +0800588 QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,
589 StaticQueue_t * pxStaticQueue )
590 {
591 QueueHandle_t xNewQueue;
592 const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800593
xiaohu.huang58292b32024-01-03 14:09:51 +0800594 /* Prevent compiler warnings about unused parameters if
595 * configUSE_TRACE_FACILITY does not equal 1. */
596 ( void ) ucQueueType;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800597
xiaohu.huang58292b32024-01-03 14:09:51 +0800598 xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
599 prvInitialiseMutex( ( Queue_t * ) xNewQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800600
xiaohu.huang58292b32024-01-03 14:09:51 +0800601 return xNewQueue;
602 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800603
604#endif /* configUSE_MUTEXES */
605/*-----------------------------------------------------------*/
606
607#if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
608
xiaohu.huang58292b32024-01-03 14:09:51 +0800609 TaskHandle_t xQueueGetMutexHolder( QueueHandle_t xSemaphore )
610 {
611 TaskHandle_t pxReturn;
612 Queue_t * const pxSemaphore = ( Queue_t * ) xSemaphore;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800613
xiaohu.huang58292b32024-01-03 14:09:51 +0800614 configASSERT( xSemaphore );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800615
xiaohu.huang58292b32024-01-03 14:09:51 +0800616 /* This function is called by xSemaphoreGetMutexHolder(), and should not
617 * be called directly. Note: This is a good way of determining if the
618 * calling task is the mutex holder, but not a good way of determining the
619 * identity of the mutex holder, as the holder may change between the
620 * following critical section exiting and the function returning. */
621 taskENTER_CRITICAL();
622 {
623 if( pxSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
624 {
625 pxReturn = pxSemaphore->u.xSemaphore.xMutexHolder;
626 }
627 else
628 {
629 pxReturn = NULL;
630 }
631 }
632 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800633
xiaohu.huang58292b32024-01-03 14:09:51 +0800634 return pxReturn;
635 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
636
637#endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800638/*-----------------------------------------------------------*/
639
640#if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
641
xiaohu.huang58292b32024-01-03 14:09:51 +0800642 TaskHandle_t xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )
643 {
644 TaskHandle_t pxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800645
xiaohu.huang58292b32024-01-03 14:09:51 +0800646 configASSERT( xSemaphore );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800647
xiaohu.huang58292b32024-01-03 14:09:51 +0800648 /* Mutexes cannot be used in interrupt service routines, so the mutex
649 * holder should not change in an ISR, and therefore a critical section is
650 * not required here. */
651 if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
652 {
653 pxReturn = ( ( Queue_t * ) xSemaphore )->u.xSemaphore.xMutexHolder;
654 }
655 else
656 {
657 pxReturn = NULL;
658 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800659
xiaohu.huang58292b32024-01-03 14:09:51 +0800660 return pxReturn;
661 } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800662
xiaohu.huang58292b32024-01-03 14:09:51 +0800663#endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800664/*-----------------------------------------------------------*/
665
666#if ( configUSE_RECURSIVE_MUTEXES == 1 )
667
xiaohu.huang58292b32024-01-03 14:09:51 +0800668 BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )
669 {
670 BaseType_t xReturn;
671 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800672
xiaohu.huang58292b32024-01-03 14:09:51 +0800673 configASSERT( pxMutex );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800674
xiaohu.huang58292b32024-01-03 14:09:51 +0800675 /* If this is the task that holds the mutex then xMutexHolder will not
676 * change outside of this task. If this task does not hold the mutex then
677 * pxMutexHolder can never coincidentally equal the tasks handle, and as
678 * this is the only condition we are interested in it does not matter if
679 * pxMutexHolder is accessed simultaneously by another task. Therefore no
680 * mutual exclusion is required to test the pxMutexHolder variable. */
681 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
682 {
683 traceGIVE_MUTEX_RECURSIVE( pxMutex );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800684
xiaohu.huang58292b32024-01-03 14:09:51 +0800685 /* uxRecursiveCallCount cannot be zero if xMutexHolder is equal to
686 * the task handle, therefore no underflow check is required. Also,
687 * uxRecursiveCallCount is only modified by the mutex holder, and as
688 * there can only be one, no mutual exclusion is required to modify the
689 * uxRecursiveCallCount member. */
690 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )--;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800691
xiaohu.huang58292b32024-01-03 14:09:51 +0800692 /* Has the recursive call count unwound to 0? */
693 if( pxMutex->u.xSemaphore.uxRecursiveCallCount == ( UBaseType_t ) 0 )
694 {
695 /* Return the mutex. This will automatically unblock any other
696 * task that might be waiting to access the mutex. */
697 ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
698 }
699 else
700 {
701 mtCOVERAGE_TEST_MARKER();
702 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800703
xiaohu.huang58292b32024-01-03 14:09:51 +0800704 xReturn = pdPASS;
705 }
706 else
707 {
708 /* The mutex cannot be given because the calling task is not the
709 * holder. */
710 xReturn = pdFAIL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800711
xiaohu.huang58292b32024-01-03 14:09:51 +0800712 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
713 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800714
xiaohu.huang58292b32024-01-03 14:09:51 +0800715 return xReturn;
716 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800717
718#endif /* configUSE_RECURSIVE_MUTEXES */
719/*-----------------------------------------------------------*/
720
721#if ( configUSE_RECURSIVE_MUTEXES == 1 )
722
xiaohu.huang58292b32024-01-03 14:09:51 +0800723 BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,
724 TickType_t xTicksToWait )
725 {
726 BaseType_t xReturn;
727 Queue_t * const pxMutex = ( Queue_t * ) xMutex;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800728
xiaohu.huang58292b32024-01-03 14:09:51 +0800729 configASSERT( pxMutex );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800730
xiaohu.huang58292b32024-01-03 14:09:51 +0800731 /* Comments regarding mutual exclusion as per those within
732 * xQueueGiveMutexRecursive(). */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800733
xiaohu.huang58292b32024-01-03 14:09:51 +0800734 traceTAKE_MUTEX_RECURSIVE( pxMutex );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800735
xiaohu.huang58292b32024-01-03 14:09:51 +0800736 if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )
737 {
738 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
739 xReturn = pdPASS;
740 }
741 else
742 {
743 xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800744
xiaohu.huang58292b32024-01-03 14:09:51 +0800745 /* pdPASS will only be returned if the mutex was successfully
746 * obtained. The calling task may have entered the Blocked state
747 * before reaching here. */
748 if( xReturn != pdFAIL )
749 {
750 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;
751 }
752 else
753 {
754 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
755 }
756 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800757
xiaohu.huang58292b32024-01-03 14:09:51 +0800758 return xReturn;
759 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800760
761#endif /* configUSE_RECURSIVE_MUTEXES */
762/*-----------------------------------------------------------*/
763
xiaohu.huang58292b32024-01-03 14:09:51 +0800764#if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800765
xiaohu.huang58292b32024-01-03 14:09:51 +0800766 QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,
767 const UBaseType_t uxInitialCount,
768 StaticQueue_t * pxStaticQueue )
769 {
770 QueueHandle_t xHandle = NULL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800771
xiaohu.huang58292b32024-01-03 14:09:51 +0800772 if( ( uxMaxCount != 0 ) &&
773 ( uxInitialCount <= uxMaxCount ) )
774 {
775 xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800776
xiaohu.huang58292b32024-01-03 14:09:51 +0800777 if( xHandle != NULL )
778 {
779 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800780
xiaohu.huang58292b32024-01-03 14:09:51 +0800781 traceCREATE_COUNTING_SEMAPHORE();
782 }
783 else
784 {
785 traceCREATE_COUNTING_SEMAPHORE_FAILED();
786 }
787 }
788 else
789 {
790 configASSERT( xHandle );
791 mtCOVERAGE_TEST_MARKER();
792 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800793
xiaohu.huang58292b32024-01-03 14:09:51 +0800794 return xHandle;
795 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800796
797#endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
798/*-----------------------------------------------------------*/
799
xiaohu.huang58292b32024-01-03 14:09:51 +0800800#if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800801
xiaohu.huang58292b32024-01-03 14:09:51 +0800802 QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
803 const UBaseType_t uxInitialCount )
804 {
805 QueueHandle_t xHandle = NULL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800806
xiaohu.huang58292b32024-01-03 14:09:51 +0800807 if( ( uxMaxCount != 0 ) &&
808 ( uxInitialCount <= uxMaxCount ) )
809 {
810 xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800811
xiaohu.huang58292b32024-01-03 14:09:51 +0800812 if( xHandle != NULL )
813 {
814 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800815
xiaohu.huang58292b32024-01-03 14:09:51 +0800816 traceCREATE_COUNTING_SEMAPHORE();
817 }
818 else
819 {
820 traceCREATE_COUNTING_SEMAPHORE_FAILED();
821 }
822 }
823 else
824 {
825 configASSERT( xHandle );
826 mtCOVERAGE_TEST_MARKER();
827 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800828
xiaohu.huang58292b32024-01-03 14:09:51 +0800829 return xHandle;
830 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800831
832#endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
833/*-----------------------------------------------------------*/
834
xiaohu.huang58292b32024-01-03 14:09:51 +0800835BaseType_t xQueueGenericSend( QueueHandle_t xQueue,
836 const void * const pvItemToQueue,
837 TickType_t xTicksToWait,
838 const BaseType_t xCopyPosition )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800839{
xiaohu.huang58292b32024-01-03 14:09:51 +0800840 BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;
841 TimeOut_t xTimeOut;
842 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800843
xiaohu.huang58292b32024-01-03 14:09:51 +0800844 configASSERT( pxQueue );
845 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
846 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
847 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
848 {
849 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
850 }
851 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800852
xiaohu.huang58292b32024-01-03 14:09:51 +0800853 /*lint -save -e904 This function relaxes the coding standard somewhat to
854 * allow return statements within the function itself. This is done in the
855 * interest of execution time efficiency. */
856 for( ; ; )
857 {
858 taskENTER_CRITICAL();
859 {
860 /* Is there room on the queue now? The running task must be the
861 * highest priority task wanting to access the queue. If the head item
862 * in the queue is to be overwritten then it does not matter if the
863 * queue is full. */
864 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
865 {
866 traceQUEUE_SEND( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800867
xiaohu.huang58292b32024-01-03 14:09:51 +0800868 #if ( configUSE_QUEUE_SETS == 1 )
869 {
870 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800871
xiaohu.huang58292b32024-01-03 14:09:51 +0800872 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800873
xiaohu.huang58292b32024-01-03 14:09:51 +0800874 if( pxQueue->pxQueueSetContainer != NULL )
875 {
876 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
877 {
878 /* Do not notify the queue set as an existing item
879 * was overwritten in the queue so the number of items
880 * in the queue has not changed. */
881 mtCOVERAGE_TEST_MARKER();
882 }
883 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
884 {
885 /* The queue is a member of a queue set, and posting
886 * to the queue set caused a higher priority task to
887 * unblock. A context switch is required. */
888 queueYIELD_IF_USING_PREEMPTION();
889 }
890 else
891 {
892 mtCOVERAGE_TEST_MARKER();
893 }
894 }
895 else
896 {
897 /* If there was a task waiting for data to arrive on the
898 * queue then unblock it now. */
899 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
900 {
901 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
902 {
903 /* The unblocked task has a priority higher than
904 * our own so yield immediately. Yes it is ok to
905 * do this from within the critical section - the
906 * kernel takes care of that. */
907 queueYIELD_IF_USING_PREEMPTION();
908 }
909 else
910 {
911 mtCOVERAGE_TEST_MARKER();
912 }
913 }
914 else if( xYieldRequired != pdFALSE )
915 {
916 /* This path is a special case that will only get
917 * executed if the task was holding multiple mutexes
918 * and the mutexes were given back in an order that is
919 * different to that in which they were taken. */
920 queueYIELD_IF_USING_PREEMPTION();
921 }
922 else
923 {
924 mtCOVERAGE_TEST_MARKER();
925 }
926 }
927 }
928 #else /* configUSE_QUEUE_SETS */
929 {
930 xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800931
xiaohu.huang58292b32024-01-03 14:09:51 +0800932 /* If there was a task waiting for data to arrive on the
933 * queue then unblock it now. */
934 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
935 {
936 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
937 {
938 /* The unblocked task has a priority higher than
939 * our own so yield immediately. Yes it is ok to do
940 * this from within the critical section - the kernel
941 * takes care of that. */
942 queueYIELD_IF_USING_PREEMPTION();
943 }
944 else
945 {
946 mtCOVERAGE_TEST_MARKER();
947 }
948 }
949 else if( xYieldRequired != pdFALSE )
950 {
951 /* This path is a special case that will only get
952 * executed if the task was holding multiple mutexes and
953 * the mutexes were given back in an order that is
954 * different to that in which they were taken. */
955 queueYIELD_IF_USING_PREEMPTION();
956 }
957 else
958 {
959 mtCOVERAGE_TEST_MARKER();
960 }
961 }
962 #endif /* configUSE_QUEUE_SETS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800963
xiaohu.huang58292b32024-01-03 14:09:51 +0800964 taskEXIT_CRITICAL();
965 return pdPASS;
966 }
967 else
968 {
969 if( xTicksToWait == ( TickType_t ) 0 )
970 {
971 /* The queue was full and no block time is specified (or
972 * the block time has expired) so leave now. */
973 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800974
xiaohu.huang58292b32024-01-03 14:09:51 +0800975 /* Return to the original privilege level before exiting
976 * the function. */
977 traceQUEUE_SEND_FAILED( pxQueue );
978 return errQUEUE_FULL;
979 }
980 else if( xEntryTimeSet == pdFALSE )
981 {
982 /* The queue was full and a block time was specified so
983 * configure the timeout structure. */
984 vTaskInternalSetTimeOutState( &xTimeOut );
985 xEntryTimeSet = pdTRUE;
986 }
987 else
988 {
989 /* Entry time was already set. */
990 mtCOVERAGE_TEST_MARKER();
991 }
992 }
993 }
994 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800995
xiaohu.huang58292b32024-01-03 14:09:51 +0800996 /* Interrupts and other tasks can send to and receive from the queue
997 * now the critical section has been exited. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800998
xiaohu.huang58292b32024-01-03 14:09:51 +0800999 vTaskSuspendAll();
1000 prvLockQueue( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001001
xiaohu.huang58292b32024-01-03 14:09:51 +08001002 /* Update the timeout state to see if it has expired yet. */
1003 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1004 {
1005 if( prvIsQueueFull( pxQueue ) != pdFALSE )
1006 {
1007 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
1008 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001009
xiaohu.huang58292b32024-01-03 14:09:51 +08001010 /* Unlocking the queue means queue events can effect the
1011 * event list. It is possible that interrupts occurring now
1012 * remove this task from the event list again - but as the
1013 * scheduler is suspended the task will go onto the pending
1014 * ready list instead of the actual ready list. */
1015 prvUnlockQueue( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001016
xiaohu.huang58292b32024-01-03 14:09:51 +08001017 /* Resuming the scheduler will move tasks from the pending
1018 * ready list into the ready list - so it is feasible that this
1019 * task is already in the ready list before it yields - in which
1020 * case the yield will not cause a context switch unless there
1021 * is also a higher priority task in the pending ready list. */
1022 if( xTaskResumeAll() == pdFALSE )
1023 {
1024 portYIELD_WITHIN_API();
1025 }
1026 }
1027 else
1028 {
1029 /* Try again. */
1030 prvUnlockQueue( pxQueue );
1031 ( void ) xTaskResumeAll();
1032 }
1033 }
1034 else
1035 {
1036 /* The timeout has expired. */
1037 prvUnlockQueue( pxQueue );
1038 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001039
xiaohu.huang58292b32024-01-03 14:09:51 +08001040 traceQUEUE_SEND_FAILED( pxQueue );
1041 return errQUEUE_FULL;
1042 }
1043 } /*lint -restore */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001044}
1045/*-----------------------------------------------------------*/
1046
xiaohu.huang58292b32024-01-03 14:09:51 +08001047BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,
1048 const void * const pvItemToQueue,
1049 BaseType_t * const pxHigherPriorityTaskWoken,
1050 const BaseType_t xCopyPosition )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001051{
xiaohu.huang58292b32024-01-03 14:09:51 +08001052 BaseType_t xReturn;
1053 UBaseType_t uxSavedInterruptStatus;
1054 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001055
xiaohu.huang58292b32024-01-03 14:09:51 +08001056 configASSERT( pxQueue );
1057 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1058 configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001059
xiaohu.huang58292b32024-01-03 14:09:51 +08001060 /* RTOS ports that support interrupt nesting have the concept of a maximum
1061 * system call (or maximum API call) interrupt priority. Interrupts that are
1062 * above the maximum system call priority are kept permanently enabled, even
1063 * when the RTOS kernel is in a critical section, but cannot make any calls to
1064 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1065 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1066 * failure if a FreeRTOS API function is called from an interrupt that has been
1067 * assigned a priority above the configured maximum system call priority.
1068 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1069 * that have been assigned a priority at or (logically) below the maximum
1070 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1071 * safe API to ensure interrupt entry is as fast and as simple as possible.
1072 * More information (albeit Cortex-M specific) is provided on the following
1073 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1074 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001075
xiaohu.huang58292b32024-01-03 14:09:51 +08001076 /* Similar to xQueueGenericSend, except without blocking if there is no room
1077 * in the queue. Also don't directly wake a task that was blocked on a queue
1078 * read, instead return a flag to say whether a context switch is required or
1079 * not (i.e. has a task with a higher priority than us been woken by this
1080 * post). */
1081 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1082 {
1083 if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )
1084 {
1085 const int8_t cTxLock = pxQueue->cTxLock;
1086 const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001087
xiaohu.huang58292b32024-01-03 14:09:51 +08001088 traceQUEUE_SEND_FROM_ISR( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001089
xiaohu.huang58292b32024-01-03 14:09:51 +08001090 /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a
1091 * semaphore or mutex. That means prvCopyDataToQueue() cannot result
1092 * in a task disinheriting a priority and prvCopyDataToQueue() can be
1093 * called here even though the disinherit function does not check if
1094 * the scheduler is suspended before accessing the ready lists. */
1095 ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001096
xiaohu.huang58292b32024-01-03 14:09:51 +08001097 /* The event list is not altered if the queue is locked. This will
1098 * be done when the queue is unlocked later. */
1099 if( cTxLock == queueUNLOCKED )
1100 {
1101 #if ( configUSE_QUEUE_SETS == 1 )
1102 {
1103 if( pxQueue->pxQueueSetContainer != NULL )
1104 {
1105 if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )
1106 {
1107 /* Do not notify the queue set as an existing item
1108 * was overwritten in the queue so the number of items
1109 * in the queue has not changed. */
1110 mtCOVERAGE_TEST_MARKER();
1111 }
1112 else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1113 {
1114 /* The queue is a member of a queue set, and posting
1115 * to the queue set caused a higher priority task to
1116 * unblock. A context switch is required. */
1117 if( pxHigherPriorityTaskWoken != NULL )
1118 {
1119 *pxHigherPriorityTaskWoken = pdTRUE;
1120 }
1121 else
1122 {
1123 mtCOVERAGE_TEST_MARKER();
1124 }
1125 }
1126 else
1127 {
1128 mtCOVERAGE_TEST_MARKER();
1129 }
1130 }
1131 else
1132 {
1133 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1134 {
1135 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1136 {
1137 /* The task waiting has a higher priority so
1138 * record that a context switch is required. */
1139 if( pxHigherPriorityTaskWoken != NULL )
1140 {
1141 *pxHigherPriorityTaskWoken = pdTRUE;
1142 }
1143 else
1144 {
1145 mtCOVERAGE_TEST_MARKER();
1146 }
1147 }
1148 else
1149 {
1150 mtCOVERAGE_TEST_MARKER();
1151 }
1152 }
1153 else
1154 {
1155 mtCOVERAGE_TEST_MARKER();
1156 }
1157 }
1158 }
1159 #else /* configUSE_QUEUE_SETS */
1160 {
1161 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1162 {
1163 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1164 {
1165 /* The task waiting has a higher priority so record that a
1166 * context switch is required. */
1167 if( pxHigherPriorityTaskWoken != NULL )
1168 {
1169 *pxHigherPriorityTaskWoken = pdTRUE;
1170 }
1171 else
1172 {
1173 mtCOVERAGE_TEST_MARKER();
1174 }
1175 }
1176 else
1177 {
1178 mtCOVERAGE_TEST_MARKER();
1179 }
1180 }
1181 else
1182 {
1183 mtCOVERAGE_TEST_MARKER();
1184 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001185
xiaohu.huang58292b32024-01-03 14:09:51 +08001186 /* Not used in this path. */
1187 ( void ) uxPreviousMessagesWaiting;
1188 }
1189 #endif /* configUSE_QUEUE_SETS */
1190 }
1191 else
1192 {
1193 /* Increment the lock count so the task that unlocks the queue
1194 * knows that data was posted while it was locked. */
1195 prvIncrementQueueTxLock( pxQueue, cTxLock );
1196 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001197
xiaohu.huang58292b32024-01-03 14:09:51 +08001198 xReturn = pdPASS;
1199 }
1200 else
1201 {
1202 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1203 xReturn = errQUEUE_FULL;
1204 }
1205 }
1206 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1207
1208 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001209}
1210/*-----------------------------------------------------------*/
1211
xiaohu.huang58292b32024-01-03 14:09:51 +08001212BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,
1213 BaseType_t * const pxHigherPriorityTaskWoken )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001214{
xiaohu.huang58292b32024-01-03 14:09:51 +08001215 BaseType_t xReturn;
1216 UBaseType_t uxSavedInterruptStatus;
1217 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001218
xiaohu.huang58292b32024-01-03 14:09:51 +08001219 /* Similar to xQueueGenericSendFromISR() but used with semaphores where the
1220 * item size is 0. Don't directly wake a task that was blocked on a queue
1221 * read, instead return a flag to say whether a context switch is required or
1222 * not (i.e. has a task with a higher priority than us been woken by this
1223 * post). */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001224
xiaohu.huang58292b32024-01-03 14:09:51 +08001225 configASSERT( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001226
xiaohu.huang58292b32024-01-03 14:09:51 +08001227 /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()
1228 * if the item size is not 0. */
1229 configASSERT( pxQueue->uxItemSize == 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001230
xiaohu.huang58292b32024-01-03 14:09:51 +08001231 /* Normally a mutex would not be given from an interrupt, especially if
1232 * there is a mutex holder, as priority inheritance makes no sense for an
1233 * interrupts, only tasks. */
1234 configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->u.xSemaphore.xMutexHolder != NULL ) ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001235
xiaohu.huang58292b32024-01-03 14:09:51 +08001236 /* RTOS ports that support interrupt nesting have the concept of a maximum
1237 * system call (or maximum API call) interrupt priority. Interrupts that are
1238 * above the maximum system call priority are kept permanently enabled, even
1239 * when the RTOS kernel is in a critical section, but cannot make any calls to
1240 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1241 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1242 * failure if a FreeRTOS API function is called from an interrupt that has been
1243 * assigned a priority above the configured maximum system call priority.
1244 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1245 * that have been assigned a priority at or (logically) below the maximum
1246 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1247 * safe API to ensure interrupt entry is as fast and as simple as possible.
1248 * More information (albeit Cortex-M specific) is provided on the following
1249 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1250 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001251
xiaohu.huang58292b32024-01-03 14:09:51 +08001252 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1253 {
1254 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001255
xiaohu.huang58292b32024-01-03 14:09:51 +08001256 /* When the queue is used to implement a semaphore no data is ever
1257 * moved through the queue but it is still valid to see if the queue 'has
1258 * space'. */
1259 if( uxMessagesWaiting < pxQueue->uxLength )
1260 {
1261 const int8_t cTxLock = pxQueue->cTxLock;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001262
xiaohu.huang58292b32024-01-03 14:09:51 +08001263 traceQUEUE_SEND_FROM_ISR( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001264
xiaohu.huang58292b32024-01-03 14:09:51 +08001265 /* A task can only have an inherited priority if it is a mutex
1266 * holder - and if there is a mutex holder then the mutex cannot be
1267 * given from an ISR. As this is the ISR version of the function it
1268 * can be assumed there is no mutex holder and no need to determine if
1269 * priority disinheritance is needed. Simply increase the count of
1270 * messages (semaphores) available. */
1271 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001272
xiaohu.huang58292b32024-01-03 14:09:51 +08001273 /* The event list is not altered if the queue is locked. This will
1274 * be done when the queue is unlocked later. */
1275 if( cTxLock == queueUNLOCKED )
1276 {
1277 #if ( configUSE_QUEUE_SETS == 1 )
1278 {
1279 if( pxQueue->pxQueueSetContainer != NULL )
1280 {
1281 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
1282 {
1283 /* The semaphore is a member of a queue set, and
1284 * posting to the queue set caused a higher priority
1285 * task to unblock. A context switch is required. */
1286 if( pxHigherPriorityTaskWoken != NULL )
1287 {
1288 *pxHigherPriorityTaskWoken = pdTRUE;
1289 }
1290 else
1291 {
1292 mtCOVERAGE_TEST_MARKER();
1293 }
1294 }
1295 else
1296 {
1297 mtCOVERAGE_TEST_MARKER();
1298 }
1299 }
1300 else
1301 {
1302 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1303 {
1304 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1305 {
1306 /* The task waiting has a higher priority so
1307 * record that a context switch is required. */
1308 if( pxHigherPriorityTaskWoken != NULL )
1309 {
1310 *pxHigherPriorityTaskWoken = pdTRUE;
1311 }
1312 else
1313 {
1314 mtCOVERAGE_TEST_MARKER();
1315 }
1316 }
1317 else
1318 {
1319 mtCOVERAGE_TEST_MARKER();
1320 }
1321 }
1322 else
1323 {
1324 mtCOVERAGE_TEST_MARKER();
1325 }
1326 }
1327 }
1328 #else /* configUSE_QUEUE_SETS */
1329 {
1330 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1331 {
1332 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1333 {
1334 /* The task waiting has a higher priority so record that a
1335 * context switch is required. */
1336 if( pxHigherPriorityTaskWoken != NULL )
1337 {
1338 *pxHigherPriorityTaskWoken = pdTRUE;
1339 }
1340 else
1341 {
1342 mtCOVERAGE_TEST_MARKER();
1343 }
1344 }
1345 else
1346 {
1347 mtCOVERAGE_TEST_MARKER();
1348 }
1349 }
1350 else
1351 {
1352 mtCOVERAGE_TEST_MARKER();
1353 }
1354 }
1355 #endif /* configUSE_QUEUE_SETS */
1356 }
1357 else
1358 {
1359 /* Increment the lock count so the task that unlocks the queue
1360 * knows that data was posted while it was locked. */
1361 prvIncrementQueueTxLock( pxQueue, cTxLock );
1362 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001363
xiaohu.huang58292b32024-01-03 14:09:51 +08001364 xReturn = pdPASS;
1365 }
1366 else
1367 {
1368 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
1369 xReturn = errQUEUE_FULL;
1370 }
1371 }
1372 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001373
xiaohu.huang58292b32024-01-03 14:09:51 +08001374 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001375}
1376/*-----------------------------------------------------------*/
1377
xiaohu.huang58292b32024-01-03 14:09:51 +08001378BaseType_t xQueueReceive( QueueHandle_t xQueue,
1379 void * const pvBuffer,
1380 TickType_t xTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001381{
xiaohu.huang58292b32024-01-03 14:09:51 +08001382 BaseType_t xEntryTimeSet = pdFALSE;
1383 TimeOut_t xTimeOut;
1384 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001385
xiaohu.huang58292b32024-01-03 14:09:51 +08001386 /* Check the pointer is not NULL. */
1387 configASSERT( ( pxQueue ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001388
xiaohu.huang58292b32024-01-03 14:09:51 +08001389 /* The buffer into which data is received can only be NULL if the data size
1390 * is zero (so no data is copied into the buffer). */
1391 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001392
xiaohu.huang58292b32024-01-03 14:09:51 +08001393 /* Cannot block if the scheduler is suspended. */
1394 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1395 {
1396 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1397 }
1398 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001399
xiaohu.huang58292b32024-01-03 14:09:51 +08001400 /*lint -save -e904 This function relaxes the coding standard somewhat to
1401 * allow return statements within the function itself. This is done in the
1402 * interest of execution time efficiency. */
1403 for( ; ; )
1404 {
1405 taskENTER_CRITICAL();
1406 {
1407 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001408
xiaohu.huang58292b32024-01-03 14:09:51 +08001409 /* Is there data in the queue now? To be running the calling task
1410 * must be the highest priority task wanting to access the queue. */
1411 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1412 {
1413 /* Data available, remove one item. */
1414 prvCopyDataFromQueue( pxQueue, pvBuffer );
1415 traceQUEUE_RECEIVE( pxQueue );
1416 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001417
xiaohu.huang58292b32024-01-03 14:09:51 +08001418 /* There is now space in the queue, were any tasks waiting to
1419 * post to the queue? If so, unblock the highest priority waiting
1420 * task. */
1421 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1422 {
1423 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1424 {
1425 queueYIELD_IF_USING_PREEMPTION();
1426 }
1427 else
1428 {
1429 mtCOVERAGE_TEST_MARKER();
1430 }
1431 }
1432 else
1433 {
1434 mtCOVERAGE_TEST_MARKER();
1435 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001436
xiaohu.huang58292b32024-01-03 14:09:51 +08001437 taskEXIT_CRITICAL();
1438 return pdPASS;
1439 }
1440 else
1441 {
1442 if( xTicksToWait == ( TickType_t ) 0 )
1443 {
1444 /* The queue was empty and no block time is specified (or
1445 * the block time has expired) so leave now. */
1446 taskEXIT_CRITICAL();
1447 traceQUEUE_RECEIVE_FAILED( pxQueue );
1448 return errQUEUE_EMPTY;
1449 }
1450 else if( xEntryTimeSet == pdFALSE )
1451 {
1452 /* The queue was empty and a block time was specified so
1453 * configure the timeout structure. */
1454 vTaskInternalSetTimeOutState( &xTimeOut );
1455 xEntryTimeSet = pdTRUE;
1456 }
1457 else
1458 {
1459 /* Entry time was already set. */
1460 mtCOVERAGE_TEST_MARKER();
1461 }
1462 }
1463 }
1464 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001465
xiaohu.huang58292b32024-01-03 14:09:51 +08001466 /* Interrupts and other tasks can send to and receive from the queue
1467 * now the critical section has been exited. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001468
xiaohu.huang58292b32024-01-03 14:09:51 +08001469 vTaskSuspendAll();
1470 prvLockQueue( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001471
xiaohu.huang58292b32024-01-03 14:09:51 +08001472 /* Update the timeout state to see if it has expired yet. */
1473 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1474 {
1475 /* The timeout has not expired. If the queue is still empty place
1476 * the task on the list of tasks waiting to receive from the queue. */
1477 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1478 {
1479 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
1480 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1481 prvUnlockQueue( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001482
xiaohu.huang58292b32024-01-03 14:09:51 +08001483 if( xTaskResumeAll() == pdFALSE )
1484 {
1485 portYIELD_WITHIN_API();
1486 }
1487 else
1488 {
1489 mtCOVERAGE_TEST_MARKER();
1490 }
1491 }
1492 else
1493 {
1494 /* The queue contains data again. Loop back to try and read the
1495 * data. */
1496 prvUnlockQueue( pxQueue );
1497 ( void ) xTaskResumeAll();
1498 }
1499 }
1500 else
1501 {
1502 /* Timed out. If there is no data in the queue exit, otherwise loop
1503 * back and attempt to read the data. */
1504 prvUnlockQueue( pxQueue );
1505 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001506
xiaohu.huang58292b32024-01-03 14:09:51 +08001507 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1508 {
1509 traceQUEUE_RECEIVE_FAILED( pxQueue );
1510 return errQUEUE_EMPTY;
1511 }
1512 else
1513 {
1514 mtCOVERAGE_TEST_MARKER();
1515 }
1516 }
1517 } /*lint -restore */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001518}
1519/*-----------------------------------------------------------*/
1520
xiaohu.huang58292b32024-01-03 14:09:51 +08001521BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,
1522 TickType_t xTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001523{
xiaohu.huang58292b32024-01-03 14:09:51 +08001524 BaseType_t xEntryTimeSet = pdFALSE;
1525 TimeOut_t xTimeOut;
1526 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001527
xiaohu.huang58292b32024-01-03 14:09:51 +08001528 #if ( configUSE_MUTEXES == 1 )
1529 BaseType_t xInheritanceOccurred = pdFALSE;
1530 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001531
xiaohu.huang58292b32024-01-03 14:09:51 +08001532 /* Check the queue pointer is not NULL. */
1533 configASSERT( ( pxQueue ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001534
xiaohu.huang58292b32024-01-03 14:09:51 +08001535 /* Check this really is a semaphore, in which case the item size will be
1536 * 0. */
1537 configASSERT( pxQueue->uxItemSize == 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001538
xiaohu.huang58292b32024-01-03 14:09:51 +08001539 /* Cannot block if the scheduler is suspended. */
1540 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1541 {
1542 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1543 }
1544 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001545
xiaohu.huang58292b32024-01-03 14:09:51 +08001546 /*lint -save -e904 This function relaxes the coding standard somewhat to allow return
1547 * statements within the function itself. This is done in the interest
1548 * of execution time efficiency. */
1549 for( ; ; )
1550 {
1551 taskENTER_CRITICAL();
1552 {
1553 /* Semaphores are queues with an item size of 0, and where the
1554 * number of messages in the queue is the semaphore's count value. */
1555 const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001556
xiaohu.huang58292b32024-01-03 14:09:51 +08001557 /* Is there data in the queue now? To be running the calling task
1558 * must be the highest priority task wanting to access the queue. */
1559 if( uxSemaphoreCount > ( UBaseType_t ) 0 )
1560 {
1561 traceQUEUE_RECEIVE( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001562
xiaohu.huang58292b32024-01-03 14:09:51 +08001563 /* Semaphores are queues with a data size of zero and where the
1564 * messages waiting is the semaphore's count. Reduce the count. */
1565 pxQueue->uxMessagesWaiting = uxSemaphoreCount - ( UBaseType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001566
xiaohu.huang58292b32024-01-03 14:09:51 +08001567 #if ( configUSE_MUTEXES == 1 )
1568 {
1569 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1570 {
1571 /* Record the information required to implement
1572 * priority inheritance should it become necessary. */
1573 pxQueue->u.xSemaphore.xMutexHolder = pvTaskIncrementMutexHeldCount();
1574 }
1575 else
1576 {
1577 mtCOVERAGE_TEST_MARKER();
1578 }
1579 }
1580 #endif /* configUSE_MUTEXES */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001581
xiaohu.huang58292b32024-01-03 14:09:51 +08001582 /* Check to see if other tasks are blocked waiting to give the
1583 * semaphore, and if so, unblock the highest priority such task. */
1584 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1585 {
1586 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1587 {
1588 queueYIELD_IF_USING_PREEMPTION();
1589 }
1590 else
1591 {
1592 mtCOVERAGE_TEST_MARKER();
1593 }
1594 }
1595 else
1596 {
1597 mtCOVERAGE_TEST_MARKER();
1598 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001599
xiaohu.huang58292b32024-01-03 14:09:51 +08001600 taskEXIT_CRITICAL();
1601 return pdPASS;
1602 }
1603 else
1604 {
1605 if( xTicksToWait == ( TickType_t ) 0 )
1606 {
1607 /* The semaphore count was 0 and no block time is specified
1608 * (or the block time has expired) so exit now. */
1609 taskEXIT_CRITICAL();
1610 traceQUEUE_RECEIVE_FAILED( pxQueue );
1611 return errQUEUE_EMPTY;
1612 }
1613 else if( xEntryTimeSet == pdFALSE )
1614 {
1615 /* The semaphore count was 0 and a block time was specified
1616 * so configure the timeout structure ready to block. */
1617 vTaskInternalSetTimeOutState( &xTimeOut );
1618 xEntryTimeSet = pdTRUE;
1619 }
1620 else
1621 {
1622 /* Entry time was already set. */
1623 mtCOVERAGE_TEST_MARKER();
1624 }
1625 }
1626 }
1627 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001628
xiaohu.huang58292b32024-01-03 14:09:51 +08001629 /* Interrupts and other tasks can give to and take from the semaphore
1630 * now the critical section has been exited. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001631
xiaohu.huang58292b32024-01-03 14:09:51 +08001632 vTaskSuspendAll();
1633 prvLockQueue( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001634
xiaohu.huang58292b32024-01-03 14:09:51 +08001635 /* Update the timeout state to see if it has expired yet. */
1636 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1637 {
1638 /* A block time is specified and not expired. If the semaphore
1639 * count is 0 then enter the Blocked state to wait for a semaphore to
1640 * become available. As semaphores are implemented with queues the
1641 * queue being empty is equivalent to the semaphore count being 0. */
1642 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1643 {
1644 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001645
xiaohu.huang58292b32024-01-03 14:09:51 +08001646 #if ( configUSE_MUTEXES == 1 )
1647 {
1648 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
1649 {
1650 taskENTER_CRITICAL();
1651 {
1652 xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );
1653 }
1654 taskEXIT_CRITICAL();
1655 }
1656 else
1657 {
1658 mtCOVERAGE_TEST_MARKER();
1659 }
1660 }
1661 #endif /* if ( configUSE_MUTEXES == 1 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001662
xiaohu.huang58292b32024-01-03 14:09:51 +08001663 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1664 prvUnlockQueue( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001665
xiaohu.huang58292b32024-01-03 14:09:51 +08001666 if( xTaskResumeAll() == pdFALSE )
1667 {
1668 portYIELD_WITHIN_API();
1669 }
1670 else
1671 {
1672 mtCOVERAGE_TEST_MARKER();
1673 }
1674 }
1675 else
1676 {
1677 /* There was no timeout and the semaphore count was not 0, so
1678 * attempt to take the semaphore again. */
1679 prvUnlockQueue( pxQueue );
1680 ( void ) xTaskResumeAll();
1681 }
1682 }
1683 else
1684 {
1685 /* Timed out. */
1686 prvUnlockQueue( pxQueue );
1687 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001688
xiaohu.huang58292b32024-01-03 14:09:51 +08001689 /* If the semaphore count is 0 exit now as the timeout has
1690 * expired. Otherwise return to attempt to take the semaphore that is
1691 * known to be available. As semaphores are implemented by queues the
1692 * queue being empty is equivalent to the semaphore count being 0. */
1693 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1694 {
1695 #if ( configUSE_MUTEXES == 1 )
1696 {
1697 /* xInheritanceOccurred could only have be set if
1698 * pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to
1699 * test the mutex type again to check it is actually a mutex. */
1700 if( xInheritanceOccurred != pdFALSE )
1701 {
1702 taskENTER_CRITICAL();
1703 {
1704 UBaseType_t uxHighestWaitingPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001705
xiaohu.huang58292b32024-01-03 14:09:51 +08001706 /* This task blocking on the mutex caused another
1707 * task to inherit this task's priority. Now this task
1708 * has timed out the priority should be disinherited
1709 * again, but only as low as the next highest priority
1710 * task that is waiting for the same mutex. */
1711 uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );
1712 vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );
1713 }
1714 taskEXIT_CRITICAL();
1715 }
1716 }
1717 #endif /* configUSE_MUTEXES */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001718
xiaohu.huang58292b32024-01-03 14:09:51 +08001719 traceQUEUE_RECEIVE_FAILED( pxQueue );
1720 return errQUEUE_EMPTY;
1721 }
1722 else
1723 {
1724 mtCOVERAGE_TEST_MARKER();
1725 }
1726 }
1727 } /*lint -restore */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001728}
1729/*-----------------------------------------------------------*/
1730
xiaohu.huang58292b32024-01-03 14:09:51 +08001731BaseType_t xQueuePeek( QueueHandle_t xQueue,
1732 void * const pvBuffer,
1733 TickType_t xTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001734{
xiaohu.huang58292b32024-01-03 14:09:51 +08001735 BaseType_t xEntryTimeSet = pdFALSE;
1736 TimeOut_t xTimeOut;
1737 int8_t * pcOriginalReadPosition;
1738 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001739
xiaohu.huang58292b32024-01-03 14:09:51 +08001740 /* Check the pointer is not NULL. */
1741 configASSERT( ( pxQueue ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001742
xiaohu.huang58292b32024-01-03 14:09:51 +08001743 /* The buffer into which data is received can only be NULL if the data size
1744 * is zero (so no data is copied into the buffer. */
1745 configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001746
xiaohu.huang58292b32024-01-03 14:09:51 +08001747 /* Cannot block if the scheduler is suspended. */
1748 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
1749 {
1750 configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
1751 }
1752 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001753
xiaohu.huang58292b32024-01-03 14:09:51 +08001754 /*lint -save -e904 This function relaxes the coding standard somewhat to
1755 * allow return statements within the function itself. This is done in the
1756 * interest of execution time efficiency. */
1757 for( ; ; )
1758 {
1759 taskENTER_CRITICAL();
1760 {
1761 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001762
xiaohu.huang58292b32024-01-03 14:09:51 +08001763 /* Is there data in the queue now? To be running the calling task
1764 * must be the highest priority task wanting to access the queue. */
1765 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1766 {
1767 /* Remember the read position so it can be reset after the data
1768 * is read from the queue as this function is only peeking the
1769 * data, not removing it. */
1770 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001771
xiaohu.huang58292b32024-01-03 14:09:51 +08001772 prvCopyDataFromQueue( pxQueue, pvBuffer );
1773 traceQUEUE_PEEK( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001774
xiaohu.huang58292b32024-01-03 14:09:51 +08001775 /* The data is not being removed, so reset the read pointer. */
1776 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001777
xiaohu.huang58292b32024-01-03 14:09:51 +08001778 /* The data is being left in the queue, so see if there are
1779 * any other tasks waiting for the data. */
1780 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
1781 {
1782 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
1783 {
1784 /* The task waiting has a higher priority than this task. */
1785 queueYIELD_IF_USING_PREEMPTION();
1786 }
1787 else
1788 {
1789 mtCOVERAGE_TEST_MARKER();
1790 }
1791 }
1792 else
1793 {
1794 mtCOVERAGE_TEST_MARKER();
1795 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001796
xiaohu.huang58292b32024-01-03 14:09:51 +08001797 taskEXIT_CRITICAL();
1798 return pdPASS;
1799 }
1800 else
1801 {
1802 if( xTicksToWait == ( TickType_t ) 0 )
1803 {
1804 /* The queue was empty and no block time is specified (or
1805 * the block time has expired) so leave now. */
1806 taskEXIT_CRITICAL();
1807 traceQUEUE_PEEK_FAILED( pxQueue );
1808 return errQUEUE_EMPTY;
1809 }
1810 else if( xEntryTimeSet == pdFALSE )
1811 {
1812 /* The queue was empty and a block time was specified so
1813 * configure the timeout structure ready to enter the blocked
1814 * state. */
1815 vTaskInternalSetTimeOutState( &xTimeOut );
1816 xEntryTimeSet = pdTRUE;
1817 }
1818 else
1819 {
1820 /* Entry time was already set. */
1821 mtCOVERAGE_TEST_MARKER();
1822 }
1823 }
1824 }
1825 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001826
xiaohu.huang58292b32024-01-03 14:09:51 +08001827 /* Interrupts and other tasks can send to and receive from the queue
1828 * now that the critical section has been exited. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001829
xiaohu.huang58292b32024-01-03 14:09:51 +08001830 vTaskSuspendAll();
1831 prvLockQueue( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001832
xiaohu.huang58292b32024-01-03 14:09:51 +08001833 /* Update the timeout state to see if it has expired yet. */
1834 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
1835 {
1836 /* Timeout has not expired yet, check to see if there is data in the
1837 * queue now, and if not enter the Blocked state to wait for data. */
1838 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1839 {
1840 traceBLOCKING_ON_QUEUE_PEEK( pxQueue );
1841 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
1842 prvUnlockQueue( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001843
xiaohu.huang58292b32024-01-03 14:09:51 +08001844 if( xTaskResumeAll() == pdFALSE )
1845 {
1846 portYIELD_WITHIN_API();
1847 }
1848 else
1849 {
1850 mtCOVERAGE_TEST_MARKER();
1851 }
1852 }
1853 else
1854 {
1855 /* There is data in the queue now, so don't enter the blocked
1856 * state, instead return to try and obtain the data. */
1857 prvUnlockQueue( pxQueue );
1858 ( void ) xTaskResumeAll();
1859 }
1860 }
1861 else
1862 {
1863 /* The timeout has expired. If there is still no data in the queue
1864 * exit, otherwise go back and try to read the data again. */
1865 prvUnlockQueue( pxQueue );
1866 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001867
xiaohu.huang58292b32024-01-03 14:09:51 +08001868 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
1869 {
1870 traceQUEUE_PEEK_FAILED( pxQueue );
1871 return errQUEUE_EMPTY;
1872 }
1873 else
1874 {
1875 mtCOVERAGE_TEST_MARKER();
1876 }
1877 }
1878 } /*lint -restore */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001879}
1880/*-----------------------------------------------------------*/
1881
xiaohu.huang58292b32024-01-03 14:09:51 +08001882BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,
1883 void * const pvBuffer,
1884 BaseType_t * const pxHigherPriorityTaskWoken )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001885{
xiaohu.huang58292b32024-01-03 14:09:51 +08001886 BaseType_t xReturn;
1887 UBaseType_t uxSavedInterruptStatus;
1888 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001889
xiaohu.huang58292b32024-01-03 14:09:51 +08001890 configASSERT( pxQueue );
1891 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001892
xiaohu.huang58292b32024-01-03 14:09:51 +08001893 /* RTOS ports that support interrupt nesting have the concept of a maximum
1894 * system call (or maximum API call) interrupt priority. Interrupts that are
1895 * above the maximum system call priority are kept permanently enabled, even
1896 * when the RTOS kernel is in a critical section, but cannot make any calls to
1897 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1898 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1899 * failure if a FreeRTOS API function is called from an interrupt that has been
1900 * assigned a priority above the configured maximum system call priority.
1901 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1902 * that have been assigned a priority at or (logically) below the maximum
1903 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1904 * safe API to ensure interrupt entry is as fast and as simple as possible.
1905 * More information (albeit Cortex-M specific) is provided on the following
1906 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1907 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001908
xiaohu.huang58292b32024-01-03 14:09:51 +08001909 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1910 {
1911 const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001912
xiaohu.huang58292b32024-01-03 14:09:51 +08001913 /* Cannot block in an ISR, so check there is data available. */
1914 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
1915 {
1916 const int8_t cRxLock = pxQueue->cRxLock;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001917
xiaohu.huang58292b32024-01-03 14:09:51 +08001918 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001919
xiaohu.huang58292b32024-01-03 14:09:51 +08001920 prvCopyDataFromQueue( pxQueue, pvBuffer );
1921 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001922
xiaohu.huang58292b32024-01-03 14:09:51 +08001923 /* If the queue is locked the event list will not be modified.
1924 * Instead update the lock count so the task that unlocks the queue
1925 * will know that an ISR has removed data while the queue was
1926 * locked. */
1927 if( cRxLock == queueUNLOCKED )
1928 {
1929 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
1930 {
1931 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
1932 {
1933 /* The task waiting has a higher priority than us so
1934 * force a context switch. */
1935 if( pxHigherPriorityTaskWoken != NULL )
1936 {
1937 *pxHigherPriorityTaskWoken = pdTRUE;
1938 }
1939 else
1940 {
1941 mtCOVERAGE_TEST_MARKER();
1942 }
1943 }
1944 else
1945 {
1946 mtCOVERAGE_TEST_MARKER();
1947 }
1948 }
1949 else
1950 {
1951 mtCOVERAGE_TEST_MARKER();
1952 }
1953 }
1954 else
1955 {
1956 /* Increment the lock count so the task that unlocks the queue
1957 * knows that data was removed while it was locked. */
1958 prvIncrementQueueRxLock( pxQueue, cRxLock );
1959 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001960
xiaohu.huang58292b32024-01-03 14:09:51 +08001961 xReturn = pdPASS;
1962 }
1963 else
1964 {
1965 xReturn = pdFAIL;
1966 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
1967 }
1968 }
1969 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001970
xiaohu.huang58292b32024-01-03 14:09:51 +08001971 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001972}
1973/*-----------------------------------------------------------*/
1974
xiaohu.huang58292b32024-01-03 14:09:51 +08001975BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,
1976 void * const pvBuffer )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001977{
xiaohu.huang58292b32024-01-03 14:09:51 +08001978 BaseType_t xReturn;
1979 UBaseType_t uxSavedInterruptStatus;
1980 int8_t * pcOriginalReadPosition;
1981 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001982
xiaohu.huang58292b32024-01-03 14:09:51 +08001983 configASSERT( pxQueue );
1984 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );
1985 configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001986
xiaohu.huang58292b32024-01-03 14:09:51 +08001987 /* RTOS ports that support interrupt nesting have the concept of a maximum
1988 * system call (or maximum API call) interrupt priority. Interrupts that are
1989 * above the maximum system call priority are kept permanently enabled, even
1990 * when the RTOS kernel is in a critical section, but cannot make any calls to
1991 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
1992 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1993 * failure if a FreeRTOS API function is called from an interrupt that has been
1994 * assigned a priority above the configured maximum system call priority.
1995 * Only FreeRTOS functions that end in FromISR can be called from interrupts
1996 * that have been assigned a priority at or (logically) below the maximum
1997 * system call interrupt priority. FreeRTOS maintains a separate interrupt
1998 * safe API to ensure interrupt entry is as fast and as simple as possible.
1999 * More information (albeit Cortex-M specific) is provided on the following
2000 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2001 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002002
xiaohu.huang58292b32024-01-03 14:09:51 +08002003 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2004 {
2005 /* Cannot block in an ISR, so check there is data available. */
2006 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2007 {
2008 traceQUEUE_PEEK_FROM_ISR( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002009
xiaohu.huang58292b32024-01-03 14:09:51 +08002010 /* Remember the read position so it can be reset as nothing is
2011 * actually being removed from the queue. */
2012 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;
2013 prvCopyDataFromQueue( pxQueue, pvBuffer );
2014 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002015
xiaohu.huang58292b32024-01-03 14:09:51 +08002016 xReturn = pdPASS;
2017 }
2018 else
2019 {
2020 xReturn = pdFAIL;
2021 traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );
2022 }
2023 }
2024 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002025
xiaohu.huang58292b32024-01-03 14:09:51 +08002026 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002027}
2028/*-----------------------------------------------------------*/
2029
2030UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )
2031{
xiaohu.huang58292b32024-01-03 14:09:51 +08002032 UBaseType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002033
xiaohu.huang58292b32024-01-03 14:09:51 +08002034 configASSERT( xQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002035
xiaohu.huang58292b32024-01-03 14:09:51 +08002036 taskENTER_CRITICAL();
2037 {
2038 uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;
2039 }
2040 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002041
xiaohu.huang58292b32024-01-03 14:09:51 +08002042 return uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002043} /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2044/*-----------------------------------------------------------*/
2045
2046UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )
2047{
xiaohu.huang58292b32024-01-03 14:09:51 +08002048 UBaseType_t uxReturn;
2049 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002050
xiaohu.huang58292b32024-01-03 14:09:51 +08002051 configASSERT( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002052
xiaohu.huang58292b32024-01-03 14:09:51 +08002053 taskENTER_CRITICAL();
2054 {
2055 uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;
2056 }
2057 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002058
xiaohu.huang58292b32024-01-03 14:09:51 +08002059 return uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002060} /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2061/*-----------------------------------------------------------*/
2062
2063UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )
2064{
xiaohu.huang58292b32024-01-03 14:09:51 +08002065 UBaseType_t uxReturn;
2066 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002067
xiaohu.huang58292b32024-01-03 14:09:51 +08002068 configASSERT( pxQueue );
2069 uxReturn = pxQueue->uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002070
xiaohu.huang58292b32024-01-03 14:09:51 +08002071 return uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002072} /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */
2073/*-----------------------------------------------------------*/
2074
2075void vQueueDelete( QueueHandle_t xQueue )
2076{
xiaohu.huang58292b32024-01-03 14:09:51 +08002077 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002078
xiaohu.huang58292b32024-01-03 14:09:51 +08002079 configASSERT( pxQueue );
2080 traceQUEUE_DELETE( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002081
xiaohu.huang58292b32024-01-03 14:09:51 +08002082 #if ( configQUEUE_REGISTRY_SIZE > 0 )
2083 {
2084 vQueueUnregisterQueue( pxQueue );
2085 }
2086 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002087
xiaohu.huang58292b32024-01-03 14:09:51 +08002088 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
2089 {
2090 /* The queue can only have been allocated dynamically - free it
2091 * again. */
2092 vPortFree( pxQueue );
2093 }
2094 #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
2095 {
2096 /* The queue could have been allocated statically or dynamically, so
2097 * check before attempting to free the memory. */
2098 if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
2099 {
2100 vPortFree( pxQueue );
2101 }
2102 else
2103 {
2104 mtCOVERAGE_TEST_MARKER();
2105 }
2106 }
2107 #else /* if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) ) */
2108 {
2109 /* The queue must have been statically allocated, so is not going to be
2110 * deleted. Avoid compiler warnings about the unused parameter. */
2111 ( void ) pxQueue;
2112 }
2113 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002114}
2115/*-----------------------------------------------------------*/
2116
2117#if ( configUSE_TRACE_FACILITY == 1 )
2118
xiaohu.huang58292b32024-01-03 14:09:51 +08002119 UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )
2120 {
2121 return ( ( Queue_t * ) xQueue )->uxQueueNumber;
2122 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002123
2124#endif /* configUSE_TRACE_FACILITY */
2125/*-----------------------------------------------------------*/
2126
2127#if ( configUSE_TRACE_FACILITY == 1 )
2128
xiaohu.huang58292b32024-01-03 14:09:51 +08002129 void vQueueSetQueueNumber( QueueHandle_t xQueue,
2130 UBaseType_t uxQueueNumber )
2131 {
2132 ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;
2133 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002134
2135#endif /* configUSE_TRACE_FACILITY */
2136/*-----------------------------------------------------------*/
2137
2138#if ( configUSE_TRACE_FACILITY == 1 )
2139
xiaohu.huang58292b32024-01-03 14:09:51 +08002140 uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )
2141 {
2142 return ( ( Queue_t * ) xQueue )->ucQueueType;
2143 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002144
2145#endif /* configUSE_TRACE_FACILITY */
2146/*-----------------------------------------------------------*/
2147
xiaohu.huang58292b32024-01-03 14:09:51 +08002148#if ( configUSE_MUTEXES == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002149
xiaohu.huang58292b32024-01-03 14:09:51 +08002150 static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue )
2151 {
2152 UBaseType_t uxHighestPriorityOfWaitingTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002153
xiaohu.huang58292b32024-01-03 14:09:51 +08002154 /* If a task waiting for a mutex causes the mutex holder to inherit a
2155 * priority, but the waiting task times out, then the holder should
2156 * disinherit the priority - but only down to the highest priority of any
2157 * other tasks that are waiting for the same mutex. For this purpose,
2158 * return the priority of the highest priority task that is waiting for the
2159 * mutex. */
2160 if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0U )
2161 {
2162 uxHighestPriorityOfWaitingTasks = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) );
2163 }
2164 else
2165 {
2166 uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;
2167 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002168
xiaohu.huang58292b32024-01-03 14:09:51 +08002169 return uxHighestPriorityOfWaitingTasks;
2170 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002171
2172#endif /* configUSE_MUTEXES */
2173/*-----------------------------------------------------------*/
2174
xiaohu.huang58292b32024-01-03 14:09:51 +08002175static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,
2176 const void * pvItemToQueue,
2177 const BaseType_t xPosition )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002178{
xiaohu.huang58292b32024-01-03 14:09:51 +08002179 BaseType_t xReturn = pdFALSE;
2180 UBaseType_t uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002181
xiaohu.huang58292b32024-01-03 14:09:51 +08002182 /* This function is called from a critical section. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002183
xiaohu.huang58292b32024-01-03 14:09:51 +08002184 uxMessagesWaiting = pxQueue->uxMessagesWaiting;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002185
xiaohu.huang58292b32024-01-03 14:09:51 +08002186 if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )
2187 {
2188 #if ( configUSE_MUTEXES == 1 )
2189 {
2190 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
2191 {
2192 /* The mutex is no longer being held. */
2193 xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );
2194 pxQueue->u.xSemaphore.xMutexHolder = NULL;
2195 }
2196 else
2197 {
2198 mtCOVERAGE_TEST_MARKER();
2199 }
2200 }
2201 #endif /* configUSE_MUTEXES */
2202 }
2203 else if( xPosition == queueSEND_TO_BACK )
2204 {
2205 ( void ) memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports, plus previous logic ensures a null pointer can only be passed to memcpy() if the copy size is 0. Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */
2206 pxQueue->pcWriteTo += pxQueue->uxItemSize; /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002207
xiaohu.huang58292b32024-01-03 14:09:51 +08002208 if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2209 {
2210 pxQueue->pcWriteTo = pxQueue->pcHead;
2211 }
2212 else
2213 {
2214 mtCOVERAGE_TEST_MARKER();
2215 }
2216 }
2217 else
2218 {
2219 ( void ) memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e9087 !e418 MISRA exception as the casts are only redundant for some ports. Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. Assert checks null pointer only used when length is 0. */
2220 pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002221
xiaohu.huang58292b32024-01-03 14:09:51 +08002222 if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */
2223 {
2224 pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );
2225 }
2226 else
2227 {
2228 mtCOVERAGE_TEST_MARKER();
2229 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002230
xiaohu.huang58292b32024-01-03 14:09:51 +08002231 if( xPosition == queueOVERWRITE )
2232 {
2233 if( uxMessagesWaiting > ( UBaseType_t ) 0 )
2234 {
2235 /* An item is not being added but overwritten, so subtract
2236 * one from the recorded number of items in the queue so when
2237 * one is added again below the number of recorded items remains
2238 * correct. */
2239 --uxMessagesWaiting;
2240 }
2241 else
2242 {
2243 mtCOVERAGE_TEST_MARKER();
2244 }
2245 }
2246 else
2247 {
2248 mtCOVERAGE_TEST_MARKER();
2249 }
2250 }
2251
2252 pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;
2253
2254 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002255}
2256/*-----------------------------------------------------------*/
2257
xiaohu.huang58292b32024-01-03 14:09:51 +08002258static void prvCopyDataFromQueue( Queue_t * const pxQueue,
2259 void * const pvBuffer )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002260{
xiaohu.huang58292b32024-01-03 14:09:51 +08002261 if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )
2262 {
2263 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize; /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */
2264
2265 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */
2266 {
2267 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2268 }
2269 else
2270 {
2271 mtCOVERAGE_TEST_MARKER();
2272 }
2273
2274 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports. Also previous logic ensures a null pointer can only be passed to memcpy() when the count is 0. Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */
2275 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002276}
2277/*-----------------------------------------------------------*/
2278
2279static void prvUnlockQueue( Queue_t * const pxQueue )
2280{
xiaohu.huang58292b32024-01-03 14:09:51 +08002281 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002282
xiaohu.huang58292b32024-01-03 14:09:51 +08002283 /* The lock counts contains the number of extra data items placed or
2284 * removed from the queue while the queue was locked. When a queue is
2285 * locked items can be added or removed, but the event lists cannot be
2286 * updated. */
2287 taskENTER_CRITICAL();
2288 {
2289 int8_t cTxLock = pxQueue->cTxLock;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002290
xiaohu.huang58292b32024-01-03 14:09:51 +08002291 /* See if data was added to the queue while it was locked. */
2292 while( cTxLock > queueLOCKED_UNMODIFIED )
2293 {
2294 /* Data was posted while the queue was locked. Are any tasks
2295 * blocked waiting for data to become available? */
2296 #if ( configUSE_QUEUE_SETS == 1 )
2297 {
2298 if( pxQueue->pxQueueSetContainer != NULL )
2299 {
2300 if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )
2301 {
2302 /* The queue is a member of a queue set, and posting to
2303 * the queue set caused a higher priority task to unblock.
2304 * A context switch is required. */
2305 vTaskMissedYield();
2306 }
2307 else
2308 {
2309 mtCOVERAGE_TEST_MARKER();
2310 }
2311 }
2312 else
2313 {
2314 /* Tasks that are removed from the event list will get
2315 * added to the pending ready list as the scheduler is still
2316 * suspended. */
2317 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2318 {
2319 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2320 {
2321 /* The task waiting has a higher priority so record that a
2322 * context switch is required. */
2323 vTaskMissedYield();
2324 }
2325 else
2326 {
2327 mtCOVERAGE_TEST_MARKER();
2328 }
2329 }
2330 else
2331 {
2332 break;
2333 }
2334 }
2335 }
2336 #else /* configUSE_QUEUE_SETS */
2337 {
2338 /* Tasks that are removed from the event list will get added to
2339 * the pending ready list as the scheduler is still suspended. */
2340 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2341 {
2342 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2343 {
2344 /* The task waiting has a higher priority so record that
2345 * a context switch is required. */
2346 vTaskMissedYield();
2347 }
2348 else
2349 {
2350 mtCOVERAGE_TEST_MARKER();
2351 }
2352 }
2353 else
2354 {
2355 break;
2356 }
2357 }
2358 #endif /* configUSE_QUEUE_SETS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002359
xiaohu.huang58292b32024-01-03 14:09:51 +08002360 --cTxLock;
2361 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002362
xiaohu.huang58292b32024-01-03 14:09:51 +08002363 pxQueue->cTxLock = queueUNLOCKED;
2364 }
2365 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002366
xiaohu.huang58292b32024-01-03 14:09:51 +08002367 /* Do the same for the Rx lock. */
2368 taskENTER_CRITICAL();
2369 {
2370 int8_t cRxLock = pxQueue->cRxLock;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002371
xiaohu.huang58292b32024-01-03 14:09:51 +08002372 while( cRxLock > queueLOCKED_UNMODIFIED )
2373 {
2374 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2375 {
2376 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2377 {
2378 vTaskMissedYield();
2379 }
2380 else
2381 {
2382 mtCOVERAGE_TEST_MARKER();
2383 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002384
xiaohu.huang58292b32024-01-03 14:09:51 +08002385 --cRxLock;
2386 }
2387 else
2388 {
2389 break;
2390 }
2391 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002392
xiaohu.huang58292b32024-01-03 14:09:51 +08002393 pxQueue->cRxLock = queueUNLOCKED;
2394 }
2395 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002396}
2397/*-----------------------------------------------------------*/
2398
xiaohu.huang58292b32024-01-03 14:09:51 +08002399static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002400{
xiaohu.huang58292b32024-01-03 14:09:51 +08002401 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002402
xiaohu.huang58292b32024-01-03 14:09:51 +08002403 taskENTER_CRITICAL();
2404 {
2405 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2406 {
2407 xReturn = pdTRUE;
2408 }
2409 else
2410 {
2411 xReturn = pdFALSE;
2412 }
2413 }
2414 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002415
xiaohu.huang58292b32024-01-03 14:09:51 +08002416 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002417}
2418/*-----------------------------------------------------------*/
2419
2420BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )
2421{
xiaohu.huang58292b32024-01-03 14:09:51 +08002422 BaseType_t xReturn;
2423 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002424
xiaohu.huang58292b32024-01-03 14:09:51 +08002425 configASSERT( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002426
xiaohu.huang58292b32024-01-03 14:09:51 +08002427 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2428 {
2429 xReturn = pdTRUE;
2430 }
2431 else
2432 {
2433 xReturn = pdFALSE;
2434 }
2435
2436 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002437} /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2438/*-----------------------------------------------------------*/
2439
xiaohu.huang58292b32024-01-03 14:09:51 +08002440static BaseType_t prvIsQueueFull( const Queue_t * pxQueue )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002441{
xiaohu.huang58292b32024-01-03 14:09:51 +08002442 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002443
xiaohu.huang58292b32024-01-03 14:09:51 +08002444 taskENTER_CRITICAL();
2445 {
2446 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2447 {
2448 xReturn = pdTRUE;
2449 }
2450 else
2451 {
2452 xReturn = pdFALSE;
2453 }
2454 }
2455 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002456
xiaohu.huang58292b32024-01-03 14:09:51 +08002457 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002458}
2459/*-----------------------------------------------------------*/
2460
2461BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )
2462{
xiaohu.huang58292b32024-01-03 14:09:51 +08002463 BaseType_t xReturn;
2464 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002465
xiaohu.huang58292b32024-01-03 14:09:51 +08002466 configASSERT( pxQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002467
xiaohu.huang58292b32024-01-03 14:09:51 +08002468 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
2469 {
2470 xReturn = pdTRUE;
2471 }
2472 else
2473 {
2474 xReturn = pdFALSE;
2475 }
2476
2477 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002478} /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
2479/*-----------------------------------------------------------*/
2480
2481#if ( configUSE_CO_ROUTINES == 1 )
2482
xiaohu.huang58292b32024-01-03 14:09:51 +08002483 BaseType_t xQueueCRSend( QueueHandle_t xQueue,
2484 const void * pvItemToQueue,
2485 TickType_t xTicksToWait )
2486 {
2487 BaseType_t xReturn;
2488 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002489
xiaohu.huang58292b32024-01-03 14:09:51 +08002490 /* If the queue is already full we may have to block. A critical section
2491 * is required to prevent an interrupt removing something from the queue
2492 * between the check to see if the queue is full and blocking on the queue. */
2493 portDISABLE_INTERRUPTS();
2494 {
2495 if( prvIsQueueFull( pxQueue ) != pdFALSE )
2496 {
2497 /* The queue is full - do we want to block or just leave without
2498 * posting? */
2499 if( xTicksToWait > ( TickType_t ) 0 )
2500 {
2501 /* As this is called from a coroutine we cannot block directly, but
2502 * return indicating that we need to block. */
2503 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
2504 portENABLE_INTERRUPTS();
2505 return errQUEUE_BLOCKED;
2506 }
2507 else
2508 {
2509 portENABLE_INTERRUPTS();
2510 return errQUEUE_FULL;
2511 }
2512 }
2513 }
2514 portENABLE_INTERRUPTS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002515
xiaohu.huang58292b32024-01-03 14:09:51 +08002516 portDISABLE_INTERRUPTS();
2517 {
2518 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2519 {
2520 /* There is room in the queue, copy the data into the queue. */
2521 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
2522 xReturn = pdPASS;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002523
xiaohu.huang58292b32024-01-03 14:09:51 +08002524 /* Were any co-routines waiting for data to become available? */
2525 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2526 {
2527 /* In this instance the co-routine could be placed directly
2528 * into the ready list as we are within a critical section.
2529 * Instead the same pending ready list mechanism is used as if
2530 * the event were caused from within an interrupt. */
2531 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2532 {
2533 /* The co-routine waiting has a higher priority so record
2534 * that a yield might be appropriate. */
2535 xReturn = errQUEUE_YIELD;
2536 }
2537 else
2538 {
2539 mtCOVERAGE_TEST_MARKER();
2540 }
2541 }
2542 else
2543 {
2544 mtCOVERAGE_TEST_MARKER();
2545 }
2546 }
2547 else
2548 {
2549 xReturn = errQUEUE_FULL;
2550 }
2551 }
2552 portENABLE_INTERRUPTS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002553
xiaohu.huang58292b32024-01-03 14:09:51 +08002554 return xReturn;
2555 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002556
2557#endif /* configUSE_CO_ROUTINES */
2558/*-----------------------------------------------------------*/
2559
2560#if ( configUSE_CO_ROUTINES == 1 )
2561
xiaohu.huang58292b32024-01-03 14:09:51 +08002562 BaseType_t xQueueCRReceive( QueueHandle_t xQueue,
2563 void * pvBuffer,
2564 TickType_t xTicksToWait )
2565 {
2566 BaseType_t xReturn;
2567 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002568
xiaohu.huang58292b32024-01-03 14:09:51 +08002569 /* If the queue is already empty we may have to block. A critical section
2570 * is required to prevent an interrupt adding something to the queue
2571 * between the check to see if the queue is empty and blocking on the queue. */
2572 portDISABLE_INTERRUPTS();
2573 {
2574 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )
2575 {
2576 /* There are no messages in the queue, do we want to block or just
2577 * leave with nothing? */
2578 if( xTicksToWait > ( TickType_t ) 0 )
2579 {
2580 /* As this is a co-routine we cannot block directly, but return
2581 * indicating that we need to block. */
2582 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
2583 portENABLE_INTERRUPTS();
2584 return errQUEUE_BLOCKED;
2585 }
2586 else
2587 {
2588 portENABLE_INTERRUPTS();
2589 return errQUEUE_FULL;
2590 }
2591 }
2592 else
2593 {
2594 mtCOVERAGE_TEST_MARKER();
2595 }
2596 }
2597 portENABLE_INTERRUPTS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002598
xiaohu.huang58292b32024-01-03 14:09:51 +08002599 portDISABLE_INTERRUPTS();
2600 {
2601 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2602 {
2603 /* Data is available from the queue. */
2604 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002605
xiaohu.huang58292b32024-01-03 14:09:51 +08002606 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2607 {
2608 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2609 }
2610 else
2611 {
2612 mtCOVERAGE_TEST_MARKER();
2613 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002614
xiaohu.huang58292b32024-01-03 14:09:51 +08002615 --( pxQueue->uxMessagesWaiting );
2616 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002617
xiaohu.huang58292b32024-01-03 14:09:51 +08002618 xReturn = pdPASS;
2619
2620 /* Were any co-routines waiting for space to become available? */
2621 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2622 {
2623 /* In this instance the co-routine could be placed directly
2624 * into the ready list as we are within a critical section.
2625 * Instead the same pending ready list mechanism is used as if
2626 * the event were caused from within an interrupt. */
2627 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2628 {
2629 xReturn = errQUEUE_YIELD;
2630 }
2631 else
2632 {
2633 mtCOVERAGE_TEST_MARKER();
2634 }
2635 }
2636 else
2637 {
2638 mtCOVERAGE_TEST_MARKER();
2639 }
2640 }
2641 else
2642 {
2643 xReturn = pdFAIL;
2644 }
2645 }
2646 portENABLE_INTERRUPTS();
2647
2648 return xReturn;
2649 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002650
2651#endif /* configUSE_CO_ROUTINES */
2652/*-----------------------------------------------------------*/
2653
2654#if ( configUSE_CO_ROUTINES == 1 )
2655
xiaohu.huang58292b32024-01-03 14:09:51 +08002656 BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue,
2657 const void * pvItemToQueue,
2658 BaseType_t xCoRoutinePreviouslyWoken )
2659 {
2660 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002661
xiaohu.huang58292b32024-01-03 14:09:51 +08002662 /* Cannot block within an ISR so if there is no space on the queue then
2663 * exit without doing anything. */
2664 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
2665 {
2666 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002667
xiaohu.huang58292b32024-01-03 14:09:51 +08002668 /* We only want to wake one co-routine per ISR, so check that a
2669 * co-routine has not already been woken. */
2670 if( xCoRoutinePreviouslyWoken == pdFALSE )
2671 {
2672 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
2673 {
2674 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
2675 {
2676 return pdTRUE;
2677 }
2678 else
2679 {
2680 mtCOVERAGE_TEST_MARKER();
2681 }
2682 }
2683 else
2684 {
2685 mtCOVERAGE_TEST_MARKER();
2686 }
2687 }
2688 else
2689 {
2690 mtCOVERAGE_TEST_MARKER();
2691 }
2692 }
2693 else
2694 {
2695 mtCOVERAGE_TEST_MARKER();
2696 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002697
xiaohu.huang58292b32024-01-03 14:09:51 +08002698 return xCoRoutinePreviouslyWoken;
2699 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002700
2701#endif /* configUSE_CO_ROUTINES */
2702/*-----------------------------------------------------------*/
2703
2704#if ( configUSE_CO_ROUTINES == 1 )
2705
xiaohu.huang58292b32024-01-03 14:09:51 +08002706 BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue,
2707 void * pvBuffer,
2708 BaseType_t * pxCoRoutineWoken )
2709 {
2710 BaseType_t xReturn;
2711 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002712
xiaohu.huang58292b32024-01-03 14:09:51 +08002713 /* We cannot block from an ISR, so check there is data available. If
2714 * not then just leave without doing anything. */
2715 if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )
2716 {
2717 /* Copy the data from the queue. */
2718 pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002719
xiaohu.huang58292b32024-01-03 14:09:51 +08002720 if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )
2721 {
2722 pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;
2723 }
2724 else
2725 {
2726 mtCOVERAGE_TEST_MARKER();
2727 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002728
xiaohu.huang58292b32024-01-03 14:09:51 +08002729 --( pxQueue->uxMessagesWaiting );
2730 ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002731
xiaohu.huang58292b32024-01-03 14:09:51 +08002732 if( ( *pxCoRoutineWoken ) == pdFALSE )
2733 {
2734 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
2735 {
2736 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
2737 {
2738 *pxCoRoutineWoken = pdTRUE;
2739 }
2740 else
2741 {
2742 mtCOVERAGE_TEST_MARKER();
2743 }
2744 }
2745 else
2746 {
2747 mtCOVERAGE_TEST_MARKER();
2748 }
2749 }
2750 else
2751 {
2752 mtCOVERAGE_TEST_MARKER();
2753 }
2754
2755 xReturn = pdPASS;
2756 }
2757 else
2758 {
2759 xReturn = pdFAIL;
2760 }
2761
2762 return xReturn;
2763 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002764
2765#endif /* configUSE_CO_ROUTINES */
2766/*-----------------------------------------------------------*/
2767
2768#if ( configQUEUE_REGISTRY_SIZE > 0 )
2769
xiaohu.huang58292b32024-01-03 14:09:51 +08002770 void vQueueAddToRegistry( QueueHandle_t xQueue,
2771 const char * pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2772 {
2773 UBaseType_t ux;
2774 QueueRegistryItem_t * pxEntryToWrite = NULL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002775
xiaohu.huang58292b32024-01-03 14:09:51 +08002776 configASSERT( xQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002777
xiaohu.huang58292b32024-01-03 14:09:51 +08002778 if( pcQueueName != NULL )
2779 {
2780 /* See if there is an empty space in the registry. A NULL name denotes
2781 * a free slot. */
2782 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2783 {
2784 /* Replace an existing entry if the queue is already in the registry. */
2785 if( xQueue == xQueueRegistry[ ux ].xHandle )
2786 {
2787 pxEntryToWrite = &( xQueueRegistry[ ux ] );
2788 break;
2789 }
2790 /* Otherwise, store in the next empty location */
2791 else if( ( pxEntryToWrite == NULL ) && ( xQueueRegistry[ ux ].pcQueueName == NULL ) )
2792 {
2793 pxEntryToWrite = &( xQueueRegistry[ ux ] );
2794 }
2795 else
2796 {
2797 mtCOVERAGE_TEST_MARKER();
2798 }
2799 }
2800 }
2801
2802 if( pxEntryToWrite != NULL )
2803 {
2804 /* Store the information on this queue. */
2805 pxEntryToWrite->pcQueueName = pcQueueName;
2806 pxEntryToWrite->xHandle = xQueue;
2807
2808 traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );
2809 }
2810 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002811
2812#endif /* configQUEUE_REGISTRY_SIZE */
2813/*-----------------------------------------------------------*/
2814
2815#if ( configQUEUE_REGISTRY_SIZE > 0 )
2816
xiaohu.huang58292b32024-01-03 14:09:51 +08002817 const char * pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2818 {
2819 UBaseType_t ux;
2820 const char * pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002821
xiaohu.huang58292b32024-01-03 14:09:51 +08002822 configASSERT( xQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002823
xiaohu.huang58292b32024-01-03 14:09:51 +08002824 /* Note there is nothing here to protect against another task adding or
2825 * removing entries from the registry while it is being searched. */
2826
2827 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2828 {
2829 if( xQueueRegistry[ ux ].xHandle == xQueue )
2830 {
2831 pcReturn = xQueueRegistry[ ux ].pcQueueName;
2832 break;
2833 }
2834 else
2835 {
2836 mtCOVERAGE_TEST_MARKER();
2837 }
2838 }
2839
2840 return pcReturn;
2841 } /*lint !e818 xQueue cannot be a pointer to const because it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002842
2843#endif /* configQUEUE_REGISTRY_SIZE */
2844/*-----------------------------------------------------------*/
2845
2846#if ( configQUEUE_REGISTRY_SIZE > 0 )
2847
xiaohu.huang58292b32024-01-03 14:09:51 +08002848 void vQueueUnregisterQueue( QueueHandle_t xQueue )
2849 {
2850 UBaseType_t ux;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002851
xiaohu.huang58292b32024-01-03 14:09:51 +08002852 configASSERT( xQueue );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002853
xiaohu.huang58292b32024-01-03 14:09:51 +08002854 /* See if the handle of the queue being unregistered in actually in the
2855 * registry. */
2856 for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )
2857 {
2858 if( xQueueRegistry[ ux ].xHandle == xQueue )
2859 {
2860 /* Set the name to NULL to show that this slot if free again. */
2861 xQueueRegistry[ ux ].pcQueueName = NULL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002862
xiaohu.huang58292b32024-01-03 14:09:51 +08002863 /* Set the handle to NULL to ensure the same queue handle cannot
2864 * appear in the registry twice if it is added, removed, then
2865 * added again. */
2866 xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;
2867 break;
2868 }
2869 else
2870 {
2871 mtCOVERAGE_TEST_MARKER();
2872 }
2873 }
2874 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002875
2876#endif /* configQUEUE_REGISTRY_SIZE */
2877/*-----------------------------------------------------------*/
2878
2879#if ( configUSE_TIMERS == 1 )
2880
xiaohu.huang58292b32024-01-03 14:09:51 +08002881 void vQueueWaitForMessageRestricted( QueueHandle_t xQueue,
2882 TickType_t xTicksToWait,
2883 const BaseType_t xWaitIndefinitely )
2884 {
2885 Queue_t * const pxQueue = xQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002886
xiaohu.huang58292b32024-01-03 14:09:51 +08002887 /* This function should not be called by application code hence the
2888 * 'Restricted' in its name. It is not part of the public API. It is
2889 * designed for use by kernel code, and has special calling requirements.
2890 * It can result in vListInsert() being called on a list that can only
2891 * possibly ever have one item in it, so the list will be fast, but even
2892 * so it should be called with the scheduler locked and not from a critical
2893 * section. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002894
xiaohu.huang58292b32024-01-03 14:09:51 +08002895 /* Only do anything if there are no messages in the queue. This function
2896 * will not actually cause the task to block, just place it on a blocked
2897 * list. It will not block until the scheduler is unlocked - at which
2898 * time a yield will be performed. If an item is added to the queue while
2899 * the queue is locked, and the calling task blocks on the queue, then the
2900 * calling task will be immediately unblocked when the queue is unlocked. */
2901 prvLockQueue( pxQueue );
2902
2903 if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )
2904 {
2905 /* There is nothing in the queue, block for the specified period. */
2906 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );
2907 }
2908 else
2909 {
2910 mtCOVERAGE_TEST_MARKER();
2911 }
2912
2913 prvUnlockQueue( pxQueue );
2914 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002915
2916#endif /* configUSE_TIMERS */
2917/*-----------------------------------------------------------*/
2918
xiaohu.huang58292b32024-01-03 14:09:51 +08002919#if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002920
xiaohu.huang58292b32024-01-03 14:09:51 +08002921 QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
2922 {
2923 QueueSetHandle_t pxQueue;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002924
xiaohu.huang58292b32024-01-03 14:09:51 +08002925 pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002926
xiaohu.huang58292b32024-01-03 14:09:51 +08002927 return pxQueue;
2928 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002929
2930#endif /* configUSE_QUEUE_SETS */
2931/*-----------------------------------------------------------*/
2932
2933#if ( configUSE_QUEUE_SETS == 1 )
2934
xiaohu.huang58292b32024-01-03 14:09:51 +08002935 BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore,
2936 QueueSetHandle_t xQueueSet )
2937 {
2938 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002939
xiaohu.huang58292b32024-01-03 14:09:51 +08002940 taskENTER_CRITICAL();
2941 {
2942 if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
2943 {
2944 /* Cannot add a queue/semaphore to more than one queue set. */
2945 xReturn = pdFAIL;
2946 }
2947 else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )
2948 {
2949 /* Cannot add a queue/semaphore to a queue set if there are already
2950 * items in the queue/semaphore. */
2951 xReturn = pdFAIL;
2952 }
2953 else
2954 {
2955 ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
2956 xReturn = pdPASS;
2957 }
2958 }
2959 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002960
xiaohu.huang58292b32024-01-03 14:09:51 +08002961 return xReturn;
2962 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002963
2964#endif /* configUSE_QUEUE_SETS */
2965/*-----------------------------------------------------------*/
2966
2967#if ( configUSE_QUEUE_SETS == 1 )
2968
xiaohu.huang58292b32024-01-03 14:09:51 +08002969 BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore,
2970 QueueSetHandle_t xQueueSet )
2971 {
2972 BaseType_t xReturn;
2973 Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002974
xiaohu.huang58292b32024-01-03 14:09:51 +08002975 if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
2976 {
2977 /* The queue was not a member of the set. */
2978 xReturn = pdFAIL;
2979 }
2980 else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )
2981 {
2982 /* It is dangerous to remove a queue from a set when the queue is
2983 * not empty because the queue set will still hold pending events for
2984 * the queue. */
2985 xReturn = pdFAIL;
2986 }
2987 else
2988 {
2989 taskENTER_CRITICAL();
2990 {
2991 /* The queue is no longer contained in the set. */
2992 pxQueueOrSemaphore->pxQueueSetContainer = NULL;
2993 }
2994 taskEXIT_CRITICAL();
2995 xReturn = pdPASS;
2996 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002997
xiaohu.huang58292b32024-01-03 14:09:51 +08002998 return xReturn;
2999 } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003000
3001#endif /* configUSE_QUEUE_SETS */
3002/*-----------------------------------------------------------*/
3003
3004#if ( configUSE_QUEUE_SETS == 1 )
3005
xiaohu.huang58292b32024-01-03 14:09:51 +08003006 QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet,
3007 TickType_t const xTicksToWait )
3008 {
3009 QueueSetMemberHandle_t xReturn = NULL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003010
xiaohu.huang58292b32024-01-03 14:09:51 +08003011 ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait ); /*lint !e961 Casting from one typedef to another is not redundant. */
3012 return xReturn;
3013 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003014
3015#endif /* configUSE_QUEUE_SETS */
3016/*-----------------------------------------------------------*/
3017
3018#if ( configUSE_QUEUE_SETS == 1 )
3019
xiaohu.huang58292b32024-01-03 14:09:51 +08003020 QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )
3021 {
3022 QueueSetMemberHandle_t xReturn = NULL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003023
xiaohu.huang58292b32024-01-03 14:09:51 +08003024 ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */
3025 return xReturn;
3026 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003027
3028#endif /* configUSE_QUEUE_SETS */
3029/*-----------------------------------------------------------*/
3030
3031#if ( configUSE_QUEUE_SETS == 1 )
3032
xiaohu.huang58292b32024-01-03 14:09:51 +08003033 static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue )
3034 {
3035 Queue_t * pxQueueSetContainer = pxQueue->pxQueueSetContainer;
3036 BaseType_t xReturn = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003037
xiaohu.huang58292b32024-01-03 14:09:51 +08003038 /* This function must be called form a critical section. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003039
xiaohu.huang58292b32024-01-03 14:09:51 +08003040 /* The following line is not reachable in unit tests because every call
3041 * to prvNotifyQueueSetContainer is preceded by a check that
3042 * pxQueueSetContainer != NULL */
3043 configASSERT( pxQueueSetContainer ); /* LCOV_EXCL_BR_LINE */
3044 configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003045
xiaohu.huang58292b32024-01-03 14:09:51 +08003046 if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
3047 {
3048 const int8_t cTxLock = pxQueueSetContainer->cTxLock;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003049
xiaohu.huang58292b32024-01-03 14:09:51 +08003050 traceQUEUE_SET_SEND( pxQueueSetContainer );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003051
xiaohu.huang58292b32024-01-03 14:09:51 +08003052 /* The data copied is the handle of the queue that contains data. */
3053 xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, queueSEND_TO_BACK );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003054
xiaohu.huang58292b32024-01-03 14:09:51 +08003055 if( cTxLock == queueUNLOCKED )
3056 {
3057 if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
3058 {
3059 if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
3060 {
3061 /* The task waiting has a higher priority. */
3062 xReturn = pdTRUE;
3063 }
3064 else
3065 {
3066 mtCOVERAGE_TEST_MARKER();
3067 }
3068 }
3069 else
3070 {
3071 mtCOVERAGE_TEST_MARKER();
3072 }
3073 }
3074 else
3075 {
3076 prvIncrementQueueTxLock( pxQueueSetContainer, cTxLock );
3077 }
3078 }
3079 else
3080 {
3081 mtCOVERAGE_TEST_MARKER();
3082 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003083
xiaohu.huang58292b32024-01-03 14:09:51 +08003084 return xReturn;
3085 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003086
3087#endif /* configUSE_QUEUE_SETS */