]> git.sur5r.net Git - freertos/commitdiff
Check in implementation of xTaskIncrementTick (replaced vTaskIncrementTick()).
authorrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Sat, 8 Jun 2013 18:30:52 +0000 (18:30 +0000)
committerrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Sat, 8 Jun 2013 18:30:52 +0000 (18:30 +0000)
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@1927 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

FreeRTOS/Source/tasks.c

index c72239cbca23e1932ae004df93b2ed411d5fbf6e..91358b8a9dca02309585b3d7edf62da345759dac 100644 (file)
@@ -194,8 +194,8 @@ PRIVILEGED_DATA static unsigned portBASE_TYPE uxTopUsedPriority                                     = tskIDLE_P
 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTopReadyPriority              = tskIDLE_PRIORITY;\r
 PRIVILEGED_DATA static volatile signed portBASE_TYPE xSchedulerRunning                         = pdFALSE;\r
 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxSchedulerSuspended            = ( unsigned portBASE_TYPE ) pdFALSE;\r
-PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxMissedTicks                   = ( unsigned portBASE_TYPE ) 0U;\r
-PRIVILEGED_DATA static volatile portBASE_TYPE xMissedYield                                             = ( portBASE_TYPE ) pdFALSE;\r
+PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxPendedTicks                   = ( unsigned portBASE_TYPE ) 0U;\r
+PRIVILEGED_DATA static volatile portBASE_TYPE xYieldPending                                            = ( portBASE_TYPE ) pdFALSE;\r
 PRIVILEGED_DATA static volatile portBASE_TYPE xNumOfOverflows                                  = ( portBASE_TYPE ) 0;\r
 PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                             = ( unsigned portBASE_TYPE ) 0U;\r
 PRIVILEGED_DATA static volatile portTickType xNextTaskUnblockTime                              = ( portTickType ) portMAX_DELAY;\r
@@ -302,6 +302,44 @@ PRIVILEGED_DATA static volatile portTickType xNextTaskUnblockTime                          = ( portTic
 \r
 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */\r
 \r
+/*-----------------------------------------------------------*/\r
+\r
+/* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick\r
+count overflows. */\r
+#define taskSWITCH_DELAYED_LISTS()                                                                                                                                     \\r
+{                                                                                                                                                                                                      \\r
+       xList *pxTemp;                                                                                                                                                                  \\r
+                                                                                                                                                                                                       \\r
+       /* The delayed tasks list should be empty when the lists are switched. */                                               \\r
+       configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );                                                                             \\r
+                                                                                                                                                                                                       \\r
+       pxTemp = pxDelayedTaskList;                                                                                                                                             \\r
+       pxDelayedTaskList = pxOverflowDelayedTaskList;                                                                                                  \\r
+       pxOverflowDelayedTaskList = pxTemp;                                                                                                                             \\r
+       xNumOfOverflows++;                                                                                                                                                              \\r
+                                                                                                                                                                                                       \\r
+       if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )                                                                                 \\r
+       {                                                                                                                                                                                               \\r
+               /* The new current delayed list is empty.  Set                                                                                          \\r
+               xNextTaskUnblockTime to the maximum possible value so it is                                                                     \\r
+               extremely unlikely that the                                                                                                                                     \\r
+               if( xTickCount >= xNextTaskUnblockTime ) test will pass until                                                           \\r
+               there is an item in the delayed list. */                                                                                                        \\r
+               xNextTaskUnblockTime = portMAX_DELAY;                                                                                                           \\r
+       }                                                                                                                                                                                               \\r
+       else                                                                                                                                                                                    \\r
+       {                                                                                                                                                                                               \\r
+               /* The new current delayed list is not empty, get the value of                                                          \\r
+               the item at the head of the delayed list.  This is the time at                                                          \\r
+               which the task at the head of the delayed list should be removed                                                        \\r
+               from the Blocked state. */                                                                                                                                      \\r
+               pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );                                          \\r
+               xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );                         \\r
+       }                                                                                                                                                                                               \\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
 /*\r
  * Place the task represented by pxTCB into the appropriate ready queue for\r
  * the task.  It is inserted at the end of the list.  One quirk of this is\r
@@ -315,67 +353,6 @@ PRIVILEGED_DATA static volatile portTickType xNextTaskUnblockTime                          = ( portTic
        vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )\r
 /*-----------------------------------------------------------*/\r
 \r
