mirror of
https://github.com/FreeRTOS/FreeRTOS-Kernel.git
synced 2025-04-19 21:11:57 -04:00
Enable MSVC Port to leave the prvProcessSimulatedInterrupts loop when scheduler is stopped (#728)
* allow to leave loop * add missing brace * Code review suggestions Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com> --------- Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com> Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com> Co-authored-by: Gaurav Aggarwal <aggarg@amazon.com>
This commit is contained in:
parent
8d80cf697a
commit
225bace85c
|
@ -340,6 +340,9 @@ SYSTEM_INFO xSystemInfo;
|
||||||
/* Start the first task. */
|
/* Start the first task. */
|
||||||
ResumeThread( pxThreadState->pvThread );
|
ResumeThread( pxThreadState->pvThread );
|
||||||
|
|
||||||
|
/* The scheduler is now running. */
|
||||||
|
xPortRunning = pdTRUE;
|
||||||
|
|
||||||
/* Handle all simulated interrupts - including yield requests and
|
/* Handle all simulated interrupts - including yield requests and
|
||||||
simulated ticks. */
|
simulated ticks. */
|
||||||
prvProcessSimulatedInterrupts();
|
prvProcessSimulatedInterrupts();
|
||||||
|
@ -376,6 +379,8 @@ uint32_t ulSwitchRequired, i;
|
||||||
ThreadState_t *pxThreadState;
|
ThreadState_t *pxThreadState;
|
||||||
void *pvObjectList[ 2 ];
|
void *pvObjectList[ 2 ];
|
||||||
CONTEXT xContext;
|
CONTEXT xContext;
|
||||||
|
DWORD xWinApiResult;
|
||||||
|
const DWORD xTimeoutMilliseconds = 1000;
|
||||||
|
|
||||||
/* Going to block on the mutex that ensured exclusive access to the simulated
|
/* Going to block on the mutex that ensured exclusive access to the simulated
|
||||||
interrupt objects, and the event that signals that a simulated interrupt
|
interrupt objects, and the event that signals that a simulated interrupt
|
||||||
|
@ -388,105 +393,109 @@ CONTEXT xContext;
|
||||||
ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
|
ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );
|
||||||
SetEvent( pvInterruptEvent );
|
SetEvent( pvInterruptEvent );
|
||||||
|
|
||||||
xPortRunning = pdTRUE;
|
while( xPortRunning == pdTRUE )
|
||||||
|
|
||||||
for(;;)
|
|
||||||
{
|
{
|
||||||
xInsideInterrupt = pdFALSE;
|
xInsideInterrupt = pdFALSE;
|
||||||
WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );
|
|
||||||
|
|
||||||
/* Cannot be in a critical section to get here. Tasks that exit a
|
/* Wait with timeout so that we can exit from this loop when
|
||||||
critical section will block on a yield mutex to wait for an interrupt to
|
* the scheduler is stopped by calling vPortEndScheduler. */
|
||||||
process if an interrupt was set pending while the task was inside the
|
xWinApiResult = WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, xTimeoutMilliseconds );
|
||||||
critical section. xInsideInterrupt prevents interrupts that contain
|
|
||||||
critical sections from doing the same. */
|
|
||||||
xInsideInterrupt = pdTRUE;
|
|
||||||
|
|
||||||
/* Used to indicate whether the simulated interrupt processing has
|
if( xWinApiResult != WAIT_TIMEOUT )
|
||||||
necessitated a context switch to another task/thread. */
|
|
||||||
ulSwitchRequired = pdFALSE;
|
|
||||||
|
|
||||||
/* For each interrupt we are interested in processing, each of which is
|
|
||||||
represented by a bit in the 32bit ulPendingInterrupts variable. */
|
|
||||||
for( i = 0; i < portMAX_INTERRUPTS; i++ )
|
|
||||||
{
|
{
|
||||||
/* Is the simulated interrupt pending? */
|
/* Cannot be in a critical section to get here. Tasks that exit a
|
||||||
if( ( ulPendingInterrupts & ( 1UL << i ) ) != 0 )
|
critical section will block on a yield mutex to wait for an interrupt to
|
||||||
|
process if an interrupt was set pending while the task was inside the
|
||||||
|
critical section. xInsideInterrupt prevents interrupts that contain
|
||||||
|
critical sections from doing the same. */
|
||||||
|
xInsideInterrupt = pdTRUE;
|
||||||
|
|
||||||
|
/* Used to indicate whether the simulated interrupt processing has
|
||||||
|
necessitated a context switch to another task/thread. */
|
||||||
|
ulSwitchRequired = pdFALSE;
|
||||||
|
|
||||||
|
/* For each interrupt we are interested in processing, each of which is
|
||||||
|
represented by a bit in the 32bit ulPendingInterrupts variable. */
|
||||||
|
for( i = 0; i < portMAX_INTERRUPTS; i++ )
|
||||||
{
|
{
|
||||||
/* Is a handler installed? */
|
/* Is the simulated interrupt pending? */
|
||||||
if( ulIsrHandler[ i ] != NULL )
|
if( ( ulPendingInterrupts & ( 1UL << i ) ) != 0 )
|
||||||
{
|
{
|
||||||
/* Run the actual handler. Handlers return pdTRUE if they
|
/* Is a handler installed? */
|
||||||
necessitate a context switch. */
|
if( ulIsrHandler[ i ] != NULL )
|
||||||
if( ulIsrHandler[ i ]() != pdFALSE )
|
|
||||||
{
|
{
|
||||||
/* A bit mask is used purely to help debugging. */
|
/* Run the actual handler. Handlers return pdTRUE if they
|
||||||
ulSwitchRequired |= ( 1 << i );
|
necessitate a context switch. */
|
||||||
|
if( ulIsrHandler[ i ]() != pdFALSE )
|
||||||
|
{
|
||||||
|
/* A bit mask is used purely to help debugging. */
|
||||||
|
ulSwitchRequired |= ( 1 << i );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Clear the interrupt pending bit. */
|
||||||
|
ulPendingInterrupts &= ~( 1UL << i );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Clear the interrupt pending bit. */
|
|
||||||
ulPendingInterrupts &= ~( 1UL << i );
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if( ulSwitchRequired != pdFALSE )
|
if( ulSwitchRequired != pdFALSE )
|
||||||
{
|
|
||||||
void *pvOldCurrentTCB;
|
|
||||||
|
|
||||||
pvOldCurrentTCB = pxCurrentTCB;
|
|
||||||
|
|
||||||
/* Select the next task to run. */
|
|
||||||
vTaskSwitchContext();
|
|
||||||
|
|
||||||
/* If the task selected to enter the running state is not the task
|
|
||||||
that is already in the running state. */
|
|
||||||
if( pvOldCurrentTCB != pxCurrentTCB )
|
|
||||||
{
|
{
|
||||||
/* Suspend the old thread. In the cases where the (simulated)
|
void *pvOldCurrentTCB;
|
||||||
interrupt is asynchronous (tick event swapping a task out rather
|
|
||||||
than a task blocking or yielding) it doesn't matter if the
|
|
||||||
'suspend' operation doesn't take effect immediately - if it
|
|
||||||
doesn't it would just be like the interrupt occurring slightly
|
|
||||||
later. In cases where the yield was caused by a task blocking
|
|
||||||
or yielding then the task will block on a yield event after the
|
|
||||||
yield operation in case the 'suspend' operation doesn't take
|
|
||||||
effect immediately. */
|
|
||||||
pxThreadState = ( ThreadState_t *) *( ( size_t * ) pvOldCurrentTCB );
|
|
||||||
SuspendThread( pxThreadState->pvThread );
|
|
||||||
|
|
||||||
/* Ensure the thread is actually suspended by performing a
|
pvOldCurrentTCB = pxCurrentTCB;
|
||||||
synchronous operation that can only complete when the thread is
|
|
||||||
actually suspended. The below code asks for dummy register
|
|
||||||
data. Experimentation shows that these two lines don't appear
|
|
||||||
to do anything now, but according to
|
|
||||||
https://devblogs.microsoft.com/oldnewthing/20150205-00/?p=44743
|
|
||||||
they do - so as they do not harm (slight run-time hit). */
|
|
||||||
xContext.ContextFlags = CONTEXT_INTEGER;
|
|
||||||
( void ) GetThreadContext( pxThreadState->pvThread, &xContext );
|
|
||||||
|
|
||||||
/* Obtain the state of the task now selected to enter the
|
/* Select the next task to run. */
|
||||||
Running state. */
|
vTaskSwitchContext();
|
||||||
pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );
|
|
||||||
|
|
||||||
/* pxThreadState->pvThread can be NULL if the task deleted
|
/* If the task selected to enter the running state is not the task
|
||||||
itself - but a deleted task should never be resumed here. */
|
that is already in the running state. */
|
||||||
configASSERT( pxThreadState->pvThread != NULL );
|
if( pvOldCurrentTCB != pxCurrentTCB )
|
||||||
ResumeThread( pxThreadState->pvThread );
|
{
|
||||||
|
/* Suspend the old thread. In the cases where the (simulated)
|
||||||
|
interrupt is asynchronous (tick event swapping a task out rather
|
||||||
|
than a task blocking or yielding) it doesn't matter if the
|
||||||
|
'suspend' operation doesn't take effect immediately - if it
|
||||||
|
doesn't it would just be like the interrupt occurring slightly
|
||||||
|
later. In cases where the yield was caused by a task blocking
|
||||||
|
or yielding then the task will block on a yield event after the
|
||||||
|
yield operation in case the 'suspend' operation doesn't take
|
||||||
|
effect immediately. */
|
||||||
|
pxThreadState = ( ThreadState_t *) *( ( size_t * ) pvOldCurrentTCB );
|
||||||
|
SuspendThread( pxThreadState->pvThread );
|
||||||
|
|
||||||
|
/* Ensure the thread is actually suspended by performing a
|
||||||
|
synchronous operation that can only complete when the thread is
|
||||||
|
actually suspended. The below code asks for dummy register
|
||||||
|
data. Experimentation shows that these two lines don't appear
|
||||||
|
to do anything now, but according to
|
||||||
|
https://devblogs.microsoft.com/oldnewthing/20150205-00/?p=44743
|
||||||
|
they do - so as they do not harm (slight run-time hit). */
|
||||||
|
xContext.ContextFlags = CONTEXT_INTEGER;
|
||||||
|
( void ) GetThreadContext( pxThreadState->pvThread, &xContext );
|
||||||
|
|
||||||
|
/* Obtain the state of the task now selected to enter the
|
||||||
|
Running state. */
|
||||||
|
pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );
|
||||||
|
|
||||||
|
/* pxThreadState->pvThread can be NULL if the task deleted
|
||||||
|
itself - but a deleted task should never be resumed here. */
|
||||||
|
configASSERT( pxThreadState->pvThread != NULL );
|
||||||
|
ResumeThread( pxThreadState->pvThread );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* If the thread that is about to be resumed stopped running
|
/* If the thread that is about to be resumed stopped running
|
||||||
because it yielded then it will wait on an event when it resumed
|
because it yielded then it will wait on an event when it resumed
|
||||||
(to ensure it does not continue running after the call to
|
(to ensure it does not continue running after the call to
|
||||||
SuspendThread() above as SuspendThread() is asynchronous).
|
SuspendThread() above as SuspendThread() is asynchronous).
|
||||||
Signal the event to ensure the thread can proceed now it is
|
Signal the event to ensure the thread can proceed now it is
|
||||||
valid for it to do so. Signaling the event is benign in the case that
|
valid for it to do so. Signaling the event is benign in the case that
|
||||||
the task was switched out asynchronously by an interrupt as the event
|
the task was switched out asynchronously by an interrupt as the event
|
||||||
is reset before the task blocks on it. */
|
is reset before the task blocks on it. */
|
||||||
pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );
|
pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );
|
||||||
SetEvent( pxThreadState->pvYieldEvent );
|
SetEvent( pxThreadState->pvYieldEvent );
|
||||||
ReleaseMutex( pvInterruptEventMutex );
|
ReleaseMutex( pvInterruptEventMutex );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------*/
|
/*-----------------------------------------------------------*/
|
||||||
|
|
Loading…
Reference in a new issue