]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/event_groups.c
Update version numbers in preparation for a new release.
[freertos] / FreeRTOS / Source / event_groups.c
index 09ddbee542be860d96c502e0cf0d6f253e71efa7..62a35d3caf4a20108a9f9eb69d40abf6e6754501 100644 (file)
@@ -1,67 +1,29 @@
 /*\r
-    FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
-    All rights reserved\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.1.0\r
+ * Copyright (C) 2017 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
 /* Standard includes. */\r
 #include <stdlib.h>\r
@@ -77,69 +39,164 @@ task.h is included from an application file. */
 #include "timers.h"\r
 #include "event_groups.h"\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
-\r
-#if ( INCLUDE_xEventGroupSetBitFromISR == 1 ) && ( configUSE_TIMERS == 0 )\r
-       #error configUSE_TIMERS must be set to 1 to make the xEventGroupSetBitFromISR() function available.\r
-#endif\r
-\r
-#if ( INCLUDE_xEventGroupSetBitFromISR == 1 ) && ( INCLUDE_xTimerPendCallbackFromISR == 0 )\r
-       #error INCLUDE_xTimerPendCallbackFromISR must also be set to one to make the xEventGroupSetBitFromISR() function available.\r
-#endif\r
-\r
+/* Lint e961, e750 and e9021 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 See comment above. */\r
 \r
+/* The following bit fields convey control information in a task's event list\r
+item value.  It is important they don't clash with the\r
+taskEVENT_LIST_ITEM_VALUE_IN_USE definition. */\r
 #if configUSE_16_BIT_TICKS == 1\r
-       #define taskCLEAR_EVENTS_ON_EXIT_BIT    0x0100U\r
-       #define taskUNBLOCKED_DUE_TO_BIT_SET    0x0200U\r
-       #define taskWAIT_FOR_ALL_BITS                   0x0400U\r
-       #define taskEVENT_BITS_CONTROL_BYTES    0xff00U\r
+       #define eventCLEAR_EVENTS_ON_EXIT_BIT   0x0100U\r
+       #define eventUNBLOCKED_DUE_TO_BIT_SET   0x0200U\r
+       #define eventWAIT_FOR_ALL_BITS                  0x0400U\r
+       #define eventEVENT_BITS_CONTROL_BYTES   0xff00U\r
 #else\r
-       #define taskCLEAR_EVENTS_ON_EXIT_BIT    0x01000000UL\r
-       #define taskUNBLOCKED_DUE_TO_BIT_SET    0x02000000UL\r
-       #define taskWAIT_FOR_ALL_BITS                   0x04000000UL\r
-       #define taskEVENT_BITS_CONTROL_BYTES    0xff000000UL\r
+       #define eventCLEAR_EVENTS_ON_EXIT_BIT   0x01000000UL\r
+       #define eventUNBLOCKED_DUE_TO_BIT_SET   0x02000000UL\r
+       #define eventWAIT_FOR_ALL_BITS                  0x04000000UL\r
+       #define eventEVENT_BITS_CONTROL_BYTES   0xff000000UL\r
 #endif\r
 \r
-typedef struct EventBitsDefinition\r
+typedef struct EventGroupDef_t\r
 {\r
-       xEventBitsType uxEventBits;\r
-       xList xTasksWaitingForBits;             /*< List of tasks waiting for a bit to be set. */\r
-} xEVENT_BITS;\r
+       EventBits_t uxEventBits;\r
+       List_t xTasksWaitingForBits;            /*< List of tasks waiting for a bit to be set. */\r
 \r
-/* Used internally only. */\r
-typedef struct EVENT_GROUP_CALLBACK_PARAMTERS\r
-{\r
-       xEventGroupHandle xTargetEventGroup;\r
-       xEventBitsType xBitsToSet;\r
-} xEventGroupCallbackParameters;\r
+       #if( configUSE_TRACE_FACILITY == 1 )\r
+               UBaseType_t uxEventGroupNumber;\r
+       #endif\r
+\r
+       #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )\r
+               uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the event group is statically allocated to ensure no attempt is made to free the memory. */\r
+       #endif\r
+} EventGroup_t;\r
 \r
 /*-----------------------------------------------------------*/\r
 \r
