Skip to content

Commit 53229b1

Browse files
authored
Assert if prvCheckForRunStateChange is called in ISR (#779)
* Assert if prvCheckForRunStateChange is called in ISR
1 parent 288d143 commit 53229b1

File tree

1 file changed

+45
-49
lines changed

1 file changed

+45
-49
lines changed

tasks.c

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -693,62 +693,58 @@ static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
693693
UBaseType_t uxPrevCriticalNesting;
694694
const TCB_t * pxThisTCB;
695695

696-
/* This should be skipped if called from an ISR. If the task on the current
697-
* core is no longer running, then vTaskSwitchContext() probably should
698-
* be run before returning, but we don't have a way to force that to happen
699-
* from here. */
700-
if( portCHECK_IF_IN_ISR() == pdFALSE )
701-
{
702-
/* This function is always called with interrupts disabled
703-
* so this is safe. */
704-
pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
705-
706-
while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
707-
{
708-
/* We are only here if we just entered a critical section
709-
* or if we just suspended the scheduler, and another task
710-
* has requested that we yield.
711-
*
712-
* This is slightly complicated since we need to save and restore
713-
* the suspension and critical nesting counts, as well as release
714-
* and reacquire the correct locks. And then, do it all over again
715-
* if our state changed again during the reacquisition. */
716-
uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
717-
718-
if( uxPrevCriticalNesting > 0U )
719-
{
720-
portSET_CRITICAL_NESTING_COUNT( 0U );
721-
portRELEASE_ISR_LOCK();
722-
}
723-
else
724-
{
725-
/* The scheduler is suspended. uxSchedulerSuspended is updated
726-
* only when the task is not requested to yield. */
727-
mtCOVERAGE_TEST_MARKER();
728-
}
696+
/* This must only be called from within a task. */
697+
portASSERT_IF_IN_ISR();
729698

730-
portRELEASE_TASK_LOCK();
699+
/* This function is always called with interrupts disabled
700+
* so this is safe. */
701+
pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
731702

732-
portMEMORY_BARRIER();
733-
configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
703+
while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
704+
{
705+
/* We are only here if we just entered a critical section
706+
* or if we just suspended the scheduler, and another task
707+
* has requested that we yield.
708+
*
709+
* This is slightly complicated since we need to save and restore
710+
* the suspension and critical nesting counts, as well as release
711+
* and reacquire the correct locks. And then, do it all over again
712+
* if our state changed again during the reacquisition. */
713+
uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
734714

735-
portENABLE_INTERRUPTS();
715+
if( uxPrevCriticalNesting > 0U )
716+
{
717+
portSET_CRITICAL_NESTING_COUNT( 0U );
718+
portRELEASE_ISR_LOCK();
719+
}
720+
else
721+
{
722+
/* The scheduler is suspended. uxSchedulerSuspended is updated
723+
* only when the task is not requested to yield. */
724+
mtCOVERAGE_TEST_MARKER();
725+
}
736726

737-
/* Enabling interrupts should cause this core to immediately
738-
* service the pending interrupt and yield. If the run state is still
739-
* yielding here then that is a problem. */
740-
configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
727+
portRELEASE_TASK_LOCK();
741728

742-
portDISABLE_INTERRUPTS();
743-
portGET_TASK_LOCK();
744-
portGET_ISR_LOCK();
729+
portMEMORY_BARRIER();
730+
configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
745731

746-
portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
732+
portENABLE_INTERRUPTS();
747733

748-
if( uxPrevCriticalNesting == 0U )
749-
{
750-
portRELEASE_ISR_LOCK();
751-
}
734+
/* Enabling interrupts should cause this core to immediately
735+
* service the pending interrupt and yield. If the run state is still
736+
* yielding here then that is a problem. */
737+
configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
738+
739+
portDISABLE_INTERRUPTS();
740+
portGET_TASK_LOCK();
741+
portGET_ISR_LOCK();
742+
743+
portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
744+
745+
if( uxPrevCriticalNesting == 0U )
746+
{
747+
portRELEASE_ISR_LOCK();
752748
}
753749
}
754750
}

0 commit comments

Comments
 (0)