Skip to content

Commit ef88897

Browse files
authored
Fix the task selection when task yields (FreeRTOS#54)
1 parent 21d9a61 commit ef88897

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

tasks.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,24 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
890890
BaseType_t xPriorityDropped = pdFALSE;
891891
#endif
892892

893+
/* A new task is created and a running task with the same priority yields
894+
* itself to run the new task. When a running task yields itself, it is still
895+
* in the ready list. This running task will be selected before the new task
896+
* since the new task is always added to the end of the ready list.
897+
* The other problem is that the running task still in the same position of
898+
* the ready list when it yields itself. It is possible that it will be selected
899+
* earlier then other tasks which waits longer than this task.
900+
*
901+
* To fix these problems, the running task should be put to the end of the
902+
* ready list before searching for the ready task in the ready list. */
903+
if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
904+
&pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
905+
{
906+
uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
907+
vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
908+
&pxCurrentTCBs[ xCoreID ]->xStateListItem );
909+
}
910+
893911
while( xTaskScheduled == pdFALSE )
894912
{
895913
#if ( configRUN_MULTIPLE_PRIORITIES == 0 )
@@ -970,10 +988,7 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
970988

971989
if( xTaskScheduled != pdFALSE )
972990
{
973-
/* Once a task has been selected to run on this core,
974-
* move it to the end of the ready task list. */
975-
uxListRemove( pxIterator );
976-
vListInsertEnd( pxReadyList, pxIterator );
991+
/* A task has been selected to run on this core. */
977992
break;
978993
}
979994
}

0 commit comments

Comments
 (0)