-xEventGroupHandle xEventGroupCreate( void )\r
-{\r
-xEVENT_BITS *pxEventBits;\r
+/*\r
+ * Test the bits set in uxCurrentEventBits to see if the wait condition is met.\r
+ * The wait condition is defined by xWaitForAllBits.  If xWaitForAllBits is\r
+ * pdTRUE then the wait condition is met if all the bits set in uxBitsToWaitFor\r
+ * are also set in uxCurrentEventBits.  If xWaitForAllBits is pdFALSE then the\r
+ * wait condition is met if any of the bits set in uxBitsToWait for are also set\r
+ * in uxCurrentEventBits.\r
+ */\r
+static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits ) PRIVILEGED_FUNCTION;\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
+\r
+       EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t *pxEventGroupBuffer )\r
+       {\r
+       EventGroup_t *pxEventBits;\r
+\r
+               /* A StaticEventGroup_t object must be provided. */\r
+               configASSERT( pxEventGroupBuffer );\r
+\r
+               #if( configASSERT_DEFINED == 1 )\r
+               {\r
+                       /* Sanity check that the size of the structure used to declare a\r
+                       variable of type StaticEventGroup_t equals the size of the real\r
+                       event group structure. */\r
+                       volatile size_t xSize = sizeof( StaticEventGroup_t );\r
+                       configASSERT( xSize == sizeof( EventGroup_t ) );\r
+               } /*lint !e529 xSize is referenced if configASSERT() is defined. */\r
+               #endif /* configASSERT_DEFINED */\r
+\r
+               /* The user has provided a statically allocated event group - use it. */\r
+               pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 !e9087 EventGroup_t and StaticEventGroup_t are deliberately aliased for data hiding purposes and guaranteed to have the same size and alignment requirement - checked by configASSERT(). */\r
+\r
+               if( pxEventBits != NULL )\r
+               {\r
+                       pxEventBits->uxEventBits = 0;\r
+                       vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );\r
+\r
+                       #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
+                       {\r
+                               /* Both static and dynamic allocation can be used, so note that\r
+                               this event group was created statically in case the event group\r
+                               is later deleted. */\r
+                               pxEventBits->ucStaticallyAllocated = pdTRUE;\r
+                       }\r
+                       #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
+\r
+                       traceEVENT_GROUP_CREATE( pxEventBits );\r
+               }\r
+               else\r
+               {\r
+                       /* xEventGroupCreateStatic should only ever be called with\r
+                       pxEventGroupBuffer pointing to a pre-allocated (compile time\r
+                       allocated) StaticEventGroup_t variable. */\r
+                       traceEVENT_GROUP_CREATE_FAILED();\r
+               }\r
+\r
+               return pxEventBits;\r
+       }\r
+\r
+#endif /* configSUPPORT_STATIC_ALLOCATION */\r
+/*-----------------------------------------------------------*/\r
 \r
-       pxEventBits = pvPortMalloc( sizeof( xEVENT_BITS ) );\r
-       if( pxEventBits != NULL )\r
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
+\r
+       EventGroupHandle_t xEventGroupCreate( void )\r
        {\r
-               pxEventBits->uxEventBits = 0;\r
-               vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );\r
+       EventGroup_t *pxEventBits;\r
+\r
+               /* Allocate the event group.  Justification for MISRA deviation as\r
+               follows:  pvPortMalloc() always ensures returned memory blocks are\r
+               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 EventGroup_t structure - which (if you\r
+               follow it through) is the alignment requirements of the TickType_t type\r
+               (EventBits_t being of TickType_t itself).  Therefore, whenever the\r
+               stack alignment requirements are greater than or equal to the\r
+               TickType_t alignment requirements the cast is safe.  In other cases,\r
+               where the natural word size of the architecture is less than\r
+               sizeof( TickType_t ), the TickType_t variables will be accessed in two\r
+               or more reads operations, and the alignment requirements is only that\r
+               of each individual read. */\r
+               pxEventBits = ( EventGroup_t * ) pvPortMalloc( sizeof( EventGroup_t ) ); /*lint !e9087 !e9079 see comment above. */\r
+\r
+               if( pxEventBits != NULL )\r
+               {\r
+                       pxEventBits->uxEventBits = 0;\r
+                       vListInitialise( &( pxEventBits->xTasksWaitingForBits ) );\r
+\r
+                       #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
+                       {\r
+                               /* Both static and dynamic allocation can be used, so note this\r
+                               event group was allocated statically in case the event group is\r
+                               later deleted. */\r
+                               pxEventBits->ucStaticallyAllocated = pdFALSE;\r
+                       }\r
+                       #endif /* configSUPPORT_STATIC_ALLOCATION */\r
+\r
+                       traceEVENT_GROUP_CREATE( pxEventBits );\r
+               }\r
+               else\r
+               {\r
+                       traceEVENT_GROUP_CREATE_FAILED(); /*lint !e9063 Else branch only exists to allow tracing and does not generate code if trace macros are not defined. */\r
+               }\r
+\r
+               return pxEventBits;\r
        }\r
 \r