-/*\r
- * Macro that looks at the list of tasks that are currently delayed to see if\r
- * any require waking.\r
- *\r
- * Tasks are stored in the queue in the order of their wake time - meaning\r
- * once one tasks has been found whose timer has not expired we need not look\r
- * any further down the list.\r
- */\r
-#define prvCheckDelayedTasks()                                                                                                                 \\r
-{                                                                                                                                                                              \\r
-portTickType xItemValue;                                                                                                                               \\r
-                                                                                                                                                                               \\r
-       /* Is the tick count greater than or equal to the wake time of the first                        \\r
-       task referenced from the delayed tasks list? */                                                                         \\r
-       if( xTickCount >= xNextTaskUnblockTime )                                                                                        \\r
-       {                                                                                                                                                                       \\r
-               for( ;; )                                                                                                                                               \\r
-               {                                                                                                                                                               \\r
-                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )                                         \\r
-                       {                                                                                                                                                       \\r
-                               /* The delayed list is empty.  Set xNextTaskUnblockTime to the                  \\r
-                               maximum possible value so it is extremely unlikely that the                             \\r
-                               if( xTickCount >= xNextTaskUnblockTime ) test will pass next                    \\r
-                               time through. */                                                                                                                \\r
-                               xNextTaskUnblockTime = portMAX_DELAY;                                                                   \\r
-                               break;                                                                                                                                  \\r
-                       }                                                                                                                                                       \\r
-                       else                                                                                                                                            \\r
-                       {                                                                                                                                                       \\r
-                               /* The delayed list is not empty, get the value of the item at                  \\r
-                               the head of the delayed list.  This is the time at which the                    \\r
-                               task at the head of the delayed list should be removed from                             \\r
-                               the Blocked state. */                                                                                                   \\r
-                               pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );  \\r
-                               xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );   \\r
-                                                                                                                                                                               \\r
-                               if( xTickCount < xItemValue )                                                                                   \\r
-                               {                                                                                                                                               \\r
-                                       /* It is not time to unblock this item yet, but the item                        \\r
-                                       value is the time at which the task at the head of the                          \\r
-                                       blocked list should be removed from the Blocked state -                         \\r
-                                       so record the item value in xNextTaskUnblockTime. */                            \\r
-                                       xNextTaskUnblockTime = xItemValue;                                                                      \\r
-                                       break;                                                                                                                          \\r
-                               }                                                                                                                                               \\r
-                                                                                                                                                                               \\r
-                               /* It is time to remove the item from the Blocked state. */                             \\r
-                               uxListRemove( &( pxTCB->xGenericListItem ) );                                                   \\r
-                                                                                                                                                                               \\r
-                               /* Is the task waiting on an event also? */                                                             \\r
-                               if( pxTCB->xEventListItem.pvContainer != NULL )                                                 \\r
-                               {                                                                                                                                               \\r
-                                       uxListRemove( &( pxTCB->xEventListItem ) );                                                     \\r
-                               }                                                                                                                                               \\r
-                               prvAddTaskToReadyQueue( pxTCB );                                                                                \\r
-                       }                                                                                                                                                       \\r
-               }                                                                                                                                                               \\r
-       }                                                                                                                                                                       \\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
 /*\r
  * Several functions take an xTaskHandle parameter that can optionally be NULL,\r
  * where NULL is used to indicate that the handle of the currently executing\r
@@ -1348,6 +1325,7 @@ signed portBASE_TYPE xTaskResumeAll( void )
 {\r
 register tskTCB *pxTCB;\r
 signed portBASE_TYPE xAlreadyYielded = pdFALSE;\r
+portBASE_TYPE xYieldRequired = pdFALSE;\r
 \r
        /* If uxSchedulerSuspended is zero then this function does not match a\r
        previous call to vTaskSuspendAll(). */\r
@@ -1366,8 +1344,6 @@ signed portBASE_TYPE xAlreadyYielded = pdFALSE;
                {\r
                        if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0U )\r
                        {\r
-                               portBASE_TYPE xYieldRequired = pdFALSE;\r
-\r
                                /* Move any readied tasks from the pending list into the\r
                                appropriate ready list. */\r
                                while( listLIST_IS_EMPTY( ( xList * ) &xPendingReadyList ) == pdFALSE )\r
@@ -1388,28 +1364,22 @@ signed portBASE_TYPE xAlreadyYielded = pdFALSE;
                                /* If any ticks occurred while the scheduler was suspended then\r
                                they should be processed now.  This ensures the tick count does not\r
                                slip, and that any delayed tasks are resumed at the correct time. */\r
-                               if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )\r
+                               if( uxPendedTicks > ( unsigned portBASE_TYPE ) 0U )\r
                                {\r
-                                       while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )\r
+                                       while( uxPendedTicks > ( unsigned portBASE_TYPE ) 0U )\r
                                        {\r
-                                               vTaskIncrementTick();\r
-                                               --uxMissedTicks;\r
-                                       }\r
-\r
-                                       /* As we have processed some ticks it is appropriate to yield\r
-                                       to ensure the highest priority task that is ready to run is\r
-                                       the task actually running. */\r
-                                       #if configUSE_PREEMPTION == 1\r
-                                       {\r
-                                               xYieldRequired = pdTRUE;\r
+                                               if( xTaskIncrementTick() != pdFALSE )\r
+                                               {\r
+                                                       xYieldRequired = pdTRUE;\r
+                                               }\r
+                                               --uxPendedTicks;\r
                                        }\r
