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