-       return ( xEventGroupHandle ) pxEventBits;\r
-}\r
+#endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
 /*-----------------------------------------------------------*/\r
 \r
-xEventBitsType xEventGroupSync( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet, xEventBitsType uxBitsToWaitFor, portTickType xTicksToWait )\r
+EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait )\r
 {\r
-xEventBitsType uxOriginalBitValue, uxReturn;\r
-xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
-portBASE_TYPE xYieldedAlready;\r
+EventBits_t uxOriginalBitValue, uxReturn;\r
+EventGroup_t *pxEventBits = xEventGroup;\r
+BaseType_t xAlreadyYielded;\r
+BaseType_t xTimeoutOccurred = pdFALSE;\r
 \r
+       configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );\r
+       configASSERT( uxBitsToWaitFor != 0 );\r
        #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
        {\r
                configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );\r
@@ -154,41 +211,53 @@ portBASE_TYPE xYieldedAlready;
 \r
                if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor )\r
                {\r
-                       /* All the rendezvous bits will have been set once this task set\r
-                       its bits - no need to block. */\r
+                       /* All the rendezvous bits are now set - no need to block. */\r
                        uxReturn = ( uxOriginalBitValue | uxBitsToSet );\r
 \r
                        /* Rendezvous always clear the bits.  They will have been cleared\r
                        already unless this is the only task in the rendezvous. */\r
-                       pxEventBits->uxEventBits &= uxBitsToWaitFor;\r
+                       pxEventBits->uxEventBits &= ~uxBitsToWaitFor;\r
 \r
                        xTicksToWait = 0;\r
                }\r
                else\r
                {\r
-                       if( xTicksToWait != ( portTickType ) 0 )\r
+                       if( xTicksToWait != ( TickType_t ) 0 )\r
                        {\r
+                               traceEVENT_GROUP_SYNC_BLOCK( xEventGroup, uxBitsToSet, uxBitsToWaitFor );\r
+\r
                                /* Store the bits that the calling task is waiting for in the\r
                                task's event list item so the kernel knows when a match is\r
                                found.  Then enter the blocked state. */\r
-                               vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | taskCLEAR_EVENTS_ON_EXIT_BIT | taskWAIT_FOR_ALL_BITS ), xTicksToWait );\r
+                               vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait );\r
+\r
+                               /* This assignment is obsolete as uxReturn will get set after\r
+                               the task unblocks, but some compilers mistakenly generate a\r
+                               warning about uxReturn being returned without being set if the\r
+                               assignment is omitted. */\r
+                               uxReturn = 0;\r
                        }\r
                        else\r
                        {\r
                                /* The rendezvous bits were not set, but no block time was\r
                                specified - just return the current event bit value. */\r
                                uxReturn = pxEventBits->uxEventBits;\r
+                               xTimeoutOccurred = pdTRUE;\r
                        }\r
                }\r
        }\r
-       xYieldedAlready = xTaskResumeAll();\r
+       xAlreadyYielded = xTaskResumeAll();\r
 \r
