]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/queue.c
Fix bug when xQueueOverwrite() and xQueueOverwrite() from ISR are used to overwrite...
[freertos] / FreeRTOS / Source / queue.c
index 4bbddb1ec995e496c2f031f0fb13b8c3f55402c5..103718dba86addb6d240b17a2a96dc7e95c4f4ea 100644 (file)
@@ -1,66 +1,29 @@
 /*\r
-    FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
-\r
-    VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
-\r
-    ***************************************************************************\r
-     *                                                                       *\r
-     *    FreeRTOS provides completely free yet professionally developed,    *\r
-     *    robust, strictly quality controlled, supported, and cross          *\r
-     *    platform software that has become a de facto standard.             *\r
-     *                                                                       *\r
-     *    Help yourself get started quickly and support the FreeRTOS         *\r
-     *    project by purchasing a FreeRTOS tutorial book, reference          *\r
-     *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
-     *                                                                       *\r
-     *    Thank you!                                                         *\r
-     *                                                                       *\r
-    ***************************************************************************\r
-\r
-    This file is part of the FreeRTOS distribution.\r
-\r
-    FreeRTOS is free software; you can redistribute it and/or modify it under\r
-    the terms of the GNU General Public License (version 2) as published by the\r
-    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
-\r
-    >>! NOTE: The modification to the GPL is included to allow you to distribute\r
-    >>! a combined work that includes FreeRTOS without being obliged to provide\r
-    >>! the source code for proprietary components outside of the FreeRTOS\r
-    >>! kernel.\r
-\r
-    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
-    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
-    FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
-    link: http://www.freertos.org/a00114.html\r
-\r
-    1 tab == 4 spaces!\r
-\r
-    ***************************************************************************\r
-     *                                                                       *\r
-     *    Having a problem?  Start by reading the FAQ "My application does   *\r
-     *    not run, what could be wrong?"                                     *\r
-     *                                                                       *\r
-     *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
-     *                                                                       *\r
-    ***************************************************************************\r
-\r
-    http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
-    license and Real Time Engineers Ltd. contact details.\r
-\r
-    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
-    including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
-    compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
-\r
-    http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
-    Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
-    licenses offer ticketed support, indemnification and middleware.\r
-\r
-    http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
-    engineered and independently SIL3 certified version for use in safety and\r
-    mission critical applications that require provable dependability.\r
-\r
-    1 tab == 4 spaces!\r
-*/\r
+ * FreeRTOS Kernel V10.2.1\r
+ * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ * http://www.FreeRTOS.org\r
+ * http://aws.amazon.com/freertos\r
+ *\r
+ * 1 tab == 4 spaces!\r
+ */\r
 \r
 #include <stdlib.h>\r
 #include <string.h>\r
@@ -78,75 +41,99 @@ task.h is included from an application file. */
        #include "croutine.h"\r
 #endif\r
 \r
-/* Lint e961 and e750 are suppressed as a MISRA exception justified because the\r
-MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the\r
-header files above, but not in this file, in order to generate the correct\r
-privileged Vs unprivileged linkage and placement. */\r
-#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */\r
+/* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified\r
+because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined\r
+for the header files above, but not in this file, in order to generate the\r
+correct privileged Vs unprivileged linkage and placement. */\r
+#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */\r
 \r
 \r
-/* Constants used with the cRxLock and xTxLock structure members. */\r
-#define queueUNLOCKED                                  ( ( signed portBASE_TYPE ) -1 )\r
-#define queueLOCKED_UNMODIFIED                 ( ( signed portBASE_TYPE ) 0 )\r
+/* Constants used with the cRxLock and cTxLock structure members. */\r
+#define queueUNLOCKED                                  ( ( int8_t ) -1 )\r
+#define queueLOCKED_UNMODIFIED                 ( ( int8_t ) 0 )\r
 \r
-/* When the xQUEUE structure is used to represent a base queue its pcHead and\r
+/* When the Queue_t structure is used to represent a base queue its pcHead and\r
 pcTail members are used as pointers into the queue storage area.  When the\r
-xQUEUE structure is used to represent a mutex pcHead and pcTail pointers are\r
+Queue_t structure is used to represent a mutex pcHead and pcTail pointers are\r
 not necessary, and the pcHead pointer is set to NULL to indicate that the\r
-pcTail pointer actually points to the mutex holder (if any).  Map alternative\r
-names to the pcHead and pcTail structure members to ensure the readability of\r
-the code is maintained despite this dual use of two structure members.  An\r
-alternative implementation would be to use a union, but use of a union is\r
-against the coding standard (although an exception to the standard has been\r
-permitted where the dual use also significantly changes the type of the\r
-structure member). */\r
-#define pxMutexHolder                                  pcTail\r
+structure instead holds a pointer to the mutex holder (if any).  Map alternative\r
+names to the pcHead and structure member to ensure the readability of the code\r
+is maintained.  The QueuePointers_t and SemaphoreData_t types are used to form\r
+a union as their usage is mutually exclusive dependent on what the queue is\r
+being used for. */\r
 #define uxQueueType                                            pcHead\r
 #define queueQUEUE_IS_MUTEX                            NULL\r
 \r
+typedef struct QueuePointers\r
+{\r
+       int8_t *pcTail;                                 /*< Points to the byte at the end of the queue storage area.  Once more byte is allocated than necessary to store the queue items, this is used as a marker. */\r
+       int8_t *pcReadFrom;                             /*< Points to the last place that a queued item was read from when the structure is used as a queue. */\r
+} QueuePointers_t;\r
+\r
+typedef struct SemaphoreData\r
+{\r
+       TaskHandle_t xMutexHolder;               /*< The handle of the task that holds the mutex. */\r
+       UBaseType_t uxRecursiveCallCount;/*< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */\r
+} SemaphoreData_t;\r
+\r
 /* Semaphores do not actually store or copy data, so have an item size of\r
 zero. */\r
-#define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )\r
-#define queueMUTEX_GIVE_BLOCK_TIME              ( ( portTickType ) 0U )\r
-\r
+#define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( UBaseType_t ) 0 )\r
+#define queueMUTEX_GIVE_BLOCK_TIME              ( ( TickType_t ) 0U )\r
+\r
+#if( configUSE_PREEMPTION == 0 )\r
+       /* If the cooperative scheduler is being used then a yield should not be\r
+       performed just because a higher priority task has been woken. */\r
+       #define queueYIELD_IF_USING_PREEMPTION()\r
+#else\r
+       #define queueYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()\r
+#endif\r
 \r
 /*\r
  * Definition of the queue used by the scheduler.\r
- * Items are queued by copy, not reference.\r
+ * Items are queued by copy, not reference.  See the following link for the\r
+ * rationale: https://www.freertos.org/Embedded-RTOS-Queues.html\r
  */\r
-typedef struct QueueDefinition\r
+typedef struct QueueDefinition                 /* The old naming convention is used to prevent breaking kernel aware debuggers. */\r
 {\r
-       signed char *pcHead;                                    /*< Points to the beginning of the queue storage area. */\r
-       signed char *pcTail;                                    /*< Points to the byte at the end of the queue storage area.  Once more byte is allocated than necessary to store the queue items, this is used as a marker. */\r
+       int8_t *pcHead;                                 /*< Points to the beginning of the queue storage area. */\r
+       int8_t *pcWriteTo;                              /*< Points to the free next place in the storage area. */\r
 \r
-       signed char *pcWriteTo;                                 /*< Points to the free next place in the storage area. */\r
-\r
-       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). */\r
+       union\r
        {\r
-               signed char *pcReadFrom;                        /*< Points to the last place that a queued item was read from when the structure is used as a queue. */\r
-               unsigned portBASE_TYPE uxRecursiveCallCount;/*< Maintains a count of the numebr of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */\r
+               QueuePointers_t xQueue;         /*< Data required exclusively when this structure is used as a queue. */\r
+               SemaphoreData_t xSemaphore; /*< Data required exclusively when this structure is used as a semaphore. */\r
        } u;\r
 \r
-       xList xTasksWaitingToSend;                              /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */\r
-       xList xTasksWaitingToReceive;                   /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */\r
+       List_t xTasksWaitingToSend;             /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */\r
+       List_t xTasksWaitingToReceive;  /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */\r
 \r
-       volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */\r
-       unsigned portBASE_TYPE uxLength;                /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */\r
-       unsigned portBASE_TYPE uxItemSize;              /*< The size of each items that the queue will hold. */\r
+       volatile UBaseType_t uxMessagesWaiting;/*< The number of items currently in the queue. */\r
+       UBaseType_t uxLength;                   /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */\r
+       UBaseType_t uxItemSize;                 /*< The size of each items that the queue will hold. */\r
 \r
-       volatile signed portBASE_TYPE xRxLock;  /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */\r
-       volatile signed portBASE_TYPE xTxLock;  /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */\r
+       volatile int8_t cRxLock;                /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */\r
+       volatile int8_t cTxLock;                /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */\r
 \r
-       #if ( configUSE_TRACE_FACILITY == 1 )\r
-               unsigned char ucQueueNumber;\r
-               unsigned char ucQueueType;\r
+       #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
+               uint8_t ucStaticallyAllocated;  /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */\r
        #endif\r
 \r
        #if ( configUSE_QUEUE_SETS == 1 )\r
                struct QueueDefinition *pxQueueSetContainer;\r
        #endif\r
 \r
+       #if ( configUSE_TRACE_FACILITY == 1 )\r
+               UBaseType_t uxQueueNumber;\r
+               uint8_t ucQueueType;\r
+       #endif\r
+\r
 } xQUEUE;\r
+\r
+/* The old xQUEUE name is maintained above then typedefed to the new Queue_t\r
+name below to enable the use of older kernel aware debuggers. */\r
+typedef xQUEUE Queue_t;\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 /*\r
@@ -160,14 +147,19 @@ typedef struct QueueDefinition
        more user friendly. */\r
        typedef struct QUEUE_REGISTRY_ITEM\r
        {\r
-               signed char *pcQueueName;\r
-               xQueueHandle xHandle;\r
+               const char *pcQueueName; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+               QueueHandle_t xHandle;\r
        } xQueueRegistryItem;\r
 \r
-       /* The queue registry is simply an array of xQueueRegistryItem structures.\r
+       /* The old xQueueRegistryItem name is maintained above then typedefed to the\r
+       new xQueueRegistryItem name below to enable the use of older kernel aware\r
+       debuggers. */\r
+       typedef xQueueRegistryItem QueueRegistryItem_t;\r
+\r
+       /* The queue registry is simply an array of QueueRegistryItem_t structures.\r
        The pcQueueName member of a structure being NULL is indicative of the\r
        array position being vacant. */\r
-       xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];\r
+       PRIVILEGED_DATA QueueRegistryItem_t xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];\r
 \r
 #endif /* configQUEUE_REGISTRY_SIZE */\r
 \r
@@ -179,41 +171,66 @@ typedef struct QueueDefinition
  * to indicate that a task may require unblocking.  When the queue in unlocked\r
  * these lock counts are inspected, and the appropriate action taken.\r
  */\r
-static void prvUnlockQueue( xQUEUE *pxQueue ) PRIVILEGED_FUNCTION;\r
+static void prvUnlockQueue( Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
  * Uses a critical section to determine if there is any data in a queue.\r
  *\r
  * @return pdTRUE if the queue contains no items, otherwise pdFALSE.\r
  */\r
-static signed portBASE_TYPE prvIsQueueEmpty( const xQUEUE *pxQueue ) PRIVILEGED_FUNCTION;\r
+static BaseType_t prvIsQueueEmpty( const Queue_t *pxQueue ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
  * Uses a critical section to determine if there is any space in a queue.\r
  *\r
  * @return pdTRUE if there is no space, otherwise pdFALSE;\r
  */\r
-static signed portBASE_TYPE prvIsQueueFull( const xQUEUE *pxQueue ) PRIVILEGED_FUNCTION;\r
+static BaseType_t prvIsQueueFull( const Queue_t *pxQueue ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
  * Copies an item into the queue, either at the front of the queue or the\r
  * back of the queue.\r
  */\r
-static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;\r
+static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue, const void *pvItemToQueue, const BaseType_t xPosition ) PRIVILEGED_FUNCTION;\r
 \r
 /*\r
  * Copies an item out of a queue.\r
  */\r
-static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void * const pvBuffer ) PRIVILEGED_FUNCTION;\r
+static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer ) PRIVILEGED_FUNCTION;\r
 \r
 #if ( configUSE_QUEUE_SETS == 1 )\r
        /*\r
         * Checks to see if a queue is a member of a queue set, and if so, notifies\r
         * the queue set that the queue contains data.\r
         */\r
-       static portBASE_TYPE prvNotifyQueueSetContainer( const xQUEUE * const pxQueue, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;\r
+       static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;\r
+#endif\r
+\r
+/*\r
+ * Called after a Queue_t structure has been allocated either statically or\r
+ * dynamically to fill in the structure's members.\r
+ */\r
+static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, const uint8_t ucQueueType, Queue_t *pxNewQueue ) PRIVILEGED_FUNCTION;\r
+\r
+/*\r
+ * Mutexes are a special type of queue.  When a mutex is created, first the\r
+ * queue is created, then prvInitialiseMutex() is called to configure the queue\r
+ * as a mutex.\r
+ */\r
+#if( configUSE_MUTEXES == 1 )\r
+       static void prvInitialiseMutex( Queue_t *pxNewQueue ) PRIVILEGED_FUNCTION;\r
 #endif\r
 \r
+#if( configUSE_MUTEXES == 1 )\r
+       /*\r
+        * If a task waiting for a mutex causes the mutex holder to inherit a\r
+        * priority, but the waiting task times out, then the holder should\r
+        * disinherit the priority - but only down to the highest priority of any\r
+        * other tasks that are waiting for the same mutex.  This function returns\r
+        * that priority.\r
+        */\r
+       static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue ) PRIVILEGED_FUNCTION;\r
+#endif\r
 /*-----------------------------------------------------------*/\r
 \r
 /*\r
@@ -223,46 +240,54 @@ static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void * const pvB
 #define prvLockQueue( pxQueue )                                                                \\r
        taskENTER_CRITICAL();                                                                   \\r
        {                                                                                                               \\r
-               if( ( pxQueue )->xRxLock == queueUNLOCKED )                     \\r
+               if( ( pxQueue )->cRxLock == queueUNLOCKED )                     \\r
                {                                                                                                       \\r
-                       ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED;  \\r
+                       ( pxQueue )->cRxLock = queueLOCKED_UNMODIFIED;  \\r
                }                                                                                                       \\r
-               if( ( pxQueue )->xTxLock == queueUNLOCKED )                     \\r
+               if( ( pxQueue )->cTxLock == queueUNLOCKED )                     \\r
                {                                                                                                       \\r
-                       ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED;  \\r
+                       ( pxQueue )->cTxLock = queueLOCKED_UNMODIFIED;  \\r
                }                                                                                                       \\r
        }                                                                                                               \\r
        taskEXIT_CRITICAL()\r
 /*-----------------------------------------------------------*/\r
 \r
