]> begriffs open source - freertos/blob - queue.c
README.md: Remove coroutine references.
[freertos] / queue.c
1 /*\r
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT\r
6  *\r
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
8  * this software and associated documentation files (the "Software"), to deal in\r
9  * the Software without restriction, including without limitation the rights to\r
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
11  * the Software, and to permit persons to whom the Software is furnished to do so,\r
12  * subject to the following conditions:\r
13  *\r
14  * The above copyright notice and this permission notice shall be included in all\r
15  * copies or substantial portions of the Software.\r
16  *\r
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\r
27  */\r
28 \r
29 #include <stdlib.h>\r
30 #include <string.h>\r
31 \r
32 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
33  * all the API functions to use the MPU wrappers.  That should only be done when\r
34  * task.h is included from an application file. */\r
35 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
36 \r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 #include "queue.h"\r
40 \r
41 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified\r
42  * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined\r
43  * for the header files above, but not in this file, in order to generate the\r
44  * correct privileged Vs unprivileged linkage and placement. */\r
45 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */\r
46 \r
47 \r
48 /* Constants used with the cRxLock and cTxLock structure members. */\r
49 #define queueUNLOCKED             ( ( int8_t ) -1 )\r
50 #define queueLOCKED_UNMODIFIED    ( ( int8_t ) 0 )\r
51 #define queueINT8_MAX             ( ( int8_t ) 127 )\r
52 \r
53 /* When the Queue_t structure is used to represent a base queue its pcHead and\r
54  * pcTail members are used as pointers into the queue storage area.  When the\r
55  * Queue_t structure is used to represent a mutex pcHead and pcTail pointers are\r
56  * not necessary, and the pcHead pointer is set to NULL to indicate that the\r
57  * structure instead holds a pointer to the mutex holder (if any).  Map alternative\r
58  * names to the pcHead and structure member to ensure the readability of the code\r
59  * is maintained.  The QueuePointers_t and SemaphoreData_t types are used to form\r
60  * a union as their usage is mutually exclusive dependent on what the queue is\r
61  * being used for. */\r
62 #define uxQueueType               pcHead\r
63 #define queueQUEUE_IS_MUTEX       NULL\r
64 \r
65 typedef struct QueuePointers\r
66 {\r
67     int8_t * pcTail;     /*< Points to the byte at the end of the queue storage area.  Once more byte is allocated than necessary to store the queue items, this is used as a marker. */\r
68     int8_t * pcReadFrom; /*< Points to the last place that a queued item was read from when the structure is used as a queue. */\r
69 } QueuePointers_t;\r
70 \r
71 typedef struct SemaphoreData\r
72 {\r
73     TaskHandle_t xMutexHolder;        /*< The handle of the task that holds the mutex. */\r
74     UBaseType_t uxRecursiveCallCount; /*< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */\r
75 } SemaphoreData_t;\r
76 \r
77 /* Semaphores do not actually store or copy data, so have an item size of\r
78  * zero. */\r
79 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH    ( ( UBaseType_t ) 0 )\r
80 #define queueMUTEX_GIVE_BLOCK_TIME          ( ( TickType_t ) 0U )\r
81 \r
82 #if ( configUSE_PREEMPTION == 0 )\r
83 \r
84 /* If the cooperative scheduler is being used then a yield should not be\r
85  * performed just because a higher priority task has been woken. */\r
86     #define queueYIELD_IF_USING_PREEMPTION()\r
87 #else\r
88     #define queueYIELD_IF_USING_PREEMPTION()    portYIELD_WITHIN_API()\r
89 #endif\r
90 \r
91 /*\r
92  * Definition of the queue used by the scheduler.\r
93  * Items are queued by copy, not reference.  See the following link for the\r
94  * rationale: https://www.FreeRTOS.org/Embedded-RTOS-Queues.html\r
95  */\r
96 typedef struct QueueDefinition /* The old naming convention is used to prevent breaking kernel aware debuggers. */\r
97 {\r
98     int8_t * pcHead;           /*< Points to the beginning of the queue storage area. */\r
99     int8_t * pcWriteTo;        /*< Points to the free next place in the storage area. */\r
100 \r
101     union\r
102     {\r
103         QueuePointers_t xQueue;     /*< Data required exclusively when this structure is used as a queue. */\r
104         SemaphoreData_t xSemaphore; /*< Data required exclusively when this structure is used as a semaphore. */\r
105     } u;\r
106 \r
107     List_t xTasksWaitingToSend;             /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */\r
108     List_t xTasksWaitingToReceive;          /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */\r
109 \r
110     volatile UBaseType_t uxMessagesWaiting; /*< The number of items currently in the queue. */\r
111     UBaseType_t uxLength;                   /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */\r
112     UBaseType_t uxItemSize;                 /*< The size of each items that the queue will hold. */\r
113 \r
114     volatile int8_t cRxLock;                /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */\r
115     volatile int8_t cTxLock;                /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */\r
116 \r
117     #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
118         uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */\r
119     #endif\r
120 \r
121     #if ( configUSE_QUEUE_SETS == 1 )\r
122         struct QueueDefinition * pxQueueSetContainer;\r
123     #endif\r
124 \r
125     #if ( configUSE_TRACE_FACILITY == 1 )\r
126         UBaseType_t uxQueueNumber;\r
127         uint8_t ucQueueType;\r
128     #endif\r
129 } xQUEUE;\r
130 \r
131 /* The old xQUEUE name is maintained above then typedefed to the new Queue_t\r
132  * name below to enable the use of older kernel aware debuggers. */\r
133 typedef xQUEUE Queue_t;\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /*\r
138  * The queue registry is just a means for kernel aware debuggers to locate\r
139  * queue structures.  It has no other purpose so is an optional component.\r
140  */\r
141 #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
142 \r
143 /* The type stored within the queue registry array.  This allows a name\r
144  * to be assigned to each queue making kernel aware debugging a little\r
145  * more user friendly. */\r
146     typedef struct QUEUE_REGISTRY_ITEM\r
147     {\r
148         const char * pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
149         QueueHandle_t xHandle;\r
150     } xQueueRegistryItem;\r
151 \r
152 /* The old xQueueRegistryItem name is maintained above then typedefed to the\r
153  * new xQueueRegistryItem name below to enable the use of older kernel aware\r
154  * debuggers. */\r
155     typedef xQueueRegistryItem QueueRegistryItem_t;\r
156 \r
157 /* The queue registry is simply an array of QueueRegistryItem_t structures.\r
158  * The pcQueueName member of a structure being NULL is indicative of the\r
159  * array position being vacant. */\r
160     PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];\r
161 \r
162 #endif /* configQUEUE_REGISTRY_SIZE */\r
163 \r
164 /*\r
165  * Unlocks a queue locked by a call to prvLockQueue.  Locking a queue does not\r
166  * prevent an ISR from adding or removing items to the queue, but does prevent\r
167  * an ISR from removing tasks from the queue event lists.  If an ISR finds a\r
168  * queue is locked it will instead increment the appropriate queue lock count\r
169  * to indicate that a task may require unblocking.  When the queue in unlocked\r
170  * these lock counts are inspected, and the appropriate action taken.\r
171  */\r
172 static void prvUnlockQueue( Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;\r
173 \r
174 /*\r
175  * Uses a critical section to determine if there is any data in a queue.\r
176  *\r
177  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.\r
178  */\r
179 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;\r
180 \r
181 /*\r
182  * Uses a critical section to determine if there is any space in a queue.\r
183  *\r
184  * @return pdTRUE if there is no space, otherwise pdFALSE;\r
185  */\r
186 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue ) PRIVILEGED_FUNCTION;\r
187 \r
188 /*\r
189  * Copies an item into the queue, either at the front of the queue or the\r
190  * back of the queue.\r
191  */\r
192 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,\r
193                                       const void * pvItemToQueue,\r
194                                       const BaseType_t xPosition ) PRIVILEGED_FUNCTION;\r
195 \r
196 /*\r
197  * Copies an item out of a queue.\r
198  */\r
199 static void prvCopyDataFromQueue( Queue_t * const pxQueue,\r
200                                   void * const pvBuffer ) PRIVILEGED_FUNCTION;\r
201 \r
202 #if ( configUSE_QUEUE_SETS == 1 )\r
203 \r
204 /*\r
205  * Checks to see if a queue is a member of a queue set, and if so, notifies\r
206  * the queue set that the queue contains data.\r
207  */\r
208     static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;\r
209 #endif\r
210 \r
211 /*\r
212  * Called after a Queue_t structure has been allocated either statically or\r
213  * dynamically to fill in the structure's members.\r
214  */\r
215 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,\r
216                                    const UBaseType_t uxItemSize,\r
217                                    uint8_t * pucQueueStorage,\r
218                                    const uint8_t ucQueueType,\r
219                                    Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;\r
220 \r
221 /*\r
222  * Mutexes are a special type of queue.  When a mutex is created, first the\r
223  * queue is created, then prvInitialiseMutex() is called to configure the queue\r
224  * as a mutex.\r
225  */\r
226 #if ( configUSE_MUTEXES == 1 )\r
227     static void prvInitialiseMutex( Queue_t * pxNewQueue ) PRIVILEGED_FUNCTION;\r
228 #endif\r
229 \r
230 #if ( configUSE_MUTEXES == 1 )\r
231 \r
232 /*\r
233  * If a task waiting for a mutex causes the mutex holder to inherit a\r
234  * priority, but the waiting task times out, then the holder should\r
235  * disinherit the priority - but only down to the highest priority of any\r
236  * other tasks that are waiting for the same mutex.  This function returns\r
237  * that priority.\r
238  */\r
239     static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;\r
240 #endif\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 /*\r
244  * Macro to mark a queue as locked.  Locking a queue prevents an ISR from\r
245  * accessing the queue event lists.\r
246  */\r
247 #define prvLockQueue( pxQueue )                            \\r
248     taskENTER_CRITICAL();                                  \\r
249     {                                                      \\r
250         if( ( pxQueue )->cRxLock == queueUNLOCKED )        \\r
251         {                                                  \\r
252             ( pxQueue )->cRxLock = queueLOCKED_UNMODIFIED; \\r
253         }                                                  \\r
254         if( ( pxQueue )->cTxLock == queueUNLOCKED )        \\r
255         {                                                  \\r
256             ( pxQueue )->cTxLock = queueLOCKED_UNMODIFIED; \\r
257         }                                                  \\r
258     }                                                      \\r
259     taskEXIT_CRITICAL()\r
260 \r
261 /*\r
262  * Macro to increment cTxLock member of the queue data structure. It is\r
263  * capped at the number of tasks in the system as we cannot unblock more\r
264  * tasks than the number of tasks in the system.\r
265  */\r
266 #define prvIncrementQueueTxLock( pxQueue, cTxLock )                           \\r
267     {                                                                         \\r
268         const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks();         \\r
269         if( ( UBaseType_t ) ( cTxLock ) < uxNumberOfTasks )                   \\r
270         {                                                                     \\r
271             configASSERT( ( cTxLock ) != queueINT8_MAX );                     \\r
272             ( pxQueue )->cTxLock = ( int8_t ) ( ( cTxLock ) + ( int8_t ) 1 ); \\r
273         }                                                                     \\r
274     }\r
275 \r
276 /*\r
277  * Macro to increment cRxLock member of the queue data structure. It is\r
278  * capped at the number of tasks in the system as we cannot unblock more\r
279  * tasks than the number of tasks in the system.\r
280  */\r
281 #define prvIncrementQueueRxLock( pxQueue, cRxLock )                           \\r
282     {                                                                         \\r
283         const UBaseType_t uxNumberOfTasks = uxTaskGetNumberOfTasks();         \\r
284         if( ( UBaseType_t ) ( cRxLock ) < uxNumberOfTasks )                   \\r
285         {                                                                     \\r
286             configASSERT( ( cRxLock ) != queueINT8_MAX );                     \\r
287             ( pxQueue )->cRxLock = ( int8_t ) ( ( cRxLock ) + ( int8_t ) 1 ); \\r
288         }                                                                     \\r
289     }\r
290 /*-----------------------------------------------------------*/\r
291 \r
292 BaseType_t xQueueGenericReset( QueueHandle_t xQueue,\r
293                                BaseType_t xNewQueue )\r
294 {\r
295     BaseType_t xReturn = pdPASS;\r
296     Queue_t * const pxQueue = xQueue;\r
297 \r
298     configASSERT( pxQueue );\r
299 \r
300     if( ( pxQueue != NULL ) &&\r
301         ( pxQueue->uxLength >= 1U ) &&\r
302         /* Check for multiplication overflow. */\r
303         ( ( SIZE_MAX / pxQueue->uxLength ) >= pxQueue->uxItemSize ) )\r
304     {\r
305         taskENTER_CRITICAL();\r
306         {\r
307             pxQueue->u.xQueue.pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */\r
308             pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;\r
309             pxQueue->pcWriteTo = pxQueue->pcHead;\r
310             pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - 1U ) * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */\r
311             pxQueue->cRxLock = queueUNLOCKED;\r
312             pxQueue->cTxLock = queueUNLOCKED;\r
313 \r
314             if( xNewQueue == pdFALSE )\r
315             {\r
316                 /* If there are tasks blocked waiting to read from the queue, then\r
317                  * the tasks will remain blocked as after this function exits the queue\r
318                  * will still be empty.  If there are tasks blocked waiting to write to\r
319                  * the queue, then one should be unblocked as after this function exits\r
320                  * it will be possible to write to it. */\r
321                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
322                 {\r
323                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
324                     {\r
325                         queueYIELD_IF_USING_PREEMPTION();\r
326                     }\r
327                     else\r
328                     {\r
329                         mtCOVERAGE_TEST_MARKER();\r
330                     }\r
331                 }\r
332                 else\r
333                 {\r
334                     mtCOVERAGE_TEST_MARKER();\r
335                 }\r
336             }\r
337             else\r
338             {\r
339                 /* Ensure the event queues start in the correct state. */\r
340                 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );\r
341                 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );\r
342             }\r
343         }\r
344         taskEXIT_CRITICAL();\r
345     }\r
346     else\r
347     {\r
348         xReturn = pdFAIL;\r
349     }\r
350 \r
351     configASSERT( xReturn != pdFAIL );\r
352 \r
353     /* A value is returned for calling semantic consistency with previous\r
354      * versions. */\r
355     return xReturn;\r
356 }\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )\r
360 \r
361     QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength,\r
362                                              const UBaseType_t uxItemSize,\r
363                                              uint8_t * pucQueueStorage,\r
364                                              StaticQueue_t * pxStaticQueue,\r
365                                              const uint8_t ucQueueType )\r
366     {\r
367         Queue_t * pxNewQueue = NULL;\r
368 \r
369         /* The StaticQueue_t structure and the queue storage area must be\r
370          * supplied. */\r
371         configASSERT( pxStaticQueue );\r
372 \r
373         if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&\r
374             ( pxStaticQueue != NULL ) &&\r
375 \r
376             /* A queue storage area should be provided if the item size is not 0, and\r
377              * should not be provided if the item size is 0. */\r
378             ( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) ) &&\r
379             ( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) ) )\r
380         {\r
381             #if ( configASSERT_DEFINED == 1 )\r
382             {\r
383                 /* Sanity check that the size of the structure used to declare a\r
384                  * variable of type StaticQueue_t or StaticSemaphore_t equals the size of\r
385                  * the real queue and semaphore structures. */\r
386                 volatile size_t xSize = sizeof( StaticQueue_t );\r
387 \r
388                 /* This assertion cannot be branch covered in unit tests */\r
389                 configASSERT( xSize == sizeof( Queue_t ) ); /* LCOV_EXCL_BR_LINE */\r
390                 ( void ) xSize;                             /* Keeps lint quiet when configASSERT() is not defined. */\r
391             }\r
392             #endif /* configASSERT_DEFINED */\r
393 \r
394             /* The address of a statically allocated queue was passed in, use it.\r
395              * The address of a statically allocated storage area was also passed in\r
396              * but is already set. */\r
397             pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */\r
398 \r
399             #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
400             {\r
401                 /* Queues can be allocated wither statically or dynamically, so\r
402                  * note this queue was allocated statically in case the queue is\r
403                  * later deleted. */\r
404                 pxNewQueue->ucStaticallyAllocated = pdTRUE;\r
405             }\r
406             #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
407 \r
408             prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );\r
409         }\r
410         else\r
411         {\r
412             configASSERT( pxNewQueue );\r
413             mtCOVERAGE_TEST_MARKER();\r
414         }\r
415 \r
416         return pxNewQueue;\r
417     }\r
418 \r
419 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
420 /*-----------------------------------------------------------*/\r
421 \r
422 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
423 \r
424     QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength,\r
425                                        const UBaseType_t uxItemSize,\r
426                                        const uint8_t ucQueueType )\r
427     {\r
428         Queue_t * pxNewQueue = NULL;\r
429         size_t xQueueSizeInBytes;\r
430         uint8_t * pucQueueStorage;\r
431 \r
432         if( ( uxQueueLength > ( UBaseType_t ) 0 ) &&\r
433             /* Check for multiplication overflow. */\r
434             ( ( SIZE_MAX / uxQueueLength ) >= uxItemSize ) &&\r
435             /* Check for addition overflow. */\r
436             ( ( SIZE_MAX - sizeof( Queue_t ) ) >= ( uxQueueLength * uxItemSize ) ) )\r
437         {\r
438             /* Allocate enough space to hold the maximum number of items that\r
439              * can be in the queue at any time.  It is valid for uxItemSize to be\r
440              * zero in the case the queue is used as a semaphore. */\r
441             xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
442 \r
443             /* Allocate the queue and storage area.  Justification for MISRA\r
444              * deviation as follows:  pvPortMalloc() always ensures returned memory\r
445              * blocks are aligned per the requirements of the MCU stack.  In this case\r
446              * pvPortMalloc() must return a pointer that is guaranteed to meet the\r
447              * alignment requirements of the Queue_t structure - which in this case\r
448              * is an int8_t *.  Therefore, whenever the stack alignment requirements\r
449              * are greater than or equal to the pointer to char requirements the cast\r
450              * is safe.  In other cases alignment requirements are not strict (one or\r
451              * two bytes). */\r
452             pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */\r
453 \r
454             if( pxNewQueue != NULL )\r
455             {\r
456                 /* Jump past the queue structure to find the location of the queue\r
457                  * storage area. */\r
458                 pucQueueStorage = ( uint8_t * ) pxNewQueue;\r
459                 pucQueueStorage += sizeof( Queue_t ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */\r
460 \r
461                 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )\r
462                 {\r
463                     /* Queues can be created either statically or dynamically, so\r
464                      * note this task was created dynamically in case it is later\r
465                      * deleted. */\r
466                     pxNewQueue->ucStaticallyAllocated = pdFALSE;\r
467                 }\r
468                 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
469 \r
470                 prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );\r
471             }\r
472             else\r
473             {\r
474                 traceQUEUE_CREATE_FAILED( ucQueueType );\r
475                 mtCOVERAGE_TEST_MARKER();\r
476             }\r
477         }\r
478         else\r
479         {\r
480             configASSERT( pxNewQueue );\r
481             mtCOVERAGE_TEST_MARKER();\r
482         }\r
483 \r
484         return pxNewQueue;\r
485     }\r
486 \r
487 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
488 /*-----------------------------------------------------------*/\r
489 \r
490 static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength,\r
491                                    const UBaseType_t uxItemSize,\r
492                                    uint8_t * pucQueueStorage,\r
493                                    const uint8_t ucQueueType,\r
494                                    Queue_t * pxNewQueue )\r
495 {\r
496     /* Remove compiler warnings about unused parameters should\r
497      * configUSE_TRACE_FACILITY not be set to 1. */\r
498     ( void ) ucQueueType;\r
499 \r
500     if( uxItemSize == ( UBaseType_t ) 0 )\r
501     {\r
502         /* No RAM was allocated for the queue storage area, but PC head cannot\r
503          * be set to NULL because NULL is used as a key to say the queue is used as\r
504          * a mutex.  Therefore just set pcHead to point to the queue as a benign\r
505          * value that is known to be within the memory map. */\r
506         pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;\r
507     }\r
508     else\r
509     {\r
510         /* Set the head to the start of the queue storage area. */\r
511         pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;\r
512     }\r
513 \r
514     /* Initialise the queue members as described where the queue type is\r
515      * defined. */\r
516     pxNewQueue->uxLength = uxQueueLength;\r
517     pxNewQueue->uxItemSize = uxItemSize;\r
518     ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );\r
519 \r
520     #if ( configUSE_TRACE_FACILITY == 1 )\r
521     {\r
522         pxNewQueue->ucQueueType = ucQueueType;\r
523     }\r
524     #endif /* configUSE_TRACE_FACILITY */\r
525 \r
526     #if ( configUSE_QUEUE_SETS == 1 )\r
527     {\r
528         pxNewQueue->pxQueueSetContainer = NULL;\r
529     }\r
530     #endif /* configUSE_QUEUE_SETS */\r
531 \r
532     traceQUEUE_CREATE( pxNewQueue );\r
533 }\r
534 /*-----------------------------------------------------------*/\r
535 \r
536 #if ( configUSE_MUTEXES == 1 )\r
537 \r
538     static void prvInitialiseMutex( Queue_t * pxNewQueue )\r
539     {\r
540         if( pxNewQueue != NULL )\r
541         {\r
542             /* The queue create function will set all the queue structure members\r
543             * correctly for a generic queue, but this function is creating a\r
544             * mutex.  Overwrite those members that need to be set differently -\r
545             * in particular the information required for priority inheritance. */\r
546             pxNewQueue->u.xSemaphore.xMutexHolder = NULL;\r
547             pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;\r
548 \r
549             /* In case this is a recursive mutex. */\r
550             pxNewQueue->u.xSemaphore.uxRecursiveCallCount = 0;\r
551 \r
552             traceCREATE_MUTEX( pxNewQueue );\r
553 \r
554             /* Start with the semaphore in the expected state. */\r
555             ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );\r
556         }\r
557         else\r
558         {\r
559             traceCREATE_MUTEX_FAILED();\r
560         }\r
561     }\r
562 \r
563 #endif /* configUSE_MUTEXES */\r
564 /*-----------------------------------------------------------*/\r
565 \r
566 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
567 \r
568     QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )\r
569     {\r
570         QueueHandle_t xNewQueue;\r
571         const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;\r
572 \r
573         xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );\r
574         prvInitialiseMutex( ( Queue_t * ) xNewQueue );\r
575 \r
576         return xNewQueue;\r
577     }\r
578 \r
579 #endif /* configUSE_MUTEXES */\r
580 /*-----------------------------------------------------------*/\r
581 \r
582 #if ( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
583 \r
584     QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType,\r
585                                            StaticQueue_t * pxStaticQueue )\r
586     {\r
587         QueueHandle_t xNewQueue;\r
588         const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;\r
589 \r
590         /* Prevent compiler warnings about unused parameters if\r
591          * configUSE_TRACE_FACILITY does not equal 1. */\r
592         ( void ) ucQueueType;\r
593 \r
594         xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );\r
595         prvInitialiseMutex( ( Queue_t * ) xNewQueue );\r
596 \r
597         return xNewQueue;\r
598     }\r
599 \r
600 #endif /* configUSE_MUTEXES */\r
601 /*-----------------------------------------------------------*/\r
602 \r
603 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )\r
604 \r
605     TaskHandle_t xQueueGetMutexHolder( QueueHandle_t xSemaphore )\r
606     {\r
607         TaskHandle_t pxReturn;\r
608         Queue_t * const pxSemaphore = ( Queue_t * ) xSemaphore;\r
609 \r
610         configASSERT( xSemaphore );\r
611 \r
612         /* This function is called by xSemaphoreGetMutexHolder(), and should not\r
613          * be called directly.  Note:  This is a good way of determining if the\r
614          * calling task is the mutex holder, but not a good way of determining the\r
615          * identity of the mutex holder, as the holder may change between the\r
616          * following critical section exiting and the function returning. */\r
617         taskENTER_CRITICAL();\r
618         {\r
619             if( pxSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )\r
620             {\r
621                 pxReturn = pxSemaphore->u.xSemaphore.xMutexHolder;\r
622             }\r
623             else\r
624             {\r
625                 pxReturn = NULL;\r
626             }\r
627         }\r
628         taskEXIT_CRITICAL();\r
629 \r
630         return pxReturn;\r
631     } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */\r
632 \r
633 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */\r
634 /*-----------------------------------------------------------*/\r
635 \r
636 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )\r
637 \r
638     TaskHandle_t xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )\r
639     {\r
640         TaskHandle_t pxReturn;\r
641 \r
642         configASSERT( xSemaphore );\r
643 \r
644         /* Mutexes cannot be used in interrupt service routines, so the mutex\r
645          * holder should not change in an ISR, and therefore a critical section is\r
646          * not required here. */\r
647         if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )\r
648         {\r
649             pxReturn = ( ( Queue_t * ) xSemaphore )->u.xSemaphore.xMutexHolder;\r
650         }\r
651         else\r
652         {\r
653             pxReturn = NULL;\r
654         }\r
655 \r
656         return pxReturn;\r
657     } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */\r
658 \r
659 #endif /* if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) ) */\r
660 /*-----------------------------------------------------------*/\r
661 \r
662 #if ( configUSE_RECURSIVE_MUTEXES == 1 )\r
663 \r
664     BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )\r
665     {\r
666         BaseType_t xReturn;\r
667         Queue_t * const pxMutex = ( Queue_t * ) xMutex;\r
668 \r
669         configASSERT( pxMutex );\r
670 \r
671         /* If this is the task that holds the mutex then xMutexHolder will not\r
672          * change outside of this task.  If this task does not hold the mutex then\r
673          * pxMutexHolder can never coincidentally equal the tasks handle, and as\r
674          * this is the only condition we are interested in it does not matter if\r
675          * pxMutexHolder is accessed simultaneously by another task.  Therefore no\r
676          * mutual exclusion is required to test the pxMutexHolder variable. */\r
677         if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )\r
678         {\r
679             traceGIVE_MUTEX_RECURSIVE( pxMutex );\r
680 \r
681             /* uxRecursiveCallCount cannot be zero if xMutexHolder is equal to\r
682              * the task handle, therefore no underflow check is required.  Also,\r
683              * uxRecursiveCallCount is only modified by the mutex holder, and as\r
684              * there can only be one, no mutual exclusion is required to modify the\r
685              * uxRecursiveCallCount member. */\r
686             ( pxMutex->u.xSemaphore.uxRecursiveCallCount )--;\r
687 \r
688             /* Has the recursive call count unwound to 0? */\r
689             if( pxMutex->u.xSemaphore.uxRecursiveCallCount == ( UBaseType_t ) 0 )\r
690             {\r
691                 /* Return the mutex.  This will automatically unblock any other\r
692                  * task that might be waiting to access the mutex. */\r
693                 ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );\r
694             }\r
695             else\r
696             {\r
697                 mtCOVERAGE_TEST_MARKER();\r
698             }\r
699 \r
700             xReturn = pdPASS;\r
701         }\r
702         else\r
703         {\r
704             /* The mutex cannot be given because the calling task is not the\r
705              * holder. */\r
706             xReturn = pdFAIL;\r
707 \r
708             traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );\r
709         }\r
710 \r
711         return xReturn;\r
712     }\r
713 \r
714 #endif /* configUSE_RECURSIVE_MUTEXES */\r
715 /*-----------------------------------------------------------*/\r
716 \r
717 #if ( configUSE_RECURSIVE_MUTEXES == 1 )\r
718 \r
719     BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex,\r
720                                          TickType_t xTicksToWait )\r
721     {\r
722         BaseType_t xReturn;\r
723         Queue_t * const pxMutex = ( Queue_t * ) xMutex;\r
724 \r
725         configASSERT( pxMutex );\r
726 \r
727         /* Comments regarding mutual exclusion as per those within\r
728          * xQueueGiveMutexRecursive(). */\r
729 \r
730         traceTAKE_MUTEX_RECURSIVE( pxMutex );\r
731 \r
732         if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )\r
733         {\r
734             ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;\r
735             xReturn = pdPASS;\r
736         }\r
737         else\r
738         {\r
739             xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );\r
740 \r
741             /* pdPASS will only be returned if the mutex was successfully\r
742              * obtained.  The calling task may have entered the Blocked state\r
743              * before reaching here. */\r
744             if( xReturn != pdFAIL )\r
745             {\r
746                 ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;\r
747             }\r
748             else\r
749             {\r
750                 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );\r
751             }\r
752         }\r
753 \r
754         return xReturn;\r
755     }\r
756 \r
757 #endif /* configUSE_RECURSIVE_MUTEXES */\r
758 /*-----------------------------------------------------------*/\r
759 \r
760 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
761 \r
762     QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount,\r
763                                                        const UBaseType_t uxInitialCount,\r
764                                                        StaticQueue_t * pxStaticQueue )\r
765     {\r
766         QueueHandle_t xHandle = NULL;\r
767 \r
768         if( ( uxMaxCount != 0 ) &&\r
769             ( uxInitialCount <= uxMaxCount ) )\r
770         {\r
771             xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );\r
772 \r
773             if( xHandle != NULL )\r
774             {\r
775                 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;\r
776 \r
777                 traceCREATE_COUNTING_SEMAPHORE();\r
778             }\r
779             else\r
780             {\r
781                 traceCREATE_COUNTING_SEMAPHORE_FAILED();\r
782             }\r
783         }\r
784         else\r
785         {\r
786             configASSERT( xHandle );\r
787             mtCOVERAGE_TEST_MARKER();\r
788         }\r
789 \r
790         return xHandle;\r
791     }\r
792 \r
793 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */\r
794 /*-----------------------------------------------------------*/\r
795 \r
796 #if ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
797 \r
798     QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,\r
799                                                  const UBaseType_t uxInitialCount )\r
800     {\r
801         QueueHandle_t xHandle = NULL;\r
802 \r
803         if( ( uxMaxCount != 0 ) &&\r
804             ( uxInitialCount <= uxMaxCount ) )\r
805         {\r
806             xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );\r
807 \r
808             if( xHandle != NULL )\r
809             {\r
810                 ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;\r
811 \r
812                 traceCREATE_COUNTING_SEMAPHORE();\r
813             }\r
814             else\r
815             {\r
816                 traceCREATE_COUNTING_SEMAPHORE_FAILED();\r
817             }\r
818         }\r
819         else\r
820         {\r
821             configASSERT( xHandle );\r
822             mtCOVERAGE_TEST_MARKER();\r
823         }\r
824 \r
825         return xHandle;\r
826     }\r
827 \r
828 #endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */\r
829 /*-----------------------------------------------------------*/\r
830 \r
831 BaseType_t xQueueGenericSend( QueueHandle_t xQueue,\r
832                               const void * const pvItemToQueue,\r
833                               TickType_t xTicksToWait,\r
834                               const BaseType_t xCopyPosition )\r
835 {\r
836     BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;\r
837     TimeOut_t xTimeOut;\r
838     Queue_t * const pxQueue = xQueue;\r
839 \r
840     configASSERT( pxQueue );\r
841     configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
842     configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );\r
843     #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
844     {\r
845         configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
846     }\r
847     #endif\r
848 \r
849     /*lint -save -e904 This function relaxes the coding standard somewhat to\r
850      * allow return statements within the function itself.  This is done in the\r
851      * interest of execution time efficiency. */\r
852     for( ; ; )\r
853     {\r
854         taskENTER_CRITICAL();\r
855         {\r
856             /* Is there room on the queue now?  The running task must be the\r
857              * highest priority task wanting to access the queue.  If the head item\r
858              * in the queue is to be overwritten then it does not matter if the\r
859              * queue is full. */\r
860             if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )\r
861             {\r
862                 traceQUEUE_SEND( pxQueue );\r
863 \r
864                 #if ( configUSE_QUEUE_SETS == 1 )\r
865                 {\r
866                     const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;\r
867 \r
868                     xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
869 \r
870                     if( pxQueue->pxQueueSetContainer != NULL )\r
871                     {\r
872                         if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )\r
873                         {\r
874                             /* Do not notify the queue set as an existing item\r
875                              * was overwritten in the queue so the number of items\r
876                              * in the queue has not changed. */\r
877                             mtCOVERAGE_TEST_MARKER();\r
878                         }\r
879                         else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )\r
880                         {\r
881                             /* The queue is a member of a queue set, and posting\r
882                              * to the queue set caused a higher priority task to\r
883                              * unblock. A context switch is required. */\r
884                             queueYIELD_IF_USING_PREEMPTION();\r
885                         }\r
886                         else\r
887                         {\r
888                             mtCOVERAGE_TEST_MARKER();\r
889                         }\r
890                     }\r
891                     else\r
892                     {\r
893                         /* If there was a task waiting for data to arrive on the\r
894                          * queue then unblock it now. */\r
895                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
896                         {\r
897                             if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
898                             {\r
899                                 /* The unblocked task has a priority higher than\r
900                                  * our own so yield immediately.  Yes it is ok to\r
901                                  * do this from within the critical section - the\r
902                                  * kernel takes care of that. */\r
903                                 queueYIELD_IF_USING_PREEMPTION();\r
904                             }\r
905                             else\r
906                             {\r
907                                 mtCOVERAGE_TEST_MARKER();\r
908                             }\r
909                         }\r
910                         else if( xYieldRequired != pdFALSE )\r
911                         {\r
912                             /* This path is a special case that will only get\r
913                              * executed if the task was holding multiple mutexes\r
914                              * and the mutexes were given back in an order that is\r
915                              * different to that in which they were taken. */\r
916                             queueYIELD_IF_USING_PREEMPTION();\r
917                         }\r
918                         else\r
919                         {\r
920                             mtCOVERAGE_TEST_MARKER();\r
921                         }\r
922                     }\r
923                 }\r
924                 #else /* configUSE_QUEUE_SETS */\r
925                 {\r
926                     xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
927 \r
928                     /* If there was a task waiting for data to arrive on the\r
929                      * queue then unblock it now. */\r
930                     if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
931                     {\r
932                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
933                         {\r
934                             /* The unblocked task has a priority higher than\r
935                              * our own so yield immediately.  Yes it is ok to do\r
936                              * this from within the critical section - the kernel\r
937                              * takes care of that. */\r
938                             queueYIELD_IF_USING_PREEMPTION();\r
939                         }\r
940                         else\r
941                         {\r
942                             mtCOVERAGE_TEST_MARKER();\r
943                         }\r
944                     }\r
945                     else if( xYieldRequired != pdFALSE )\r
946                     {\r
947                         /* This path is a special case that will only get\r
948                          * executed if the task was holding multiple mutexes and\r
949                          * the mutexes were given back in an order that is\r
950                          * different to that in which they were taken. */\r
951                         queueYIELD_IF_USING_PREEMPTION();\r
952                     }\r
953                     else\r
954                     {\r
955                         mtCOVERAGE_TEST_MARKER();\r
956                     }\r
957                 }\r
958                 #endif /* configUSE_QUEUE_SETS */\r
959 \r
960                 taskEXIT_CRITICAL();\r
961                 return pdPASS;\r
962             }\r
963             else\r
964             {\r
965                 if( xTicksToWait == ( TickType_t ) 0 )\r
966                 {\r
967                     /* The queue was full and no block time is specified (or\r
968                      * the block time has expired) so leave now. */\r
969                     taskEXIT_CRITICAL();\r
970 \r
971                     /* Return to the original privilege level before exiting\r
972                      * the function. */\r
973                     traceQUEUE_SEND_FAILED( pxQueue );\r
974                     return errQUEUE_FULL;\r
975                 }\r
976                 else if( xEntryTimeSet == pdFALSE )\r
977                 {\r
978                     /* The queue was full and a block time was specified so\r
979                      * configure the timeout structure. */\r
980                     vTaskInternalSetTimeOutState( &xTimeOut );\r
981                     xEntryTimeSet = pdTRUE;\r
982                 }\r
983                 else\r
984                 {\r
985                     /* Entry time was already set. */\r
986                     mtCOVERAGE_TEST_MARKER();\r
987                 }\r
988             }\r
989         }\r
990         taskEXIT_CRITICAL();\r
991 \r
992         /* Interrupts and other tasks can send to and receive from the queue\r
993          * now the critical section has been exited. */\r
994 \r
995         vTaskSuspendAll();\r
996         prvLockQueue( pxQueue );\r
997 \r
998         /* Update the timeout state to see if it has expired yet. */\r
999         if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
1000         {\r
1001             if( prvIsQueueFull( pxQueue ) != pdFALSE )\r
1002             {\r
1003                 traceBLOCKING_ON_QUEUE_SEND( pxQueue );\r
1004                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
1005 \r
1006                 /* Unlocking the queue means queue events can effect the\r
1007                  * event list. It is possible that interrupts occurring now\r
1008                  * remove this task from the event list again - but as the\r
1009                  * scheduler is suspended the task will go onto the pending\r
1010                  * ready list instead of the actual ready list. */\r
1011                 prvUnlockQueue( pxQueue );\r
1012 \r
1013                 /* Resuming the scheduler will move tasks from the pending\r
1014                  * ready list into the ready list - so it is feasible that this\r
1015                  * task is already in the ready list before it yields - in which\r
1016                  * case the yield will not cause a context switch unless there\r
1017                  * is also a higher priority task in the pending ready list. */\r
1018                 if( xTaskResumeAll() == pdFALSE )\r
1019                 {\r
1020                     portYIELD_WITHIN_API();\r
1021                 }\r
1022             }\r
1023             else\r
1024             {\r
1025                 /* Try again. */\r
1026                 prvUnlockQueue( pxQueue );\r
1027                 ( void ) xTaskResumeAll();\r
1028             }\r
1029         }\r
1030         else\r
1031         {\r
1032             /* The timeout has expired. */\r
1033             prvUnlockQueue( pxQueue );\r
1034             ( void ) xTaskResumeAll();\r
1035 \r
1036             traceQUEUE_SEND_FAILED( pxQueue );\r
1037             return errQUEUE_FULL;\r
1038         }\r
1039     } /*lint -restore */\r
1040 }\r
1041 /*-----------------------------------------------------------*/\r
1042 \r
1043 BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue,\r
1044                                      const void * const pvItemToQueue,\r
1045                                      BaseType_t * const pxHigherPriorityTaskWoken,\r
1046                                      const BaseType_t xCopyPosition )\r
1047 {\r
1048     BaseType_t xReturn;\r
1049     UBaseType_t uxSavedInterruptStatus;\r
1050     Queue_t * const pxQueue = xQueue;\r
1051 \r
1052     configASSERT( pxQueue );\r
1053     configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
1054     configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );\r
1055 \r
1056     /* RTOS ports that support interrupt nesting have the concept of a maximum\r
1057      * system call (or maximum API call) interrupt priority.  Interrupts that are\r
1058      * above the maximum system call priority are kept permanently enabled, even\r
1059      * when the RTOS kernel is in a critical section, but cannot make any calls to\r
1060      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h\r
1061      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
1062      * failure if a FreeRTOS API function is called from an interrupt that has been\r
1063      * assigned a priority above the configured maximum system call priority.\r
1064      * Only FreeRTOS functions that end in FromISR can be called from interrupts\r
1065      * that have been assigned a priority at or (logically) below the maximum\r
1066      * system call interrupt priority.  FreeRTOS maintains a separate interrupt\r
1067      * safe API to ensure interrupt entry is as fast and as simple as possible.\r
1068      * More information (albeit Cortex-M specific) is provided on the following\r
1069      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
1070     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();\r
1071 \r
1072     /* Similar to xQueueGenericSend, except without blocking if there is no room\r
1073      * in the queue.  Also don't directly wake a task that was blocked on a queue\r
1074      * read, instead return a flag to say whether a context switch is required or\r
1075      * not (i.e. has a task with a higher priority than us been woken by this\r
1076      * post). */\r
1077     uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
1078     {\r
1079         if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )\r
1080         {\r
1081             const int8_t cTxLock = pxQueue->cTxLock;\r
1082             const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;\r
1083 \r
1084             traceQUEUE_SEND_FROM_ISR( pxQueue );\r
1085 \r
1086             /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a\r
1087              *  semaphore or mutex.  That means prvCopyDataToQueue() cannot result\r
1088              *  in a task disinheriting a priority and prvCopyDataToQueue() can be\r
1089              *  called here even though the disinherit function does not check if\r
1090              *  the scheduler is suspended before accessing the ready lists. */\r
1091             ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
1092 \r
1093             /* The event list is not altered if the queue is locked.  This will\r
1094              * be done when the queue is unlocked later. */\r
1095             if( cTxLock == queueUNLOCKED )\r
1096             {\r
1097                 #if ( configUSE_QUEUE_SETS == 1 )\r
1098                 {\r
1099                     if( pxQueue->pxQueueSetContainer != NULL )\r
1100                     {\r
1101                         if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )\r
1102                         {\r
1103                             /* Do not notify the queue set as an existing item\r
1104                              * was overwritten in the queue so the number of items\r
1105                              * in the queue has not changed. */\r
1106                             mtCOVERAGE_TEST_MARKER();\r
1107                         }\r
1108                         else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )\r
1109                         {\r
1110                             /* The queue is a member of a queue set, and posting\r
1111                              * to the queue set caused a higher priority task to\r
1112                              * unblock.  A context switch is required. */\r
1113                             if( pxHigherPriorityTaskWoken != NULL )\r
1114                             {\r
1115                                 *pxHigherPriorityTaskWoken = pdTRUE;\r
1116                             }\r
1117                             else\r
1118                             {\r
1119                                 mtCOVERAGE_TEST_MARKER();\r
1120                             }\r
1121                         }\r
1122                         else\r
1123                         {\r
1124                             mtCOVERAGE_TEST_MARKER();\r
1125                         }\r
1126                     }\r
1127                     else\r
1128                     {\r
1129                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1130                         {\r
1131                             if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1132                             {\r
1133                                 /* The task waiting has a higher priority so\r
1134                                  *  record that a context switch is required. */\r
1135                                 if( pxHigherPriorityTaskWoken != NULL )\r
1136                                 {\r
1137                                     *pxHigherPriorityTaskWoken = pdTRUE;\r
1138                                 }\r
1139                                 else\r
1140                                 {\r
1141                                     mtCOVERAGE_TEST_MARKER();\r
1142                                 }\r
1143                             }\r
1144                             else\r
1145                             {\r
1146                                 mtCOVERAGE_TEST_MARKER();\r
1147                             }\r
1148                         }\r
1149                         else\r
1150                         {\r
1151                             mtCOVERAGE_TEST_MARKER();\r
1152                         }\r
1153                     }\r
1154                 }\r
1155                 #else /* configUSE_QUEUE_SETS */\r
1156                 {\r
1157                     if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1158                     {\r
1159                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1160                         {\r
1161                             /* The task waiting has a higher priority so record that a\r
1162                              * context switch is required. */\r
1163                             if( pxHigherPriorityTaskWoken != NULL )\r
1164                             {\r
1165                                 *pxHigherPriorityTaskWoken = pdTRUE;\r
1166                             }\r
1167                             else\r
1168                             {\r
1169                                 mtCOVERAGE_TEST_MARKER();\r
1170                             }\r
1171                         }\r
1172                         else\r
1173                         {\r
1174                             mtCOVERAGE_TEST_MARKER();\r
1175                         }\r
1176                     }\r
1177                     else\r
1178                     {\r
1179                         mtCOVERAGE_TEST_MARKER();\r
1180                     }\r
1181 \r
1182                     /* Not used in this path. */\r
1183                     ( void ) uxPreviousMessagesWaiting;\r
1184                 }\r
1185                 #endif /* configUSE_QUEUE_SETS */\r
1186             }\r
1187             else\r
1188             {\r
1189                 /* Increment the lock count so the task that unlocks the queue\r
1190                  * knows that data was posted while it was locked. */\r
1191                 prvIncrementQueueTxLock( pxQueue, cTxLock );\r
1192             }\r
1193 \r
1194             xReturn = pdPASS;\r
1195         }\r
1196         else\r
1197         {\r
1198             traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );\r
1199             xReturn = errQUEUE_FULL;\r
1200         }\r
1201     }\r
1202     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
1203 \r
1204     return xReturn;\r
1205 }\r
1206 /*-----------------------------------------------------------*/\r
1207 \r
1208 BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue,\r
1209                               BaseType_t * const pxHigherPriorityTaskWoken )\r
1210 {\r
1211     BaseType_t xReturn;\r
1212     UBaseType_t uxSavedInterruptStatus;\r
1213     Queue_t * const pxQueue = xQueue;\r
1214 \r
1215     /* Similar to xQueueGenericSendFromISR() but used with semaphores where the\r
1216      * item size is 0.  Don't directly wake a task that was blocked on a queue\r
1217      * read, instead return a flag to say whether a context switch is required or\r
1218      * not (i.e. has a task with a higher priority than us been woken by this\r
1219      * post). */\r
1220 \r
1221     configASSERT( pxQueue );\r
1222 \r
1223     /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()\r
1224      * if the item size is not 0. */\r
1225     configASSERT( pxQueue->uxItemSize == 0 );\r
1226 \r
1227     /* Normally a mutex would not be given from an interrupt, especially if\r
1228      * there is a mutex holder, as priority inheritance makes no sense for an\r
1229      * interrupts, only tasks. */\r
1230     configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->u.xSemaphore.xMutexHolder != NULL ) ) );\r
1231 \r
1232     /* RTOS ports that support interrupt nesting have the concept of a maximum\r
1233      * system call (or maximum API call) interrupt priority.  Interrupts that are\r
1234      * above the maximum system call priority are kept permanently enabled, even\r
1235      * when the RTOS kernel is in a critical section, but cannot make any calls to\r
1236      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h\r
1237      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
1238      * failure if a FreeRTOS API function is called from an interrupt that has been\r
1239      * assigned a priority above the configured maximum system call priority.\r
1240      * Only FreeRTOS functions that end in FromISR can be called from interrupts\r
1241      * that have been assigned a priority at or (logically) below the maximum\r
1242      * system call interrupt priority.  FreeRTOS maintains a separate interrupt\r
1243      * safe API to ensure interrupt entry is as fast and as simple as possible.\r
1244      * More information (albeit Cortex-M specific) is provided on the following\r
1245      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
1246     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();\r
1247 \r
1248     uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
1249     {\r
1250         const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
1251 \r
1252         /* When the queue is used to implement a semaphore no data is ever\r
1253          * moved through the queue but it is still valid to see if the queue 'has\r
1254          * space'. */\r
1255         if( uxMessagesWaiting < pxQueue->uxLength )\r
1256         {\r
1257             const int8_t cTxLock = pxQueue->cTxLock;\r
1258 \r
1259             traceQUEUE_SEND_FROM_ISR( pxQueue );\r
1260 \r
1261             /* A task can only have an inherited priority if it is a mutex\r
1262              * holder - and if there is a mutex holder then the mutex cannot be\r
1263              * given from an ISR.  As this is the ISR version of the function it\r
1264              * can be assumed there is no mutex holder and no need to determine if\r
1265              * priority disinheritance is needed.  Simply increase the count of\r
1266              * messages (semaphores) available. */\r
1267             pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;\r
1268 \r
1269             /* The event list is not altered if the queue is locked.  This will\r
1270              * be done when the queue is unlocked later. */\r
1271             if( cTxLock == queueUNLOCKED )\r
1272             {\r
1273                 #if ( configUSE_QUEUE_SETS == 1 )\r
1274                 {\r
1275                     if( pxQueue->pxQueueSetContainer != NULL )\r
1276                     {\r
1277                         if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )\r
1278                         {\r
1279                             /* The semaphore is a member of a queue set, and\r
1280                              * posting to the queue set caused a higher priority\r
1281                              * task to unblock.  A context switch is required. */\r
1282                             if( pxHigherPriorityTaskWoken != NULL )\r
1283                             {\r
1284                                 *pxHigherPriorityTaskWoken = pdTRUE;\r
1285                             }\r
1286                             else\r
1287                             {\r
1288                                 mtCOVERAGE_TEST_MARKER();\r
1289                             }\r
1290                         }\r
1291                         else\r
1292                         {\r
1293                             mtCOVERAGE_TEST_MARKER();\r
1294                         }\r
1295                     }\r
1296                     else\r
1297                     {\r
1298                         if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1299                         {\r
1300                             if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1301                             {\r
1302                                 /* The task waiting has a higher priority so\r
1303                                  *  record that a context switch is required. */\r
1304                                 if( pxHigherPriorityTaskWoken != NULL )\r
1305                                 {\r
1306                                     *pxHigherPriorityTaskWoken = pdTRUE;\r
1307                                 }\r
1308                                 else\r
1309                                 {\r
1310                                     mtCOVERAGE_TEST_MARKER();\r
1311                                 }\r
1312                             }\r
1313                             else\r
1314                             {\r
1315                                 mtCOVERAGE_TEST_MARKER();\r
1316                             }\r
1317                         }\r
1318                         else\r
1319                         {\r
1320                             mtCOVERAGE_TEST_MARKER();\r
1321                         }\r
1322                     }\r
1323                 }\r
1324                 #else /* configUSE_QUEUE_SETS */\r
1325                 {\r
1326                     if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1327                     {\r
1328                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1329                         {\r
1330                             /* The task waiting has a higher priority so record that a\r
1331                              * context switch is required. */\r
1332                             if( pxHigherPriorityTaskWoken != NULL )\r
1333                             {\r
1334                                 *pxHigherPriorityTaskWoken = pdTRUE;\r
1335                             }\r
1336                             else\r
1337                             {\r
1338                                 mtCOVERAGE_TEST_MARKER();\r
1339                             }\r
1340                         }\r
1341                         else\r
1342                         {\r
1343                             mtCOVERAGE_TEST_MARKER();\r
1344                         }\r
1345                     }\r
1346                     else\r
1347                     {\r
1348                         mtCOVERAGE_TEST_MARKER();\r
1349                     }\r
1350                 }\r
1351                 #endif /* configUSE_QUEUE_SETS */\r
1352             }\r
1353             else\r
1354             {\r
1355                 /* Increment the lock count so the task that unlocks the queue\r
1356                  * knows that data was posted while it was locked. */\r
1357                 prvIncrementQueueTxLock( pxQueue, cTxLock );\r
1358             }\r
1359 \r
1360             xReturn = pdPASS;\r
1361         }\r
1362         else\r
1363         {\r
1364             traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );\r
1365             xReturn = errQUEUE_FULL;\r
1366         }\r
1367     }\r
1368     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
1369 \r
1370     return xReturn;\r
1371 }\r
1372 /*-----------------------------------------------------------*/\r
1373 \r
1374 BaseType_t xQueueReceive( QueueHandle_t xQueue,\r
1375                           void * const pvBuffer,\r
1376                           TickType_t xTicksToWait )\r
1377 {\r
1378     BaseType_t xEntryTimeSet = pdFALSE;\r
1379     TimeOut_t xTimeOut;\r
1380     Queue_t * const pxQueue = xQueue;\r
1381 \r
1382     /* Check the pointer is not NULL. */\r
1383     configASSERT( ( pxQueue ) );\r
1384 \r
1385     /* The buffer into which data is received can only be NULL if the data size\r
1386      * is zero (so no data is copied into the buffer). */\r
1387     configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
1388 \r
1389     /* Cannot block if the scheduler is suspended. */\r
1390     #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
1391     {\r
1392         configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
1393     }\r
1394     #endif\r
1395 \r
1396     /*lint -save -e904  This function relaxes the coding standard somewhat to\r
1397      * allow return statements within the function itself.  This is done in the\r
1398      * interest of execution time efficiency. */\r
1399     for( ; ; )\r
1400     {\r
1401         taskENTER_CRITICAL();\r
1402         {\r
1403             const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
1404 \r
1405             /* Is there data in the queue now?  To be running the calling task\r
1406              * must be the highest priority task wanting to access the queue. */\r
1407             if( uxMessagesWaiting > ( UBaseType_t ) 0 )\r
1408             {\r
1409                 /* Data available, remove one item. */\r
1410                 prvCopyDataFromQueue( pxQueue, pvBuffer );\r
1411                 traceQUEUE_RECEIVE( pxQueue );\r
1412                 pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;\r
1413 \r
1414                 /* There is now space in the queue, were any tasks waiting to\r
1415                  * post to the queue?  If so, unblock the highest priority waiting\r
1416                  * task. */\r
1417                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1418                 {\r
1419                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1420                     {\r
1421                         queueYIELD_IF_USING_PREEMPTION();\r
1422                     }\r
1423                     else\r
1424                     {\r
1425                         mtCOVERAGE_TEST_MARKER();\r
1426                     }\r
1427                 }\r
1428                 else\r
1429                 {\r
1430                     mtCOVERAGE_TEST_MARKER();\r
1431                 }\r
1432 \r
1433                 taskEXIT_CRITICAL();\r
1434                 return pdPASS;\r
1435             }\r
1436             else\r
1437             {\r
1438                 if( xTicksToWait == ( TickType_t ) 0 )\r
1439                 {\r
1440                     /* The queue was empty and no block time is specified (or\r
1441                      * the block time has expired) so leave now. */\r
1442                     taskEXIT_CRITICAL();\r
1443                     traceQUEUE_RECEIVE_FAILED( pxQueue );\r
1444                     return errQUEUE_EMPTY;\r
1445                 }\r
1446                 else if( xEntryTimeSet == pdFALSE )\r
1447                 {\r
1448                     /* The queue was empty and a block time was specified so\r
1449                      * configure the timeout structure. */\r
1450                     vTaskInternalSetTimeOutState( &xTimeOut );\r
1451                     xEntryTimeSet = pdTRUE;\r
1452                 }\r
1453                 else\r
1454                 {\r
1455                     /* Entry time was already set. */\r
1456                     mtCOVERAGE_TEST_MARKER();\r
1457                 }\r
1458             }\r
1459         }\r
1460         taskEXIT_CRITICAL();\r
1461 \r
1462         /* Interrupts and other tasks can send to and receive from the queue\r
1463          * now the critical section has been exited. */\r
1464 \r
1465         vTaskSuspendAll();\r
1466         prvLockQueue( pxQueue );\r
1467 \r
1468         /* Update the timeout state to see if it has expired yet. */\r
1469         if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
1470         {\r
1471             /* The timeout has not expired.  If the queue is still empty place\r
1472              * the task on the list of tasks waiting to receive from the queue. */\r
1473             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
1474             {\r
1475                 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
1476                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
1477                 prvUnlockQueue( pxQueue );\r
1478 \r
1479                 if( xTaskResumeAll() == pdFALSE )\r
1480                 {\r
1481                     portYIELD_WITHIN_API();\r
1482                 }\r
1483                 else\r
1484                 {\r
1485                     mtCOVERAGE_TEST_MARKER();\r
1486                 }\r
1487             }\r
1488             else\r
1489             {\r
1490                 /* The queue contains data again.  Loop back to try and read the\r
1491                  * data. */\r
1492                 prvUnlockQueue( pxQueue );\r
1493                 ( void ) xTaskResumeAll();\r
1494             }\r
1495         }\r
1496         else\r
1497         {\r
1498             /* Timed out.  If there is no data in the queue exit, otherwise loop\r
1499              * back and attempt to read the data. */\r
1500             prvUnlockQueue( pxQueue );\r
1501             ( void ) xTaskResumeAll();\r
1502 \r
1503             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
1504             {\r
1505                 traceQUEUE_RECEIVE_FAILED( pxQueue );\r
1506                 return errQUEUE_EMPTY;\r
1507             }\r
1508             else\r
1509             {\r
1510                 mtCOVERAGE_TEST_MARKER();\r
1511             }\r
1512         }\r
1513     } /*lint -restore */\r
1514 }\r
1515 /*-----------------------------------------------------------*/\r
1516 \r
1517 BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue,\r
1518                                 TickType_t xTicksToWait )\r
1519 {\r
1520     BaseType_t xEntryTimeSet = pdFALSE;\r
1521     TimeOut_t xTimeOut;\r
1522     Queue_t * const pxQueue = xQueue;\r
1523 \r
1524     #if ( configUSE_MUTEXES == 1 )\r
1525         BaseType_t xInheritanceOccurred = pdFALSE;\r
1526     #endif\r
1527 \r
1528     /* Check the queue pointer is not NULL. */\r
1529     configASSERT( ( pxQueue ) );\r
1530 \r
1531     /* Check this really is a semaphore, in which case the item size will be\r
1532      * 0. */\r
1533     configASSERT( pxQueue->uxItemSize == 0 );\r
1534 \r
1535     /* Cannot block if the scheduler is suspended. */\r
1536     #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
1537     {\r
1538         configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
1539     }\r
1540     #endif\r
1541 \r
1542     /*lint -save -e904 This function relaxes the coding standard somewhat to allow return\r
1543      * statements within the function itself.  This is done in the interest\r
1544      * of execution time efficiency. */\r
1545     for( ; ; )\r
1546     {\r
1547         taskENTER_CRITICAL();\r
1548         {\r
1549             /* Semaphores are queues with an item size of 0, and where the\r
1550              * number of messages in the queue is the semaphore's count value. */\r
1551             const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;\r
1552 \r
1553             /* Is there data in the queue now?  To be running the calling task\r
1554              * must be the highest priority task wanting to access the queue. */\r
1555             if( uxSemaphoreCount > ( UBaseType_t ) 0 )\r
1556             {\r
1557                 traceQUEUE_RECEIVE( pxQueue );\r
1558 \r
1559                 /* Semaphores are queues with a data size of zero and where the\r
1560                  * messages waiting is the semaphore's count.  Reduce the count. */\r
1561                 pxQueue->uxMessagesWaiting = uxSemaphoreCount - ( UBaseType_t ) 1;\r
1562 \r
1563                 #if ( configUSE_MUTEXES == 1 )\r
1564                 {\r
1565                     if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
1566                     {\r
1567                         /* Record the information required to implement\r
1568                          * priority inheritance should it become necessary. */\r
1569                         pxQueue->u.xSemaphore.xMutexHolder = pvTaskIncrementMutexHeldCount();\r
1570                     }\r
1571                     else\r
1572                     {\r
1573                         mtCOVERAGE_TEST_MARKER();\r
1574                     }\r
1575                 }\r
1576                 #endif /* configUSE_MUTEXES */\r
1577 \r
1578                 /* Check to see if other tasks are blocked waiting to give the\r
1579                  * semaphore, and if so, unblock the highest priority such task. */\r
1580                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1581                 {\r
1582                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1583                     {\r
1584                         queueYIELD_IF_USING_PREEMPTION();\r
1585                     }\r
1586                     else\r
1587                     {\r
1588                         mtCOVERAGE_TEST_MARKER();\r
1589                     }\r
1590                 }\r
1591                 else\r
1592                 {\r
1593                     mtCOVERAGE_TEST_MARKER();\r
1594                 }\r
1595 \r
1596                 taskEXIT_CRITICAL();\r
1597                 return pdPASS;\r
1598             }\r
1599             else\r
1600             {\r
1601                 if( xTicksToWait == ( TickType_t ) 0 )\r
1602                 {\r
1603                     /* The semaphore count was 0 and no block time is specified\r
1604                      * (or the block time has expired) so exit now. */\r
1605                     taskEXIT_CRITICAL();\r
1606                     traceQUEUE_RECEIVE_FAILED( pxQueue );\r
1607                     return errQUEUE_EMPTY;\r
1608                 }\r
1609                 else if( xEntryTimeSet == pdFALSE )\r
1610                 {\r
1611                     /* The semaphore count was 0 and a block time was specified\r
1612                      * so configure the timeout structure ready to block. */\r
1613                     vTaskInternalSetTimeOutState( &xTimeOut );\r
1614                     xEntryTimeSet = pdTRUE;\r
1615                 }\r
1616                 else\r
1617                 {\r
1618                     /* Entry time was already set. */\r
1619                     mtCOVERAGE_TEST_MARKER();\r
1620                 }\r
1621             }\r
1622         }\r
1623         taskEXIT_CRITICAL();\r
1624 \r
1625         /* Interrupts and other tasks can give to and take from the semaphore\r
1626          * now the critical section has been exited. */\r
1627 \r
1628         vTaskSuspendAll();\r
1629         prvLockQueue( pxQueue );\r
1630 \r
1631         /* Update the timeout state to see if it has expired yet. */\r
1632         if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
1633         {\r
1634             /* A block time is specified and not expired.  If the semaphore\r
1635              * count is 0 then enter the Blocked state to wait for a semaphore to\r
1636              * become available.  As semaphores are implemented with queues the\r
1637              * queue being empty is equivalent to the semaphore count being 0. */\r
1638             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
1639             {\r
1640                 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
1641 \r
1642                 #if ( configUSE_MUTEXES == 1 )\r
1643                 {\r
1644                     if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
1645                     {\r
1646                         taskENTER_CRITICAL();\r
1647                         {\r
1648                             xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );\r
1649                         }\r
1650                         taskEXIT_CRITICAL();\r
1651                     }\r
1652                     else\r
1653                     {\r
1654                         mtCOVERAGE_TEST_MARKER();\r
1655                     }\r
1656                 }\r
1657                 #endif /* if ( configUSE_MUTEXES == 1 ) */\r
1658 \r
1659                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
1660                 prvUnlockQueue( pxQueue );\r
1661 \r
1662                 if( xTaskResumeAll() == pdFALSE )\r
1663                 {\r
1664                     portYIELD_WITHIN_API();\r
1665                 }\r
1666                 else\r
1667                 {\r
1668                     mtCOVERAGE_TEST_MARKER();\r
1669                 }\r
1670             }\r
1671             else\r
1672             {\r
1673                 /* There was no timeout and the semaphore count was not 0, so\r
1674                  * attempt to take the semaphore again. */\r
1675                 prvUnlockQueue( pxQueue );\r
1676                 ( void ) xTaskResumeAll();\r
1677             }\r
1678         }\r
1679         else\r
1680         {\r
1681             /* Timed out. */\r
1682             prvUnlockQueue( pxQueue );\r
1683             ( void ) xTaskResumeAll();\r
1684 \r
1685             /* If the semaphore count is 0 exit now as the timeout has\r
1686              * expired.  Otherwise return to attempt to take the semaphore that is\r
1687              * known to be available.  As semaphores are implemented by queues the\r
1688              * queue being empty is equivalent to the semaphore count being 0. */\r
1689             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
1690             {\r
1691                 #if ( configUSE_MUTEXES == 1 )\r
1692                 {\r
1693                     /* xInheritanceOccurred could only have be set if\r
1694                      * pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to\r
1695                      * test the mutex type again to check it is actually a mutex. */\r
1696                     if( xInheritanceOccurred != pdFALSE )\r
1697                     {\r
1698                         taskENTER_CRITICAL();\r
1699                         {\r
1700                             UBaseType_t uxHighestWaitingPriority;\r
1701 \r
1702                             /* This task blocking on the mutex caused another\r
1703                              * task to inherit this task's priority.  Now this task\r
1704                              * has timed out the priority should be disinherited\r
1705                              * again, but only as low as the next highest priority\r
1706                              * task that is waiting for the same mutex. */\r
1707                             uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );\r
1708                             vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );\r
1709                         }\r
1710                         taskEXIT_CRITICAL();\r
1711                     }\r
1712                 }\r
1713                 #endif /* configUSE_MUTEXES */\r
1714 \r
1715                 traceQUEUE_RECEIVE_FAILED( pxQueue );\r
1716                 return errQUEUE_EMPTY;\r
1717             }\r
1718             else\r
1719             {\r
1720                 mtCOVERAGE_TEST_MARKER();\r
1721             }\r
1722         }\r
1723     } /*lint -restore */\r
1724 }\r
1725 /*-----------------------------------------------------------*/\r
1726 \r
1727 BaseType_t xQueuePeek( QueueHandle_t xQueue,\r
1728                        void * const pvBuffer,\r
1729                        TickType_t xTicksToWait )\r
1730 {\r
1731     BaseType_t xEntryTimeSet = pdFALSE;\r
1732     TimeOut_t xTimeOut;\r
1733     int8_t * pcOriginalReadPosition;\r
1734     Queue_t * const pxQueue = xQueue;\r
1735 \r
1736     /* Check the pointer is not NULL. */\r
1737     configASSERT( ( pxQueue ) );\r
1738 \r
1739     /* The buffer into which data is received can only be NULL if the data size\r
1740      * is zero (so no data is copied into the buffer. */\r
1741     configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
1742 \r
1743     /* Cannot block if the scheduler is suspended. */\r
1744     #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
1745     {\r
1746         configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
1747     }\r
1748     #endif\r
1749 \r
1750     /*lint -save -e904  This function relaxes the coding standard somewhat to\r
1751      * allow return statements within the function itself.  This is done in the\r
1752      * interest of execution time efficiency. */\r
1753     for( ; ; )\r
1754     {\r
1755         taskENTER_CRITICAL();\r
1756         {\r
1757             const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
1758 \r
1759             /* Is there data in the queue now?  To be running the calling task\r
1760              * must be the highest priority task wanting to access the queue. */\r
1761             if( uxMessagesWaiting > ( UBaseType_t ) 0 )\r
1762             {\r
1763                 /* Remember the read position so it can be reset after the data\r
1764                  * is read from the queue as this function is only peeking the\r
1765                  * data, not removing it. */\r
1766                 pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;\r
1767 \r
1768                 prvCopyDataFromQueue( pxQueue, pvBuffer );\r
1769                 traceQUEUE_PEEK( pxQueue );\r
1770 \r
1771                 /* The data is not being removed, so reset the read pointer. */\r
1772                 pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;\r
1773 \r
1774                 /* The data is being left in the queue, so see if there are\r
1775                  * any other tasks waiting for the data. */\r
1776                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
1777                 {\r
1778                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
1779                     {\r
1780                         /* The task waiting has a higher priority than this task. */\r
1781                         queueYIELD_IF_USING_PREEMPTION();\r
1782                     }\r
1783                     else\r
1784                     {\r
1785                         mtCOVERAGE_TEST_MARKER();\r
1786                     }\r
1787                 }\r
1788                 else\r
1789                 {\r
1790                     mtCOVERAGE_TEST_MARKER();\r
1791                 }\r
1792 \r
1793                 taskEXIT_CRITICAL();\r
1794                 return pdPASS;\r
1795             }\r
1796             else\r
1797             {\r
1798                 if( xTicksToWait == ( TickType_t ) 0 )\r
1799                 {\r
1800                     /* The queue was empty and no block time is specified (or\r
1801                      * the block time has expired) so leave now. */\r
1802                     taskEXIT_CRITICAL();\r
1803                     traceQUEUE_PEEK_FAILED( pxQueue );\r
1804                     return errQUEUE_EMPTY;\r
1805                 }\r
1806                 else if( xEntryTimeSet == pdFALSE )\r
1807                 {\r
1808                     /* The queue was empty and a block time was specified so\r
1809                      * configure the timeout structure ready to enter the blocked\r
1810                      * state. */\r
1811                     vTaskInternalSetTimeOutState( &xTimeOut );\r
1812                     xEntryTimeSet = pdTRUE;\r
1813                 }\r
1814                 else\r
1815                 {\r
1816                     /* Entry time was already set. */\r
1817                     mtCOVERAGE_TEST_MARKER();\r
1818                 }\r
1819             }\r
1820         }\r
1821         taskEXIT_CRITICAL();\r
1822 \r
1823         /* Interrupts and other tasks can send to and receive from the queue\r
1824          * now that the critical section has been exited. */\r
1825 \r
1826         vTaskSuspendAll();\r
1827         prvLockQueue( pxQueue );\r
1828 \r
1829         /* Update the timeout state to see if it has expired yet. */\r
1830         if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
1831         {\r
1832             /* Timeout has not expired yet, check to see if there is data in the\r
1833             * queue now, and if not enter the Blocked state to wait for data. */\r
1834             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
1835             {\r
1836                 traceBLOCKING_ON_QUEUE_PEEK( pxQueue );\r
1837                 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
1838                 prvUnlockQueue( pxQueue );\r
1839 \r
1840                 if( xTaskResumeAll() == pdFALSE )\r
1841                 {\r
1842                     portYIELD_WITHIN_API();\r
1843                 }\r
1844                 else\r
1845                 {\r
1846                     mtCOVERAGE_TEST_MARKER();\r
1847                 }\r
1848             }\r
1849             else\r
1850             {\r
1851                 /* There is data in the queue now, so don't enter the blocked\r
1852                  * state, instead return to try and obtain the data. */\r
1853                 prvUnlockQueue( pxQueue );\r
1854                 ( void ) xTaskResumeAll();\r
1855             }\r
1856         }\r
1857         else\r
1858         {\r
1859             /* The timeout has expired.  If there is still no data in the queue\r
1860              * exit, otherwise go back and try to read the data again. */\r
1861             prvUnlockQueue( pxQueue );\r
1862             ( void ) xTaskResumeAll();\r
1863 \r
1864             if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
1865             {\r
1866                 traceQUEUE_PEEK_FAILED( pxQueue );\r
1867                 return errQUEUE_EMPTY;\r
1868             }\r
1869             else\r
1870             {\r
1871                 mtCOVERAGE_TEST_MARKER();\r
1872             }\r
1873         }\r
1874     } /*lint -restore */\r
1875 }\r
1876 /*-----------------------------------------------------------*/\r
1877 \r
1878 BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue,\r
1879                                  void * const pvBuffer,\r
1880                                  BaseType_t * const pxHigherPriorityTaskWoken )\r
1881 {\r
1882     BaseType_t xReturn;\r
1883     UBaseType_t uxSavedInterruptStatus;\r
1884     Queue_t * const pxQueue = xQueue;\r
1885 \r
1886     configASSERT( pxQueue );\r
1887     configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
1888 \r
1889     /* RTOS ports that support interrupt nesting have the concept of a maximum\r
1890      * system call (or maximum API call) interrupt priority.  Interrupts that are\r
1891      * above the maximum system call priority are kept permanently enabled, even\r
1892      * when the RTOS kernel is in a critical section, but cannot make any calls to\r
1893      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h\r
1894      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
1895      * failure if a FreeRTOS API function is called from an interrupt that has been\r
1896      * assigned a priority above the configured maximum system call priority.\r
1897      * Only FreeRTOS functions that end in FromISR can be called from interrupts\r
1898      * that have been assigned a priority at or (logically) below the maximum\r
1899      * system call interrupt priority.  FreeRTOS maintains a separate interrupt\r
1900      * safe API to ensure interrupt entry is as fast and as simple as possible.\r
1901      * More information (albeit Cortex-M specific) is provided on the following\r
1902      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
1903     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();\r
1904 \r
1905     uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
1906     {\r
1907         const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
1908 \r
1909         /* Cannot block in an ISR, so check there is data available. */\r
1910         if( uxMessagesWaiting > ( UBaseType_t ) 0 )\r
1911         {\r
1912             const int8_t cRxLock = pxQueue->cRxLock;\r
1913 \r
1914             traceQUEUE_RECEIVE_FROM_ISR( pxQueue );\r
1915 \r
1916             prvCopyDataFromQueue( pxQueue, pvBuffer );\r
1917             pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;\r
1918 \r
1919             /* If the queue is locked the event list will not be modified.\r
1920              * Instead update the lock count so the task that unlocks the queue\r
1921              * will know that an ISR has removed data while the queue was\r
1922              * locked. */\r
1923             if( cRxLock == queueUNLOCKED )\r
1924             {\r
1925                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
1926                 {\r
1927                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
1928                     {\r
1929                         /* The task waiting has a higher priority than us so\r
1930                          * force a context switch. */\r
1931                         if( pxHigherPriorityTaskWoken != NULL )\r
1932                         {\r
1933                             *pxHigherPriorityTaskWoken = pdTRUE;\r
1934                         }\r
1935                         else\r
1936                         {\r
1937                             mtCOVERAGE_TEST_MARKER();\r
1938                         }\r
1939                     }\r
1940                     else\r
1941                     {\r
1942                         mtCOVERAGE_TEST_MARKER();\r
1943                     }\r
1944                 }\r
1945                 else\r
1946                 {\r
1947                     mtCOVERAGE_TEST_MARKER();\r
1948                 }\r
1949             }\r
1950             else\r
1951             {\r
1952                 /* Increment the lock count so the task that unlocks the queue\r
1953                  * knows that data was removed while it was locked. */\r
1954                 prvIncrementQueueRxLock( pxQueue, cRxLock );\r
1955             }\r
1956 \r
1957             xReturn = pdPASS;\r
1958         }\r
1959         else\r
1960         {\r
1961             xReturn = pdFAIL;\r
1962             traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );\r
1963         }\r
1964     }\r
1965     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
1966 \r
1967     return xReturn;\r
1968 }\r
1969 /*-----------------------------------------------------------*/\r
1970 \r
1971 BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,\r
1972                               void * const pvBuffer )\r
1973 {\r
1974     BaseType_t xReturn;\r
1975     UBaseType_t uxSavedInterruptStatus;\r
1976     int8_t * pcOriginalReadPosition;\r
1977     Queue_t * const pxQueue = xQueue;\r
1978 \r
1979     configASSERT( pxQueue );\r
1980     configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
1981     configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */\r
1982 \r
1983     /* RTOS ports that support interrupt nesting have the concept of a maximum\r
1984      * system call (or maximum API call) interrupt priority.  Interrupts that are\r
1985      * above the maximum system call priority are kept permanently enabled, even\r
1986      * when the RTOS kernel is in a critical section, but cannot make any calls to\r
1987      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h\r
1988      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
1989      * failure if a FreeRTOS API function is called from an interrupt that has been\r
1990      * assigned a priority above the configured maximum system call priority.\r
1991      * Only FreeRTOS functions that end in FromISR can be called from interrupts\r
1992      * that have been assigned a priority at or (logically) below the maximum\r
1993      * system call interrupt priority.  FreeRTOS maintains a separate interrupt\r
1994      * safe API to ensure interrupt entry is as fast and as simple as possible.\r
1995      * More information (albeit Cortex-M specific) is provided on the following\r
1996      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */\r
1997     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();\r
1998 \r
1999     uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
2000     {\r
2001         /* Cannot block in an ISR, so check there is data available. */\r
2002         if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )\r
2003         {\r
2004             traceQUEUE_PEEK_FROM_ISR( pxQueue );\r
2005 \r
2006             /* Remember the read position so it can be reset as nothing is\r
2007              * actually being removed from the queue. */\r
2008             pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;\r
2009             prvCopyDataFromQueue( pxQueue, pvBuffer );\r
2010             pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;\r
2011 \r
2012             xReturn = pdPASS;\r
2013         }\r
2014         else\r
2015         {\r
2016             xReturn = pdFAIL;\r
2017             traceQUEUE_PEEK_FROM_ISR_FAILED( pxQueue );\r
2018         }\r
2019     }\r
2020     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
2021 \r
2022     return xReturn;\r
2023 }\r
2024 /*-----------------------------------------------------------*/\r
2025 \r
2026 UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )\r
2027 {\r
2028     UBaseType_t uxReturn;\r
2029 \r
2030     configASSERT( xQueue );\r
2031 \r
2032     taskENTER_CRITICAL();\r
2033     {\r
2034         uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;\r
2035     }\r
2036     taskEXIT_CRITICAL();\r
2037 \r
2038     return uxReturn;\r
2039 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */\r
2040 /*-----------------------------------------------------------*/\r
2041 \r
2042 UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )\r
2043 {\r
2044     UBaseType_t uxReturn;\r
2045     Queue_t * const pxQueue = xQueue;\r
2046 \r
2047     configASSERT( pxQueue );\r
2048 \r
2049     taskENTER_CRITICAL();\r
2050     {\r
2051         uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;\r
2052     }\r
2053     taskEXIT_CRITICAL();\r
2054 \r
2055     return uxReturn;\r
2056 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */\r
2057 /*-----------------------------------------------------------*/\r
2058 \r
2059 UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )\r
2060 {\r
2061     UBaseType_t uxReturn;\r
2062     Queue_t * const pxQueue = xQueue;\r
2063 \r
2064     configASSERT( pxQueue );\r
2065     uxReturn = pxQueue->uxMessagesWaiting;\r
2066 \r
2067     return uxReturn;\r
2068 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */\r
2069 /*-----------------------------------------------------------*/\r
2070 \r
2071 void vQueueDelete( QueueHandle_t xQueue )\r
2072 {\r
2073     Queue_t * const pxQueue = xQueue;\r
2074 \r
2075     configASSERT( pxQueue );\r
2076     traceQUEUE_DELETE( pxQueue );\r
2077 \r
2078     #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
2079     {\r
2080         vQueueUnregisterQueue( pxQueue );\r
2081     }\r
2082     #endif\r
2083 \r
2084     #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )\r
2085     {\r
2086         /* The queue can only have been allocated dynamically - free it\r
2087          * again. */\r
2088         vPortFree( pxQueue );\r
2089     }\r
2090     #elif ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
2091     {\r
2092         /* The queue could have been allocated statically or dynamically, so\r
2093          * check before attempting to free the memory. */\r
2094         if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )\r
2095         {\r
2096             vPortFree( pxQueue );\r
2097         }\r
2098         else\r
2099         {\r
2100             mtCOVERAGE_TEST_MARKER();\r
2101         }\r
2102     }\r
2103     #else /* if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) ) */\r
2104     {\r
2105         /* The queue must have been statically allocated, so is not going to be\r
2106          * deleted.  Avoid compiler warnings about the unused parameter. */\r
2107         ( void ) pxQueue;\r
2108     }\r
2109     #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
2110 }\r
2111 /*-----------------------------------------------------------*/\r
2112 \r
2113 #if ( configUSE_TRACE_FACILITY == 1 )\r
2114 \r
2115     UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )\r
2116     {\r
2117         return ( ( Queue_t * ) xQueue )->uxQueueNumber;\r
2118     }\r
2119 \r
2120 #endif /* configUSE_TRACE_FACILITY */\r
2121 /*-----------------------------------------------------------*/\r
2122 \r
2123 #if ( configUSE_TRACE_FACILITY == 1 )\r
2124 \r
2125     void vQueueSetQueueNumber( QueueHandle_t xQueue,\r
2126                                UBaseType_t uxQueueNumber )\r
2127     {\r
2128         ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;\r
2129     }\r
2130 \r
2131 #endif /* configUSE_TRACE_FACILITY */\r
2132 /*-----------------------------------------------------------*/\r
2133 \r
2134 #if ( configUSE_TRACE_FACILITY == 1 )\r
2135 \r
2136     uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )\r
2137     {\r
2138         return ( ( Queue_t * ) xQueue )->ucQueueType;\r
2139     }\r
2140 \r
2141 #endif /* configUSE_TRACE_FACILITY */\r
2142 /*-----------------------------------------------------------*/\r
2143 \r
2144 #if ( configUSE_MUTEXES == 1 )\r
2145 \r
2146     static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue )\r
2147     {\r
2148         UBaseType_t uxHighestPriorityOfWaitingTasks;\r
2149 \r
2150         /* If a task waiting for a mutex causes the mutex holder to inherit a\r
2151          * priority, but the waiting task times out, then the holder should\r
2152          * disinherit the priority - but only down to the highest priority of any\r
2153          * other tasks that are waiting for the same mutex.  For this purpose,\r
2154          * return the priority of the highest priority task that is waiting for the\r
2155          * mutex. */\r
2156         if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0U )\r
2157         {\r
2158             uxHighestPriorityOfWaitingTasks = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) );\r
2159         }\r
2160         else\r
2161         {\r
2162             uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;\r
2163         }\r
2164 \r
2165         return uxHighestPriorityOfWaitingTasks;\r
2166     }\r
2167 \r
2168 #endif /* configUSE_MUTEXES */\r
2169 /*-----------------------------------------------------------*/\r
2170 \r
2171 static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue,\r
2172                                       const void * pvItemToQueue,\r
2173                                       const BaseType_t xPosition )\r
2174 {\r
2175     BaseType_t xReturn = pdFALSE;\r
2176     UBaseType_t uxMessagesWaiting;\r
2177 \r
2178     /* This function is called from a critical section. */\r
2179 \r
2180     uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
2181 \r
2182     if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )\r
2183     {\r
2184         #if ( configUSE_MUTEXES == 1 )\r
2185         {\r
2186             if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
2187             {\r
2188                 /* The mutex is no longer being held. */\r
2189                 xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );\r
2190                 pxQueue->u.xSemaphore.xMutexHolder = NULL;\r
2191             }\r
2192             else\r
2193             {\r
2194                 mtCOVERAGE_TEST_MARKER();\r
2195             }\r
2196         }\r
2197         #endif /* configUSE_MUTEXES */\r
2198     }\r
2199     else if( xPosition == queueSEND_TO_BACK )\r
2200     {\r
2201         ( void ) memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports, plus previous logic ensures a null pointer can only be passed to memcpy() if the copy size is 0.  Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */\r
2202         pxQueue->pcWriteTo += pxQueue->uxItemSize;                                                       /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */\r
2203 \r
2204         if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail )                                             /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */\r
2205         {\r
2206             pxQueue->pcWriteTo = pxQueue->pcHead;\r
2207         }\r
2208         else\r
2209         {\r
2210             mtCOVERAGE_TEST_MARKER();\r
2211         }\r
2212     }\r
2213     else\r
2214     {\r
2215         ( void ) memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e9087 !e418 MISRA exception as the casts are only redundant for some ports.  Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes.  Assert checks null pointer only used when length is 0. */\r
2216         pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;\r
2217 \r
2218         if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */\r
2219         {\r
2220             pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );\r
2221         }\r
2222         else\r
2223         {\r
2224             mtCOVERAGE_TEST_MARKER();\r
2225         }\r
2226 \r
2227         if( xPosition == queueOVERWRITE )\r
2228         {\r
2229             if( uxMessagesWaiting > ( UBaseType_t ) 0 )\r
2230             {\r
2231                 /* An item is not being added but overwritten, so subtract\r
2232                  * one from the recorded number of items in the queue so when\r
2233                  * one is added again below the number of recorded items remains\r
2234                  * correct. */\r
2235                 --uxMessagesWaiting;\r
2236             }\r
2237             else\r
2238             {\r
2239                 mtCOVERAGE_TEST_MARKER();\r
2240             }\r
2241         }\r
2242         else\r
2243         {\r
2244             mtCOVERAGE_TEST_MARKER();\r
2245         }\r
2246     }\r
2247 \r
2248     pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;\r
2249 \r
2250     return xReturn;\r
2251 }\r
2252 /*-----------------------------------------------------------*/\r
2253 \r
2254 static void prvCopyDataFromQueue( Queue_t * const pxQueue,\r
2255                                   void * const pvBuffer )\r
2256 {\r
2257     if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )\r
2258     {\r
2259         pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;           /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */\r
2260 \r
2261         if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */\r
2262         {\r
2263             pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;\r
2264         }\r
2265         else\r
2266         {\r
2267             mtCOVERAGE_TEST_MARKER();\r
2268         }\r
2269 \r
2270         ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports.  Also previous logic ensures a null pointer can only be passed to memcpy() when the count is 0.  Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */\r
2271     }\r
2272 }\r
2273 /*-----------------------------------------------------------*/\r
2274 \r
2275 static void prvUnlockQueue( Queue_t * const pxQueue )\r
2276 {\r
2277     /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */\r
2278 \r
2279     /* The lock counts contains the number of extra data items placed or\r
2280      * removed from the queue while the queue was locked.  When a queue is\r
2281      * locked items can be added or removed, but the event lists cannot be\r
2282      * updated. */\r
2283     taskENTER_CRITICAL();\r
2284     {\r
2285         int8_t cTxLock = pxQueue->cTxLock;\r
2286 \r
2287         /* See if data was added to the queue while it was locked. */\r
2288         while( cTxLock > queueLOCKED_UNMODIFIED )\r
2289         {\r
2290             /* Data was posted while the queue was locked.  Are any tasks\r
2291              * blocked waiting for data to become available? */\r
2292             #if ( configUSE_QUEUE_SETS == 1 )\r
2293             {\r
2294                 if( pxQueue->pxQueueSetContainer != NULL )\r
2295                 {\r
2296                     if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )\r
2297                     {\r
2298                         /* The queue is a member of a queue set, and posting to\r
2299                          * the queue set caused a higher priority task to unblock.\r
2300                          * A context switch is required. */\r
2301                         vTaskMissedYield();\r
2302                     }\r
2303                     else\r
2304                     {\r
2305                         mtCOVERAGE_TEST_MARKER();\r
2306                     }\r
2307                 }\r
2308                 else\r
2309                 {\r
2310                     /* Tasks that are removed from the event list will get\r
2311                      * added to the pending ready list as the scheduler is still\r
2312                      * suspended. */\r
2313                     if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
2314                     {\r
2315                         if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
2316                         {\r
2317                             /* The task waiting has a higher priority so record that a\r
2318                              * context switch is required. */\r
2319                             vTaskMissedYield();\r
2320                         }\r
2321                         else\r
2322                         {\r
2323                             mtCOVERAGE_TEST_MARKER();\r
2324                         }\r
2325                     }\r
2326                     else\r
2327                     {\r
2328                         break;\r
2329                     }\r
2330                 }\r
2331             }\r
2332             #else /* configUSE_QUEUE_SETS */\r
2333             {\r
2334                 /* Tasks that are removed from the event list will get added to\r
2335                  * the pending ready list as the scheduler is still suspended. */\r
2336                 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
2337                 {\r
2338                     if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
2339                     {\r
2340                         /* The task waiting has a higher priority so record that\r
2341                          * a context switch is required. */\r
2342                         vTaskMissedYield();\r
2343                     }\r
2344                     else\r
2345                     {\r
2346                         mtCOVERAGE_TEST_MARKER();\r
2347                     }\r
2348                 }\r
2349                 else\r
2350                 {\r
2351                     break;\r
2352                 }\r
2353             }\r
2354             #endif /* configUSE_QUEUE_SETS */\r
2355 \r
2356             --cTxLock;\r
2357         }\r
2358 \r
2359         pxQueue->cTxLock = queueUNLOCKED;\r
2360     }\r
2361     taskEXIT_CRITICAL();\r
2362 \r
2363     /* Do the same for the Rx lock. */\r
2364     taskENTER_CRITICAL();\r
2365     {\r
2366         int8_t cRxLock = pxQueue->cRxLock;\r
2367 \r
2368         while( cRxLock > queueLOCKED_UNMODIFIED )\r
2369         {\r
2370             if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
2371             {\r
2372                 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
2373                 {\r
2374                     vTaskMissedYield();\r
2375                 }\r
2376                 else\r
2377                 {\r
2378                     mtCOVERAGE_TEST_MARKER();\r
2379                 }\r
2380 \r
2381                 --cRxLock;\r
2382             }\r
2383             else\r
2384             {\r
2385                 break;\r
2386             }\r
2387         }\r
2388 \r
2389         pxQueue->cRxLock = queueUNLOCKED;\r
2390     }\r
2391     taskEXIT_CRITICAL();\r
2392 }\r
2393 /*-----------------------------------------------------------*/\r
2394 \r
2395 static BaseType_t prvIsQueueEmpty( const Queue_t * pxQueue )\r
2396 {\r
2397     BaseType_t xReturn;\r
2398 \r
2399     taskENTER_CRITICAL();\r
2400     {\r
2401         if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )\r
2402         {\r
2403             xReturn = pdTRUE;\r
2404         }\r
2405         else\r
2406         {\r
2407             xReturn = pdFALSE;\r
2408         }\r
2409     }\r
2410     taskEXIT_CRITICAL();\r
2411 \r
2412     return xReturn;\r
2413 }\r
2414 /*-----------------------------------------------------------*/\r
2415 \r
2416 BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )\r
2417 {\r
2418     BaseType_t xReturn;\r
2419     Queue_t * const pxQueue = xQueue;\r
2420 \r
2421     configASSERT( pxQueue );\r
2422 \r
2423     if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )\r
2424     {\r
2425         xReturn = pdTRUE;\r
2426     }\r
2427     else\r
2428     {\r
2429         xReturn = pdFALSE;\r
2430     }\r
2431 \r
2432     return xReturn;\r
2433 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */\r
2434 /*-----------------------------------------------------------*/\r
2435 \r
2436 static BaseType_t prvIsQueueFull( const Queue_t * pxQueue )\r
2437 {\r
2438     BaseType_t xReturn;\r
2439 \r
2440     taskENTER_CRITICAL();\r
2441     {\r
2442         if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )\r
2443         {\r
2444             xReturn = pdTRUE;\r
2445         }\r
2446         else\r
2447         {\r
2448             xReturn = pdFALSE;\r
2449         }\r
2450     }\r
2451     taskEXIT_CRITICAL();\r
2452 \r
2453     return xReturn;\r
2454 }\r
2455 /*-----------------------------------------------------------*/\r
2456 \r
2457 BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )\r
2458 {\r
2459     BaseType_t xReturn;\r
2460     Queue_t * const pxQueue = xQueue;\r
2461 \r
2462     configASSERT( pxQueue );\r
2463 \r
2464     if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )\r
2465     {\r
2466         xReturn = pdTRUE;\r
2467     }\r
2468     else\r
2469     {\r
2470         xReturn = pdFALSE;\r
2471     }\r
2472 \r
2473     return xReturn;\r
2474 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */\r
2475 /*-----------------------------------------------------------*/\r
2476 \r
2477 #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
2478 \r
2479     void vQueueAddToRegistry( QueueHandle_t xQueue,\r
2480                               const char * pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
2481     {\r
2482         UBaseType_t ux;\r
2483         QueueRegistryItem_t * pxEntryToWrite = NULL;\r
2484 \r
2485         configASSERT( xQueue );\r
2486 \r
2487         if( pcQueueName != NULL )\r
2488         {\r
2489             /* See if there is an empty space in the registry.  A NULL name denotes\r
2490              * a free slot. */\r
2491             for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )\r
2492             {\r
2493                 /* Replace an existing entry if the queue is already in the registry. */\r
2494                 if( xQueue == xQueueRegistry[ ux ].xHandle )\r
2495                 {\r
2496                     pxEntryToWrite = &( xQueueRegistry[ ux ] );\r
2497                     break;\r
2498                 }\r
2499                 /* Otherwise, store in the next empty location */\r
2500                 else if( ( pxEntryToWrite == NULL ) && ( xQueueRegistry[ ux ].pcQueueName == NULL ) )\r
2501                 {\r
2502                     pxEntryToWrite = &( xQueueRegistry[ ux ] );\r
2503                 }\r
2504                 else\r
2505                 {\r
2506                     mtCOVERAGE_TEST_MARKER();\r
2507                 }\r
2508             }\r
2509         }\r
2510 \r
2511         if( pxEntryToWrite != NULL )\r
2512         {\r
2513             /* Store the information on this queue. */\r
2514             pxEntryToWrite->pcQueueName = pcQueueName;\r
2515             pxEntryToWrite->xHandle = xQueue;\r
2516 \r
2517             traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );\r
2518         }\r
2519     }\r
2520 \r
2521 #endif /* configQUEUE_REGISTRY_SIZE */\r
2522 /*-----------------------------------------------------------*/\r
2523 \r
2524 #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
2525 \r
2526     const char * pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
2527     {\r
2528         UBaseType_t ux;\r
2529         const char * pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
2530 \r
2531         configASSERT( xQueue );\r
2532 \r
2533         /* Note there is nothing here to protect against another task adding or\r
2534          * removing entries from the registry while it is being searched. */\r
2535 \r
2536         for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )\r
2537         {\r
2538             if( xQueueRegistry[ ux ].xHandle == xQueue )\r
2539             {\r
2540                 pcReturn = xQueueRegistry[ ux ].pcQueueName;\r
2541                 break;\r
2542             }\r
2543             else\r
2544             {\r
2545                 mtCOVERAGE_TEST_MARKER();\r
2546             }\r
2547         }\r
2548 \r
2549         return pcReturn;\r
2550     } /*lint !e818 xQueue cannot be a pointer to const because it is a typedef. */\r
2551 \r
2552 #endif /* configQUEUE_REGISTRY_SIZE */\r
2553 /*-----------------------------------------------------------*/\r
2554 \r
2555 #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
2556 \r
2557     void vQueueUnregisterQueue( QueueHandle_t xQueue )\r
2558     {\r
2559         UBaseType_t ux;\r
2560 \r
2561         configASSERT( xQueue );\r
2562 \r
2563         /* See if the handle of the queue being unregistered in actually in the\r
2564          * registry. */\r
2565         for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )\r
2566         {\r
2567             if( xQueueRegistry[ ux ].xHandle == xQueue )\r
2568             {\r
2569                 /* Set the name to NULL to show that this slot if free again. */\r
2570                 xQueueRegistry[ ux ].pcQueueName = NULL;\r
2571 \r
2572                 /* Set the handle to NULL to ensure the same queue handle cannot\r
2573                  * appear in the registry twice if it is added, removed, then\r
2574                  * added again. */\r
2575                 xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;\r
2576                 break;\r
2577             }\r
2578             else\r
2579             {\r
2580                 mtCOVERAGE_TEST_MARKER();\r
2581             }\r
2582         }\r
2583     } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */\r
2584 \r
2585 #endif /* configQUEUE_REGISTRY_SIZE */\r
2586 /*-----------------------------------------------------------*/\r
2587 \r
2588 #if ( configUSE_TIMERS == 1 )\r
2589 \r
2590     void vQueueWaitForMessageRestricted( QueueHandle_t xQueue,\r
2591                                          TickType_t xTicksToWait,\r
2592                                          const BaseType_t xWaitIndefinitely )\r
2593     {\r
2594         Queue_t * const pxQueue = xQueue;\r
2595 \r
2596         /* This function should not be called by application code hence the\r
2597          * 'Restricted' in its name.  It is not part of the public API.  It is\r
2598          * designed for use by kernel code, and has special calling requirements.\r
2599          * It can result in vListInsert() being called on a list that can only\r
2600          * possibly ever have one item in it, so the list will be fast, but even\r
2601          * so it should be called with the scheduler locked and not from a critical\r
2602          * section. */\r
2603 \r
2604         /* Only do anything if there are no messages in the queue.  This function\r
2605          *  will not actually cause the task to block, just place it on a blocked\r
2606          *  list.  It will not block until the scheduler is unlocked - at which\r
2607          *  time a yield will be performed.  If an item is added to the queue while\r
2608          *  the queue is locked, and the calling task blocks on the queue, then the\r
2609          *  calling task will be immediately unblocked when the queue is unlocked. */\r
2610         prvLockQueue( pxQueue );\r
2611 \r
2612         if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )\r
2613         {\r
2614             /* There is nothing in the queue, block for the specified period. */\r
2615             vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );\r
2616         }\r
2617         else\r
2618         {\r
2619             mtCOVERAGE_TEST_MARKER();\r
2620         }\r
2621 \r
2622         prvUnlockQueue( pxQueue );\r
2623     }\r
2624 \r
2625 #endif /* configUSE_TIMERS */\r
2626 /*-----------------------------------------------------------*/\r
2627 \r
2628 #if ( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
2629 \r
2630     QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )\r
2631     {\r
2632         QueueSetHandle_t pxQueue;\r
2633 \r
2634         pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );\r
2635 \r
2636         return pxQueue;\r
2637     }\r
2638 \r
2639 #endif /* configUSE_QUEUE_SETS */\r
2640 /*-----------------------------------------------------------*/\r
2641 \r
2642 #if ( configUSE_QUEUE_SETS == 1 )\r
2643 \r
2644     BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore,\r
2645                                QueueSetHandle_t xQueueSet )\r
2646     {\r
2647         BaseType_t xReturn;\r
2648 \r
2649         taskENTER_CRITICAL();\r
2650         {\r
2651             if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )\r
2652             {\r
2653                 /* Cannot add a queue/semaphore to more than one queue set. */\r
2654                 xReturn = pdFAIL;\r
2655             }\r
2656             else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )\r
2657             {\r
2658                 /* Cannot add a queue/semaphore to a queue set if there are already\r
2659                  * items in the queue/semaphore. */\r
2660                 xReturn = pdFAIL;\r
2661             }\r
2662             else\r
2663             {\r
2664                 ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;\r
2665                 xReturn = pdPASS;\r
2666             }\r
2667         }\r
2668         taskEXIT_CRITICAL();\r
2669 \r
2670         return xReturn;\r
2671     }\r
2672 \r
2673 #endif /* configUSE_QUEUE_SETS */\r
2674 /*-----------------------------------------------------------*/\r
2675 \r
2676 #if ( configUSE_QUEUE_SETS == 1 )\r
2677 \r
2678     BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore,\r
2679                                     QueueSetHandle_t xQueueSet )\r
2680     {\r
2681         BaseType_t xReturn;\r
2682         Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;\r
2683 \r
2684         if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )\r
2685         {\r
2686             /* The queue was not a member of the set. */\r
2687             xReturn = pdFAIL;\r
2688         }\r
2689         else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )\r
2690         {\r
2691             /* It is dangerous to remove a queue from a set when the queue is\r
2692              * not empty because the queue set will still hold pending events for\r
2693              * the queue. */\r
2694             xReturn = pdFAIL;\r
2695         }\r
2696         else\r
2697         {\r
2698             taskENTER_CRITICAL();\r
2699             {\r
2700                 /* The queue is no longer contained in the set. */\r
2701                 pxQueueOrSemaphore->pxQueueSetContainer = NULL;\r
2702             }\r
2703             taskEXIT_CRITICAL();\r
2704             xReturn = pdPASS;\r
2705         }\r
2706 \r
2707         return xReturn;\r
2708     } /*lint !e818 xQueueSet could not be declared as pointing to const as it is a typedef. */\r
2709 \r
2710 #endif /* configUSE_QUEUE_SETS */\r
2711 /*-----------------------------------------------------------*/\r
2712 \r
2713 #if ( configUSE_QUEUE_SETS == 1 )\r
2714 \r
2715     QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet,\r
2716                                                 TickType_t const xTicksToWait )\r
2717     {\r
2718         QueueSetMemberHandle_t xReturn = NULL;\r
2719 \r
2720         ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait ); /*lint !e961 Casting from one typedef to another is not redundant. */\r
2721         return xReturn;\r
2722     }\r
2723 \r
2724 #endif /* configUSE_QUEUE_SETS */\r
2725 /*-----------------------------------------------------------*/\r
2726 \r
2727 #if ( configUSE_QUEUE_SETS == 1 )\r
2728 \r
2729     QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )\r
2730     {\r
2731         QueueSetMemberHandle_t xReturn = NULL;\r
2732 \r
2733         ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */\r
2734         return xReturn;\r
2735     }\r
2736 \r
2737 #endif /* configUSE_QUEUE_SETS */\r
2738 /*-----------------------------------------------------------*/\r
2739 \r
2740 #if ( configUSE_QUEUE_SETS == 1 )\r
2741 \r
2742     static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue )\r
2743     {\r
2744         Queue_t * pxQueueSetContainer = pxQueue->pxQueueSetContainer;\r
2745         BaseType_t xReturn = pdFALSE;\r
2746 \r
2747         /* This function must be called form a critical section. */\r
2748 \r
2749         /* The following line is not reachable in unit tests because every call\r
2750          * to prvNotifyQueueSetContainer is preceded by a check that\r
2751          * pxQueueSetContainer != NULL */\r
2752         configASSERT( pxQueueSetContainer ); /* LCOV_EXCL_BR_LINE */\r
2753         configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );\r
2754 \r
2755         if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )\r
2756         {\r
2757             const int8_t cTxLock = pxQueueSetContainer->cTxLock;\r
2758 \r
2759             traceQUEUE_SET_SEND( pxQueueSetContainer );\r
2760 \r
2761             /* The data copied is the handle of the queue that contains data. */\r
2762             xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, queueSEND_TO_BACK );\r
2763 \r
2764             if( cTxLock == queueUNLOCKED )\r
2765             {\r
2766                 if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )\r
2767                 {\r
2768                     if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )\r
2769                     {\r
2770                         /* The task waiting has a higher priority. */\r
2771                         xReturn = pdTRUE;\r
2772                     }\r
2773                     else\r
2774                     {\r
2775                         mtCOVERAGE_TEST_MARKER();\r
2776                     }\r
2777                 }\r
2778                 else\r
2779                 {\r
2780                     mtCOVERAGE_TEST_MARKER();\r
2781                 }\r
2782             }\r
2783             else\r
2784             {\r
2785                 prvIncrementQueueTxLock( pxQueueSetContainer, cTxLock );\r
2786             }\r
2787         }\r
2788         else\r
2789         {\r
2790             mtCOVERAGE_TEST_MARKER();\r
2791         }\r
2792 \r
2793         return xReturn;\r
2794     }\r
2795 \r
2796 #endif /* configUSE_QUEUE_SETS */\r