-       if( xTicksToWait != ( portTickType ) 0 )\r
+       if( xTicksToWait != ( TickType_t ) 0 )\r
        {\r
-               if( xYieldedAlready == pdFALSE )\r
+               if( xAlreadyYielded == pdFALSE )\r
                {\r
                        portYIELD_WITHIN_API();\r
                }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
 \r
                /* The task blocked to wait for its required bits to be set - at this\r
                point either the required bits were set or the block time expired.  If\r
@@ -196,32 +265,60 @@ portBASE_TYPE xYieldedAlready;
                event list item, and they should now be retrieved then cleared. */\r
                uxReturn = uxTaskResetEventItemValue();\r
 \r
-               if( ( uxReturn & taskUNBLOCKED_DUE_TO_BIT_SET ) == ( xEventBitsType ) 0 )\r
+               if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )\r
                {\r
                        /* The task timed out, just return the current event bit value. */\r
-                       uxReturn = pxEventBits->uxEventBits;\r
+                       taskENTER_CRITICAL();\r
+                       {\r
+                               uxReturn = pxEventBits->uxEventBits;\r
+\r
+                               /* Although the task got here because it timed out before the\r
+                               bits it was waiting for were set, it is possible that since it\r
+                               unblocked another task has set the bits.  If this is the case\r
+                               then it needs to clear the bits before exiting. */\r
+                               if( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor )\r
+                               {\r
+                                       pxEventBits->uxEventBits &= ~uxBitsToWaitFor;\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+                       }\r
+                       taskEXIT_CRITICAL();\r
+\r
+                       xTimeoutOccurred = pdTRUE;\r
                }\r
                else\r
                {\r
-                       /* The task unblocked because the bits were set.  Clear the control\r
-                       bits before returning the value. */\r
-                       uxReturn &= ~taskEVENT_BITS_CONTROL_BYTES;\r
+                       /* The task unblocked because the bits were set. */\r
                }\r
+\r
+               /* Control bits might be set as the task had blocked should not be\r
+               returned. */\r
+               uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;\r
        }\r
 \r
+       traceEVENT_GROUP_SYNC_END( xEventGroup, uxBitsToSet, uxBitsToWaitFor, xTimeoutOccurred );\r
+\r
+       /* Prevent compiler warnings when trace macros are not used. */\r
+       ( void ) xTimeoutOccurred;\r
+\r
        return uxReturn;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-xEventBitsType xEventGroupWaitBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToWaitFor, portBASE_TYPE xClearOnExit, portBASE_TYPE xWaitForAllBits, portTickType xTicksToWait )\r
+EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait )\r
 {\r
-xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
-const xEventBitsType uxCurrentEventBits = pxEventBits->uxEventBits;\r
-xEventBitsType uxReturn, uxControlBits = 0;\r
+EventGroup_t *pxEventBits = xEventGroup;\r
+EventBits_t uxReturn, uxControlBits = 0;\r
+BaseType_t xWaitConditionMet, xAlreadyYielded;\r
+BaseType_t xTimeoutOccurred = pdFALSE;\r
 \r
        /* Check the user is not attempting to wait on the bits used by the kernel\r
        itself, and that at least one bit is being requested. */\r
-       configASSERT( ( uxBitsToWaitFor & taskEVENT_BITS_CONTROL_BYTES ) == 0 );\r
+       configASSERT( xEventGroup );\r
+       configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );\r
        configASSERT( uxBitsToWaitFor != 0 );\r
        #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
        {\r
@@ -229,42 +326,36 @@ xEventBitsType uxReturn, uxControlBits = 0;
        }\r
        #endif\r
 \r
-       taskENTER_CRITICAL();\r
+       vTaskSuspendAll();\r
        {\r
-               if( xWaitForAllBits == pdFALSE )\r
-               {\r
-                       /* Task only has to wait for one bit within uxBitsToWaitFor to be set.  Is\r
-                       one already set? */\r
-                       if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( xEventBitsType ) 0 )\r
-                       {\r
-                               /* At least one of the bits was set.  No need to block. */\r
-                               xTicksToWait = 0;\r
-                       }\r
-               }\r
-               else\r
-               {\r
-                       /* Task has to wait for all the bits in uxBitsToWaitFor to be set.  Are they\r
-                       set already? */\r
-                       if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )\r
-                       {\r
-                               /* All the bits were set, no need to block. */\r
-                               xTicksToWait = 0;\r
-                       }\r
-               }\r
+               const EventBits_t uxCurrentEventBits = pxEventBits->uxEventBits;\r
+\r
+               /* Check to see if the wait condition is already met or not. */\r
+               xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits );\r
 \r