-                                       #endif\r
                                }\r
 \r
-                               if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )\r
+                               if( ( xYieldRequired == pdTRUE ) || ( xYieldPending == pdTRUE ) )\r
                                {\r
                                        xAlreadyYielded = pdTRUE;\r
-                                       xMissedYield = pdFALSE;\r
+                                       xYieldPending = pdFALSE;\r
                                        portYIELD_WITHIN_API();\r
                                }\r
                        }\r
@@ -1638,9 +1608,11 @@ implementations require configUSE_TICKLESS_IDLE to be set to a value other than
 #endif /* configUSE_TICKLESS_IDLE */\r
 /*----------------------------------------------------------*/\r
 \r
-void vTaskIncrementTick( void )\r
+portBASE_TYPE xTaskIncrementTick( void )\r
 {\r
 tskTCB * pxTCB;\r
+portTickType xItemValue;\r
+portBASE_TYPE xSwitchRequired = pdFALSE;\r
 \r
        /* Called by the portable layer each time a tick interrupt occurs.\r
        Increments the tick then checks to see if the new tick value will cause any\r
@@ -1648,47 +1620,97 @@ tskTCB * pxTCB;
        traceTASK_INCREMENT_TICK( xTickCount );\r
        if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
        {\r
+               /* Increment the RTOS tick, switching the delayed and overflowed\r
+               delayed lists if it wraps to 0. */\r
                ++xTickCount;\r
                if( xTickCount == ( portTickType ) 0U )\r
                {\r
-                       xList *pxTemp;\r
+                       taskSWITCH_DELAYED_LISTS();\r
+               }\r
+\r
+               /* See if this tick has made a timeout expire.  Tasks are stored in the\r
+               queue in the order of their wake time - meaning once one tasks has been\r
+               found whose block time has not expired there is no need not look any\r
+               further down the list. */\r
+               if( xTickCount >= xNextTaskUnblockTime )\r
+               {\r
+                       for( ;; )\r
+                       {\r
+                               if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )\r
+                               {\r
+                                       /* The delayed list is empty.  Set xNextTaskUnblockTime to\r
+                                       the     maximum possible value so it is extremely unlikely that\r
+                                       the if( xTickCount >= xNextTaskUnblockTime ) test will pass\r
+                                       next time through. */\r
+                                       xNextTaskUnblockTime = portMAX_DELAY;\r
+                                       break;\r
+                               }\r
+                               else\r
+                               {\r
+                                       /* The delayed list is not empty, get the value of the item\r
+                                       at the head of the delayed list.  This is the time at which\r
+                                       the task at the head of the delayed list must be removed\r
+                                       from the Blocked state. */\r
+                                       pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );\r
+                                       xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );\r
+\r
+                                       if( xTickCount < xItemValue )\r
+                                       {\r
+                                               /* It is not time to unblock this item yet, but the item\r
+                                               value is the time at which the task at the head of the\r
+                                               blocked list must be removed from the Blocked state -\r
+                                               so record the item value in xNextTaskUnblockTime. */\r
+                                               xNextTaskUnblockTime = xItemValue;\r
+                                               break;\r
+                                       }\r
 \r
-                       /* Tick count has overflowed so we need to swap the delay lists.\r
-                       If there are any items in pxDelayedTaskList here then there is\r
-                       an error! */\r
-                       configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );\r
+                                       /* It is time to remove the item from the Blocked state. */\r
+                                       uxListRemove( &( pxTCB->xGenericListItem ) );\r
 \r
-                       pxTemp = pxDelayedTaskList;\r
-                       pxDelayedTaskList = pxOverflowDelayedTaskList;\r
-                       pxOverflowDelayedTaskList = pxTemp;\r
-                       xNumOfOverflows++;\r
+                                       /* Is the task waiting on an event also?  If so remove it\r
+                                       from the event list. */\r
+                                       if( pxTCB->xEventListItem.pvContainer != NULL )\r
+                                       {\r
+                                               uxListRemove( &( pxTCB->xEventListItem ) );\r
+                                       }\r
 \r
