From: rtel Date: Mon, 23 Dec 2013 16:02:03 +0000 (+0000) Subject: In process of module testing event_groups.c. X-Git-Tag: V8.0.0rc1~24 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=41d0034b019a826a236301e429ad95f1c250ee00;p=freertos In process of module testing event_groups.c. Introduce xPortRunning variable into Win32 simulator port layer. Add port optimised task selection macro for the GCC Win32 port layer (the MSVC version has had one for a while). Ensure the event list item value does not get modified by code in tasks.c (priority inheritance, or priority change) when it is in use by the event group implementation. git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@2137 1d2547de-c912-0410-9cb9-b8ca96c0e9e2 --- diff --git a/FreeRTOS/Source/event_groups.c b/FreeRTOS/Source/event_groups.c index 644539965..756f2f64e 100644 --- a/FreeRTOS/Source/event_groups.c +++ b/FreeRTOS/Source/event_groups.c @@ -91,17 +91,19 @@ privileged Vs unprivileged linkage and placement. */ #error INCLUDE_xTimerPendCallbackFromISR must also be set to one to make the xEventGroupSetBitFromISR() function available. #endif - +/* The following bit fields convey control information in a task's event list +item value. It is important they don't clash with the +taskEVENT_LIST_ITEM_VALUE_IN_USE definition. */ #if configUSE_16_BIT_TICKS == 1 - #define taskCLEAR_EVENTS_ON_EXIT_BIT 0x0100U - #define taskUNBLOCKED_DUE_TO_BIT_SET 0x0200U - #define taskWAIT_FOR_ALL_BITS 0x0400U - #define taskEVENT_BITS_CONTROL_BYTES 0xff00U + #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x0100U + #define eventUNBLOCKED_DUE_TO_BIT_SET 0x0200U + #define eventWAIT_FOR_ALL_BITS 0x0400U + #define eventEVENT_BITS_CONTROL_BYTES 0xff00U #else - #define taskCLEAR_EVENTS_ON_EXIT_BIT 0x01000000UL - #define taskUNBLOCKED_DUE_TO_BIT_SET 0x02000000UL - #define taskWAIT_FOR_ALL_BITS 0x04000000UL - #define taskEVENT_BITS_CONTROL_BYTES 0xff000000UL + #define eventCLEAR_EVENTS_ON_EXIT_BIT 0x01000000UL + #define eventUNBLOCKED_DUE_TO_BIT_SET 0x02000000UL + #define eventWAIT_FOR_ALL_BITS 0x04000000UL + #define eventEVENT_BITS_CONTROL_BYTES 0xff000000UL #endif typedef struct EventBitsDefinition @@ -119,6 +121,18 @@ typedef struct EVENT_GROUP_CALLBACK_PARAMTERS /*-----------------------------------------------------------*/ +/* + * Test the bits set in uxCurrentEventBits to see if the wait condition is met. + * The wait condition is defined by xWaitForAllBits. If xWaitForAllBits is + * pdTRUE then the wait condition is met if all the bits set in uxBitsToWaitFor + * are also set in uxCurrentEventBits. If xWaitForAllBits is pdFALSE then the + * wait condition is met if any of the bits set in uxBitsToWait for are also set + * in uxCurrentEventBits. + */ +static portBASE_TYPE prvTestWaitCondition( const xEventBitsType uxCurrentEventBits, const xEventBitsType uxBitsToWaitFor, const portBASE_TYPE xWaitForAllBits ); + +/*-----------------------------------------------------------*/ + xEventGroupHandle xEventGroupCreate( void ) { xEVENT_BITS *pxEventBits; @@ -128,7 +142,7 @@ xEVENT_BITS *pxEventBits; { pxEventBits->uxEventBits = 0; vListInitialise( &( pxEventBits->xTasksWaitingForBits ) ); - traceEVENT_GROUP_CREATE( pxEventBits ); + traceEVENT_GROUP_CREATE( pxEventBits ); } else { @@ -139,12 +153,14 @@ xEVENT_BITS *pxEventBits; } /*-----------------------------------------------------------*/ -xEventBitsType xEventGroupSync( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet, xEventBitsType uxBitsToWaitFor, portTickType xTicksToWait ) +xEventBitsType xEventGroupSync( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToSet, const xEventBitsType uxBitsToWaitFor, portTickType xTicksToWait ) { xEventBitsType uxOriginalBitValue, uxReturn; xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup; -portBASE_TYPE xYieldedAlready; +portBASE_TYPE xAlreadyYielded; + configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 ); + configASSERT( uxBitsToWaitFor != 0 ); #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) { configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) ); @@ -161,8 +177,7 @@ portBASE_TYPE xYieldedAlready; if( ( ( uxOriginalBitValue | uxBitsToSet ) & uxBitsToWaitFor ) == uxBitsToWaitFor ) { - /* All the rendezvous bits will have been set once this task set - its bits - no need to block. */ + /* All the rendezvous bits are now set - no need to block. */ uxReturn = ( uxOriginalBitValue | uxBitsToSet ); /* Rendezvous always clear the bits. They will have been cleared @@ -178,11 +193,12 @@ portBASE_TYPE xYieldedAlready; /* Store the bits that the calling task is waiting for in the task's event list item so the kernel knows when a match is found. Then enter the blocked state. */ - vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | taskCLEAR_EVENTS_ON_EXIT_BIT | taskWAIT_FOR_ALL_BITS ), xTicksToWait ); + vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | eventCLEAR_EVENTS_ON_EXIT_BIT | eventWAIT_FOR_ALL_BITS ), xTicksToWait ); - /* This is obsolete as it will get set after the task unblocks, - but some compilers mistakenly generate a warning about the - variable being returned without being set if it is not done. */ + /* This assignment is obsolete as uxReturn will get set after + the task unblocks, but some compilers mistakenly generate a + warning about uxReturn being returned without being set if the + assignment is omitted. */ uxReturn = 0; } else @@ -193,14 +209,18 @@ portBASE_TYPE xYieldedAlready; } } } - xYieldedAlready = xTaskResumeAll(); + xAlreadyYielded = xTaskResumeAll(); if( xTicksToWait != ( portTickType ) 0 ) { - if( xYieldedAlready == pdFALSE ) + if( xAlreadyYielded == pdFALSE ) { portYIELD_WITHIN_API(); } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } /* The task blocked to wait for its required bits to be set - at this point either the required bits were set or the block time expired. If @@ -208,16 +228,33 @@ portBASE_TYPE xYieldedAlready; event list item, and they should now be retrieved then cleared. */ uxReturn = uxTaskResetEventItemValue(); - if( ( uxReturn & taskUNBLOCKED_DUE_TO_BIT_SET ) == ( xEventBitsType ) 0 ) + if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( xEventBitsType ) 0 ) { /* The task timed out, just return the current event bit value. */ - uxReturn = pxEventBits->uxEventBits; + taskENTER_CRITICAL(); + { + uxReturn = pxEventBits->uxEventBits; + + /* Although the task got here because it timed out before the + bits it was waiting for were set, it is possible that since it + unblocked another task has set the bits. If this is the case + then it may be required to clear the bits before exiting. */ + if( ( uxReturn & uxBitsToWaitFor ) == uxBitsToWaitFor ) + { + pxEventBits->uxEventBits &= ~uxBitsToWaitFor; + } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } + } + taskEXIT_CRITICAL(); } else { /* The task unblocked because the bits were set. Clear the control bits before returning the value. */ - uxReturn &= ~taskEVENT_BITS_CONTROL_BYTES; + uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES; } } @@ -226,14 +263,15 @@ portBASE_TYPE xYieldedAlready; } /*-----------------------------------------------------------*/ -xEventBitsType xEventGroupWaitBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToWaitFor, portBASE_TYPE xClearOnExit, portBASE_TYPE xWaitForAllBits, portTickType xTicksToWait ) +xEventBitsType xEventGroupWaitBits( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToWaitFor, const portBASE_TYPE xClearOnExit, const portBASE_TYPE xWaitForAllBits, portTickType xTicksToWait ) { xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup; xEventBitsType uxReturn, uxControlBits = 0; +portBASE_TYPE xWaitConditionMet, xAlreadyYielded; /* Check the user is not attempting to wait on the bits used by the kernel itself, and that at least one bit is being requested. */ - configASSERT( ( uxBitsToWaitFor & taskEVENT_BITS_CONTROL_BYTES ) == 0 ); + configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 ); configASSERT( uxBitsToWaitFor != 0 ); #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) { @@ -241,46 +279,37 @@ xEventBitsType uxReturn, uxControlBits = 0; } #endif - taskENTER_CRITICAL(); + vTaskSuspendAll(); { const xEventBitsType uxCurrentEventBits = pxEventBits->uxEventBits; traceEVENT_GROUP_WAIT_BITS_START( xEventGroup, uxBitsToWaitFor ); - if( xWaitForAllBits == pdFALSE ) - { - /* Task only has to wait for one bit within uxBitsToWaitFor to be set. Is - one already set? */ - if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( xEventBitsType ) 0 ) - { - /* At least one of the bits was set. No need to block. */ - xTicksToWait = 0; - } - } - else - { - /* Task has to wait for all the bits in uxBitsToWaitFor to be set. Are they - set already? */ - if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor ) - { - /* All the bits were set, no need to block. */ - xTicksToWait = 0; - } - } + /* Check to see if the wait condition is already met or not. */ + xWaitConditionMet = prvTestWaitCondition( uxCurrentEventBits, uxBitsToWaitFor, xWaitForAllBits ); - /* The task can return now if either its wait condition is already met - or the requested block time is 0. */ - if( xTicksToWait == ( portTickType ) 0 ) + if( xWaitConditionMet != pdFALSE ) { - /* No need to block, just set the return value. */ + /* The wait condition has already been met so there is no need to + block. */ uxReturn = uxCurrentEventBits; + xTicksToWait = ( portTickType ) 0; + /* Clear the wait bits if requested to do so. */ if( xClearOnExit != pdFALSE ) { - /* The user requested the bits be cleared again prior to exiting - this function. */ pxEventBits->uxEventBits &= ~uxBitsToWaitFor; } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } + } + else if( xTicksToWait == ( portTickType ) 0 ) + { + /* The wait condition has not been met, but no block time was + specified, so just return the current value. */ + uxReturn = uxCurrentEventBits; } else { @@ -290,19 +319,26 @@ xEventBitsType uxReturn, uxControlBits = 0; unblock the task. */ if( xClearOnExit != pdFALSE ) { - uxControlBits |= taskCLEAR_EVENTS_ON_EXIT_BIT; + uxControlBits |= eventCLEAR_EVENTS_ON_EXIT_BIT; + } + else + { + mtBRANCH_TEST_INSTRUCTION(); } if( xWaitForAllBits != pdFALSE ) { - uxControlBits |= taskWAIT_FOR_ALL_BITS; + uxControlBits |= eventWAIT_FOR_ALL_BITS; + } + else + { + mtBRANCH_TEST_INSTRUCTION(); } /* Store the bits that the calling task is waiting for in the task's event list item so the kernel knows when a match is found. Then enter the blocked state. */ vTaskPlaceOnUnorderedEventList( &( pxEventBits->xTasksWaitingForBits ), ( uxBitsToWaitFor | uxControlBits ), xTicksToWait ); - portYIELD_WITHIN_API(); /* This is obsolete as it will get set after the task unblocks, but some compilers mistakenly generate a warning about the variable @@ -310,26 +346,57 @@ xEventBitsType uxReturn, uxControlBits = 0; uxReturn = 0; } } - taskEXIT_CRITICAL(); + xAlreadyYielded = xTaskResumeAll(); if( xTicksToWait != ( portTickType ) 0 ) { + if( xAlreadyYielded == pdFALSE ) + { + portYIELD_WITHIN_API(); + } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } + /* The task blocked to wait for its required bits to be set - at this point either the required bits were set or the block time expired. If the required bits were set they will have been stored in the task's event list item, and they should now be retrieved then cleared. */ uxReturn = uxTaskResetEventItemValue(); - if( ( uxReturn & taskUNBLOCKED_DUE_TO_BIT_SET ) == ( xEventBitsType ) 0 ) + if( ( uxReturn & eventUNBLOCKED_DUE_TO_BIT_SET ) == ( xEventBitsType ) 0 ) { - /* The task timed out, just return the current event bit value. */ - uxReturn = pxEventBits->uxEventBits; + taskENTER_CRITICAL(); + { + /* The task timed out, just return the current event bit value. */ + uxReturn = pxEventBits->uxEventBits; + + /* It is possible that the event bits were updated between this + task leaving the Blocked state and running again. */ + if( prvTestWaitCondition( uxReturn, uxBitsToWaitFor, xWaitForAllBits ) != pdFALSE ) + { + if( xClearOnExit != pdFALSE ) + { + pxEventBits->uxEventBits &= ~uxBitsToWaitFor; + } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } + } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } + } + taskEXIT_CRITICAL(); } else { /* The task unblocked because the bits were set. Clear the control bits before returning the value. */ - uxReturn &= ~taskEVENT_BITS_CONTROL_BYTES; + uxReturn &= ~eventEVENT_BITS_CONTROL_BYTES; } } @@ -338,26 +405,25 @@ xEventBitsType uxReturn, uxControlBits = 0; } /*-----------------------------------------------------------*/ -xEventBitsType xEventGroupClearBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToClear ) +xEventBitsType xEventGroupClearBits( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToClear ) { xEVENT_BITS *pxEventBits = ( xEVENT_BITS * ) xEventGroup; xEventBitsType uxReturn; /* Check the user is not attempting to clear the bits used by the kernel itself. */ - configASSERT( ( uxBitsToClear & taskEVENT_BITS_CONTROL_BYTES ) == 0 ); + configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 ); - uxBitsToClear = ~uxBitsToClear; taskENTER_CRITICAL(); { - traceEVENT_GROUP_CLEAR_BITS( xEventGroup, ~uxBitsToClear ); + traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear ); /* The value returned is the event group value prior to the bits being cleared. */ uxReturn = pxEventBits->uxEventBits; /* Clear the bits. */ - pxEventBits->uxEventBits &= uxBitsToClear; + pxEventBits->uxEventBits &= ~uxBitsToClear; } taskEXIT_CRITICAL(); @@ -365,7 +431,7 @@ xEventBitsType uxReturn; } /*-----------------------------------------------------------*/ -xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet ) +xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToSet ) { xListItem *pxListItem, *pxNext; xListItem const *pxListEnd; @@ -376,7 +442,7 @@ portBASE_TYPE xMatchFound = pdFALSE; /* Check the user is not attempting to set the bits used by the kernel itself. */ - configASSERT( ( uxBitsToSet & taskEVENT_BITS_CONTROL_BYTES ) == 0 ); + configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 ); pxList = &( pxEventBits->xTasksWaitingForBits ); 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. */ @@ -397,16 +463,20 @@ portBASE_TYPE xMatchFound = pdFALSE; xMatchFound = pdFALSE; /* Split the bits waited for from the control bits. */ - uxControlBits = uxBitsWaitedFor & taskEVENT_BITS_CONTROL_BYTES; - uxBitsWaitedFor &= ~taskEVENT_BITS_CONTROL_BYTES; + uxControlBits = uxBitsWaitedFor & eventEVENT_BITS_CONTROL_BYTES; + uxBitsWaitedFor &= ~eventEVENT_BITS_CONTROL_BYTES; - if( ( uxControlBits & taskWAIT_FOR_ALL_BITS ) == ( xEventBitsType ) 0 ) + if( ( uxControlBits & eventWAIT_FOR_ALL_BITS ) == ( xEventBitsType ) 0 ) { /* Just looking for single bit being set. */ if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) != ( xEventBitsType ) 0 ) { xMatchFound = pdTRUE; } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } } else if( ( uxBitsWaitedFor & pxEventBits->uxEventBits ) == uxBitsWaitedFor ) { @@ -421,17 +491,21 @@ portBASE_TYPE xMatchFound = pdFALSE; if( xMatchFound != pdFALSE ) { /* The bits match. Should the bits be cleared on exit? */ - if( ( uxControlBits & taskCLEAR_EVENTS_ON_EXIT_BIT ) != ( xEventBitsType ) 0 ) + if( ( uxControlBits & eventCLEAR_EVENTS_ON_EXIT_BIT ) != ( xEventBitsType ) 0 ) { uxBitsToClear |= uxBitsWaitedFor; } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } /* Store the actual event flag value in the task's event list item before removing the task from the event list. The - taskUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows + eventUNBLOCKED_DUE_TO_BIT_SET bit is set so the task knows that is was unblocked due to its required bits matching, rather than because it timed out. */ - ( void ) xTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | taskUNBLOCKED_DUE_TO_BIT_SET ); + ( void ) xTaskRemoveFromUnorderedEventList( pxListItem, pxEventBits->uxEventBits | eventUNBLOCKED_DUE_TO_BIT_SET ); } /* Move onto the next list item. Note pxListItem->pxNext is not @@ -440,7 +514,7 @@ portBASE_TYPE xMatchFound = pdFALSE; pxListItem = pxNext; } - /* Clear any bits that matched when the taskCLEAR_EVENTS_ON_EXIT_BIT + /* Clear any bits that matched when the eventCLEAR_EVENTS_ON_EXIT_BIT bit was set in the control word. */ pxEventBits->uxEventBits &= ~uxBitsToClear; } @@ -464,7 +538,7 @@ const xList *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits ); /* Unblock the task, returning 0 as the event list is being deleted and cannot therefore have any bits set. */ configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( xListItem * ) &( pxTasksWaitingForBits->xListEnd ) ); - ( void ) xTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, ( portTickType ) taskUNBLOCKED_DUE_TO_BIT_SET ); + ( void ) xTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, ( portTickType ) eventUNBLOCKED_DUE_TO_BIT_SET ); } vPortFree( pxEventBits ); @@ -475,9 +549,44 @@ const xList *pxTasksWaitingForBits = &( pxEventBits->xTasksWaitingForBits ); /* For internal use only - execute a 'set bits' command that was pended from an interrupt. */ -void vEventGroupSetBitsCallback( void *pvEventGroup, unsigned long ulBitsToSet ) +void vEventGroupSetBitsCallback( void *pvEventGroup, const unsigned long ulBitsToSet ) { ( void ) xEventGroupSetBits( pvEventGroup, ( xEventBitsType ) ulBitsToSet ); } +/*-----------------------------------------------------------*/ + +static portBASE_TYPE prvTestWaitCondition( const xEventBitsType uxCurrentEventBits, const xEventBitsType uxBitsToWaitFor, const portBASE_TYPE xWaitForAllBits ) +{ +portBASE_TYPE xWaitConditionMet = pdFALSE; + + if( xWaitForAllBits == pdFALSE ) + { + /* Task only has to wait for one bit within uxBitsToWaitFor to be + set. Is one already set? */ + if( ( uxCurrentEventBits & uxBitsToWaitFor ) != ( xEventBitsType ) 0 ) + { + xWaitConditionMet = pdTRUE; + } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } + } + else + { + /* Task has to wait for all the bits in uxBitsToWaitFor to be set. + Are they set already? */ + if( ( uxCurrentEventBits & uxBitsToWaitFor ) == uxBitsToWaitFor ) + { + xWaitConditionMet = pdTRUE; + } + else + { + mtBRANCH_TEST_INSTRUCTION(); + } + } + + return xWaitConditionMet; +} diff --git a/FreeRTOS/Source/include/FreeRTOS.h b/FreeRTOS/Source/include/FreeRTOS.h index 7b29229d3..c53f337b1 100644 --- a/FreeRTOS/Source/include/FreeRTOS.h +++ b/FreeRTOS/Source/include/FreeRTOS.h @@ -689,6 +689,10 @@ typedef portTickType xEventBitsType; #define configUSE_TRACE_FACILITY 0 #endif +#ifndef configBRANCH_TEST_INSTRUCTION + #define configBRANCH_TEST_INSTRUCTION() +#endif + /* For backward compatability. */ #define eTaskStateGet eTaskGetState diff --git a/FreeRTOS/Source/include/event_groups.h b/FreeRTOS/Source/include/event_groups.h index 3ca5ac3f5..b10eb563f 100644 --- a/FreeRTOS/Source/include/event_groups.h +++ b/FreeRTOS/Source/include/event_groups.h @@ -82,7 +82,7 @@ extern "C" { * the status of various CAN bus related events in which bit 0 might mean "A CAN * message has been received and is ready for processing", bit 1 might mean "The * application has queued a message that is ready for sending onto the CAN - * network", and bit 2 might mean "it is time to send a SYNC message onto the + * network", and bit 2 might mean "It is time to send a SYNC message onto the * CAN network" etc. A task can then test the bit values to see which events * are active, and optionally enter the Blocked state to wait for a specified * bit or a group of specified bits to be active. To continue the CAN bus @@ -165,10 +165,10 @@ xEventGroupHandle xEventGroupCreate( void ) PRIVILEGED_FUNCTION; * event_groups.h *
 	xEventBitsType xEventGroupWaitBits( xEventGroupHandle xEventGroup,
-										xEventBitsType uxBitsToWaitFor,
-										portBASE_TYPE xClearOnExit,
-										portBASE_TYPE xWaitForAllBits,
-										portTickType xTicksToWait );
+										const xEventBitsType uxBitsToWaitFor,
+										const portBASE_TYPE xClearOnExit,
+										const portBASE_TYPE xWaitForAllBits,
+										const portTickType xTicksToWait );
  