-               /* The task can return now if either its wait condition is already met\r
-               or the requested block time is 0. */\r
-               if( xTicksToWait == ( portTickType ) 0 )\r
+               if( xWaitConditionMet != pdFALSE )\r
                {\r
-                       /* No need to block, just set the return value. */\r
+                       /* The wait condition has already been met so there is no need to\r
+                       block. */\r
                        uxReturn = uxCurrentEventBits;\r
+                       xTicksToWait = ( TickType_t ) 0;\r
 \r
+                       /* Clear the wait bits if requested to do so. */\r
                        if( xClearOnExit != pdFALSE )\r
                        {\r
-                               /* The user requested the bits be cleared again prior to exiting\r
-                               this function. */\r
                                pxEventBits->uxEventBits &= ~uxBitsToWaitFor;\r
                        }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
+               }\r
+               else if( xTicksToWait == ( TickType_t ) 0 )\r
+               {\r
+                       /* The wait condition has not been met, but no block time was\r
+                       specified, so just return the current value. */\r
+                       uxReturn = uxCurrentEventBits;\r
+                       xTimeoutOccurred = pdTRUE;\r
                }\r
                else\r
                {\r
@@ -274,66 +365,119 @@ xEventBitsType uxReturn, uxControlBits = 0;
                        unblock the task. */\r
                        if( xClearOnExit != pdFALSE )\r
                        {\r
-                               uxControlBits |= taskCLEAR_EVENTS_ON_EXIT_BIT;\r
+                               uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT;\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
                        }\r
 \r
                        if( xWaitForAllBits != pdFALSE )\r
                        {\r
-                               uxControlBits |= taskWAIT_FOR_ALL_BITS;\r
+                               uxControlBits |= eventWAIT_FOR_ALL_BITS;\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
                        }\r
 \r
                        /* Store the bits that the calling task is waiting for in the\r
                        task's event list item so the kernel knows when a match is\r
                        found.  Then enter the blocked state. */\r
                        vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait );\r
-                       portYIELD_WITHIN_API();\r
+\r
+                       /* This is obsolete as it will get set after the task unblocks, but\r
+                       some compilers mistakenly generate a warning about the variable\r
+                       being returned without being set if it is not done. */\r
+                       uxReturn = 0;\r
+\r
+                       traceEVENT_GROUP_WAIT_BITS_BLOCK( xEventGroup, uxBitsToWaitFor );\r
                }\r
        }\r
-       taskEXIT_CRITICAL();\r
+       xAlreadyYielded = xTaskResumeAll();\r
 \r
-       if( xTicksToWait != ( portTickType ) 0 )\r
+       if( xTicksToWait != ( TickType_t ) 0 )\r
        {\r
+               if( xAlreadyYielded == pdFALSE )\r
+               {\r
+                       portYIELD_WITHIN_API();\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
+\r
                /* The task blocked to wait for its required bits to be set - at this\r
                point either the required bits were set or the block time expired.  If\r
                the required bits were set they will have been stored in the task's\r
                event list item, and they should now be retrieved then cleared. */\r
                uxReturn = uxTaskResetEventItemValue();\r
 \r
-               if( ( uxReturn & taskUNBLOCKED_DUE_TO_BIT_SET ) == ( xEventBitsType ) 0 )\r
+               if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( EventBits_t ) 0 )\r
                {\r
-                       /* The task timed out, just return the current event bit value. */\r
-                       uxReturn = pxEventBits->uxEventBits;\r
+                       taskENTER_CRITICAL();\r
+                       {\r
+                               /* The task timed out, just return the current event bit value. */\r
+                               uxReturn = pxEventBits->uxEventBits;\r
+\r
+                               /* It is possible that the event bits were updated between this\r
+                               task leaving the Blocked state and running again. */\r
+                               if( prvTestWaitCondition( uxReturn, uxBitsToWaitFor, xWaitForAllBits ) != pdFALSE )\r
+                               {\r
+                                       if( xClearOnExit != pdFALSE )\r
+                                       {\r
+                                               pxEventBits->uxEventBits &= ~uxBitsToWaitFor;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
+                               xTimeoutOccurred = pdTRUE;\r
+                       }\r
+                       taskEXIT_CRITICAL();\r
                }\r
                else\r
                {\r
-                       /* The task unblocked because the bits were set.  Clear the control\r
-                       bits before returning the value. */\r
-                       uxReturn &= ~taskEVENT_BITS_CONTROL_BYTES;\r
+                       /* The task unblocked because the bits were set. */\r
                }\r
+\r
+               /* The task blocked so control bits may have been set. */\r
+               uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES;\r
        }\r
+       traceEVENT_GROUP_WAIT_BITS_END( xEventGroup, uxBitsToWaitFor, xTimeoutOccurred );\r
+\r
+       /* Prevent compiler warnings when trace macros are not used. */\r
+       ( void ) xTimeoutOccurred;\r
 \r
        return uxReturn;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-xEventBitsType xEventGroupClearBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToClear )\r
+EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )\r
 {\r
-xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
-xEventBitsType uxReturn;\r
+EventGroup_t *pxEventBits = xEventGroup;\r
+EventBits_t uxReturn;\r
 \r
        /* Check the user is not attempting to clear the bits used by the kernel\r
        itself. */\r
-       configASSERT( ( uxBitsToClear & taskEVENT_BITS_CONTROL_BYTES ) == 0 );\r
+       configASSERT( xEventGroup );\r
+       configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );\r
 \r