-portBASE_TYPE xQueueGenericReset( xQueueHandle xQueue, portBASE_TYPE xNewQueue )\r
+BaseType_t xQueueGenericReset( QueueHandle_t xQueue, BaseType_t xNewQueue )\r
 {\r
-xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
        configASSERT( pxQueue );\r
 \r
        taskENTER_CRITICAL();\r
        {\r
-               pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );\r
-               pxQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;\r
+               pxQueue->u.xQueue.pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */\r
+               pxQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;\r
                pxQueue->pcWriteTo = pxQueue->pcHead;\r
-               pxQueue->u.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );\r
-               pxQueue->xRxLock = queueUNLOCKED;\r
-               pxQueue->xTxLock = queueUNLOCKED;\r
+               pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - 1U ) * pxQueue->uxItemSize ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */\r
+               pxQueue->cRxLock = queueUNLOCKED;\r
+               pxQueue->cTxLock = queueUNLOCKED;\r
 \r
                if( xNewQueue == pdFALSE )\r
                {\r
                        /* If there are tasks blocked waiting to read from the queue, then\r
                        the tasks will remain blocked as after this function exits the queue\r
-                       will still be empty.  If there are tasks blocked waiting to     write to\r
+                       will still be empty.  If there are tasks blocked waiting to write to\r
                        the queue, then one should be unblocked as after this function exits\r
                        it will be possible to write to it. */\r
                        if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
                        {\r
-                               if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )\r
+                               if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
                                {\r
-                                       portYIELD_WITHIN_API();\r
+                                       queueYIELD_IF_USING_PREEMPTION();\r
                                }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
                        }\r
                }\r
                else\r
@@ -280,124 +305,223 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType )\r
-{\r
-xQUEUE *pxNewQueue;\r
-size_t xQueueSizeInBytes;\r
-xQueueHandle xReturn = NULL;\r
-\r
-       /* Remove compiler warnings about unused parameters should\r
-       configUSE_TRACE_FACILITY not be set to 1. */\r
-       ( void ) ucQueueType;\r
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
 \r
-       /* Allocate the new queue structure. */\r
-       if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )\r
+       QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, StaticQueue_t *pxStaticQueue, const uint8_t ucQueueType )\r
        {\r
-               pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
-               if( pxNewQueue != NULL )\r
+       Queue_t *pxNewQueue;\r
+\r
+               configASSERT( uxQueueLength > ( UBaseType_t ) 0 );\r
+\r
+               /* The StaticQueue_t structure and the queue storage area must be\r
+               supplied. */\r
+               configASSERT( pxStaticQueue != NULL );\r
+\r
+               /* A queue storage area should be provided if the item size is not 0, and\r
+               should not be provided if the item size is 0. */\r
+               configASSERT( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) );\r
+               configASSERT( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) );\r
+\r
+               #if( configASSERT_DEFINED == 1 )\r
                {\r
-                       /* Create the list of pointers to queue items.  The queue is one byte\r
-                       longer than asked for to make wrap checking easier/faster. */\r
-                       xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+                       /* Sanity check that the size of the structure used to declare a\r
+                       variable of type StaticQueue_t or StaticSemaphore_t equals the size of\r
+                       the real queue and semaphore structures. */\r
+                       volatile size_t xSize = sizeof( StaticQueue_t );\r
+                       configASSERT( xSize == sizeof( Queue_t ) );\r
+                       ( void ) xSize; /* Keeps lint quiet when configASSERT() is not defined. */\r
+               }\r
+               #endif /* configASSERT_DEFINED */\r
+\r
+               /* The address of a statically allocated queue was passed in, use it.\r
+               The address of a statically allocated storage area was also passed in\r
+               but is already set. */\r
+               pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */\r
 \r
-                       pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );\r
-                       if( pxNewQueue->pcHead != NULL )\r
+               if( pxNewQueue != NULL )\r
+               {\r
+                       #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
                        {\r
-                               /* Initialise the queue members as described above where the\r
-                               queue type is defined. */\r
-                               pxNewQueue->uxLength = uxQueueLength;\r
-                               pxNewQueue->uxItemSize = uxItemSize;\r
-                               ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );\r
+                               /* Queues can be allocated wither statically or dynamically, so\r
+                               note this queue was allocated statically in case the queue is\r
+                               later deleted. */\r
+                               pxNewQueue->ucStaticallyAllocated = pdTRUE;\r
+                       }\r
+                       #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
 \r
-                               #if ( configUSE_TRACE_FACILITY == 1 )\r
-                               {\r
-                                       pxNewQueue->ucQueueType = ucQueueType;\r
-                               }\r
-                               #endif /* configUSE_TRACE_FACILITY */\r
+                       prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );\r
+               }\r
+               else\r
+               {\r
+                       traceQUEUE_CREATE_FAILED( ucQueueType );\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
 \r
-                               #if( configUSE_QUEUE_SETS == 1 )\r
-                               {\r
-                                       pxNewQueue->pxQueueSetContainer = NULL;\r
-                               }\r
-                               #endif /* configUSE_QUEUE_SETS */\r
+               return pxNewQueue;\r
+       }\r
 \r
-                               traceQUEUE_CREATE( pxNewQueue );\r
-                               xReturn = pxNewQueue;\r
-                       }\r
-                       else\r
+#endif /* configSUPPORT_STATIC_ALLOCATION */\r
+/*-----------------------------------------------------------*/\r
+\r
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
+\r
+       QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType )\r
+       {\r
+       Queue_t *pxNewQueue;\r
+       size_t xQueueSizeInBytes;\r
+       uint8_t *pucQueueStorage;\r
+\r
+               configASSERT( uxQueueLength > ( UBaseType_t ) 0 );\r
+\r
+               /* Allocate enough space to hold the maximum number of items that\r
+               can be in the queue at any time.  It is valid for uxItemSize to be\r
+               zero in the case the queue is used as a semaphore. */\r
+               xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
+\r
+               /* Allocate the queue and storage area.  Justification for MISRA\r
+               deviation as follows:  pvPortMalloc() always ensures returned memory\r
+               blocks are aligned per the requirements of the MCU stack.  In this case\r
+               pvPortMalloc() must return a pointer that is guaranteed to meet the\r
+               alignment requirements of the Queue_t structure - which in this case\r
+               is an int8_t *.  Therefore, whenever the stack alignment requirements\r
+               are greater than or equal to the pointer to char requirements the cast\r
+               is safe.  In other cases alignment requirements are not strict (one or\r
+               two bytes). */\r
+               pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes ); /*lint !e9087 !e9079 see comment above. */\r
+\r
+               if( pxNewQueue != NULL )\r
+               {\r
+                       /* Jump past the queue structure to find the location of the queue\r
+                       storage area. */\r
+                       pucQueueStorage = ( uint8_t * ) pxNewQueue;\r
+                       pucQueueStorage += sizeof( Queue_t ); /*lint !e9016 Pointer arithmetic allowed on char types, especially when it assists conveying intent. */\r
+\r
+                       #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
                        {\r
-                               traceQUEUE_CREATE_FAILED( ucQueueType );\r
-                               vPortFree( pxNewQueue );\r
+                               /* Queues can be created either statically or dynamically, so\r
+                               note this task was created dynamically in case it is later\r
+                               deleted. */\r
+                               pxNewQueue->ucStaticallyAllocated = pdFALSE;\r
                        }\r
+                       #endif /* configSUPPORT_STATIC_ALLOCATION */\r
+\r
+                       prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );\r
+               }\r
+               else\r
+               {\r
+                       traceQUEUE_CREATE_FAILED( ucQueueType );\r
+                       mtCOVERAGE_TEST_MARKER();\r
                }\r
-       }\r
 \r
-       configASSERT( xReturn );\r
+               return pxNewQueue;\r
+       }\r
 \r
-       return xReturn;\r
-}\r
+#endif /* configSUPPORT_STATIC_ALLOCATION */\r
 /*-----------------------------------------------------------*/\r
 \r
-#if ( configUSE_MUTEXES == 1 )\r
+static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, const uint8_t ucQueueType, Queue_t *pxNewQueue )\r
+{\r
+       /* Remove compiler warnings about unused parameters should\r
+       configUSE_TRACE_FACILITY not be set to 1. */\r
+       ( void ) ucQueueType;\r
 \r
-       xQueueHandle xQueueCreateMutex( unsigned char ucQueueType )\r
+       if( uxItemSize == ( UBaseType_t ) 0 )\r
+       {\r
+               /* No RAM was allocated for the queue storage area, but PC head cannot\r
+               be set to NULL because NULL is used as a key to say the queue is used as\r
+               a mutex.  Therefore just set pcHead to point to the queue as a benign\r
+               value that is known to be within the memory map. */\r
+               pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;\r
+       }\r
+       else\r
        {\r
-       xQUEUE *pxNewQueue;\r
+               /* Set the head to the start of the queue storage area. */\r
+               pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;\r
+       }\r
 \r
-               /* Prevent compiler warnings about unused parameters if\r
-               configUSE_TRACE_FACILITY does not equal 1. */\r
-               ( void ) ucQueueType;\r
+       /* Initialise the queue members as described where the queue type is\r
+       defined. */\r
+       pxNewQueue->uxLength = uxQueueLength;\r
+       pxNewQueue->uxItemSize = uxItemSize;\r
+       ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );\r
 \r
-               /* Allocate the new queue structure. */\r
-               pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );\r
-               if( pxNewQueue != NULL )\r
-               {\r
-                       /* Information required for priority inheritance. */\r
-                       pxNewQueue->pxMutexHolder = NULL;\r
-                       pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;\r
+       #if ( configUSE_TRACE_FACILITY == 1 )\r
+       {\r
+               pxNewQueue->ucQueueType = ucQueueType;\r
+       }\r
+       #endif /* configUSE_TRACE_FACILITY */\r
 \r
-                       /* Queues used as a mutex no data is actually copied into or out\r
-                       of the queue. */\r
-                       pxNewQueue->pcWriteTo = NULL;\r
-                       pxNewQueue->u.pcReadFrom = NULL;\r
+       #if( configUSE_QUEUE_SETS == 1 )\r
+       {\r
+               pxNewQueue->pxQueueSetContainer = NULL;\r
+       }\r
+       #endif /* configUSE_QUEUE_SETS */\r
 \r
-                       /* Each mutex has a length of 1 (like a binary semaphore) and\r
-                       an item size of 0 as nothing is actually copied into or out\r
-                       of the mutex. */\r
-                       pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;\r
-                       pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;\r
-                       pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;\r
-                       pxNewQueue->xRxLock = queueUNLOCKED;\r
-                       pxNewQueue->xTxLock = queueUNLOCKED;\r
+       traceQUEUE_CREATE( pxNewQueue );\r
+}\r
+/*-----------------------------------------------------------*/\r
 \r
-                       #if ( configUSE_TRACE_FACILITY == 1 )\r
-                       {\r
-                               pxNewQueue->ucQueueType = ucQueueType;\r
-                       }\r
-                       #endif\r
+#if( configUSE_MUTEXES == 1 )\r
 \r
-                       #if ( configUSE_QUEUE_SETS == 1 )\r
-                       {\r
-                               pxNewQueue->pxQueueSetContainer = NULL;\r
-                       }\r
-                       #endif\r
+       static void prvInitialiseMutex( Queue_t *pxNewQueue )\r
+       {\r
+               if( pxNewQueue != NULL )\r
+               {\r
+                       /* The queue create function will set all the queue structure members\r
+                       correctly for a generic queue, but this function is creating a\r
+                       mutex.  Overwrite those members that need to be set differently -\r
+                       in particular the information required for priority inheritance. */\r
+                       pxNewQueue->u.xSemaphore.xMutexHolder = NULL;\r
+                       pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;\r
 \r
-                       /* Ensure the event queues start with the correct state. */\r
-                       vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );\r
-                       vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );\r
+                       /* In case this is a recursive mutex. */\r
+                       pxNewQueue->u.xSemaphore.uxRecursiveCallCount = 0;\r
 \r
                        traceCREATE_MUTEX( pxNewQueue );\r
 \r
                        /* Start with the semaphore in the expected state. */\r
-                       ( void ) xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );\r
+                       ( void ) xQueueGenericSend( pxNewQueue, NULL, ( TickType_t ) 0U, queueSEND_TO_BACK );\r
                }\r
                else\r
                {\r
                        traceCREATE_MUTEX_FAILED();\r
                }\r
+       }\r
 \r
-               configASSERT( pxNewQueue );\r
-               return pxNewQueue;\r
+#endif /* configUSE_MUTEXES */\r
+/*-----------------------------------------------------------*/\r
+\r
+#if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
+\r
+       QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )\r
+       {\r
+       QueueHandle_t xNewQueue;\r
+       const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;\r
+\r
+               xNewQueue = xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );\r
+               prvInitialiseMutex( ( Queue_t * ) xNewQueue );\r
+\r
+               return xNewQueue;\r
+       }\r
+\r
+#endif /* configUSE_MUTEXES */\r
+/*-----------------------------------------------------------*/\r
+\r
+#if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
+\r
+       QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType, StaticQueue_t *pxStaticQueue )\r
+       {\r
+       QueueHandle_t xNewQueue;\r
+       const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;\r
+\r
+               /* Prevent compiler warnings about unused parameters if\r
+               configUSE_TRACE_FACILITY does not equal 1. */\r
+               ( void ) ucQueueType;\r
+\r
+               xNewQueue = xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );\r
+               prvInitialiseMutex( ( Queue_t * ) xNewQueue );\r
+\r
+               return xNewQueue;\r
        }\r
 \r
 #endif /* configUSE_MUTEXES */\r
@@ -405,20 +529,21 @@ xQueueHandle xReturn = NULL;
 \r
 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )\r
 \r