* * [Potentially] block to wait for one or more bits to be set within a @@ -187,15 +187,17 @@ xEventGroupHandle xEventGroupCreate( void ) PRIVILEGED_FUNCTION; * * @param xClearOnExit If xClearOnExit is set to pdTRUE then any bits within * uxBitsToWaitFor that are set within the event group will be cleared before - * xEventGroupWaitBits() returns. If xClearOnExit is set to pdFALSE then the - * bits set in the event group are not altered when the call to + * xEventGroupWaitBits() returns if the wait condition was met (if the function + * returns for a reason other than a timeout). If xClearOnExit is set to + * pdFALSE then the bits set in the event group are not altered when the call to * xEventGroupWaitBits() returns. * * @param xWaitForAllBits If xWaitForAllBits is set to pdTRUE then * xEventGroupWaitBits() will return when either all the bits in uxBitsToWaitFor * are set or the specified block time expires. If xWaitForAllBits is set to * pdFALSE then xEventGroupWaitBits() will return when any one of the bits set - * in uxBitsToWaitFor is set or the specified block time expires. + * in uxBitsToWaitFor is set or the specified block time expires. The block + * time is specified by the xTicksToWait parameter. * * @param xTicksToWait The maximum amount of time (specified in 'ticks') to wait * for one/all (depending on the xWaitForAllBits value) of the bits specified by @@ -207,7 +209,8 @@ xEventGroupHandle xEventGroupCreate( void ) PRIVILEGED_FUNCTION; * expired then not all the bits being waited for will be set. If * xEventGroupWaitBits() returned because the bits it was waiting for were set * then the returned value is the event group value before any bits were - * automatically cleared because the xClearOnExit parameter was set to pdTRUE. + * automatically cleared in the case that xClearOnExit parameter was set to + * pdTRUE. * * Example usage:
@@ -250,12 +253,12 @@ xEventGroupHandle xEventGroupCreate( void ) PRIVILEGED_FUNCTION;
  * \defgroup xEventGroupWaitBits xEventGroupWaitBits
  * \ingroup EventGroup
  */