-       uxBitsToClear = ~uxBitsToClear;\r
        taskENTER_CRITICAL();\r
        {\r
+               traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );\r
+\r
                /* The value returned is the event group value prior to the bits being\r
                cleared. */\r
                uxReturn = pxEventBits->uxEventBits;\r
 \r
                /* Clear the bits. */\r
-               pxEventBits->uxEventBits &= uxBitsToClear;\r
+               pxEventBits->uxEventBits &= ~uxBitsToClear;\r
        }\r
        taskEXIT_CRITICAL();\r
 \r
@@ -341,23 +485,57 @@ xEventBitsType uxReturn;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet )\r
+#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )\r
+\r
+       BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear )\r
+       {\r
+               BaseType_t xReturn;\r
+\r
+               traceEVENT_GROUP_CLEAR_BITS_FROM_ISR( xEventGroup, uxBitsToClear );\r
+               xReturn = xTimerPendFunctionCallFromISR( vEventGroupClearBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToClear, NULL ); /*lint !e9087 Can't avoid cast to void* as a generic callback function not specific to this use case. Callback casts back to original type so safe. */\r
+\r
+               return xReturn;\r
+       }\r
+\r
+#endif\r
+/*-----------------------------------------------------------*/\r
+\r
+EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )\r
 {\r
-xListItem *pxListItem, *pxNext;\r
-xListItem const *pxListEnd;\r
-xList *pxList;\r
-xEventBitsType uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;\r
-xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
-portBASE_TYPE xMatchFound = pdFALSE;\r
+UBaseType_t uxSavedInterruptStatus;\r
+EventGroup_t const * const pxEventBits = xEventGroup;\r
+EventBits_t uxReturn;\r
+\r
+       uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
+       {\r
+               uxReturn = pxEventBits->uxEventBits;\r
+       }\r
+       portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
+\r
+       return uxReturn;\r
+} /*lint !e818 EventGroupHandle_t is a typedef used in other functions to so can't be pointer to const. */\r
+/*-----------------------------------------------------------*/\r
+\r
+EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet )\r
+{\r
+ListItem_t *pxListItem, *pxNext;\r
+ListItem_t const *pxListEnd;\r
+List_t const * pxList;\r
+EventBits_t uxBitsToClear = 0, uxBitsWaitedFor, uxControlBits;\r
+EventGroup_t *pxEventBits = xEventGroup;\r
+BaseType_t xMatchFound = pdFALSE;\r
 \r
        /* Check the user is not attempting to set the bits used by the kernel\r
        itself. */\r
-       configASSERT( ( uxBitsToSet & taskEVENT_BITS_CONTROL_BYTES ) == 0 );\r
+       configASSERT( xEventGroup );\r
+       configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );\r
 \r
        pxList = &( pxEventBits->xTasksWaitingForBits );\r
