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