-                       if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )\r
-                       {\r
-                               /* The new current delayed list is empty.  Set\r
-                               xNextTaskUnblockTime to the maximum possible value so it is\r
-                               extremely unlikely that the\r
-                               if( xTickCount >= xNextTaskUnblockTime ) test will pass until\r
-                               there is an item in the delayed list. */\r
-                               xNextTaskUnblockTime = portMAX_DELAY;\r
+                                       /* Place the unblocked task into the appropriate ready\r
+                                       list. */\r
+                                       prvAddTaskToReadyQueue( pxTCB );\r
+\r
+                                       /* A task being unblocked cannot cause an immediate context\r
+                                       switch if preemption is turned off. */\r
+                                       #if (  configUSE_PREEMPTION == 1 )\r
+                                       {\r
+                                               /* Preemption is on, but a context switch should only\r
+                                               be performed if the unblocked task has a priority that\r
+                                               is equal to or higher than the currently executing\r
+                                               task. */\r
+                                               if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
+                                               {\r
+                                                       xSwitchRequired = pdTRUE;\r
+                                               }\r
+                                       }\r
+                                       #endif /* configUSE_PREEMPTION */\r
+                               }\r
                        }\r
-                       else\r
+               }\r
+\r
+               /* Tasks of equal priority to the currently running task will share\r
+               processing time (time slice) if preemption is on, and the application\r
+               writer has not explicitly turned time slicing off. */\r
+               #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )\r
+               {\r
+                       if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1 )\r
                        {\r
-                               /* The new current delayed list is not empty, get the value of\r
-                               the item at the head of the delayed list.  This is the time at\r
-                               which the task at the head of the delayed list should be removed\r
-                               from the Blocked state. */\r
-                               pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );\r
-                               xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );\r
+                               xSwitchRequired = pdTRUE;\r
                        }\r
                }\r
-\r
-               /* See if this tick has made a timeout expire. */\r
-               prvCheckDelayedTasks();\r
+               #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */\r
        }\r
        else\r
        {\r
-               ++uxMissedTicks;\r
+               ++uxPendedTicks;\r
 \r
                /* The tick hook gets called at regular intervals, even if the\r
                scheduler is locked. */\r
@@ -1702,13 +1724,15 @@ tskTCB * pxTCB;
        #if ( configUSE_TICK_HOOK == 1 )\r
        {\r
                /* Guard against the tick hook being called when the missed tick\r
-               count is being unwound (when the scheduler is being unlocked. */\r
-               if( uxMissedTicks == ( unsigned portBASE_TYPE ) 0U )\r
+               count is being unwound (when the scheduler is being unlocked). */\r
+               if( uxPendedTicks == ( unsigned portBASE_TYPE ) 0U )\r
                {\r
                        vApplicationTickHook();\r
                }\r
        }\r
        #endif /* configUSE_TICK_HOOK */\r
+\r
+       return xSwitchRequired;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1805,7 +1829,7 @@ void vTaskSwitchContext( void )
        {\r
                /* The scheduler is currently suspended - do not allow a context\r
                switch. */\r
-               xMissedYield = pdTRUE;\r
+               xYieldPending = pdTRUE;\r
        }\r
        else\r
        {\r
@@ -2039,7 +2063,7 @@ portBASE_TYPE xReturn;
 \r
 void vTaskMissedYield( void )\r
 {\r
-       xMissedYield = pdTRUE;\r
+       xYieldPending = pdTRUE;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -2192,7 +2216,7 @@ static portTASK_FUNCTION( prvIdleTask, pvParameters )
                        /* A task was made ready while the scheduler was suspended. */\r
                        eReturn = eAbortSleep;\r
                }\r
-               else if( xMissedYield != pdFALSE )\r
+               else if( xYieldPending != pdFALSE )\r
                {\r
                        /* A yield was pended while the scheduler was suspended. */\r
                        eReturn = eAbortSleep;\r
@@ -2483,7 +2507,7 @@ tskTCB *pxNewTCB;
                        /* Divide by zero check. */\r
                        if( ulTotalRunTimeDiv100 > 0UL )\r
                        {\r
-                               xExistingStringLength = strlen( pcWriteBuffer );\r
+                               xExistingStringLength = strlen( ( char * ) pcWriteBuffer );\r
 \r
                                /* Has the task run at all? */\r
                                if( pxNextTCB->ulRunTimeCounter == 0UL )\r
@@ -2502,7 +2526,7 @@ tskTCB *pxNewTCB;
                                        {\r
                                                #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
                                                {\r
-                                                       sprintf( &( pcWriteBuffer[ xExistingStringLength ] ), ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );\r
+                                                       sprintf( ( char * ) &( pcWriteBuffer[ xExistingStringLength ] ), ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );\r
                                                }\r
                                                #else\r
                                                {\r
@@ -2518,7 +2542,7 @@ tskTCB *pxNewTCB;
                                                consumed less than 1% of the total run time. */\r
                                                #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
                                                {\r
-                                                       sprintf( &( pcWriteBuffer[ xExistingStringLength ] ), ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );\r
+                                                       sprintf( ( char * ) &( pcWriteBuffer[ xExistingStringLength ] ), ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );\r
                                                }\r
                                                #else\r
                                                {\r