-       void* xQueueGetMutexHolder( xQueueHandle xSemaphore )\r
+       TaskHandle_t xQueueGetMutexHolder( QueueHandle_t xSemaphore )\r
        {\r
-       void *pxReturn;\r
+       TaskHandle_t pxReturn;\r
+       Queue_t * const pxSemaphore = ( Queue_t * ) xSemaphore;\r
 \r
                /* This function is called by xSemaphoreGetMutexHolder(), and should not\r
-               be called directly.  Note:  This is is a good way of determining if the\r
+               be called directly.  Note:  This is a good way of determining if the\r
                calling task is the mutex holder, but not a good way of determining the\r
                identity of the mutex holder, as the holder may change between the\r
                following critical section exiting and the function returning. */\r
                taskENTER_CRITICAL();\r
                {\r
-                       if( ( ( xQUEUE * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )\r
+                       if( pxSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )\r
                        {\r
-                               pxReturn = ( void * ) ( ( xQUEUE * ) xSemaphore )->pxMutexHolder;\r
+                               pxReturn = pxSemaphore->u.xSemaphore.xMutexHolder;\r
                        }\r
                        else\r
                        {\r
@@ -428,50 +553,81 @@ xQueueHandle xReturn = NULL;
                taskEXIT_CRITICAL();\r
 \r
                return pxReturn;\r
-       }\r
+       } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */\r
+\r
+#endif\r
+/*-----------------------------------------------------------*/\r
+\r
+#if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )\r
+\r
+       TaskHandle_t xQueueGetMutexHolderFromISR( QueueHandle_t xSemaphore )\r
+       {\r
+       TaskHandle_t pxReturn;\r
+\r
+               configASSERT( xSemaphore );\r
+\r
+               /* Mutexes cannot be used in interrupt service routines, so the mutex\r
+               holder should not change in an ISR, and therefore a critical section is\r
+               not required here. */\r
+               if( ( ( Queue_t * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )\r
+               {\r
+                       pxReturn = ( ( Queue_t * ) xSemaphore )->u.xSemaphore.xMutexHolder;\r
+               }\r
+               else\r
+               {\r
+                       pxReturn = NULL;\r
+               }\r
+\r
+               return pxReturn;\r
+       } /*lint !e818 xSemaphore cannot be a pointer to const because it is a typedef. */\r
 \r
 #endif\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_RECURSIVE_MUTEXES == 1 )\r
 \r
-       portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex )\r
+       BaseType_t xQueueGiveMutexRecursive( QueueHandle_t xMutex )\r
        {\r
-       portBASE_TYPE xReturn;\r
-       xQUEUE * const pxMutex = ( xQUEUE * ) xMutex;\r
+       BaseType_t xReturn;\r
+       Queue_t * const pxMutex = ( Queue_t * ) xMutex;\r
 \r
                configASSERT( pxMutex );\r
 \r
-               /* If this is the task that holds the mutex then pxMutexHolder will not\r
+               /* If this is the task that holds the mutex then xMutexHolder will not\r
                change outside of this task.  If this task does not hold the mutex then\r
                pxMutexHolder can never coincidentally equal the tasks handle, and as\r
                this is the only condition we are interested in it does not matter if\r
                pxMutexHolder is accessed simultaneously by another task.  Therefore no\r
                mutual exclusion is required to test the pxMutexHolder variable. */\r
-               if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Not a redundant cast as xTaskHandle is a typedef. */\r
+               if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )\r
                {\r
                        traceGIVE_MUTEX_RECURSIVE( pxMutex );\r
 \r
-                       /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to\r
+                       /* uxRecursiveCallCount cannot be zero if xMutexHolder is equal to\r
                        the task handle, therefore no underflow check is required.  Also,\r
                        uxRecursiveCallCount is only modified by the mutex holder, and as\r
                        there can only be one, no mutual exclusion is required to modify the\r
                        uxRecursiveCallCount member. */\r
-                       ( pxMutex->u.uxRecursiveCallCount )--;\r
+                       ( pxMutex->u.xSemaphore.uxRecursiveCallCount )--;\r
 \r
-                       /* Have we unwound the call count? */\r
-                       if( pxMutex->u.uxRecursiveCallCount == ( unsigned portBASE_TYPE ) 0 )\r
+                       /* Has the recursive call count unwound to 0? */\r
+                       if( pxMutex->u.xSemaphore.uxRecursiveCallCount == ( UBaseType_t ) 0 )\r
                        {\r
                                /* Return the mutex.  This will automatically unblock any other\r
                                task that might be waiting to access the mutex. */\r
                                ( void ) xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );\r
                        }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
 \r
                        xReturn = pdPASS;\r
                }\r
                else\r
                {\r
-                       /* We cannot give the mutex because we are not the holder. */\r
+                       /* The mutex cannot be given because the calling task is not the\r
+                       holder. */\r
                        xReturn = pdFAIL;\r
 \r
                        traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );\r
@@ -485,10 +641,10 @@ xQueueHandle xReturn = NULL;
 \r
 #if ( configUSE_RECURSIVE_MUTEXES == 1 )\r
 \r
-       portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime )\r
+       BaseType_t xQueueTakeMutexRecursive( QueueHandle_t xMutex, TickType_t xTicksToWait )\r
        {\r
-       portBASE_TYPE xReturn;\r
-       xQUEUE * const pxMutex = ( xQUEUE * ) xMutex;\r
+       BaseType_t xReturn;\r
+       Queue_t * const pxMutex = ( Queue_t * ) xMutex;\r
 \r
                configASSERT( pxMutex );\r
 \r
@@ -497,20 +653,21 @@ xQueueHandle xReturn = NULL;
 \r
                traceTAKE_MUTEX_RECURSIVE( pxMutex );\r
 \r
-               if( pxMutex->pxMutexHolder == ( void * ) xTaskGetCurrentTaskHandle() ) /*lint !e961 Cast is not redundant as xTaskHandle is a typedef. */\r
+               if( pxMutex->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle() )\r
                {\r
-                       ( pxMutex->u.uxRecursiveCallCount )++;\r
+                       ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;\r
                        xReturn = pdPASS;\r
                }\r
                else\r
                {\r
-                       xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );\r
+                       xReturn = xQueueSemaphoreTake( pxMutex, xTicksToWait );\r
 \r
-                       /* pdPASS will only be returned if we successfully obtained the mutex,\r
-                       we may have blocked to reach here. */\r
-                       if( xReturn == pdPASS )\r
+                       /* pdPASS will only be returned if the mutex was successfully\r
+                       obtained.  The calling task may have entered the Blocked state\r
+                       before reaching here. */\r
+                       if( xReturn != pdFAIL )\r
                        {\r
-                               ( pxMutex->u.uxRecursiveCallCount )++;\r
+                               ( pxMutex->u.xSemaphore.uxRecursiveCallCount )++;\r
                        }\r
                        else\r
                        {\r
@@ -524,17 +681,48 @@ xQueueHandle xReturn = NULL;
 #endif /* configUSE_RECURSIVE_MUTEXES */\r
 /*-----------------------------------------------------------*/\r
 \r
-#if ( configUSE_COUNTING_SEMAPHORES == 1 )\r
+#if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
+\r
+       QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount, StaticQueue_t *pxStaticQueue )\r
+       {\r
+       QueueHandle_t xHandle;\r
+\r
+               configASSERT( uxMaxCount != 0 );\r
+               configASSERT( uxInitialCount <= uxMaxCount );\r
+\r
+               xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );\r
+\r
+               if( xHandle != NULL )\r
+               {\r
+                       ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;\r
+\r
+                       traceCREATE_COUNTING_SEMAPHORE();\r
+               }\r
+               else\r
+               {\r
+                       traceCREATE_COUNTING_SEMAPHORE_FAILED();\r
+               }\r
+\r
+               return xHandle;\r
+       }\r
+\r
+#endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */\r
+/*-----------------------------------------------------------*/\r
+\r
+#if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
 \r
-       xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )\r
+       QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount )\r
        {\r
-       xQueueHandle xHandle;\r
+       QueueHandle_t xHandle;\r
+\r
+               configASSERT( uxMaxCount != 0 );\r
+               configASSERT( uxInitialCount <= uxMaxCount );\r
 \r
-               xHandle = xQueueGenericCreate( uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );\r
+               xHandle = xQueueGenericCreate( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );\r
 \r
                if( xHandle != NULL )\r
                {\r
-                       ( ( xQUEUE * ) xHandle )->uxMessagesWaiting = uxInitialCount;\r
+                       ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;\r
 \r
                        traceCREATE_COUNTING_SEMAPHORE();\r
                }\r
@@ -543,49 +731,68 @@ xQueueHandle xReturn = NULL;
                        traceCREATE_COUNTING_SEMAPHORE_FAILED();\r
                }\r
 \r
-               configASSERT( xHandle );\r
                return xHandle;\r
        }\r
 \r
-#endif /* configUSE_COUNTING_SEMAPHORES */\r
+#endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */\r
 /*-----------------------------------------------------------*/\r
 \r
-signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
+BaseType_t xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, const BaseType_t xCopyPosition )\r
 {\r
-signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
-xTimeOutType xTimeOut;\r
-xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+BaseType_t xEntryTimeSet = pdFALSE, xYieldRequired;\r
+TimeOut_t xTimeOut;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
        configASSERT( pxQueue );\r
-       configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
+       configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
        configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );\r
+       #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
+       {\r
+               configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
+       }\r
+       #endif\r
 \r
-       /* This function relaxes the coding standard somewhat to allow return\r
-       statements within the function itself.  This is done in the interest\r
-       of execution time efficiency. */\r
+\r
+       /*lint -save -e904 This function relaxes the coding standard somewhat to\r
+       allow return statements within the function itself.  This is done in the\r
+       interest of execution time efficiency. */\r
        for( ;; )\r
        {\r
                taskENTER_CRITICAL();\r
                {\r
-                       /* Is there room on the queue now?  The running task must be\r
-                       the highest priority task wanting to access the queue.  If\r
-                       the head item in the queue is to be overwritten then it does\r
-                       not matter if the queue is full. */\r
+                       /* Is there room on the queue now?  The running task must be the\r
+                       highest priority task wanting to access the queue.  If the head item\r
+                       in the queue is to be overwritten then it does not matter if the\r
+                       queue is full. */\r
                        if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )\r
                        {\r
                                traceQUEUE_SEND( pxQueue );\r
-                               prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
 \r
                                #if ( configUSE_QUEUE_SETS == 1 )\r
                                {\r
+                               const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;\r
+\r
+                                       xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
+\r
                                        if( pxQueue->pxQueueSetContainer != NULL )\r
                                        {\r
-                                               if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )\r
+                                               if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )\r
+                                               {\r
+                                                       /* Do not notify the queue set as an existing item\r
+                                                       was overwritten in the queue so the number of items\r
+                                                       in the queue has not changed. */\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
+                                               }\r
+                                               else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )\r
                                                {\r
                                                        /* The queue is a member of a queue set, and posting\r
                                                        to the queue set caused a higher priority task to\r
                                                        unblock. A context switch is required. */\r
-                                                       portYIELD_WITHIN_API();\r
+                                                       queueYIELD_IF_USING_PREEMPTION();\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
                                                }\r
                                        }\r
                                        else\r
@@ -594,44 +801,75 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                                                queue then unblock it now. */\r
                                                if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
                                                {\r
-                                                       if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )\r
+                                                       if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
                                                        {\r
                                                                /* The unblocked task has a priority higher than\r
                                                                our own so yield immediately.  Yes it is ok to\r
                                                                do this from within the critical section - the\r
                                                                kernel takes care of that. */\r
-                                                               portYIELD_WITHIN_API();\r
+                                                               queueYIELD_IF_USING_PREEMPTION();\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               mtCOVERAGE_TEST_MARKER();\r
                                                        }\r
                                                }\r
+                                               else if( xYieldRequired != pdFALSE )\r
+                                               {\r
+                                                       /* This path is a special case that will only get\r
+                                                       executed if the task was holding multiple mutexes\r
+                                                       and the mutexes were given back in an order that is\r
+                                                       different to that in which they were taken. */\r
+                                                       queueYIELD_IF_USING_PREEMPTION();\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
+                                               }\r
                                        }\r
                                }\r
                                #else /* configUSE_QUEUE_SETS */\r
                                {\r
+                                       xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
+\r
                                        /* If there was a task waiting for data to arrive on the\r
                                        queue then unblock it now. */\r
                                        if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
                                        {\r
-                                               if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )\r
+                                               if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
                                                {\r
                                                        /* The unblocked task has a priority higher than\r
                                                        our own so yield immediately.  Yes it is ok to do\r
                                                        this from within the critical section - the kernel\r
                                                        takes care of that. */\r
-                                                       portYIELD_WITHIN_API();\r
+                                                       queueYIELD_IF_USING_PREEMPTION();\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
                                                }\r
                                        }\r
+                                       else if( xYieldRequired != pdFALSE )\r
+                                       {\r
+                                               /* This path is a special case that will only get\r
+                                               executed if the task was holding multiple mutexes and\r
+                                               the mutexes were given back in an order that is\r
+                                               different to that in which they were taken. */\r
+                                               queueYIELD_IF_USING_PREEMPTION();\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
                                }\r
                                #endif /* configUSE_QUEUE_SETS */\r
 \r
                                taskEXIT_CRITICAL();\r
-\r
-                               /* Return to the original privilege level before exiting the\r
-                               function. */\r
                                return pdPASS;\r
                        }\r
                        else\r
                        {\r
-                               if( xTicksToWait == ( portTickType ) 0 )\r
+                               if( xTicksToWait == ( TickType_t ) 0 )\r
                                {\r
                                        /* The queue was full and no block time is specified (or\r
                                        the block time has expired) so leave now. */\r
@@ -646,12 +884,13 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                                {\r
                                        /* The queue was full and a block time was specified so\r
                                        configure the timeout structure. */\r
-                                       vTaskSetTimeOutState( &xTimeOut );\r
+                                       vTaskInternalSetTimeOutState( &xTimeOut );\r
                                        xEntryTimeSet = pdTRUE;\r
                                }\r
                                else\r
                                {\r
                                        /* Entry time was already set. */\r
+                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
                        }\r
                }\r
@@ -672,8 +911,8 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                                vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
 \r
                                /* Unlocking the queue means queue events can effect the\r
-                               event list.  It is possible     that interrupts occurring now\r
-                               remove this task from the event list again - but as the\r
+                               event list.  It is possible that interrupts occurring now\r
+                               remove this task from the event list again - but as the\r
                                scheduler is suspended the task will go onto the pending\r
                                ready last instead of the actual ready list. */\r
                                prvUnlockQueue( pxQueue );\r
@@ -701,238 +940,206 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                        prvUnlockQueue( pxQueue );\r
                        ( void ) xTaskResumeAll();\r
 \r
-                       /* Return to the original privilege level before exiting the\r
-                       function. */\r
                        traceQUEUE_SEND_FAILED( pxQueue );\r
                        return errQUEUE_FULL;\r
                }\r
-       }\r
+       } /*lint -restore */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-#if ( configUSE_ALTERNATIVE_API == 1 )\r
+BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t * const pxHigherPriorityTaskWoken, const BaseType_t xCopyPosition )\r
+{\r
+BaseType_t xReturn;\r
+UBaseType_t uxSavedInterruptStatus;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
-       signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )\r
-       {\r
-       signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
-       xTimeOutType xTimeOut;\r
-       xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+       configASSERT( pxQueue );\r
+       configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
+       configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );\r
 \r
-               configASSERT( pxQueue );\r
-               configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
+       /* RTOS ports that support interrupt nesting have the concept of a maximum\r
+       system call (or maximum API call) interrupt priority.  Interrupts that are\r
+       above the maximum system call priority are kept permanently enabled, even\r
+       when the RTOS kernel is in a critical section, but cannot make any calls to\r
+       FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h\r
+       then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
+       failure if a FreeRTOS API function is called from an interrupt that has been\r
+       assigned a priority above the configured maximum system call priority.\r
+       Only FreeRTOS functions that end in FromISR can be called from interrupts\r
+       that have been assigned a priority at or (logically) below the maximum\r
+       system call     interrupt priority.  FreeRTOS maintains a separate interrupt\r
+       safe API to ensure interrupt entry is as fast and as simple as possible.\r
+       More information (albeit Cortex-M specific) is provided on the following\r
+       link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */\r
+       portASSERT_IF_INTERRUPT_PRIORITY_INVALID();\r
 \r