-xEventBitsType xEventGroupWaitBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToWaitFor, portBASE_TYPE xClearOnExit, portBASE_TYPE xWaitForAllBits, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
+xEventBitsType xEventGroupWaitBits( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToWaitFor, const portBASE_TYPE xClearOnExit, const portBASE_TYPE xWaitForAllBits, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
 
 /**
  * event_groups.h
  *
-	xEventBitsType xEventGroupClearBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToClear );
+	xEventBitsType xEventGroupClearBits( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToClear );
  
* * Clear bits within an event group. This function cannot be called from an @@ -285,9 +288,8 @@ xEventBitsType xEventGroupWaitBits( xEventGroupHandle xEventGroup, xEventBitsTyp if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) ) { - // Both bit 0 and bit 4 were set before the call to - // xEventGroupClearBits() was called. Both will now be clear (not - // set). + // Both bit 0 and bit 4 were set before xEventGroupClearBits() was + // called. Both will now be clear (not set). } else if( ( uxBits & BIT_0 ) != 0 ) { @@ -308,12 +310,12 @@ xEventBitsType xEventGroupWaitBits( xEventGroupHandle xEventGroup, xEventBitsTyp * \defgroup xEventGroupClearBits xEventGroupClearBits * \ingroup EventGroup */ -xEventBitsType xEventGroupClearBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToClear ) PRIVILEGED_FUNCTION; +xEventBitsType xEventGroupClearBits( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToClear ) PRIVILEGED_FUNCTION; /** * event_groups.h *
-	xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet );
+	xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToSet );
  
* * Set bits within an event group. @@ -374,20 +376,20 @@ xEventBitsType xEventGroupClearBits( xEventGroupHandle xEventGroup, xEventBitsTy else { // Neither bit 0 nor bit 4 remained set. It might be that a task - // was waiting for either or both of the bits to be set, and the - // bits were cleared as the task left the Blocked state. + // was waiting for both of the bits to be set, and the bits were + // cleared as the task left the Blocked state. } }
* \defgroup xEventGroupSetBits xEventGroupSetBits * \ingroup EventGroup */ -xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet ) PRIVILEGED_FUNCTION; +xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToSet ) PRIVILEGED_FUNCTION; /** * event_groups.h *
-	xEventBitsType xEventGroupSetBitsFromISR( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet, portBASE_TYPE *pxHigherPriorityTaskWoken );
+	xEventBitsType xEventGroupSetBitsFromISR( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToSet, portBASE_TYPE *pxHigherPriorityTaskWoken );
  
* * A version of xEventGroupSetBits() that can be called from an interrupt. @@ -406,13 +408,14 @@ xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType * For example, to set bit 3 only, set uxBitsToSet to 0x08. To set bit 3 * and bit 0 set uxBitsToSet to 0x09. * - * @ pxHigherPriorityTaskWoken As mentioned above, calling this function will - * result in a message being sent to the timer daemon task. If the priority of - * the timer daemon task is higher than the priority of the currently running - * task (the task the interrupt interrupted) then *pxHigherPriorityTaskWoken - * will be set to pdTRUE by xEventGroupSetBitsFromISR(), indicating that a - * context switch should be requested before the interrupt exits. For that - * reason *pxHigherPriorityTaskWoken must be initialised to pdFALSE. See the + * @param pxHigherPriorityTaskWoken As mentioned above, calling this function + * will result in a message being sent to the timer daemon task. If the + * priority of the timer daemon task is higher than the priority of the + * currently running task (the task the interrupt interrupted) then + * *pxHigherPriorityTaskWoken will be set to pdTRUE by + * xEventGroupSetBitsFromISR(), indicating that a context switch should be + * requested before the interrupt exits. For that reason + * *pxHigherPriorityTaskWoken must be initialised to pdFALSE. See the * example code below. * * @return If the callback request was registered successfully then pdPASS is @@ -424,7 +427,7 @@ xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType #define BIT_0 ( 1 << 0 ) #define BIT_4 ( 1 << 4 ) - // An event group which it is assume has already been created by a call to + // An event group which it is assumed has already been created by a call to // xEventGroupCreate(). xEventGroupHandle xEventGroup; @@ -432,7 +435,7 @@ xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType { portBASE_TYPE xHigherPriorityTaskWoken; - // xHigherPriorityTaskWoken must be initialised to pdFALSE; + // xHigherPriorityTaskWoken must be initialised to pdFALSE. xHigherPriorityTaskWoken = pdFALSE; // Set bit 0 and bit 4 in xEventGroup. @@ -457,8 +460,8 @@ xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType * event_groups.h *
 	xEventBitsType xEventGroupSync(	xEventGroupHandle xEventGroup,
-									xEventBitsType uxBitsToSet,
-									xEventBitsType uxBitsToWaitFor,
+									const xEventBitsType uxBitsToSet,
+									const xEventBitsType uxBitsToWaitFor,
 									portTickType xTicksToWait );
  
* @@ -577,7 +580,7 @@ xEventBitsType xEventGroupSetBits( xEventGroupHandle xEventGroup, xEventBitsType * \defgroup xEventGroupSync xEventGroupSync * \ingroup EventGroup */ -xEventBitsType xEventGroupSync( xEventGroupHandle xEventGroup, xEventBitsType uxBitsToSet, xEventBitsType uxBitsToWaitFor, portTickType xTicksToWait ) PRIVILEGED_FUNCTION; +xEventBitsType xEventGroupSync( xEventGroupHandle xEventGroup, const xEventBitsType uxBitsToSet, const xEventBitsType uxBitsToWaitFor, portTickType xTicksToWait ) PRIVILEGED_FUNCTION; /** @@ -613,7 +616,7 @@ xEventBitsType xEventGroupSync( xEventGroupHandle xEventGroup, xEventBitsType ux void vEventGroupDelete( xEventGroupHandle xEventGroup ); /* For internal use only. */ -void vEventGroupSetBitsCallback( void *pvEventGroup, unsigned long ulBitsToSet ); +void vEventGroupSetBitsCallback( void *pvEventGroup, const unsigned long ulBitsToSet ); #ifdef __cplusplus diff --git a/FreeRTOS/Source/include/timers.h b/FreeRTOS/Source/include/timers.h index 4b6775cda..539744f70 100644 --- a/FreeRTOS/Source/include/timers.h +++ b/FreeRTOS/Source/include/timers.h @@ -993,6 +993,9 @@ xTaskHandle xTimerGetTimerDaemonTaskHandle( void ); * *pxHigherPriorityTaskWoken must be initialised to pdFALSE. See the * example code below. * + * @return pdPASS is returned if the message was successfully sent to the + * timer daemon task, otherwise pdFALSE is returned. + * * Example usage: * @verbatim * diff --git a/FreeRTOS/Source/portable/MSVC-MingW/port.c b/FreeRTOS/Source/portable/MSVC-MingW/port.c index bfd651373..39fc22664 100644 --- a/FreeRTOS/Source/portable/MSVC-MingW/port.c +++ b/FreeRTOS/Source/portable/MSVC-MingW/port.c @@ -148,6 +148,9 @@ static unsigned long (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 }; /* Pointer to the TCB of the currently executing task. */ extern void *pxCurrentTCB; +/* Used to ensure nothing is processed during the startup sequence. */ +static portBASE_TYPE xPortRunning = pdFALSE; + /*-----------------------------------------------------------*/ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter ) @@ -190,6 +193,8 @@ TIMECAPS xTimeCaps; Sleep( portTICK_RATE_MS ); } + configASSERT( xPortRunning ); + WaitForSingleObject( pvInterruptEventMutex, INFINITE ); /* The timer has expired, generate the simulated tick event. */ @@ -197,7 +202,10 @@ TIMECAPS xTimeCaps; /* The interrupt is now pending - notify the simulated interrupt handler thread. */ - SetEvent( pvInterruptEvent ); + if( ulCriticalNesting == 0 ) + { + SetEvent( pvInterruptEvent ); + } /* Give back the mutex so the simulated interrupt handler unblocks and can access the interrupt handler variables. */ @@ -312,7 +320,7 @@ xThreadState *pxThreadState; ulCriticalNesting = portNO_CRITICAL_NESTING; /* Bump up the priority of the thread that is going to run, in the - hope that this will asist in getting the Windows thread scheduler to + hope that this will assist in getting the Windows thread scheduler to behave as an embedded engineer might expect. */ ResumeThread( pxThreadState->pvThread ); @@ -338,6 +346,7 @@ static unsigned long prvProcessTickInterrupt( void ) unsigned long ulSwitchRequired; /* Process the tick itself. */ + configASSERT( xPortRunning ); ulSwitchRequired = ( unsigned long ) xTaskIncrementTick(); return ulSwitchRequired; @@ -356,6 +365,13 @@ void *pvObjectList[ 2 ]; pvObjectList[ 0 ] = pvInterruptEventMutex; pvObjectList[ 1 ] = pvInterruptEvent; + /* Create a pending tick to ensure the first task is started as soon as + this thread pends. */ + ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK ); + SetEvent( pvInterruptEvent ); + + xPortRunning = pdTRUE; + for(;;) { WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE ); @@ -489,6 +505,8 @@ void vPortEndScheduler( void ) void vPortGenerateSimulatedInterrupt( unsigned long ulInterruptNumber ) { + configASSERT( xPortRunning ); + if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) ) { /* Yield interrupts are processed even when critical nesting is non-zero. */ @@ -528,7 +546,7 @@ void vPortSetInterruptHandler( unsigned long ulInterruptNumber, unsigned long (* void vPortEnterCritical( void ) { - if( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED ) + if( xPortRunning == pdTRUE ) { /* The interrupt event mutex is held for the entire critical section, effectively disabling (simulated) interrupts. */ @@ -561,6 +579,7 @@ long lMutexNeedsReleasing; (simulated) disabled? */ if( ulPendingInterrupts != 0UL ) { + configASSERT( xPortRunning ); SetEvent( pvInterruptEvent ); /* Mutex will be released now, so does not require releasing @@ -577,9 +596,13 @@ long lMutexNeedsReleasing; } } - if( lMutexNeedsReleasing == pdTRUE ) + if( pvInterruptEventMutex != NULL ) { - ReleaseMutex( pvInterruptEventMutex ); + if( lMutexNeedsReleasing == pdTRUE ) + { + configASSERT( xPortRunning ); + ReleaseMutex( pvInterruptEventMutex ); + } } } /*-----------------------------------------------------------*/ diff --git a/FreeRTOS/Source/portable/MSVC-MingW/portmacro.h b/FreeRTOS/Source/portable/MSVC-MingW/portmacro.h index d8dd2f57a..528e04e5e 100644 --- a/FreeRTOS/Source/portable/MSVC-MingW/portmacro.h +++ b/FreeRTOS/Source/portable/MSVC-MingW/portmacro.h @@ -99,8 +99,8 @@ void vPortCloseRunningThread( void *pvTaskToDelete, volatile portBASE_TYPE *pxPe void vPortDeleteThread( void *pvThreadToDelete ); #define portCLEAN_UP_TCB( pxTCB ) vPortDeleteThread( pxTCB ) #define portPRE_TASK_DELETE_HOOK( pvTaskToDelete, pxPendYield ) vPortCloseRunningThread( ( pvTaskToDelete ), ( pxPendYield ) ) -#define portDISABLE_INTERRUPTS() -#define portENABLE_INTERRUPTS() +#define portDISABLE_INTERRUPTS() vPortEnterCritical() +#define portENABLE_INTERRUPTS() vPortExitCritical() /* Critical section handling. */ void vPortEnterCritical( void ); @@ -109,27 +109,34 @@ void vPortExitCritical( void ); #define portENTER_CRITICAL() vPortEnterCritical() #define portEXIT_CRITICAL() vPortExitCritical() -#ifndef __GNUC__ - #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1 +#if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1 /* Check the configuration. */ #if( configMAX_PRIORITIES > 32 ) #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32. It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice. #endif - /* Store/clear the ready priorities in a bit map. */ - #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) ) - #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) ) + /* Store/clear the ready priorities in a bit map. */ + #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) ) + #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) ) - /*-----------------------------------------------------------*/ + /*-----------------------------------------------------------*/ + #ifdef __GNUC__ + #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) \ + __asm volatile( "mov %0, %%eax \n\t" \ + "bsr %%eax, %%eax \n\t" \ + "mov %%eax, %1 \n\t" \ + :"=r"(uxTopPriority) : "r"(uxReadyPriorities) : "eax" ) + #else /* BitScanReverse returns the bit position of the most significant '1' in the word. */ #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) _BitScanReverse( ( DWORD * ) &( uxTopPriority ), ( uxReadyPriorities ) ) + #endif /* __GNUC__ */ + +#endif /* taskRECORD_READY_PRIORITY */ - #endif /* taskRECORD_READY_PRIORITY */ -#endif /* __GNUC__ */ diff --git a/FreeRTOS/Source/tasks.c b/FreeRTOS/Source/tasks.c index 66723ae99..1acd88745 100644 --- a/FreeRTOS/Source/tasks.c +++ b/FreeRTOS/Source/tasks.c @@ -362,9 +362,28 @@ count overflows. */ */ #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) ( pxHandle ) ) +/* The item value of the event list item is normally used to hold the priority +of the task to which it belongs (coded to allow it to be held in reverse +priority order). However, it is occasionally borrowed for other purposes. It +is important its value is not updated due to a task priority change while it is +being used for another purpose. The following bit definition is used to inform +the scheduler that the value should not be changed - in which case it is the +responsibility of whichever module is using the value to ensure it gets set back +to its original value when it is released. */ +#if configUSE_16_BIT_TICKS == 1 + #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U +#else + #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL +#endif + /* Callback function prototypes. --------------------------*/ -extern void vApplicationStackOverflowHook( xTaskHandle xTask, signed char *pcTaskName ); -extern void vApplicationTickHook( void ); +#if configCHECK_FOR_STACK_OVERFLOW > 0 + extern void vApplicationStackOverflowHook( xTaskHandle xTask, signed char *pcTaskName ); +#endif + +#if configUSE_TICK_HOOK > 0 + extern void vApplicationTickHook( void ); +#endif /* File private functions. --------------------------------*/ @@ -1014,7 +1033,12 @@ tskTCB * pxNewTCB; } #endif - listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( portTickType ) configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */ + /* Only reset the event list item value if the value is not + being used for anything else. */ + if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0 ) + { + listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( portTickType ) configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */ + } /* If the task is in the blocked or suspended list we need do nothing more than change it's priority variable. However, if @@ -1424,8 +1448,9 @@ portBASE_TYPE xAlreadyYielded = pdFALSE; } /* If any ticks occurred while the scheduler was suspended then - they should be processed now. This ensures the tick count does not - slip, and that any delayed tasks are resumed at the correct time. */ + they should be processed now. This ensures the tick count does + not slip, and that any delayed tasks are resumed at the correct + time. */ if( uxPendedTicks > ( unsigned portBASE_TYPE ) 0U ) { while( uxPendedTicks > ( unsigned portBASE_TYPE ) 0U ) @@ -1975,7 +2000,7 @@ portTickType xTimeToWake; SCHEDULER SUSPENDED. */ /* Store the item value in the event list item. */ - listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue ); + listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE ); /* Place the event list item of the TCB at the end of the appropriate event list. */ @@ -2124,7 +2149,7 @@ portBASE_TYPE xReturn; SCHEDULER SUSPENDED. It can also be called from within an ISR. */ /* Store the new item value in the event list. */ - listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue ); + listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE ); /* Remove the TCB from the delayed list, and add it to the ready list. */ @@ -2852,8 +2877,13 @@ tskTCB *pxTCB; { if( pxTCB->uxPriority < pxCurrentTCB->uxPriority ) { - /* Adjust the mutex holder state to account for its new priority. */ - listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( portTickType ) configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */ + /* Adjust the mutex holder state to account for its new + priority. Only reset the event list item value if the value is + not being used for anything else. */ + if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0 ) + { + listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( portTickType ) configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */ + } /* If the task being modified is in the ready state it will need to be moved into a new list. */ @@ -2903,7 +2933,13 @@ tskTCB *pxTCB; ready list. */ traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority ); pxTCB->uxPriority = pxTCB->uxBasePriority; - listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( portTickType ) configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */ + + /* Only reset the event list item value if the value is not + being used for anything else. */ + if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0 ) + { + listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( portTickType ) configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */ + } prvAddTaskToReadyList( pxTCB ); } } @@ -3150,4 +3186,7 @@ portTickType uxReturn; } /*-----------------------------------------------------------*/ +#ifdef FREERTOS_MODULE_TEST + #include "tasks_test_access_functions.h" +#endif