/* Handle of the thread that executes the task. */\r
void *pvThread;\r
\r
-} xThreadState;\r
+ /* Event used to makes sure the thread does not execute past a yield point\r
+ between the call to SuspendThread() to suspend the thread and the\r
+ asynchronous SuspendThread() operation actually being performed. */\r
+ void *pvYieldEvent;\r
+} ThreadState_t;\r
\r
/* Simulated interrupts waiting to be processed. This is a bit mask where each\r
bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */\r
initialisation is probably not critical in this simulated environment as the\r
simulated interrupt handlers do not get created until the FreeRTOS scheduler is\r
started anyway. */\r
-static uint32_t ulCriticalNesting = 9999UL;\r
+static volatile uint32_t ulCriticalNesting = 9999UL;\r
\r
/* Handlers for all the simulated software interrupts. The first two positions\r
are used for the Yield and Tick interrupts so are handled slightly differently,\r
static uint32_t (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };\r
\r
/* Pointer to the TCB of the currently executing task. */\r
-extern void *pxCurrentTCB;\r
+extern void * volatile pxCurrentTCB;\r
\r
/* Used to ensure nothing is processed during the startup sequence. */\r
static BaseType_t xPortRunning = pdFALSE;\r
\r
/* The interrupt is now pending - notify the simulated interrupt\r
handler thread. */\r
- if( ulCriticalNesting == 0 )\r
+ if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
{\r
SetEvent( pvInterruptEvent );\r
}\r
\r
StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
{\r
-xThreadState *pxThreadState = NULL;\r
+ThreadState_t *pxThreadState = NULL;\r
int8_t *pcTopOfStack = ( int8_t * ) pxTopOfStack;\r
const SIZE_T xStackSize = 1024; /* Set the size to a small number which will get rounded up to the minimum possible. */\r
\r
/* In this simulated case a stack is not initialised, but instead a thread\r
is created that will execute the task being created. The thread handles\r
- the context switching itself. The xThreadState object is placed onto\r
+ the context switching itself. The ThreadState_t object is placed onto\r
the stack that was created for the task - so the stack buffer is still\r
used, just not in the conventional way. It will not be used for anything\r
other than holding this structure. */\r
- pxThreadState = ( xThreadState * ) ( pcTopOfStack - sizeof( xThreadState ) );\r
+ pxThreadState = ( ThreadState_t * ) ( pcTopOfStack - sizeof( ThreadState_t ) );\r
+\r
+ /* Create the event used to prevent the thread from executing past its yield\r
+ point if the SuspendThread() call that suspends the thread does not take\r
+ effect immediately (it is an asynchronous call). */\r
+ pxThreadState->pvYieldEvent = CreateEvent( NULL, /* Default security attributes. */\r
+ FALSE, /* Auto reset. */\r
+ FALSE, /* Start not signalled. */\r
+ NULL );/* No name. */\r
\r
/* Create the thread itself. */\r
pxThreadState->pvThread = CreateThread( NULL, xStackSize, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, NULL );\r
{\r
void *pvHandle = NULL;\r
int32_t lSuccess;\r
-xThreadState *pxThreadState = NULL;\r
+ThreadState_t *pxThreadState = NULL;\r
SYSTEM_INFO xSystemInfo;\r
\r
/* This port runs windows threads with extremely high priority. All the\r
\r
/* Start the highest priority task by obtaining its associated thread\r
state structure, in which is stored the thread handle. */\r
- pxThreadState = ( xThreadState * ) *( ( size_t * ) pxCurrentTCB );\r
+ pxThreadState = ( ThreadState_t * ) *( ( size_t * ) pxCurrentTCB );\r
ulCriticalNesting = portNO_CRITICAL_NESTING;\r
\r
- /* Bump up the priority of the thread that is going to run, in the\r
- hope that this will assist in getting the Windows thread scheduler to\r
- behave as an embedded engineer might expect. */\r
+ /* Start the first task. */\r
ResumeThread( pxThreadState->pvThread );\r
\r
/* Handle all simulated interrupts - including yield requests and\r
static void prvProcessSimulatedInterrupts( void )\r
{\r
uint32_t ulSwitchRequired, i;\r
-xThreadState *pxThreadState;\r
+ThreadState_t *pxThreadState;\r
void *pvObjectList[ 2 ];\r
CONTEXT xContext;\r
\r
if( pvOldCurrentTCB != pxCurrentTCB )\r
{\r
/* Suspend the old thread. */\r
- pxThreadState = ( xThreadState *) *( ( size_t * ) pvOldCurrentTCB );\r
+ pxThreadState = ( ThreadState_t *) *( ( size_t * ) pvOldCurrentTCB );\r
SuspendThread( pxThreadState->pvThread );\r
\r
/* Ensure the thread is actually suspended by performing a\r
synchronous operation that can only complete when the thread is\r
actually suspended. The below code asks for dummy register\r
- data. */\r
+ data. Experimentation shows that these two lines don't appear\r
+ to do anything now, but according to\r
+ https://devblogs.microsoft.com/oldnewthing/20150205-00/?p=44743\r
+ they do - so as they do not harm (slight run-time hit) they have\r
+ been left it. */\r
xContext.ContextFlags = CONTEXT_INTEGER;\r
( void ) GetThreadContext( pxThreadState->pvThread, &xContext );\r
\r
/* Obtain the state of the task now selected to enter the\r
Running state. */\r
- pxThreadState = ( xThreadState * ) ( *( size_t *) pxCurrentTCB );\r
+ pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );\r
+\r
+ /* pxThreadState->pvThread can be NULL if the task deleted\r
+ itself - but a deleted task should never be resumed here. */\r
+ configASSERT( pxThreadState->pvThread != NULL );\r
ResumeThread( pxThreadState->pvThread );\r
}\r
}\r
\r
+ /* If the thread that is about to be resumed stopped running\r
+ because it yielded then it will wait on an event when it resumed\r
+ (to ensure it does not continue running after the call to\r
+ SuspendThread() above as SuspendThread() is asynchronous).\r
+ Signal the event to ensure the thread can proceed now it is\r
+ valid for it to do so. */\r
+ pxThreadState = ( ThreadState_t * ) ( *( size_t *) pxCurrentTCB );\r
+ SetEvent( pxThreadState->pvYieldEvent );\r
ReleaseMutex( pvInterruptEventMutex );\r
}\r
}\r
\r
void vPortDeleteThread( void *pvTaskToDelete )\r
{\r
-xThreadState *pxThreadState;\r
+ThreadState_t *pxThreadState;\r
uint32_t ulErrorCode;\r
\r
/* Remove compiler warnings if configASSERT() is not defined. */\r
( void ) ulErrorCode;\r
\r
/* Find the handle of the thread being deleted. */\r
- pxThreadState = ( xThreadState * ) ( *( size_t *) pvTaskToDelete );\r
+ pxThreadState = ( ThreadState_t * ) ( *( size_t *) pvTaskToDelete );\r
\r
/* Check that the thread is still valid, it might have been closed by\r
vPortCloseRunningThread() - which will be the case if the task associated\r
\r
void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendYield )\r
{\r
-xThreadState *pxThreadState;\r
+ThreadState_t *pxThreadState;\r
void *pvThread;\r
uint32_t ulErrorCode;\r
\r
( void ) ulErrorCode;\r
\r
/* Find the handle of the thread being deleted. */\r
- pxThreadState = ( xThreadState * ) ( *( size_t *) pvTaskToDelete );\r
+ pxThreadState = ( ThreadState_t * ) ( *( size_t *) pvTaskToDelete );\r
pvThread = pxThreadState->pvThread;\r
\r
/* Raise the Windows priority of the thread to ensure the FreeRTOS scheduler\r
\r
void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber )\r
{\r
+ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );\r
+\r
configASSERT( xPortRunning );\r
\r
if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
process it yet if this call is within a critical section. It is\r
possible for this to be in a critical section as calls to wait for\r
mutexes are accumulative. */\r
- if( ulCriticalNesting == 0 )\r
+ if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
{\r
SetEvent( pvInterruptEvent );\r
+\r
+ if( ulInterruptNumber == portINTERRUPT_YIELD )\r
+ {\r
+ /* Going to wait for an event - make sure the event is not already\r
+ signaled. */\r
+ ResetEvent( pxThreadState->pvYieldEvent );\r
+ }\r
}\r
\r
ReleaseMutex( pvInterruptEventMutex );\r
+\r
+ if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
+ {\r
+ if( ulInterruptNumber == portINTERRUPT_YIELD )\r
+ {\r
+ /* The task was yielding so will be suspended however the\r
+ SuspendThread() function is asynchronous so this event is used to\r
+ prevent the task executing further if SuspendThread() does not take\r
+ effect immediately. */\r
+ WaitForSingleObject( pxThreadState->pvYieldEvent, INFINITE );\r
+ }\r
+ }\r
}\r
}\r
/*-----------------------------------------------------------*/\r
/* The interrupt event mutex is held for the entire critical section,\r
effectively disabling (simulated) interrupts. */\r
WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
- ulCriticalNesting++;\r
- }\r
- else\r
- {\r
- ulCriticalNesting++;\r
}\r
+\r
+ ulCriticalNesting++;\r
}\r
/*-----------------------------------------------------------*/\r
\r
void vPortExitCritical( void )\r
{\r
-int32_t lMutexNeedsReleasing;\r
+int32_t lMutexNeedsReleasing, lWaitForYield = pdFALSE;\r
\r
/* The interrupt event mutex should already be held by this thread as it was\r
obtained on entry to the critical section. */\r
(simulated) disabled? */\r
if( ulPendingInterrupts != 0UL )\r
{\r
+ ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );\r
+\r
configASSERT( xPortRunning );\r
SetEvent( pvInterruptEvent );\r
\r
+ if( ( ulPendingInterrupts & ( 1 << portINTERRUPT_YIELD ) ) != 0 )\r
+ {\r
+ /* Going to wait for an event - make sure the event is not already\r
+ signaled. */\r
+ ResetEvent( pxThreadState->pvYieldEvent );\r
+ lWaitForYield = pdTRUE;\r
+ }\r
+\r
/* Mutex will be released now, so does not require releasing\r
on function exit. */\r
lMutexNeedsReleasing = pdFALSE;\r
ReleaseMutex( pvInterruptEventMutex );\r
+\r
+ if( lWaitForYield != pdFALSE )\r
+ {\r
+ /* The task was yielding so will be suspended however the\r
+ SuspendThread() function is asynchronous so this event is\r
+ used to prevent the task executing further if\r
+ SuspendThread() does not take effect immediately. */\r
+ WaitForSingleObject( pxThreadState->pvYieldEvent, INFINITE );\r
+ }\r
}\r
}\r
else\r