-               for( ;; )\r
+       /* Similar to xQueueGenericSend, except without blocking if there is no room\r
+       in the queue.  Also don't directly wake a task that was blocked on a queue\r
+       read, instead return a flag to say whether a context switch is required or\r
+       not (i.e. has a task with a higher priority than us been woken by this\r
+       post). */\r
+       uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
+       {\r
+               if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )\r
                {\r
-                       taskENTER_CRITICAL();\r
+                       const int8_t cTxLock = pxQueue->cTxLock;\r
+\r
+                       traceQUEUE_SEND_FROM_ISR( pxQueue );\r
+\r
+                       /* The event list is not altered if the queue is locked.  This will\r
+                       be done when the queue is unlocked later. */\r
+                       if( cTxLock == queueUNLOCKED )\r
                        {\r
-                               /* Is there room on the queue now?  To be running we must be\r
-                               the highest priority task wanting to access the queue. */\r
-                               if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )\r
+                               #if ( configUSE_QUEUE_SETS == 1 )\r
                                {\r
-                                       traceQUEUE_SEND( pxQueue );\r
-                                       prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
+                                       const UBaseType_t uxPreviousMessagesWaiting = pxQueue->uxMessagesWaiting;\r
 \r
-                                       /* If there was a task waiting for data to arrive on the\r
-                                       queue then unblock it now. */\r
-                                       if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
+                                       /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a\r
+                                       semaphore or mutex.  That means prvCopyDataToQueue() cannot result\r
+                                       in a task disinheriting a priority and prvCopyDataToQueue() can be\r
+                                       called here even though the disinherit function does not check if\r
+                                       the scheduler is suspended before accessing the ready lists. */\r
+                                       ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
+\r
+                                       if( pxQueue->pxQueueSetContainer != NULL )\r
                                        {\r
-                                               if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )\r
+                                               if( ( xCopyPosition == queueOVERWRITE ) && ( uxPreviousMessagesWaiting != ( UBaseType_t ) 0 ) )\r
                                                {\r
-                                                       /* The unblocked task has a priority higher than\r
-                                                       our own so yield immediately. */\r
-                                                       portYIELD_WITHIN_API();\r
+                                                       /* Do not notify the queue set as an existing item\r
+                                                       was overwritten in the queue so the number of items\r
+                                                       in the queue has not changed. */\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
+                                               }\r
+                                               else if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )\r
+                                               {\r
+                                                       /* The queue is a member of a queue set, and posting\r
+                                                       to the queue set caused a higher priority task to\r
+                                                       unblock.  A context switch is required. */\r
+                                                       if( pxHigherPriorityTaskWoken != NULL )\r
+                                                       {\r
+                                                               *pxHigherPriorityTaskWoken = pdTRUE;\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               mtCOVERAGE_TEST_MARKER();\r
+                                                       }\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
                                                }\r
                                        }\r
-\r
-                                       taskEXIT_CRITICAL();\r
-                                       return pdPASS;\r
-                               }\r
-                               else\r
-                               {\r
-                                       if( xTicksToWait == ( portTickType ) 0 )\r
-                                       {\r
-                                               taskEXIT_CRITICAL();\r
-                                               return errQUEUE_FULL;\r
-                                       }\r
-                                       else if( xEntryTimeSet == pdFALSE )\r
-                                       {\r
-                                               vTaskSetTimeOutState( &xTimeOut );\r
-                                               xEntryTimeSet = pdTRUE;\r
-                                       }\r
-                               }\r
-                       }\r
-                       taskEXIT_CRITICAL();\r
-\r
-                       taskENTER_CRITICAL();\r
-                       {\r
-                               if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
-                               {\r
-                                       if( prvIsQueueFull( pxQueue ) != pdFALSE )\r
-                                       {\r
-                                               traceBLOCKING_ON_QUEUE_SEND( pxQueue );\r
-                                               vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );\r
-                                               portYIELD_WITHIN_API();\r
-                                       }\r
-                               }\r
-                               else\r
-                               {\r
-                                       taskEXIT_CRITICAL();\r
-                                       traceQUEUE_SEND_FAILED( pxQueue );\r
-                                       return errQUEUE_FULL;\r
-                               }\r
-                       }\r
-                       taskEXIT_CRITICAL();\r
-               }\r
-       }\r
-\r
-#endif /* configUSE_ALTERNATIVE_API */\r
-/*-----------------------------------------------------------*/\r
-\r
-#if ( configUSE_ALTERNATIVE_API == 1 )\r
-\r
-       signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle xQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
-       {\r
-       signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
-       xTimeOutType xTimeOut;\r
-       signed char *pcOriginalReadPosition;\r
-       xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
-\r
-               configASSERT( pxQueue );\r
-               configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
-\r
-               for( ;; )\r
-               {\r
-                       taskENTER_CRITICAL();\r
-                       {\r
-                               if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
-                               {\r
-                                       /* Remember our read position in case we are just peeking. */\r
-                                       pcOriginalReadPosition = pxQueue->u.pcReadFrom;\r
-\r
-                                       prvCopyDataFromQueue( pxQueue, pvBuffer );\r
-\r
-                                       if( xJustPeeking == pdFALSE )\r
+                                       else\r
                                        {\r
-                                               traceQUEUE_RECEIVE( pxQueue );\r
-\r
-                                               /* Data is actually being removed (not just peeked). */\r
-                                               --( pxQueue->uxMessagesWaiting );\r
-\r
-                                               #if ( configUSE_MUTEXES == 1 )\r
+                                               if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
                                                {\r
-                                                       if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
+                                                       if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
                                                        {\r
-                                                               /* Record the information required to implement\r
-                                                               priority inheritance should it become necessary. */\r
-                                                               pxQueue->pxMutexHolder = ( signed char * ) xTaskGetCurrentTaskHandle();\r
+                                                               /* The task waiting has a higher priority so\r
+                                                               record that a context switch is required. */\r
+                                                               if( pxHigherPriorityTaskWoken != NULL )\r
+                                                               {\r
+                                                                       *pxHigherPriorityTaskWoken = pdTRUE;\r
+                                                               }\r
+                                                               else\r
+                                                               {\r
+                                                                       mtCOVERAGE_TEST_MARKER();\r
+                                                               }\r
                                                        }\r
-                                               }\r
-                                               #endif\r
-\r
-                                               if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
-                                               {\r
-                                                       if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )\r
+                                                       else\r
                                                        {\r
-                                                               portYIELD_WITHIN_API();\r
+                                                               mtCOVERAGE_TEST_MARKER();\r
                                                        }\r
                                                }\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               traceQUEUE_PEEK( pxQueue );\r
-\r
-                                               /* We are not removing the data, so reset our read\r
-                                               pointer. */\r
-                                               pxQueue->u.pcReadFrom = pcOriginalReadPosition;\r
-\r
-                                               /* The data is being left in the queue, so see if there are\r
-                                               any other tasks waiting for the data. */\r
-                                               if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
+                                               else\r
                                                {\r
-                                                       /* Tasks that are removed from the event list will get added to\r
-                                                       the pending ready list as the scheduler is still suspended. */\r
-                                                       if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
-                                                       {\r
-                                                               /* The task waiting has a higher priority than this task. */\r
-                                                               portYIELD_WITHIN_API();\r
-                                                       }\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
                                                }\r
-\r
                                        }\r
-\r
-                                       taskEXIT_CRITICAL();\r
-                                       return pdPASS;\r
                                }\r
-                               else\r
+                               #else /* configUSE_QUEUE_SETS */\r
                                {\r
-                                       if( xTicksToWait == ( portTickType ) 0 )\r
-                                       {\r
-                                               taskEXIT_CRITICAL();\r
-                                               traceQUEUE_RECEIVE_FAILED( pxQueue );\r
-                                               return errQUEUE_EMPTY;\r
-                                       }\r
-                                       else if( xEntryTimeSet == pdFALSE )\r
-                                       {\r
-                                               vTaskSetTimeOutState( &xTimeOut );\r
-                                               xEntryTimeSet = pdTRUE;\r
-                                       }\r
-                               }\r
-                       }\r
-                       taskEXIT_CRITICAL();\r
+                                       /* Semaphores use xQueueGiveFromISR(), so pxQueue will not be a\r
+                                       semaphore or mutex.  That means prvCopyDataToQueue() cannot result\r
+                                       in a task disinheriting a priority and prvCopyDataToQueue() can be\r
+                                       called here even though the disinherit function does not check if\r
+                                       the scheduler is suspended before accessing the ready lists. */\r
+                                       ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
 \r
-                       taskENTER_CRITICAL();\r
-                       {\r
-                               if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
-                               {\r
-                                       if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
+                                       if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
                                        {\r
-                                               traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
-\r
-                                               #if ( configUSE_MUTEXES == 1 )\r
+                                               if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
                                                {\r
-                                                       if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
+                                                       /* The task waiting has a higher priority so record that a\r
+                                                       context switch is required. */\r
+                                                       if( pxHigherPriorityTaskWoken != NULL )\r
                                                        {\r
-                                                               portENTER_CRITICAL();\r
-                                                               {\r
-                                                                       vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );\r
-                                                               }\r
-                                                               portEXIT_CRITICAL();\r
+                                                               *pxHigherPriorityTaskWoken = pdTRUE;\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               mtCOVERAGE_TEST_MARKER();\r
                                                        }\r
                                                }\r
-                                               #endif\r
-\r
-                                               vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
-                                               portYIELD_WITHIN_API();\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
+                                               }\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
                                        }\r
                                }\r
-                               else\r
-                               {\r
-                                       taskEXIT_CRITICAL();\r
-                                       traceQUEUE_RECEIVE_FAILED( pxQueue );\r
-                                       return errQUEUE_EMPTY;\r
-                               }\r
+                               #endif /* configUSE_QUEUE_SETS */\r
                        }\r
-                       taskEXIT_CRITICAL();\r
+                       else\r
+                       {\r
+                               /* Increment the lock count so the task that unlocks the queue\r
+                               knows that data was posted while it was locked. */\r
+                               pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );\r
+                       }\r
+\r
+                       xReturn = pdPASS;\r
+               }\r
+               else\r
+               {\r
+                       traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );\r
+                       xReturn = errQUEUE_FULL;\r
                }\r
        }\r
+       portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
 \r
-\r
-#endif /* configUSE_ALTERNATIVE_API */\r
+       return xReturn;\r
+}\r
 /*-----------------------------------------------------------*/\r
 \r
-signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle xQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )\r
+BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue, BaseType_t * const pxHigherPriorityTaskWoken )\r
 {\r
-signed portBASE_TYPE xReturn;\r
-unsigned portBASE_TYPE uxSavedInterruptStatus;\r
-xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+BaseType_t xReturn;\r
+UBaseType_t uxSavedInterruptStatus;\r
+Queue_t * const pxQueue = xQueue;\r
+\r
+       /* Similar to xQueueGenericSendFromISR() but used with semaphores where the\r
+       item size is 0.  Don't directly wake a task that was blocked on a queue\r
+       read, instead return a flag to say whether a context switch is required or\r
+       not (i.e. has a task with a higher priority than us been woken by this\r
+       post). */\r
 \r
        configASSERT( pxQueue );\r
-       configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
-       configASSERT( !( ( xCopyPosition == queueOVERWRITE ) && ( pxQueue->uxLength != 1 ) ) );\r
+\r
+       /* xQueueGenericSendFromISR() should be used instead of xQueueGiveFromISR()\r
+       if the item size is not 0. */\r
+       configASSERT( pxQueue->uxItemSize == 0 );\r
+\r
+       /* Normally a mutex would not be given from an interrupt, especially if\r
+       there is a mutex holder, as priority inheritance makes no sense for an\r
+       interrupts, only tasks. */\r
+       configASSERT( !( ( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX ) && ( pxQueue->u.xSemaphore.xMutexHolder != NULL ) ) );\r
 \r
        /* RTOS ports that support interrupt nesting have the concept of a maximum\r
        system call (or maximum API call) interrupt priority.  Interrupts that are\r
-       above the maximum system call priority are keep permanently enabled, even\r
+       above the maximum system call priority are kept permanently enabled, even\r
        when the RTOS kernel is in a critical section, but cannot make any calls to\r
        FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h\r
        then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
@@ -946,36 +1153,52 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
        link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */\r
        portASSERT_IF_INTERRUPT_PRIORITY_INVALID();\r
 \r
-       /* Similar to xQueueGenericSend, except we don't block if there is no room\r
-       in the queue.  Also we don't directly wake a task that was blocked on a\r
-       queue read, instead we return a flag to say whether a context switch is\r
-       required or not (i.e. has a task with a higher priority than us been woken\r
-       by this post). */\r
        uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
        {\r
-               if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )\r
+               const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
+\r
+               /* When the queue is used to implement a semaphore no data is ever\r
+               moved through the queue but it is still valid to see if the queue 'has\r
+               space'. */\r
+               if( uxMessagesWaiting < pxQueue->uxLength )\r
                {\r
+                       const int8_t cTxLock = pxQueue->cTxLock;\r
+\r
                        traceQUEUE_SEND_FROM_ISR( pxQueue );\r
 \r
-                       prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );\r
+                       /* A task can only have an inherited priority if it is a mutex\r
+                       holder - and if there is a mutex holder then the mutex cannot be\r
+                       given from an ISR.  As this is the ISR version of the function it\r
+                       can be assumed there is no mutex holder and no need to determine if\r
+                       priority disinheritance is needed.  Simply increase the count of\r
+                       messages (semaphores) available. */\r
+                       pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;\r
 \r
-                       /* If the queue is locked we do not alter the event list.  This will\r
+                       /* The event list is not altered if the queue is locked.  This will\r
                        be done when the queue is unlocked later. */\r
-                       if( pxQueue->xTxLock == queueUNLOCKED )\r
+                       if( cTxLock == queueUNLOCKED )\r
                        {\r
                                #if ( configUSE_QUEUE_SETS == 1 )\r
                                {\r
                                        if( pxQueue->pxQueueSetContainer != NULL )\r
                                        {\r
-                                               if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )\r
+                                               if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )\r
                                                {\r
-                                                       /* The queue is a member of a queue set, and posting\r
-                                                       to the queue set caused a higher priority task to\r
-                                                       unblock.  A context switch is required. */\r
+                                                       /* The semaphore is a member of a queue set, and\r
+                                                       posting to the queue set caused a higher priority\r
+                                                       task to unblock.  A context switch is required. */\r
                                                        if( pxHigherPriorityTaskWoken != NULL )\r
                                                        {\r
                                                                *pxHigherPriorityTaskWoken = pdTRUE;\r
                                                        }\r
+                                                       else\r
+                                                       {\r
+                                                               mtCOVERAGE_TEST_MARKER();\r
+                                                       }\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
                                                }\r
                                        }\r
                                        else\r
@@ -984,14 +1207,26 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                                                {\r
                                                        if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
                                                        {\r
-                                                               /* The task waiting has a higher priority so record that a\r
-                                                               context switch is required. */\r
+                                                               /* The task waiting has a higher priority so\r
+                                                               record that a context switch is required. */\r
                                                                if( pxHigherPriorityTaskWoken != NULL )\r
                                                                {\r
                                                                        *pxHigherPriorityTaskWoken = pdTRUE;\r
                                                                }\r
+                                                               else\r
+                                                               {\r
+                                                                       mtCOVERAGE_TEST_MARKER();\r
+                                                               }\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               mtCOVERAGE_TEST_MARKER();\r
                                                        }\r
                                                }\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
+                                               }\r
                                        }\r
                                }\r
                                #else /* configUSE_QUEUE_SETS */\r