-       pxListEnd = listGET_END_MARKER( pxList ); /*lint !e826 !e740 The mini list structure is used as the list end to save RAM.  This is checked and valid. */\r
+       pxListEnd = listGET_END_MARKER( pxList ); /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM.  This is checked and valid. */\r
        vTaskSuspendAll();\r
        {\r
+               traceEVENT_GROUP_SET_BITS( xEventGroup, uxBitsToSet );\r
+\r
                pxListItem = listGET_HEAD_ENTRY( pxList );\r
 \r
                /* Set the bits. */\r
@@ -371,16 +549,20 @@ portBASE_TYPE xMatchFound = pdFALSE;
                        xMatchFound = pdFALSE;\r
 \r
                        /* Split the bits waited for from the control bits. */\r
-                       uxControlBits = uxBitsWaitedFor & taskEVENT_BITS_CONTROL_BYTES;\r
-                       uxBitsWaitedFor &= ~taskEVENT_BITS_CONTROL_BYTES;\r
+                       uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES;\r
+                       uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES;\r
 \r
-                       if( ( uxControlBits & taskWAIT_FOR_ALL_BITS ) == ( xEventBitsType ) 0 )\r
+                       if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( EventBits_t ) 0 )\r
                        {\r
                                /* Just looking for single bit being set. */\r
-                               if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( xEventBitsType ) 0 )\r
+                               if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( EventBits_t ) 0 )\r
                                {\r
                                        xMatchFound = pdTRUE;\r
                                }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
                        }\r
                        else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor )\r
                        {\r
@@ -395,17 +577,21 @@ portBASE_TYPE xMatchFound = pdFALSE;
                        if( xMatchFound != pdFALSE )\r
                        {\r
                                /* The bits match.  Should the bits be cleared on exit? */\r
-                               if( ( uxControlBits & taskCLEAR_EVENTS_ON_EXIT_BIT ) != ( xEventBitsType ) 0 )\r
+                               if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( EventBits_t ) 0 )\r
                                {\r
                                        uxBitsToClear |= uxBitsWaitedFor;\r
                                }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
 \r
                                /* Store the actual event flag value in the task's event list\r
                                item before removing the task from the event list.  The\r
-                               taskUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows\r
+                               eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows\r
                                that is was unblocked due to its required bits matching, rather\r
                                than because it timed out. */\r
-                               ( void ) xTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | taskUNBLOCKED_DUE_TO_BIT_SET );\r
+                               vTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET );\r
                        }\r
 \r
                        /* Move onto the next list item.  Note pxListItem->pxNext is not\r
@@ -414,7 +600,7 @@ portBASE_TYPE xMatchFound = pdFALSE;
                        pxListItem = pxNext;\r
                }\r
 \r
-               /* Clear any bits that matched when the taskCLEAR_EVENTS_ON_EXIT_BIT\r
+               /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT\r
                bit was set in the control word. */\r
                pxEventBits->uxEventBits &= ~uxBitsToClear;\r
        }\r
@@ -424,22 +610,43 @@ portBASE_TYPE xMatchFound = pdFALSE;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-void vEventGroupDelete( xEventGroupHandle xEventGroup )\r
+void vEventGroupDelete( EventGroupHandle_t xEventGroup )\r
 {\r
-xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup;\r
-const xList *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );\r
+EventGroup_t *pxEventBits = xEventGroup;\r
+const List_t *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );\r
 \r
        vTaskSuspendAll();\r
        {\r
-               while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( unsigned portBASE_TYPE ) 0 )\r
+               traceEVENT_GROUP_DELETE( xEventGroup );\r
+\r
+               while( listCURRENT_LIST_LENGTH( pxTasksWaitingForBits ) > ( UBaseType_t ) 0 )\r
                {\r
                        /* Unblock the task, returning 0 as the event list is being deleted\r
-                       and     cannot therefore have any bits set. */\r
-                       configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( xListItem * ) &( pxTasksWaitingForBits->xListEnd ) );\r
-                       ( void ) xTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, ( portTickType ) taskUNBLOCKED_DUE_TO_BIT_SET );\r
+                       and cannot therefore have any bits set. */\r
+                       configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( const ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );\r
+                       vTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );\r
                }\r
 \r
-               vPortFree( pxEventBits );\r
+               #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )\r
+               {\r
+                       /* The event group can only have been allocated dynamically - free\r
+                       it again. */\r
+                       vPortFree( pxEventBits );\r
+               }\r
+               #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
+               {\r
+                       /* The event group could have been allocated statically or\r
+                       dynamically, so check before attempting to free the memory. */\r
+                       if( pxEventBits->ucStaticallyAllocated == ( uint8_t ) pdFALSE )\r
+                       {\r
+                               vPortFree( pxEventBits );\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
+               }\r
+               #endif /* configSUPPORT_DYNAMIC_ALLOCATION */\r
        }\r
        ( void ) xTaskResumeAll();\r
 }\r
@@ -447,9 +654,100 @@ const xList *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits );
 \r
 /* For internal use only - execute a 'set bits' command that was pended from\r
 an interrupt. */\r