@@ -1006,8 +1241,20 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                                                        {\r
                                                                *pxHigherPriorityTaskWoken = pdTRUE;\r
                                                        }\r
+                                                       else\r
+                                                       {\r
+                                                               mtCOVERAGE_TEST_MARKER();\r
+                                                       }\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
                                                }\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
                                }\r
                                #endif /* configUSE_QUEUE_SETS */\r
                        }\r
@@ -1015,7 +1262,7 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                        {\r
                                /* Increment the lock count so the task that unlocks the queue\r
                                knows that data was posted while it was locked. */\r
-                               ++( pxQueue->xTxLock );\r
+                               pxQueue->cTxLock = ( int8_t ) ( cTxLock + 1 );\r
                        }\r
 \r
                        xReturn = pdPASS;\r
@@ -1032,80 +1279,428 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-signed portBASE_TYPE xQueueGenericReceive( xQueueHandle xQueue, const void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )\r
+BaseType_t xQueueReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait )\r
 {\r
-signed portBASE_TYPE xEntryTimeSet = pdFALSE;\r
-xTimeOutType xTimeOut;\r
-signed char *pcOriginalReadPosition;\r
-xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+BaseType_t xEntryTimeSet = pdFALSE;\r
+TimeOut_t xTimeOut;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
-       configASSERT( pxQueue );\r
-       configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
+       /* Check the pointer is not NULL. */\r
+       configASSERT( ( pxQueue ) );\r
+\r
+       /* The buffer into which data is received can only be NULL if the data size\r
+       is zero (so no data is copied into the buffer. */\r
+       configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
+\r
+       /* Cannot block if the scheduler is suspended. */\r
+       #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
+       {\r
+               configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
+       }\r
+       #endif\r
+\r
+\r
+       /*lint -save -e904  This function relaxes the coding standard somewhat to\r
+       allow return statements within the function itself.  This is done in the\r
+       interest of execution time efficiency. */\r
+       for( ;; )\r
+       {\r
+               taskENTER_CRITICAL();\r
+               {\r
+                       const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
+\r
+                       /* Is there data in the queue now?  To be running the calling task\r
+                       must be the highest priority task wanting to access the queue. */\r
+                       if( uxMessagesWaiting > ( UBaseType_t ) 0 )\r
+                       {\r
+                               /* Data available, remove one item. */\r
+                               prvCopyDataFromQueue( pxQueue, pvBuffer );\r
+                               traceQUEUE_RECEIVE( pxQueue );\r
+                               pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;\r
+\r
+                               /* There is now space in the queue, were any tasks waiting to\r
+                               post to the queue?  If so, unblock the highest priority waiting\r
+                               task. */\r
+                               if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
+                               {\r
+                                       if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
+                                       {\r
+                                               queueYIELD_IF_USING_PREEMPTION();\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+\r
+                               taskEXIT_CRITICAL();\r
+                               return pdPASS;\r
+                       }\r
+                       else\r
+                       {\r
+                               if( xTicksToWait == ( TickType_t ) 0 )\r
+                               {\r
+                                       /* The queue was empty and no block time is specified (or\r
+                                       the block time has expired) so leave now. */\r
+                                       taskEXIT_CRITICAL();\r
+                                       traceQUEUE_RECEIVE_FAILED( pxQueue );\r
+                                       return errQUEUE_EMPTY;\r
+                               }\r
+                               else if( xEntryTimeSet == pdFALSE )\r
+                               {\r
+                                       /* The queue was empty and a block time was specified so\r
+                                       configure the timeout structure. */\r
+                                       vTaskInternalSetTimeOutState( &xTimeOut );\r
+                                       xEntryTimeSet = pdTRUE;\r
+                               }\r
+                               else\r
+                               {\r
+                                       /* Entry time was already set. */\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+                       }\r
+               }\r
+               taskEXIT_CRITICAL();\r
+\r
+               /* Interrupts and other tasks can send to and receive from the queue\r
+               now the critical section has been exited. */\r
+\r
+               vTaskSuspendAll();\r
+               prvLockQueue( pxQueue );\r
+\r
+               /* Update the timeout state to see if it has expired yet. */\r
+               if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
+               {\r
+                       /* The timeout has not expired.  If the queue is still empty place\r
+                       the task on the list of tasks waiting to receive from the queue. */\r
+                       if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
+                       {\r
+                               traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
+                               vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
+                               prvUnlockQueue( pxQueue );\r
+                               if( xTaskResumeAll() == pdFALSE )\r
+                               {\r
+                                       portYIELD_WITHIN_API();\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               /* The queue contains data again.  Loop back to try and read the\r
+                               data. */\r
+                               prvUnlockQueue( pxQueue );\r
+                               ( void ) xTaskResumeAll();\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       /* Timed out.  If there is no data in the queue exit, otherwise loop\r
+                       back and attempt to read the data. */\r
+                       prvUnlockQueue( pxQueue );\r
+                       ( void ) xTaskResumeAll();\r
+\r
+                       if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
+                       {\r
+                               traceQUEUE_RECEIVE_FAILED( pxQueue );\r
+                               return errQUEUE_EMPTY;\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
+               }\r
+       } /*lint -restore */\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+BaseType_t xQueueSemaphoreTake( QueueHandle_t xQueue, TickType_t xTicksToWait )\r
+{\r
+BaseType_t xEntryTimeSet = pdFALSE;\r
+TimeOut_t xTimeOut;\r
+Queue_t * const pxQueue = xQueue;\r
+\r
+#if( configUSE_MUTEXES == 1 )\r
+       BaseType_t xInheritanceOccurred = pdFALSE;\r
+#endif\r
+\r
+       /* Check the queue pointer is not NULL. */\r
+       configASSERT( ( pxQueue ) );\r
+\r
+       /* Check this really is a semaphore, in which case the item size will be\r
+       0. */\r
+       configASSERT( pxQueue->uxItemSize == 0 );\r
+\r
+       /* Cannot block if the scheduler is suspended. */\r
+       #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
+       {\r
+               configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
+       }\r
+       #endif\r
+\r
+\r
+       /*lint -save -e904 This function relaxes the coding standard somewhat to allow return\r
+       statements within the function itself.  This is done in the interest\r
+       of execution time efficiency. */\r
+       for( ;; )\r
+       {\r
+               taskENTER_CRITICAL();\r
+               {\r
+                       /* Semaphores are queues with an item size of 0, and where the\r
+                       number of messages in the queue is the semaphore's count value. */\r
+                       const UBaseType_t uxSemaphoreCount = pxQueue->uxMessagesWaiting;\r
+\r
+                       /* Is there data in the queue now?  To be running the calling task\r
+                       must be the highest priority task wanting to access the queue. */\r
+                       if( uxSemaphoreCount > ( UBaseType_t ) 0 )\r
+                       {\r
+                               traceQUEUE_RECEIVE( pxQueue );\r
+\r
+                               /* Semaphores are queues with a data size of zero and where the\r
+                               messages waiting is the semaphore's count.  Reduce the count. */\r
+                               pxQueue->uxMessagesWaiting = uxSemaphoreCount - ( UBaseType_t ) 1;\r
+\r
+                               #if ( configUSE_MUTEXES == 1 )\r
+                               {\r
+                                       if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
+                                       {\r
+                                               /* Record the information required to implement\r
+                                               priority inheritance should it become necessary. */\r
+                                               pxQueue->u.xSemaphore.xMutexHolder = pvTaskIncrementMutexHeldCount();\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               #endif /* configUSE_MUTEXES */\r
+\r
+                               /* Check to see if other tasks are blocked waiting to give the\r
+                               semaphore, and if so, unblock the highest priority such task. */\r
+                               if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
+                               {\r
+                                       if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )\r
+                                       {\r
+                                               queueYIELD_IF_USING_PREEMPTION();\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+\r
+                               taskEXIT_CRITICAL();\r
+                               return pdPASS;\r
+                       }\r
+                       else\r
+                       {\r
+                               if( xTicksToWait == ( TickType_t ) 0 )\r
+                               {\r
+                                       /* For inheritance to have occurred there must have been an\r
+                                       initial timeout, and an adjusted timeout cannot become 0, as\r
+                                       if it were 0 the function would have exited. */\r
+                                       #if( configUSE_MUTEXES == 1 )\r
+                                       {\r
+                                               configASSERT( xInheritanceOccurred == pdFALSE );\r
+                                       }\r
+                                       #endif /* configUSE_MUTEXES */\r
+\r
+                                       /* The semaphore count was 0 and no block time is specified\r
+                                       (or the block time has expired) so exit now. */\r
+                                       taskEXIT_CRITICAL();\r
+                                       traceQUEUE_RECEIVE_FAILED( pxQueue );\r
+                                       return errQUEUE_EMPTY;\r
+                               }\r
+                               else if( xEntryTimeSet == pdFALSE )\r
+                               {\r
+                                       /* The semaphore count was 0 and a block time was specified\r
+                                       so configure the timeout structure ready to block. */\r
+                                       vTaskInternalSetTimeOutState( &xTimeOut );\r
+                                       xEntryTimeSet = pdTRUE;\r
+                               }\r
+                               else\r
+                               {\r
+                                       /* Entry time was already set. */\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+                       }\r
+               }\r
+               taskEXIT_CRITICAL();\r
+\r
+               /* Interrupts and other tasks can give to and take from the semaphore\r
+               now the critical section has been exited. */\r
+\r
+               vTaskSuspendAll();\r
+               prvLockQueue( pxQueue );\r
+\r
+               /* Update the timeout state to see if it has expired yet. */\r
+               if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
+               {\r
+                       /* A block time is specified and not expired.  If the semaphore\r
+                       count is 0 then enter the Blocked state to wait for a semaphore to\r
+                       become available.  As semaphores are implemented with queues the\r
+                       queue being empty is equivalent to the semaphore count being 0. */\r
+                       if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
+                       {\r
+                               traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
+\r
+                               #if ( configUSE_MUTEXES == 1 )\r
+                               {\r
+                                       if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
+                                       {\r
+                                               taskENTER_CRITICAL();\r
+                                               {\r
+                                                       xInheritanceOccurred = xTaskPriorityInherit( pxQueue->u.xSemaphore.xMutexHolder );\r
+                                               }\r
+                                               taskEXIT_CRITICAL();\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               #endif\r
+\r
+                               vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
+                               prvUnlockQueue( pxQueue );\r
+                               if( xTaskResumeAll() == pdFALSE )\r
+                               {\r
+                                       portYIELD_WITHIN_API();\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               /* There was no timeout and the semaphore count was not 0, so\r
+                               attempt to take the semaphore again. */\r
+                               prvUnlockQueue( pxQueue );\r
+                               ( void ) xTaskResumeAll();\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       /* Timed out. */\r
+                       prvUnlockQueue( pxQueue );\r
+                       ( void ) xTaskResumeAll();\r
+\r
+                       /* If the semaphore count is 0 exit now as the timeout has\r
+                       expired.  Otherwise return to attempt to take the semaphore that is\r
+                       known to be available.  As semaphores are implemented by queues the\r
+                       queue being empty is equivalent to the semaphore count being 0. */\r
+                       if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
+                       {\r
+                               #if ( configUSE_MUTEXES == 1 )\r
+                               {\r
+                                       /* xInheritanceOccurred could only have be set if\r
+                                       pxQueue->uxQueueType == queueQUEUE_IS_MUTEX so no need to\r
+                                       test the mutex type again to check it is actually a mutex. */\r
+                                       if( xInheritanceOccurred != pdFALSE )\r
+                                       {\r
+                                               taskENTER_CRITICAL();\r
+                                               {\r
+                                                       UBaseType_t uxHighestWaitingPriority;\r
+\r
+                                                       /* This task blocking on the mutex caused another\r
+                                                       task to inherit this task's priority.  Now this task\r
+                                                       has timed out the priority should be disinherited\r
+                                                       again, but only as low as the next highest priority\r
+                                                       task that is waiting for the same mutex. */\r
+                                                       uxHighestWaitingPriority = prvGetDisinheritPriorityAfterTimeout( pxQueue );\r
+                                                       vTaskPriorityDisinheritAfterTimeout( pxQueue->u.xSemaphore.xMutexHolder, uxHighestWaitingPriority );\r
+                                               }\r
+                                               taskEXIT_CRITICAL();\r
+                                       }\r
+                               }\r
+                               #endif /* configUSE_MUTEXES */\r
+\r
+                               traceQUEUE_RECEIVE_FAILED( pxQueue );\r
+                               return errQUEUE_EMPTY;\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
+               }\r
+       } /*lint -restore */\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+BaseType_t xQueuePeek( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait )\r
+{\r
+BaseType_t xEntryTimeSet = pdFALSE;\r
+TimeOut_t xTimeOut;\r
+int8_t *pcOriginalReadPosition;\r
+Queue_t * const pxQueue = xQueue;\r
+\r
+       /* Check the pointer is not NULL. */\r
+       configASSERT( ( pxQueue ) );\r
+\r
+       /* The buffer into which data is received can only be NULL if the data size\r
+       is zero (so no data is copied into the buffer. */\r
+       configASSERT( !( ( ( pvBuffer ) == NULL ) && ( ( pxQueue )->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
+\r
+       /* Cannot block if the scheduler is suspended. */\r
+       #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
+       {\r
+               configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
+       }\r
+       #endif\r
 \r
-       /* This function relaxes the coding standard somewhat to allow return\r
-       statements within the function itself.  This is done in the interest\r
-       of execution time efficiency. */\r
 \r
+       /*lint -save -e904  This function relaxes the coding standard somewhat to\r
+       allow return statements within the function itself.  This is done in the\r
+       interest of execution time efficiency. */\r
        for( ;; )\r
        {\r
                taskENTER_CRITICAL();\r
                {\r
-                       /* Is there data in the queue now?  To be running we must be\r
-                       the highest priority task wanting to access the queue. */\r
-                       if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
+                       const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
+\r
+                       /* Is there data in the queue now?  To be running the calling task\r
+                       must be the highest priority task wanting to access the queue. */\r
+                       if( uxMessagesWaiting > ( UBaseType_t ) 0 )\r
                        {\r
-                               /* Remember the read position in case the queue is only being\r
-                               peeked. */\r
-                               pcOriginalReadPosition = pxQueue->u.pcReadFrom;\r
+                               /* Remember the read position so it can be reset after the data\r
+                               is read from the queue as this function is only peeking the\r
+                               data, not removing it. */\r
+                               pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;\r
 \r
                                prvCopyDataFromQueue( pxQueue, pvBuffer );\r
+                               traceQUEUE_PEEK( pxQueue );\r
 \r
-                               if( xJustPeeking == pdFALSE )\r
-                               {\r
-                                       traceQUEUE_RECEIVE( pxQueue );\r
-\r
-                                       /* Actually removing data, not just peeking. */\r
-                                       --( pxQueue->uxMessagesWaiting );\r
+                               /* The data is not being removed, so reset the read pointer. */\r
+                               pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;\r
 \r
-                                       #if ( configUSE_MUTEXES == 1 )\r
+                               /* The data is being left in the queue, so see if there are\r
+                               any other tasks waiting for the data. */\r
+                               if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
+                               {\r
+                                       if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
                                        {\r
-                                               if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
-                                               {\r
-                                                       /* Record the information required to implement\r
-                                                       priority inheritance should it become necessary. */\r
-                                                       pxQueue->pxMutexHolder = ( signed char * ) xTaskGetCurrentTaskHandle(); /*lint !e961 Cast is not redundant as xTaskHandle is a typedef. */\r
-                                               }\r
+                                               /* The task waiting has a higher priority than this task. */\r
+                                               queueYIELD_IF_USING_PREEMPTION();\r
                                        }\r
-                                       #endif\r
-\r
-                                       if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
+                                       else\r
                                        {\r
-                                               if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )\r
-                                               {\r
-                                                       portYIELD_WITHIN_API();\r
-                                               }\r
+                                               mtCOVERAGE_TEST_MARKER();\r
                                        }\r
                                }\r
                                else\r
                                {\r
-                                       traceQUEUE_PEEK( pxQueue );\r
-\r
-                                       /* The data is not being removed, so reset the read\r
-                                       pointer. */\r
-                                       pxQueue->u.pcReadFrom = pcOriginalReadPosition;\r
-\r
-                                       /* The data is being left in the queue, so see if there are\r
-                                       any other tasks waiting for the data. */\r
-                                       if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
-                                       {\r
-                                               /* Tasks that are removed from the event list will get added to\r
-                                               the pending ready list as the scheduler is still suspended. */\r
-                                               if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
-                                               {\r
-                                                       /* The task waiting has a higher priority than this task. */\r
-                                                       portYIELD_WITHIN_API();\r
-                                               }\r
-                                       }\r
+                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
 \r
                                taskEXIT_CRITICAL();\r
@@ -1113,24 +1708,26 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                        }\r
                        else\r
                        {\r
-                               if( xTicksToWait == ( portTickType ) 0 )\r
+                               if( xTicksToWait == ( TickType_t ) 0 )\r
                                {\r
                                        /* The queue was empty and no block time is specified (or\r
                                        the block time has expired) so leave now. */\r
                                        taskEXIT_CRITICAL();\r
-                                       traceQUEUE_RECEIVE_FAILED( pxQueue );\r
+                                       traceQUEUE_PEEK_FAILED( pxQueue );\r
                                        return errQUEUE_EMPTY;\r
                                }\r
                                else if( xEntryTimeSet == pdFALSE )\r
                                {\r
                                        /* The queue was empty and a block time was specified so\r
-                                       configure the timeout structure. */\r
-                                       vTaskSetTimeOutState( &xTimeOut );\r
+                                       configure the timeout structure ready to enter the blocked\r
+                                       state. */\r
+                                       vTaskInternalSetTimeOutState( &xTimeOut );\r
                                        xEntryTimeSet = pdTRUE;\r
                                }\r
                                else\r
                                {\r
                                        /* Entry time was already set. */\r
+                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
                        }\r
                }\r
@@ -1145,60 +1742,63 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                /* Update the timeout state to see if it has expired yet. */\r
                if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )\r
                {\r
+                       /* Timeout has not expired yet, check to see if there is data in the\r
+                       queue now, and if not enter the Blocked state to wait for data. */\r
                        if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
                        {\r
-                               traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );\r
-\r
-                               #if ( configUSE_MUTEXES == 1 )\r
-                               {\r
-                                       if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
-                                       {\r
-                                               portENTER_CRITICAL();\r
-                                               {\r
-                                                       vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );\r
-                                               }\r
-                                               portEXIT_CRITICAL();\r
-                                       }\r
-                               }\r
-                               #endif\r
-\r
+                               traceBLOCKING_ON_QUEUE_PEEK( pxQueue );\r
                                vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
                                prvUnlockQueue( pxQueue );\r
                                if( xTaskResumeAll() == pdFALSE )\r
                                {\r
                                        portYIELD_WITHIN_API();\r
                                }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
                        }\r
                        else\r
                        {\r
-                               /* Try again. */\r
+                               /* There is data in the queue now, so don't enter the blocked\r
+                               state, instead return to try and obtain the data. */\r
                                prvUnlockQueue( pxQueue );\r
                                ( void ) xTaskResumeAll();\r
                        }\r
                }\r
                else\r
                {\r
+                       /* The timeout has expired.  If there is still no data in the queue\r
+                       exit, otherwise go back and try to read the data again. */\r
                        prvUnlockQueue( pxQueue );\r
                        ( void ) xTaskResumeAll();\r
-                       traceQUEUE_RECEIVE_FAILED( pxQueue );\r
-                       return errQUEUE_EMPTY;\r
+\r
+                       if( prvIsQueueEmpty( pxQueue ) != pdFALSE )\r
+                       {\r
+                               traceQUEUE_PEEK_FAILED( pxQueue );\r
+                               return errQUEUE_EMPTY;\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
                }\r
-       }\r
+       } /*lint -restore */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle xQueue, const void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken )\r
+BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, void * const pvBuffer, BaseType_t * const pxHigherPriorityTaskWoken )\r
 {\r
-signed portBASE_TYPE xReturn;\r
-unsigned portBASE_TYPE uxSavedInterruptStatus;\r
-xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+BaseType_t xReturn;\r
+UBaseType_t uxSavedInterruptStatus;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
        configASSERT( pxQueue );\r
-       configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
+       configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
 \r
        /* RTOS ports that support interrupt nesting have the concept of a maximum\r
        system call (or maximum API call) interrupt priority.  Interrupts that are\r
-       above the maximum system call priority are keep permanently enabled, even\r
+       above the maximum system call priority are kept permanently enabled, even\r
        when the RTOS kernel is in a critical section, but cannot make any calls to\r
        FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h\r
        then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
@@ -1214,19 +1814,23 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
 \r
        uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
        {\r
+               const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
+\r
                /* Cannot block in an ISR, so check there is data available. */\r
-               if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
+               if( uxMessagesWaiting > ( UBaseType_t ) 0 )\r
                {\r
+                       const int8_t cRxLock = pxQueue->cRxLock;\r
+\r
                        traceQUEUE_RECEIVE_FROM_ISR( pxQueue );\r
 \r
                        prvCopyDataFromQueue( pxQueue, pvBuffer );\r
-                       --( pxQueue->uxMessagesWaiting );\r
+                       pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;\r
 \r
                        /* If the queue is locked the event list will not be modified.\r
                        Instead update the lock count so the task that unlocks the queue\r
                        will know that an ISR has removed data while the queue was\r
                        locked. */\r
-                       if( pxQueue->xRxLock == queueUNLOCKED )\r
+                       if( cRxLock == queueUNLOCKED )\r
                        {\r
                                if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
                                {\r
@@ -1238,14 +1842,26 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
                                                {\r
                                                        *pxHigherPriorityTaskWoken = pdTRUE;\r
                                                }\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
+                                               }\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
                                        }\r
                                }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
                        }\r
                        else\r
                        {\r
                                /* Increment the lock count so the task that unlocks the queue\r
                                knows that data was removed while it was locked. */\r
-                               ++( pxQueue->xRxLock );\r
+                               pxQueue->cRxLock = ( int8_t ) ( cRxLock + 1 );\r
                        }\r
 \r
                        xReturn = pdPASS;\r
@@ -1262,19 +1878,20 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-signed portBASE_TYPE xQueuePeekFromISR( xQueueHandle xQueue, const void * const pvBuffer )\r
+BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue,  void * const pvBuffer )\r
 {\r
-signed portBASE_TYPE xReturn;\r
-unsigned portBASE_TYPE uxSavedInterruptStatus;\r
-signed char *pcOriginalReadPosition;\r
-xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+BaseType_t xReturn;\r
+UBaseType_t uxSavedInterruptStatus;\r
+int8_t *pcOriginalReadPosition;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
        configASSERT( pxQueue );\r
-       configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );\r
+       configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );\r
+       configASSERT( pxQueue->uxItemSize != 0 ); /* Can't peek a semaphore. */\r
 \r
        /* RTOS ports that support interrupt nesting have the concept of a maximum\r
        system call (or maximum API call) interrupt priority.  Interrupts that are\r
-       above the maximum system call priority are keep permanently enabled, even\r
+       above the maximum system call priority are kept permanently enabled, even\r
        when the RTOS kernel is in a critical section, but cannot make any calls to\r
        FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h\r
        then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion\r
@@ -1291,15 +1908,15 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
        uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
        {\r
                /* Cannot block in an ISR, so check there is data available. */\r
-               if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
+               if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )\r
                {\r
                        traceQUEUE_PEEK_FROM_ISR( pxQueue );\r
 \r
                        /* Remember the read position so it can be reset as nothing is\r
                        actually being removed from the queue. */\r
-                       pcOriginalReadPosition = pxQueue->u.pcReadFrom;\r
+                       pcOriginalReadPosition = pxQueue->u.xQueue.pcReadFrom;\r
                        prvCopyDataFromQueue( pxQueue, pvBuffer );\r
-                       pxQueue->u.pcReadFrom = pcOriginalReadPosition;\r
+                       pxQueue->u.xQueue.pcReadFrom = pcOriginalReadPosition;\r
 \r
                        xReturn = pdPASS;\r
                }\r
@@ -1315,70 +1932,98 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle xQueue )\r
+UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue )\r
 {\r
-unsigned portBASE_TYPE uxReturn;\r
+UBaseType_t uxReturn;\r
 \r
        configASSERT( xQueue );\r
 \r
        taskENTER_CRITICAL();\r
-               uxReturn = ( ( xQUEUE * ) xQueue )->uxMessagesWaiting;\r
+       {\r
+               uxReturn = ( ( Queue_t * ) xQueue )->uxMessagesWaiting;\r
+       }\r
        taskEXIT_CRITICAL();\r
 \r
        return uxReturn;\r
 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */\r
 /*-----------------------------------------------------------*/\r
 \r
-unsigned portBASE_TYPE uxQueueSpacesAvailable( const xQueueHandle xQueue )\r
+UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue )\r
 {\r
-unsigned portBASE_TYPE uxReturn;\r
-xQUEUE *pxQueue;\r
+UBaseType_t uxReturn;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
-       pxQueue = ( xQUEUE * ) xQueue;\r
        configASSERT( pxQueue );\r
 \r
        taskENTER_CRITICAL();\r
+       {\r
                uxReturn = pxQueue->uxLength - pxQueue->uxMessagesWaiting;\r
+       }\r
        taskEXIT_CRITICAL();\r
 \r
        return uxReturn;\r
 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */\r
 /*-----------------------------------------------------------*/\r
 \r
-unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle xQueue )\r
+UBaseType_t uxQueueMessagesWaitingFromISR( const QueueHandle_t xQueue )\r
 {\r
-unsigned portBASE_TYPE uxReturn;\r
+UBaseType_t uxReturn;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
-       configASSERT( xQueue );\r
-\r
-       uxReturn = ( ( xQUEUE * ) xQueue )->uxMessagesWaiting;\r
+       configASSERT( pxQueue );\r
+       uxReturn = pxQueue->uxMessagesWaiting;\r
 \r
        return uxReturn;\r
 } /*lint !e818 Pointer cannot be declared const as xQueue is a typedef not pointer. */\r
 /*-----------------------------------------------------------*/\r
 \r
-void vQueueDelete( xQueueHandle xQueue )\r
+void vQueueDelete( QueueHandle_t xQueue )\r
 {\r
-xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
        configASSERT( pxQueue );\r
-\r
        traceQUEUE_DELETE( pxQueue );\r
+\r
        #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
        {\r
                vQueueUnregisterQueue( pxQueue );\r
        }\r
        #endif\r
-       vPortFree( pxQueue->pcHead );\r
-       vPortFree( pxQueue );\r
+\r
+       #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )\r
+       {\r
+               /* The queue can only have been allocated dynamically - free it\r
+               again. */\r
+               vPortFree( pxQueue );\r
+       }\r
+       #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
+       {\r
+               /* The queue could have been allocated statically or dynamically, so\r
+               check before attempting to free the memory. */\r
+               if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )\r
+               {\r
+                       vPortFree( pxQueue );\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
+       }\r
+       #else\r
+       {\r
+               /* The queue must have been statically allocated, so is not going to be\r
+               deleted.  Avoid compiler warnings about the unused parameter. */\r
+               ( void ) pxQueue;\r
+       }\r
+       #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       unsigned char ucQueueGetQueueNumber( xQueueHandle xQueue )\r
+       UBaseType_t uxQueueGetQueueNumber( QueueHandle_t xQueue )\r
        {\r
-               return ( ( xQUEUE * ) xQueue )->ucQueueNumber;\r
+               return ( ( Queue_t * ) xQueue )->uxQueueNumber;\r
        }\r
 \r
 #endif /* configUSE_TRACE_FACILITY */\r
@@ -1386,9 +2031,9 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
 \r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       void vQueueSetQueueNumber( xQueueHandle xQueue, unsigned char ucQueueNumber )\r
+       void vQueueSetQueueNumber( QueueHandle_t xQueue, UBaseType_t uxQueueNumber )\r
        {\r
-               ( ( xQUEUE * ) xQueue )->ucQueueNumber = ucQueueNumber;\r
+               ( ( Queue_t * ) xQueue )->uxQueueNumber = uxQueueNumber;\r
        }\r
 \r
 #endif /* configUSE_TRACE_FACILITY */\r
@@ -1396,79 +2041,139 @@ xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;
 \r
 #if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
-       unsigned char ucQueueGetQueueType( xQueueHandle xQueue )\r
+       uint8_t ucQueueGetQueueType( QueueHandle_t xQueue )\r
        {\r
-               return ( ( xQUEUE * ) xQueue )->ucQueueType;\r
+               return ( ( Queue_t * ) xQueue )->ucQueueType;\r
        }\r
 \r
 #endif /* configUSE_TRACE_FACILITY */\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )\r
+#if( configUSE_MUTEXES == 1 )\r
+\r
+       static UBaseType_t prvGetDisinheritPriorityAfterTimeout( const Queue_t * const pxQueue )\r
+       {\r
+       UBaseType_t uxHighestPriorityOfWaitingTasks;\r
+\r
+               /* If a task waiting for a mutex causes the mutex holder to inherit a\r
+               priority, but the waiting task times out, then the holder should\r
+               disinherit the priority - but only down to the highest priority of any\r
+               other tasks that are waiting for the same mutex.  For this purpose,\r
+               return the priority of the highest priority task that is waiting for the\r
+               mutex. */\r
+               if( listCURRENT_LIST_LENGTH( &( pxQueue->xTasksWaitingToReceive ) ) > 0U )\r
+               {\r
+                       uxHighestPriorityOfWaitingTasks = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) listGET_ITEM_VALUE_OF_HEAD_ENTRY( &( pxQueue->xTasksWaitingToReceive ) );\r
+               }\r
+               else\r
+               {\r
+                       uxHighestPriorityOfWaitingTasks = tskIDLE_PRIORITY;\r
+               }\r
+\r
+               return uxHighestPriorityOfWaitingTasks;\r
+       }\r
+\r
+#endif /* configUSE_MUTEXES */\r
+/*-----------------------------------------------------------*/\r
+\r
+static BaseType_t prvCopyDataToQueue( Queue_t * const pxQueue, const void *pvItemToQueue, const BaseType_t xPosition )\r
 {\r
-       if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )\r
+BaseType_t xReturn = pdFALSE;\r
+UBaseType_t uxMessagesWaiting;\r
+\r
+       /* This function is called from a critical section. */\r
+\r
+       uxMessagesWaiting = pxQueue->uxMessagesWaiting;\r
+\r
+       if( pxQueue->uxItemSize == ( UBaseType_t ) 0 )\r
        {\r
                #if ( configUSE_MUTEXES == 1 )\r
                {\r
                        if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )\r
                        {\r
                                /* The mutex is no longer being held. */\r
-                               vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );\r
-                               pxQueue->pxMutexHolder = NULL;\r
+                               xReturn = xTaskPriorityDisinherit( pxQueue->u.xSemaphore.xMutexHolder );\r
+                               pxQueue->u.xSemaphore.xMutexHolder = NULL;\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
                        }\r
                }\r
                #endif /* configUSE_MUTEXES */\r
        }\r
        else if( xPosition == queueSEND_TO_BACK )\r
        {\r
-               ( 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. */\r
-               pxQueue->pcWriteTo += pxQueue->uxItemSize;\r
-               if( pxQueue->pcWriteTo >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */\r
+               ( void ) memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports, plus previous logic ensures a null pointer can only be passed to memcpy() if the copy size is 0.  Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */\r
+               pxQueue->pcWriteTo += pxQueue->uxItemSize; /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */\r
+               if( pxQueue->pcWriteTo >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */\r
                {\r
                        pxQueue->pcWriteTo = pxQueue->pcHead;\r
                }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
        }\r
        else\r
        {\r
-               ( void ) memcpy( ( void * ) pxQueue->u.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */\r
-               pxQueue->u.pcReadFrom -= pxQueue->uxItemSize;\r
-               if( pxQueue->u.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */\r
+               ( void ) memcpy( ( void * ) pxQueue->u.xQueue.pcReadFrom, pvItemToQueue, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e9087 !e418 MISRA exception as the casts are only redundant for some ports.  Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes.  Assert checks null pointer only used when length is 0. */\r
+               pxQueue->u.xQueue.pcReadFrom -= pxQueue->uxItemSize;\r
+               if( pxQueue->u.xQueue.pcReadFrom < pxQueue->pcHead ) /*lint !e946 MISRA exception justified as comparison of pointers is the cleanest solution. */\r
+               {\r
+                       pxQueue->u.xQueue.pcReadFrom = ( pxQueue->u.xQueue.pcTail - pxQueue->uxItemSize );\r
+               }\r
+               else\r
                {\r
-                       pxQueue->u.pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );\r
+                       mtCOVERAGE_TEST_MARKER();\r
                }\r
 \r
                if( xPosition == queueOVERWRITE )\r
                {\r
-                       if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
+                       if( uxMessagesWaiting > ( UBaseType_t ) 0 )\r
                        {\r
                                /* An item is not being added but overwritten, so subtract\r
                                one from the recorded number of items in the queue so when\r
                                one is added again below the number of recorded items remains\r
                                correct. */\r
-                               --( pxQueue->uxMessagesWaiting );\r
+                               --uxMessagesWaiting;\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
                        }\r
                }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
        }\r
 \r
-       ++( pxQueue->uxMessagesWaiting );\r
+       pxQueue->uxMessagesWaiting = uxMessagesWaiting + ( UBaseType_t ) 1;\r
+\r
+       return xReturn;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void * const pvBuffer )\r
+static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer )\r
 {\r
-       if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )\r
+       if( pxQueue->uxItemSize != ( UBaseType_t ) 0 )\r
        {\r
-               pxQueue->u.pcReadFrom += pxQueue->uxItemSize;\r
-               if( pxQueue->u.pcReadFrom >= pxQueue->pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */\r
+               pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize; /*lint !e9016 Pointer arithmetic on char types ok, especially in this use case where it is the clearest way of conveying intent. */\r
+               if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail ) /*lint !e946 MISRA exception justified as use of the relational operator is the cleanest solutions. */\r
+               {\r
+                       pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;\r
+               }\r
+               else\r
                {\r
-                       pxQueue->u.pcReadFrom = pxQueue->pcHead;\r
+                       mtCOVERAGE_TEST_MARKER();\r
                }\r
-               ( 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. */\r
+               ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( size_t ) pxQueue->uxItemSize ); /*lint !e961 !e418 !e9087 MISRA exception as the casts are only redundant for some ports.  Also previous logic ensures a null pointer can only be passed to memcpy() when the count is 0.  Cast to void required by function signature and safe as no alignment requirement and copy length specified in bytes. */\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvUnlockQueue( xQUEUE *pxQueue )\r
+static void prvUnlockQueue( Queue_t * const pxQueue )\r
 {\r
        /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */\r
 \r
@@ -1478,8 +2183,10 @@ static void prvUnlockQueue( xQUEUE *pxQueue )
        updated. */\r
        taskENTER_CRITICAL();\r
        {\r
+               int8_t cTxLock = pxQueue->cTxLock;\r
+\r
                /* See if data was added to the queue while it was locked. */\r
-               while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )\r
+               while( cTxLock > queueLOCKED_UNMODIFIED )\r
                {\r
                        /* Data was posted while the queue was locked.  Are any tasks\r
                        blocked waiting for data to become available? */\r
@@ -1487,18 +2194,23 @@ static void prvUnlockQueue( xQUEUE *pxQueue )
                        {\r
                                if( pxQueue->pxQueueSetContainer != NULL )\r
                                {\r
-                                       if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) == pdTRUE )\r
+                                       if( prvNotifyQueueSetContainer( pxQueue ) != pdFALSE )\r
                                        {\r
                                                /* The queue is a member of a queue set, and posting to\r
                                                the queue set caused a higher priority task to unblock.\r
                                                A context switch is required. */\r
                                                vTaskMissedYield();\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
                                }\r
                                else\r
                                {\r
-                                       /* Tasks that are removed from the event list will get added to\r
-                                       the pending ready list as the scheduler is still suspended. */\r
+                                       /* Tasks that are removed from the event list will get\r
+                                       added to the pending ready list as the scheduler is still\r
+                                       suspended. */\r
                                        if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )\r
                                        {\r
                                                if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
@@ -1507,6 +2219,10 @@ static void prvUnlockQueue( xQUEUE *pxQueue )
                                                        context switch is required. */\r
                                                        vTaskMissedYield();\r
                                                }\r
+                                               else\r
+                                               {\r
+                                                       mtCOVERAGE_TEST_MARKER();\r
+                                               }\r
                                        }\r
                                        else\r
                                        {\r
@@ -1522,10 +2238,14 @@ static void prvUnlockQueue( xQUEUE *pxQueue )
                                {\r
                                        if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )\r
                                        {\r
-                                               /* The task waiting has a higher priority so record that a\r
-                                               context switch is required. */\r
+                                               /* The task waiting has a higher priority so record that\r
+                                               context switch is required. */\r
                                                vTaskMissedYield();\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
                                }\r
                                else\r
                                {\r
@@ -1534,17 +2254,19 @@ static void prvUnlockQueue( xQUEUE *pxQueue )
                        }\r
                        #endif /* configUSE_QUEUE_SETS */\r
 \r
-                       --( pxQueue->xTxLock );\r
+                       --cTxLock;\r
                }\r
 \r
-               pxQueue->xTxLock = queueUNLOCKED;\r
+               pxQueue->cTxLock = queueUNLOCKED;\r
        }\r
        taskEXIT_CRITICAL();\r
 \r
        /* Do the same for the Rx lock. */\r
        taskENTER_CRITICAL();\r
        {\r
-               while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )\r
+               int8_t cRxLock = pxQueue->cRxLock;\r
+\r
+               while( cRxLock > queueLOCKED_UNMODIFIED )\r
                {\r
                        if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )\r
                        {\r
@@ -1552,8 +2274,12 @@ static void prvUnlockQueue( xQUEUE *pxQueue )
                                {\r
                                        vTaskMissedYield();\r
                                }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
 \r
-                               --( pxQueue->xRxLock );\r
+                               --cRxLock;\r
                        }\r
                        else\r
                        {\r
@@ -1561,19 +2287,19 @@ static void prvUnlockQueue( xQUEUE *pxQueue )
                        }\r
                }\r
 \r
-               pxQueue->xRxLock = queueUNLOCKED;\r
+               pxQueue->cRxLock = queueUNLOCKED;\r
        }\r
        taskEXIT_CRITICAL();\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static signed portBASE_TYPE prvIsQueueEmpty( const xQUEUE *pxQueue )\r
+static BaseType_t prvIsQueueEmpty( const Queue_t *pxQueue )\r
 {\r
-signed portBASE_TYPE xReturn;\r
+BaseType_t xReturn;\r
 \r
        taskENTER_CRITICAL();\r
        {\r
-               if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE )  0 )\r
+               if( pxQueue->uxMessagesWaiting == ( UBaseType_t )  0 )\r
                {\r
                        xReturn = pdTRUE;\r
                }\r
@@ -1588,12 +2314,13 @@ signed portBASE_TYPE xReturn;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle xQueue )\r
+BaseType_t xQueueIsQueueEmptyFromISR( const QueueHandle_t xQueue )\r
 {\r
-signed portBASE_TYPE xReturn;\r
+BaseType_t xReturn;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
-       configASSERT( xQueue );\r
-       if( ( ( xQUEUE * ) xQueue )->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
+       configASSERT( pxQueue );\r
+       if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )\r
        {\r
                xReturn = pdTRUE;\r
        }\r
@@ -1606,9 +2333,9 @@ signed portBASE_TYPE xReturn;
 } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */\r
 /*-----------------------------------------------------------*/\r
 \r
-static signed portBASE_TYPE prvIsQueueFull( const xQUEUE *pxQueue )\r
+static BaseType_t prvIsQueueFull( const Queue_t *pxQueue )\r
 {\r
-signed portBASE_TYPE xReturn;\r
+BaseType_t xReturn;\r
 \r
        taskENTER_CRITICAL();\r
        {\r
@@ -1627,12 +2354,13 @@ signed portBASE_TYPE xReturn;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle xQueue )\r
+BaseType_t xQueueIsQueueFullFromISR( const QueueHandle_t xQueue )\r
 {\r
-signed portBASE_TYPE xReturn;\r
+BaseType_t xReturn;\r
+Queue_t * const pxQueue = xQueue;\r
 \r
-       configASSERT( xQueue );\r
-       if( ( ( xQUEUE * ) xQueue )->uxMessagesWaiting == ( ( xQUEUE * ) xQueue )->uxLength )\r
+       configASSERT( pxQueue );\r
+       if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )\r
        {\r
                xReturn = pdTRUE;\r
        }\r
@@ -1647,10 +2375,10 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_CO_ROUTINES == 1 )\r
 \r
-       signed portBASE_TYPE xQueueCRSend( xQueueHandle xQueue, const void *pvItemToQueue, portTickType xTicksToWait )\r
+       BaseType_t xQueueCRSend( QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait )\r
        {\r
-       signed portBASE_TYPE xReturn;\r
-       xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+       BaseType_t xReturn;\r
+       Queue_t * const pxQueue = xQueue;\r
 \r
                /* If the queue is already full we may have to block.  A critical section\r
                is required to prevent an interrupt removing something from the queue\r
@@ -1661,7 +2389,7 @@ signed portBASE_TYPE xReturn;
                        {\r
                                /* The queue is full - do we want to block or just leave without\r
                                posting? */\r
-                               if( xTicksToWait > ( portTickType ) 0 )\r
+                               if( xTicksToWait > ( TickType_t ) 0 )\r
                                {\r
                                        /* As this is called from a coroutine we cannot block directly, but\r
                                        return indicating that we need to block. */\r
@@ -1699,6 +2427,14 @@ signed portBASE_TYPE xReturn;
                                                that a yield might be appropriate. */\r
                                                xReturn = errQUEUE_YIELD;\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
                        }\r
                        else\r
@@ -1716,21 +2452,21 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_CO_ROUTINES == 1 )\r
 \r
-       signed portBASE_TYPE xQueueCRReceive( xQueueHandle xQueue, void *pvBuffer, portTickType xTicksToWait )\r
+       BaseType_t xQueueCRReceive( QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait )\r
        {\r
-       signed portBASE_TYPE xReturn;\r
-       xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+       BaseType_t xReturn;\r
+       Queue_t * const pxQueue = xQueue;\r
 \r
                /* If the queue is already empty we may have to block.  A critical section\r
                is required to prevent an interrupt adding something to the queue\r
                between the check to see if the queue is empty and blocking on the queue. */\r
                portDISABLE_INTERRUPTS();\r
                {\r
-                       if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )\r
+                       if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0 )\r
                        {\r
                                /* There are no messages in the queue, do we want to block or just\r
                                leave with nothing? */\r
-                               if( xTicksToWait > ( portTickType ) 0 )\r
+                               if( xTicksToWait > ( TickType_t ) 0 )\r
                                {\r
                                        /* As this is a co-routine we cannot block directly, but return\r
                                        indicating that we need to block. */\r
@@ -1744,21 +2480,29 @@ signed portBASE_TYPE xReturn;
                                        return errQUEUE_FULL;\r
                                }\r
                        }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
                }\r
                portENABLE_INTERRUPTS();\r
 \r
                portDISABLE_INTERRUPTS();\r
                {\r
-                       if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
+                       if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )\r
                        {\r
                                /* Data is available from the queue. */\r
-                               pxQueue->u.pcReadFrom += pxQueue->uxItemSize;\r
-                               if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )\r
+                               pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;\r
+                               if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )\r
                                {\r
-                                       pxQueue->u.pcReadFrom = pxQueue->pcHead;\r
+                                       pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
                                --( pxQueue->uxMessagesWaiting );\r
-                               ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
+                               ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
 \r
                                xReturn = pdPASS;\r
 \r
@@ -1773,6 +2517,14 @@ signed portBASE_TYPE xReturn;
                                        {\r
                                                xReturn = errQUEUE_YIELD;\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
                        }\r
                        else\r
@@ -1790,9 +2542,9 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_CO_ROUTINES == 1 )\r
 \r
-       signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle xQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )\r
+       BaseType_t xQueueCRSendFromISR( QueueHandle_t xQueue, const void *pvItemToQueue, BaseType_t xCoRoutinePreviouslyWoken )\r
        {\r
-       xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+       Queue_t * const pxQueue = xQueue;\r
 \r
                /* Cannot block within an ISR so if there is no space on the queue then\r
                exit without doing anything. */\r
@@ -1810,9 +2562,25 @@ signed portBASE_TYPE xReturn;
                                        {\r
                                                return pdTRUE;\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
                                }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
                        }\r
                }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
 \r
                return xCoRoutinePreviouslyWoken;\r
        }\r
@@ -1822,23 +2590,27 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_CO_ROUTINES == 1 )\r
 \r
-       signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle xQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )\r
+       BaseType_t xQueueCRReceiveFromISR( QueueHandle_t xQueue, void *pvBuffer, BaseType_t *pxCoRoutineWoken )\r
        {\r
-       signed portBASE_TYPE xReturn;\r
-       xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+       BaseType_t xReturn;\r
+       Queue_t * const pxQueue = xQueue;\r
 \r
                /* We cannot block from an ISR, so check there is data available. If\r
                not then just leave without doing anything. */\r
-               if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )\r
+               if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 )\r
                {\r
                        /* Copy the data from the queue. */\r
-                       pxQueue->u.pcReadFrom += pxQueue->uxItemSize;\r
-                       if( pxQueue->u.pcReadFrom >= pxQueue->pcTail )\r
+                       pxQueue->u.xQueue.pcReadFrom += pxQueue->uxItemSize;\r
+                       if( pxQueue->u.xQueue.pcReadFrom >= pxQueue->u.xQueue.pcTail )\r
+                       {\r
+                               pxQueue->u.xQueue.pcReadFrom = pxQueue->pcHead;\r
+                       }\r
+                       else\r
                        {\r
-                               pxQueue->u.pcReadFrom = pxQueue->pcHead;\r
+                               mtCOVERAGE_TEST_MARKER();\r
                        }\r
                        --( pxQueue->uxMessagesWaiting );\r
-                       ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
+                       ( void ) memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->u.xQueue.pcReadFrom, ( unsigned ) pxQueue->uxItemSize );\r
 \r
                        if( ( *pxCoRoutineWoken ) == pdFALSE )\r
                        {\r
@@ -1848,8 +2620,20 @@ signed portBASE_TYPE xReturn;
                                        {\r
                                                *pxCoRoutineWoken = pdTRUE;\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
                        }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
 \r
                        xReturn = pdPASS;\r
                }\r
@@ -1866,21 +2650,27 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
 \r
-       void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )\r
+       void vQueueAddToRegistry( QueueHandle_t xQueue, const char *pcQueueName ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
        {\r
-       unsigned portBASE_TYPE ux;\r
+       UBaseType_t ux;\r
 \r
                /* See if there is an empty space in the registry.  A NULL name denotes\r
                a free slot. */\r
-               for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )\r
+               for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )\r
                {\r
                        if( xQueueRegistry[ ux ].pcQueueName == NULL )\r
                        {\r
                                /* Store the information on this queue. */\r
                                xQueueRegistry[ ux ].pcQueueName = pcQueueName;\r
                                xQueueRegistry[ ux ].xHandle = xQueue;\r
+\r
+                               traceQUEUE_REGISTRY_ADD( xQueue, pcQueueName );\r
                                break;\r
                        }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
                }\r
        }\r
 \r
@@ -1889,20 +2679,57 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
 \r
-       void vQueueUnregisterQueue( xQueueHandle xQueue )\r
+       const char *pcQueueGetName( QueueHandle_t xQueue ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+       {\r
+       UBaseType_t ux;\r
+       const char *pcReturn = NULL; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
+\r
+               /* Note there is nothing here to protect against another task adding or\r
+               removing entries from the registry while it is being searched. */\r
+               for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )\r
+               {\r
+                       if( xQueueRegistry[ ux ].xHandle == xQueue )\r
+                       {\r
+                               pcReturn = xQueueRegistry[ ux ].pcQueueName;\r
+                               break;\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
+               }\r
+\r
+               return pcReturn;\r
+       } /*lint !e818 xQueue cannot be a pointer to const because it is a typedef. */\r
+\r
+#endif /* configQUEUE_REGISTRY_SIZE */\r
+/*-----------------------------------------------------------*/\r
+\r
+#if ( configQUEUE_REGISTRY_SIZE > 0 )\r
+\r
+       void vQueueUnregisterQueue( QueueHandle_t xQueue )\r
        {\r
-       unsigned portBASE_TYPE ux;\r
+       UBaseType_t ux;\r
 \r
                /* See if the handle of the queue being unregistered in actually in the\r
                registry. */\r
-               for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )\r
+               for( ux = ( UBaseType_t ) 0U; ux < ( UBaseType_t ) configQUEUE_REGISTRY_SIZE; ux++ )\r
                {\r
                        if( xQueueRegistry[ ux ].xHandle == xQueue )\r
                        {\r
                                /* Set the name to NULL to show that this slot if free again. */\r
                                xQueueRegistry[ ux ].pcQueueName = NULL;\r
+\r
+                               /* Set the handle to NULL to ensure the same queue handle cannot\r
+                               appear in the registry twice if it is added, removed, then\r
+                               added again. */\r
+                               xQueueRegistry[ ux ].xHandle = ( QueueHandle_t ) 0;\r
                                break;\r
                        }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
                }\r
 \r
        } /*lint !e818 xQueue could not be pointer to const because it is a typedef. */\r
@@ -1912,9 +2739,9 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_TIMERS == 1 )\r
 \r
-       void vQueueWaitForMessageRestricted( xQueueHandle xQueue, portTickType xTicksToWait )\r
+       void vQueueWaitForMessageRestricted( QueueHandle_t xQueue, TickType_t xTicksToWait, const BaseType_t xWaitIndefinitely )\r
        {\r
-       xQUEUE * const pxQueue = ( xQUEUE * ) xQueue;\r
+       Queue_t * const pxQueue = xQueue;\r
 \r
                /* This function should not be called by application code hence the\r
                'Restricted' in its name.  It is not part of the public API.  It is\r
@@ -1931,10 +2758,14 @@ signed portBASE_TYPE xReturn;
                the queue is locked, and the calling task blocks on the queue, then the\r
                calling task will be immediately unblocked when the queue is unlocked. */\r
                prvLockQueue( pxQueue );\r
-               if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )\r
+               if( pxQueue->uxMessagesWaiting == ( UBaseType_t ) 0U )\r
                {\r
                        /* There is nothing in the queue, block for the specified period. */\r
-                       vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );\r
+                       vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait, xWaitIndefinitely );\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
                }\r
                prvUnlockQueue( pxQueue );\r
        }\r