-void vEventGroupSetBitsCallback( void *pvEventGroup, unsigned long ulBitsToSet )\r
+void vEventGroupSetBitsCallback( void *pvEventGroup, const uint32_t ulBitsToSet )\r
+{\r
+       ( void ) xEventGroupSetBits( pvEventGroup, ( EventBits_t ) ulBitsToSet ); /*lint !e9079 Can't avoid cast to void* as a generic timer callback prototype. Callback casts back to original type so safe. */\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/* For internal use only - execute a 'clear bits' command that was pended from\r
+an interrupt. */\r
+void vEventGroupClearBitsCallback( void *pvEventGroup, const uint32_t ulBitsToClear )\r
 {\r
-       ( void ) xEventGroupSetBits( pvEventGroup, ( xEventBitsType ) ulBitsToSet );\r
+       ( void ) xEventGroupClearBits( pvEventGroup, ( EventBits_t ) ulBitsToClear ); /*lint !e9079 Can't avoid cast to void* as a generic timer callback prototype. Callback casts back to original type so safe. */\r
 }\r
+/*-----------------------------------------------------------*/\r
+\r
+static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits, const EventBits_t uxBitsToWaitFor, const BaseType_t xWaitForAllBits )\r
+{\r
+BaseType_t xWaitConditionMet = pdFALSE;\r
+\r
+       if( xWaitForAllBits == pdFALSE )\r
+       {\r
+               /* Task only has to wait for one bit within uxBitsToWaitFor to be\r
+               set.  Is one already set? */\r
+               if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( EventBits_t ) 0 )\r
+               {\r
+                       xWaitConditionMet = pdTRUE;\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
+       }\r
+       else\r
+       {\r
+               /* Task has to wait for all the bits in uxBitsToWaitFor to be set.\r
+               Are they set already? */\r
+               if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor )\r
+               {\r
+                       xWaitConditionMet = pdTRUE;\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
+       }\r
+\r
+       return xWaitConditionMet;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 1 ) )\r
+\r
+       BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, BaseType_t *pxHigherPriorityTaskWoken )\r
+       {\r
+       BaseType_t xReturn;\r
+\r
+               traceEVENT_GROUP_SET_BITS_FROM_ISR( xEventGroup, uxBitsToSet );\r
+               xReturn = xTimerPendFunctionCallFromISR( vEventGroupSetBitsCallback, ( void * ) xEventGroup, ( uint32_t ) uxBitsToSet, pxHigherPriorityTaskWoken ); /*lint !e9087 Can't avoid cast to void* as a generic callback function not specific to this use case. Callback casts back to original type so safe. */\r
+\r
+               return xReturn;\r
+       }\r
+\r
+#endif\r
+/*-----------------------------------------------------------*/\r
+\r
+#if (configUSE_TRACE_FACILITY == 1)\r
+\r
+       UBaseType_t uxEventGroupGetNumber( void* xEventGroup )\r
+       {\r
+       UBaseType_t xReturn;\r
+       EventGroup_t const *pxEventBits = ( EventGroup_t * ) xEventGroup; /*lint !e9087 !e9079 EventGroupHandle_t is a pointer to an EventGroup_t, but EventGroupHandle_t is kept opaque outside of this file for data hiding purposes. */\r
+\r
+               if( xEventGroup == NULL )\r
+               {\r
+                       xReturn = 0;\r
+               }\r
+               else\r
+               {\r
+                       xReturn = pxEventBits->uxEventGroupNumber;\r
+               }\r
+\r
+               return xReturn;\r
+       }\r
+\r
+#endif /* configUSE_TRACE_FACILITY */\r
+/*-----------------------------------------------------------*/\r
+\r
+#if ( configUSE_TRACE_FACILITY == 1 )\r
+\r
+       void vEventGroupSetNumber( void * xEventGroup, UBaseType_t uxEventGroupNumber )\r
+       {\r
+               ( ( EventGroup_t * ) xEventGroup )->uxEventGroupNumber = uxEventGroupNumber; /*lint !e9087 !e9079 EventGroupHandle_t is a pointer to an EventGroup_t, but EventGroupHandle_t is kept opaque outside of this file for data hiding purposes. */\r
+       }\r
+\r
+#endif /* configUSE_TRACE_FACILITY */\r
+/*-----------------------------------------------------------*/\r
 \r
 \r