@@ -1942,13 +2773,13 @@ signed portBASE_TYPE xReturn;
 #endif /* configUSE_TIMERS */\r
 /*-----------------------------------------------------------*/\r
 \r
-#if ( configUSE_QUEUE_SETS == 1 )\r
+#if( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
 \r
-       xQueueSetHandle xQueueCreateSet( unsigned portBASE_TYPE uxEventQueueLength )\r
+       QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )\r
        {\r
-       xQueueSetHandle pxQueue;\r
+       QueueSetHandle_t pxQueue;\r
 \r
-               pxQueue = xQueueGenericCreate( uxEventQueueLength, sizeof( xQUEUE * ), queueQUEUE_TYPE_SET );\r
+               pxQueue = xQueueGenericCreate( uxEventQueueLength, ( UBaseType_t ) sizeof( Queue_t * ), queueQUEUE_TYPE_SET );\r
 \r
                return pxQueue;\r
        }\r
@@ -1958,30 +2789,30 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_QUEUE_SETS == 1 )\r
 \r
-       portBASE_TYPE xQueueAddToSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet )\r
+       BaseType_t xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )\r
        {\r
-       portBASE_TYPE xReturn;\r
+       BaseType_t xReturn;\r
 \r
-               if( ( ( xQUEUE * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )\r
-               {\r
-                       /* Cannot add a queue/semaphore to more than one queue set. */\r
-                       xReturn = pdFAIL;\r
-               }\r
-               else if( ( ( xQUEUE * ) xQueueOrSemaphore )->uxMessagesWaiting != ( unsigned portBASE_TYPE ) 0 )\r
-               {\r
-                       /* Cannot add a queue/semaphore to a queue set if there are already\r
-                       items in the queue/semaphore. */\r
-                       xReturn = pdFAIL;\r
-               }\r
-               else\r
+               taskENTER_CRITICAL();\r
                {\r
-                       taskENTER_CRITICAL();\r
+                       if( ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )\r
                        {\r
-                               ( ( xQUEUE * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;\r
+                               /* Cannot add a queue/semaphore to more than one queue set. */\r
+                               xReturn = pdFAIL;\r
+                       }\r
+                       else if( ( ( Queue_t * ) xQueueOrSemaphore )->uxMessagesWaiting != ( UBaseType_t ) 0 )\r
+                       {\r
+                               /* Cannot add a queue/semaphore to a queue set if there are already\r
+                               items in the queue/semaphore. */\r
+                               xReturn = pdFAIL;\r
+                       }\r
+                       else\r
+                       {\r
+                               ( ( Queue_t * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;\r
+                               xReturn = pdPASS;\r
                        }\r
-                       taskEXIT_CRITICAL();\r
-                       xReturn = pdPASS;\r
                }\r
+               taskEXIT_CRITICAL();\r
 \r
                return xReturn;\r
        }\r
@@ -1991,17 +2822,17 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_QUEUE_SETS == 1 )\r
 \r
-       portBASE_TYPE xQueueRemoveFromSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet )\r
+       BaseType_t xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet )\r
        {\r
-       portBASE_TYPE xReturn;\r
-       xQUEUE * const pxQueueOrSemaphore = ( xQUEUE * ) xQueueOrSemaphore;\r
+       BaseType_t xReturn;\r
+       Queue_t * const pxQueueOrSemaphore = ( Queue_t * ) xQueueOrSemaphore;\r
 \r
                if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )\r
                {\r
                        /* The queue was not a member of the set. */\r
                        xReturn = pdFAIL;\r
                }\r
-               else if( pxQueueOrSemaphore->uxMessagesWaiting != ( unsigned portBASE_TYPE ) 0 )\r
+               else if( pxQueueOrSemaphore->uxMessagesWaiting != ( UBaseType_t ) 0 )\r
                {\r
                        /* It is dangerous to remove a queue from a set when the queue is\r
                        not empty because the queue set will still hold pending events for\r
@@ -2027,11 +2858,11 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_QUEUE_SETS == 1 )\r
 \r
-       xQueueSetMemberHandle xQueueSelectFromSet( xQueueSetHandle xQueueSet, portTickType xBlockTimeTicks )\r
+       QueueSetMemberHandle_t xQueueSelectFromSet( QueueSetHandle_t xQueueSet, TickType_t const xTicksToWait )\r
        {\r
-       xQueueSetMemberHandle xReturn = NULL;\r
+       QueueSetMemberHandle_t xReturn = NULL;\r
 \r
-               ( void ) xQueueGenericReceive( ( xQueueHandle ) xQueueSet, &xReturn, xBlockTimeTicks, pdFALSE ); /*lint !e961 Casting from one typedef to another is not redundant. */\r
+               ( void ) xQueueReceive( ( QueueHandle_t ) xQueueSet, &xReturn, xTicksToWait ); /*lint !e961 Casting from one typedef to another is not redundant. */\r
                return xReturn;\r
        }\r
 \r
@@ -2040,11 +2871,11 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_QUEUE_SETS == 1 )\r
 \r
-       xQueueSetMemberHandle xQueueSelectFromSetFromISR( xQueueSetHandle xQueueSet )\r
+       QueueSetMemberHandle_t xQueueSelectFromSetFromISR( QueueSetHandle_t xQueueSet )\r
        {\r
-       xQueueSetMemberHandle xReturn = NULL;\r
+       QueueSetMemberHandle_t xReturn = NULL;\r
 \r
-               ( void ) xQueueReceiveFromISR( ( xQueueHandle ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */\r
+               ( void ) xQueueReceiveFromISR( ( QueueHandle_t ) xQueueSet, &xReturn, NULL ); /*lint !e961 Casting from one typedef to another is not redundant. */\r
                return xReturn;\r
        }\r
 \r
@@ -2053,27 +2884,52 @@ signed portBASE_TYPE xReturn;
 \r
 #if ( configUSE_QUEUE_SETS == 1 )\r
 \r
-       static portBASE_TYPE prvNotifyQueueSetContainer( const xQUEUE * const pxQueue, portBASE_TYPE xCopyPosition )\r
+       static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue )\r
        {\r
-       xQUEUE *pxQueueSetContainer = pxQueue->pxQueueSetContainer;\r
-       portBASE_TYPE xReturn = pdFALSE;\r
+       Queue_t *pxQueueSetContainer = pxQueue->pxQueueSetContainer;\r
+       BaseType_t xReturn = pdFALSE;\r
+\r
+               /* This function must be called form a critical section. */\r
 \r
                configASSERT( pxQueueSetContainer );\r
                configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );\r
 \r
                if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )\r
                {\r
+                       const int8_t cTxLock = pxQueueSetContainer->cTxLock;\r
+\r
                        traceQUEUE_SEND( pxQueueSetContainer );\r
-                       /* The data copies is the handle of the queue that contains data. */\r
-                       prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, xCopyPosition );\r
-                       if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )\r
+\r
+                       /* The data copied is the handle of the queue that contains data. */\r
+                       xReturn = prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, queueSEND_TO_BACK );\r
+\r
+                       if( cTxLock == queueUNLOCKED )\r
                        {\r
-                               if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )\r
+                               if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )\r
+                               {\r
+                                       if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )\r
+                                       {\r
+                                               /* The task waiting has a higher priority. */\r
+                                               xReturn = pdTRUE;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               else\r
                                {\r
-                                       /* The task waiting has a higher priority */\r
-                                       xReturn = pdTRUE;\r
+                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
                        }\r
+                       else\r
+                       {\r
+                               pxQueueSetContainer->cTxLock = ( int8_t ) ( cTxLock + 1 );\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
                }\r
 \r
                return xReturn;\r
@@ -2081,3 +2937,14 @@ signed portBASE_TYPE xReturn;
 \r
 #endif /* configUSE_QUEUE_SETS */\r
 \r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r