]> git.sur5r.net Git - freertos/commitdiff
Win32 port.c - working ok in co-operative mode, but pre-emptive thread scheduling...
authorrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Thu, 18 Nov 2010 16:41:44 +0000 (16:41 +0000)
committerrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Thu, 18 Nov 2010 16:41:44 +0000 (16:41 +0000)
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@1145 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

Source/portable/MSVC-MingW/port.c

index f37eb2c0af72602bf23e302a9eda6b8e8074e313..e880a0977808f19226e7b5865f3cef2f555ac66e 100644 (file)
@@ -66,7 +66,8 @@
 /*\r
  * Created as a high priority thread, this function uses a timer to simulate\r
  * a tick interrupt being generated on an embedded target.  In this Windows\r
- * environment the timer does not achieve real time performance though.\r
+ * environment the timer does not achieve anything approaching real time \r
+ * performance though.\r
  */\r
 static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );\r
 \r
@@ -74,7 +75,7 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );
  * Process all the simulated interrupts - each represented by a bit in \r
  * ulPendingInterrupts variable.\r
  */\r
-static void prvProcessEvents( void );\r
+static void prvProcessPseudoInterrupts( void );\r
 \r
 /*-----------------------------------------------------------*/\r
 \r
@@ -85,9 +86,9 @@ the only thing it will ever hold.  The structure indirectly maps the task handle
 to a thread handle. */\r
 typedef struct\r
 {\r
-       /* Set to true for tasks that call the generate pseudo interrupt function,\r
-       as the event handler needs to know whether to signal the interrupt ack\r
-       event when the task next runs. */\r
+       /* Set to true if the task run by the thread yielded control to the pseudo\r
+       interrupt handler manually - either by yielding or when exiting a critical\r
+       section while pseudo interrupts were pending. */\r
        long lWaitingInterruptAck;                      \r
 \r
        /* Critical nesting count of the task - each task has its own. */\r
@@ -101,18 +102,15 @@ typedef struct
 bit represents one interrupt, so a maximum of 32 interrupts can be simulated. */\r
 static volatile unsigned long ulPendingInterrupts = 0UL;\r
 \r
-/* An event used to inform the interrupt dispatch thread (a high priority thread\r
-that simulated interrupt processing) that an IRQ or SWI type interrupt is\r
+/* An event used to inform the pseudo interrupt processing thread (a high \r
+priority thread that simulated interrupt processing) that an interrupt is\r
 pending. */\r
 static void *pvInterruptEvent = NULL;\r
 \r
-/* Mutex used to protect all the pseudo interrupt variables that are accessed by\r
-multiple threads. */\r
+/* Mutex used to protect all the pseudo interrupt variables that are accessed \r
+by multiple threads. */\r
 static void *pvInterruptEventMutex = NULL;\r
 \r
-/* The main thread, which also acts as the pseudo interrupt handler. */\r
-static void *pvMainThreadAndInterruptHandler;\r
-\r
 /* Events used to manage sequencing. */\r
 static void *pvTickAcknowledgeEvent = NULL, *pvInterruptAcknowledgeEvent = NULL;\r
 \r
@@ -121,8 +119,8 @@ initialised to a non-zero value so interrupts do not become enabled during
 the initialisation phase.  As each task has its own critical nesting value \r
 ulCriticalNesting will get set to zero when the first task runs.  This \r
 initialisation is probably not critical in this simulated environment as the\r
-pseudo interrupt handlers/dispatchers do not get created until the FreeRTOS\r
-scheduler is started. */\r
+pseudo interrupt handlers do not get created until the FreeRTOS scheduler is \r
+started anyway. */\r
 static unsigned long ulCriticalNesting = 9999UL;\r
 \r
 /* Handlers for all the simulated software interrupts.  The first two positions\r
@@ -142,41 +140,41 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
 \r
        for(;;)\r
        {\r
-               /* The timer is reset on each iteration of this loop rather than being set\r
-               to function periodically - this is for the reasons stated in the comments\r
-               where the timer is created. */\r
                vPortTrace( "prvSimulatedPeripheralTimer: Tick acked, re-Sleeping()\r\n" );\r
 \r
                /* Wait until the timer expires and we can access the pseudo interrupt \r
-               variables. */\r
+               variables.  *NOTE* this is not a 'real time' way of generating tick \r
+               events as the next wake time should be relative to the previous wake \r
+               time, not the time that Sleep() is called.  It is done this way to \r
+               prevent overruns in this very non real time simulated/emulated \r
+               environment. */\r
                Sleep( portTICK_RATE_MS );\r
 \r
-               vPortTrace( "prvSimulatedPeripheralTimer: Timer signalled, waiting interrupt event mutex\r\n" );\r
+               vPortTrace( "prvSimulatedPeripheralTimer: Sleep expired, waiting interrupt event mutex\r\n" );\r
                WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
                vPortTrace( "prvSimulatedPeripheralTimer: Got interrupt event mutex\r\n" );\r
                \r
                /* The timer has expired, generate the simulated tick event. */\r
                ulPendingInterrupts |= ( 1 << portINTERRUPT_TICK );\r
-               if( pvInterruptEvent != NULL )\r
+\r
+               /* The interrupt is now pending - but should only be processed if\r
+               interrupts are actually enabled. */\r
+               if( ulCriticalNesting == 0UL )\r
                {\r
                        vPortTrace( "prvSimulatedPeripheralTimer: Setting interrupt event to signal tick\r\n" );\r
                        SetEvent( pvInterruptEvent );\r
-               }\r
 \r
-               /* Give back the mutex so the pseudo interrupt handler unblocks and can\r
-               access the interrupt handler variables.  This high priority task will then\r
-               loop back round to wait for the lower priority pseudo interrupt handler \r
-               thread to acknowledge the tick. */\r
-               if( pvInterruptEventMutex != NULL )\r
-               {\r
+                       /* Give back the mutex so the pseudo interrupt handler unblocks \r
+                       and can access the interrupt handler variables.  This high priority\r
+                       task will then loop back round after waiting for the lower priority \r
+                       pseudo interrupt handler thread to acknowledge the tick. */\r
                        vPortTrace( "prvSimulatedPeripheralTimer: Releasing interrupt event mutex so tick can be processed\r\n" );\r
+                       SignalObjectAndWait( pvInterruptEventMutex, pvTickAcknowledgeEvent, INFINITE, FALSE );\r
+               }\r
+               else\r
+               {\r
                        ReleaseMutex( pvInterruptEventMutex );\r
                }\r
-\r
-               /* Wait for the previous tick to be acknowledged before resetting the timer.\r
-               As mentioned above this is done to prevent timer overruns in the non real-\r
-               time environment.  THIS IS NOT HOW A REAL PORT SHOULD USE TIMERS! */\r
-               WaitForSingleObject( pvTickAcknowledgeEvent, INFINITE );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -193,11 +191,12 @@ xThreadState *pxThreadState = NULL;
        other than holding this structure. */\r
        pxThreadState = ( xThreadState * ) ( pxTopOfStack - sizeof( xThreadState ) );\r
 \r
-               /* Create the thread itself. */\r
-               pxThreadState->pvThread = ( void * ) CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );\r
-               pxThreadState->ulCriticalNesting = portNO_CRITICAL_NESTING;\r
-               pxThreadState->lWaitingInterruptAck = pdFALSE;\r
-               SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );\r
+       /* Create the thread itself. */\r
+       pxThreadState->pvThread = ( void * ) CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED, NULL );\r
+       SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );\r
+       pxThreadState->ulCriticalNesting = portNO_CRITICAL_NESTING;\r
+       pxThreadState->lWaitingInterruptAck = pdFALSE;\r
+       SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );\r
        \r
        return ( portSTACK_TYPE * ) pxThreadState;\r
 }\r
@@ -209,10 +208,22 @@ void *pvHandle;
 long lSuccess = pdPASS;\r
 xThreadState *pxThreadState;\r
 \r
-       /* Set the priority of this thread such that it is above the priority of the\r
-       threads that run tasks, but below the priority of the thread that generates\r
-       the pseudo tick interrupts.  This priority is chosen because this is the\r
-       thread that actually handles the pseudo interrupts. */\r
+       /* Create the events and mutexes that are used to synchronise all the\r
+       threads. */\r
+       pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
+       pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
+       pvTickAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
+       pvInterruptAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
+\r
+       if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) || ( pvTickAcknowledgeEvent == NULL ) || ( pvInterruptAcknowledgeEvent == NULL ) )\r
+       {\r
+               lSuccess = pdFAIL;\r
+       }\r
+\r
+       /* Set the priority of this thread such that it is above the priority of \r
+       the threads that run tasks.  This higher priority is required to ensure\r
+       pseudo interrupts take priority over tasks. */\r
+       SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS );\r
        pvHandle = GetCurrentThread();\r
        if( pvHandle == NULL )\r
        {\r
@@ -221,48 +232,49 @@ xThreadState *pxThreadState;
        \r
        if( lSuccess == pdPASS )\r
        {\r
-               if( SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL ) == 0 )\r
+               if( SetThreadPriority( pvHandle, THREAD_PRIORITY_HIGHEST ) == 0 )\r
                {\r
                        lSuccess = pdFAIL;\r
                }\r
+               SetThreadPriorityBoost( pvHandle, TRUE );\r
        }\r
 \r
        if( lSuccess == pdPASS )\r
        {\r
-               /* Create the events and mutexes that are used to synchronise all the\r
-               threads. */\r
-               pvInterruptEventMutex = CreateMutex( NULL, FALSE, NULL );\r
-               pvInterruptEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
-               pvTickAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
-               pvInterruptAcknowledgeEvent = CreateEvent( NULL, FALSE, FALSE, NULL );\r
-\r
                /* Start the thread that simulates the timer peripheral to generate\r
                tick interrupts. */\r
                pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, 0, NULL );\r
                if( pvHandle != NULL )\r
                {\r
-                       SetThreadPriority( pvHandle, THREAD_PRIORITY_ABOVE_NORMAL );\r
+                       SetThreadPriority( pvHandle, THREAD_PRIORITY_HIGHEST );\r
+                       SetThreadPriorityBoost( pvHandle, TRUE );\r
                }\r
                \r
-               /* Start the highest priority task by obtaining its associated thread state\r
-               structure, in which is stored the thread handle. */\r
+               /* Start the highest priority task by obtaining its associated thread \r
+               state structure, in which is stored the thread handle. */\r
                pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
                ulCriticalNesting = portNO_CRITICAL_NESTING;\r
 \r
                vPortTrace( "Created system threads, starting task" );\r
 \r
+               /* Bump up the priority of the thread that is going to run, in the\r
+               hope that this will asist in getting the Windows thread scheduler to\r
+               behave as an embedded engineer might expect. */\r
+               SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_ABOVE_NORMAL );\r
                ResumeThread( pxThreadState->pvThread );\r
+\r
+               /* Handle all pseudo interrupts - including yield requests and \r
+               simulated ticks. */\r
+               prvProcessPseudoInterrupts();\r
        }       \r
        \r
-       /* Handle all pseudo interrupts - including yield requests and simulated ticks. */\r
-       prvProcessEvents();\r
-\r
-       /* Would not expect to return from prvProcessEvents(), so should not get here. */\r
+       /* Would not expect to return from prvProcessPseudoInterrupts(), so should \r
+       not get here. */\r
        return 0;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvProcessEvents( void )\r
+static void prvProcessPseudoInterrupts( void )\r
 {\r
 long lSwitchRequired;\r
 xThreadState *pxThreadState;\r
@@ -270,27 +282,25 @@ void *pvObjectList[ 2 ];
 unsigned long i;\r
 //char cTraceBuffer[ 256 ];\r
 \r
-       vPortTrace( "Entering prvProcessEvents\r\n" );\r
+       vPortTrace( "Entering prvProcessPseudoInterrupts\r\n" );\r
 \r
        /* Going to block on the mutex that ensured exclusive access to the pseudo \r
-       interrupt objects, and the event that signals that an interrupt is waiting\r
-       to be processed. */\r
+       interrupt objects, and the event that signals that a pseudo interrupt\r
+       should be processed. */\r
        pvObjectList[ 0 ] = pvInterruptEventMutex;\r
        pvObjectList[ 1 ] = pvInterruptEvent;\r
 \r
        for(;;)\r
        {\r
-               vPortTrace( "prvProcessEvents: Waiting for next interrupt event\r\n" );\r
+               vPortTrace( "prvProcessPseudoInterrupts: Waiting for next interrupt event\r\n" );\r
                WaitForMultipleObjects( sizeof( pvObjectList ) / sizeof( void * ), pvObjectList, TRUE, INFINITE );\r
-               vPortTrace( "prvProcessEvents: Got interrupt event and mutex\r\n" );\r
-               //vPortTrace( "prvProcessEvents: Waiting for next interrupt event\r\n" );\r
-               //WaitForSingleObject( pvInterruptEvent, INFINITE );\r
-               //vPortTrace( "prvProcessEvents: Waiting interrupt event mutex to access interrupt data\r\n" );\r
-               //WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
+               vPortTrace( "prvProcessPseudoInterrupts: Got interrupt event and mutex\r\n" );\r
 \r
+               /* Used to indicate whether the pseudo interrupt processing has\r
+               necessitated a context switch to another task/thread. */\r
                lSwitchRequired = pdFALSE;\r
 \r
-               /* For each interrupt we are interested in processing, each of which is \r
+               /* For each interrupt we are interested in processing, each of which is\r
                represented by a bit in the 32bit ulPendingInterrupts variable. */\r
                for( i = 0; i < portMAX_INTERRUPTS; i++ )\r
                {\r
@@ -301,8 +311,7 @@ unsigned long i;
                                {\r
                                        case portINTERRUPT_YIELD:\r
 \r
-                                               vPortTrace( "prvProcessEvents: Processing Yield\r\n" );\r
-                                               /* Yield interrupts occur no matter what the critical nesting count. */\r
+                                               vPortTrace( "prvProcessPseudoInterrupts: Processing Yield\r\n" );\r
                                                lSwitchRequired = pdTRUE;\r
 \r
                                                /* Clear the interrupt pending bit. */\r
@@ -311,46 +320,64 @@ unsigned long i;
 \r
                                        case portINTERRUPT_TICK:\r
                                        \r
-                                               /* Tick interrupts should only be processed if the critical nesting count\r
-                                               is zero.  The critical nesting count represents the interrupt mask on \r
-                                               real target hardware. */\r
-                                               vPortTrace( "prvProcessEvents: Processing tick event\r\n" );\r
-                                               if( ulCriticalNesting == 0 )\r
+                                               /* Tick interrupts should only be processed if the \r
+                                               critical nesting count is zero.  The critical nesting \r
+                                               count represents the interrupt mask on real target \r
+                                               hardware.  The thread that genereates ticks will not\r
+                                               actually ask for the tick to be processed unless the\r
+                                               critical nesting count is zero anyway, but it is \r
+                                               possible that a tick is pending when a yield is \r
+                                               performed (depending on if the simulation/emulation is\r
+                                               set up to process yields while within a critical \r
+                                               section. */\r
+                                               vPortTrace( "prvProcessPseudoInterrupts: Processing tick event\r\n" );\r
+                                               if( ulCriticalNesting == 0UL )\r
                                                {\r
                                                        /* Process the tick itself. */\r
-                                                       vPortTrace( "prvProcessEvents: Incrementing tick\r\n" );\r
+                                                       vPortTrace( "prvProcessPseudoInterrupts: Incrementing tick\r\n" );\r
                                                        vTaskIncrementTick();\r
                                                        #if( configUSE_PREEMPTION != 0 )\r
                                                        {\r
-                                                               /* A context switch is only automatically performed from the tick\r
-                                                               interrupt if the pre-emptive scheduler is being used. */\r
+                                                               /* A context switch is only automatically \r
+                                                               performed from the tick interrupt if the \r
+                                                               pre-emptive scheduler is being used. */\r
                                                                lSwitchRequired = pdTRUE;\r
                                                        }\r
                                                        #endif\r
                                                        \r
-                                                       vPortTrace( "prvProcessEvents: Acking tick\r\n" );\r
-                                                       SetEvent( pvTickAcknowledgeEvent );\r
-\r
                                                        /* Clear the interrupt pending bit. */\r
                                                        ulPendingInterrupts &= ~( 1UL << portINTERRUPT_TICK );\r
+\r
+                                                       vPortTrace( "prvProcessPseudoInterrupts: Acking tick\r\n" );\r
+                                                       SetEvent( pvTickAcknowledgeEvent );\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       /* The tick is held pending in ulCriticalNesting\r
+                                                       until such time that pseudo interrupts are enabled\r
+                                                       again. */\r
                                                }\r
                                                break;\r
 \r
                                        default:\r
 \r
-                                               /* Is a handler installed? */\r
-                                               if( vIsrHandler[ i ] != NULL )\r
+                                               if( ulCriticalNesting == 0UL )\r
                                                {\r
-                                                       lSwitchRequired = pdTRUE;\r
+                                                       /* Is a handler installed? */\r
+                                                       if( vIsrHandler[ i ] != NULL )\r
+                                                       {\r
+                                                               lSwitchRequired = pdTRUE;\r
 \r
-                                                       /* Run the actual handler. */\r
-                                                       vIsrHandler[ i ]();\r
+                                                               /* Run the actual handler. */\r
+                                                               vIsrHandler[ i ]();\r
 \r
-                                                       /* Clear the interrupt pending bit. */\r
-                                                       ulPendingInterrupts &= ~( 1UL << i );\r
+                                                               /* Clear the interrupt pending bit. */\r
+                                                               ulPendingInterrupts &= ~( 1UL << i );\r
 \r
-                                                       /* TODO:  Need to have some sort of handshake event here for non-tick\r
-                                                       and none yield interrupts. */\r
+                                                               /* TODO:  Need to have some sort of handshake \r
+                                                               event here for non-tick and none yield \r
+                                                               interrupts. */\r
+                                                       }\r
                                                }\r
                                                break;\r
                                }\r
@@ -375,23 +402,39 @@ unsigned long i;
                        if( pvOldCurrentTCB != pxCurrentTCB )\r
                        {\r
                                /* Suspend the old thread. */\r
+                               SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_IDLE );\r
                                SuspendThread( pxThreadState->pvThread );\r
+\r
                                //sprintf( cTraceBuffer, "Event processor: suspending %s, resuming %s\r\n", ((xTCB*)pvOldCurrentTCB)->pcTaskName, ((xTCB*)pxCurrentTCB)->pcTaskName );\r
                                //vPortTrace( cTraceBuffer );\r
 \r
                                /* Obtain the state of the task now selected to enter the Running state. */\r
                                pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );\r
                                ulCriticalNesting = pxThreadState->ulCriticalNesting;\r
+                               SetThreadPriority( pxThreadState->pvThread, THREAD_PRIORITY_ABOVE_NORMAL );\r
                                ResumeThread( pxThreadState->pvThread );\r
 \r
                                if( pxThreadState->lWaitingInterruptAck == pdTRUE )\r
                                {\r
                                        pxThreadState->lWaitingInterruptAck = pdFALSE;\r
-                                       vPortTrace( "prvProcessEvents: Acking interrupt\r\n" );\r
+                                       vPortTrace( "prvProcessPseudoInterrupts: Acking interrupt\r\n" );\r
                                        SetEvent( pvInterruptAcknowledgeEvent );\r
                                }\r
                        }\r
                }\r
+               else\r
+               {\r
+                       /* On exiting a critical section a task may have blocked on the\r
+                       interrupt event when only a tick needed processing, in which case\r
+                       it will not have been released from waiting on the event yet. */\r
+                       pxThreadState = ( xThreadState * ) ( *( unsigned long *) pxCurrentTCB );\r
+                       if( pxThreadState->lWaitingInterruptAck == pdTRUE )\r
+                       {\r
+                               pxThreadState->lWaitingInterruptAck = pdFALSE;\r
+                               vPortTrace( "prvProcessPseudoInterrupts: Acking interrupt even though a yield has not been performed.\r\n" );\r
+                               SetEvent( pvInterruptAcknowledgeEvent );\r
+                       }\r
+               }\r
 \r
                ReleaseMutex( pvInterruptEventMutex );\r
        }\r
@@ -410,11 +453,11 @@ xThreadState *pxThreadState;
        if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )\r
        {\r
                /* Yield interrupts are processed even when critical nesting is non-zero. */\r
-               if( ( ulCriticalNesting == 0 ) || ( ulInterruptNumber == portINTERRUPT_YIELD ) )\r
-               {\r
-                       WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
-                       ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
+               WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
+               ulPendingInterrupts |= ( 1 << ulInterruptNumber );\r
 \r
+               if( ulCriticalNesting == 0 ) //|| ( ulInterruptNumber == portINTERRUPT_YIELD ) )\r
+               {\r
                        /* The event handler needs to know to signal the interrupt acknowledge event\r
                        the next time this task runs. */\r
                        pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
@@ -431,13 +474,18 @@ xThreadState *pxThreadState;
                                __asm { NOP };\r
                        }\r
 \r
+                       SignalObjectAndWait( pvInterruptEventMutex, pvInterruptAcknowledgeEvent, INFINITE, FALSE );\r
                        vPortTrace( "vPortGeneratePseudoInterrupt: About to release interrupt event mutex\r\n" );\r
-                       ReleaseMutex( pvInterruptEventMutex );\r
+//                     ReleaseMutex( pvInterruptEventMutex );\r
                        vPortTrace( "vPortGeneratePseudoInterrupt: Interrupt event mutex released, going to wait for interrupt ack\r\n" );\r
 \r
-                       WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
+//                     WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
                        vPortTrace( "vPortGeneratePseudoInterrupt: Interrupt acknowledged, leaving vPortGeneratePseudoInterrupt()\r\n" );\r
                }\r
+               else\r
+               {\r
+                       ReleaseMutex( pvInterruptEventMutex );\r
+               }\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -462,7 +510,17 @@ void vPortSetInterruptHandler( unsigned long ulInterruptNumber, void (*pvHandler
 \r
 void vPortEnterCritical( void )\r
 {\r
-       ulCriticalNesting++;\r
+       if( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED )\r
+       {\r
+               WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
+//             SuspendThread( pvSimulatedTimerThread );\r
+               ulCriticalNesting++;\r
+               ReleaseMutex( pvInterruptEventMutex );\r
+       }\r
+       else\r
+       {\r
+               ulCriticalNesting++;\r
+       }       \r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -472,37 +530,59 @@ xThreadState *pxThreadState;
 \r
        if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
        {\r
-               ulCriticalNesting--;\r
-\r
-               if( ulCriticalNesting == 0 )\r
+               if( ulCriticalNesting == ( portNO_CRITICAL_NESTING + 1 ) )\r
                {\r
+                       /* Wait for the interrupt event mutex prior to manipulating or \r
+                       testing the pseudo interrupt control variables. */\r
+                       WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
+                       vPortTrace( "vPortExitCritical:  Got interrupt event mutex\r\n" );\r
+\r
+//                     ResumeThread( pvSimulatedTimerThread );\r
+\r
+                       /* Now it is safe to decrement the critical nesting count as no\r
+                       tick events will be processed until the interrupt event mutex is\r
+                       given back. */\r
+                       ulCriticalNesting--;\r
+\r
                        /* Were any interrupts set to pending while interrupts were \r
                        (pseudo) disabled? */\r
                        if( ulPendingInterrupts != 0UL )\r
                        {\r
-                               WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
-                               vPortTrace( "vPortExitCritical:  Setting interrupt event\r\n" );\r
                                SetEvent( pvInterruptEvent );\r
 \r
-                       /* The interrupt ack event should not be signaled yet - if it is then \r
-                       there is an error in the logical simulation. */\r
-                       if( WaitForSingleObject( pvInterruptAcknowledgeEvent, 0 ) != WAIT_TIMEOUT )\r
-                       {\r
-                               /* This line is for a break point only. */\r
-                               __asm { NOP };\r
-                       }\r
+                               /* The interrupt ack event should not be signaled yet - if it \r
+                               is then there is an error in the logical simulation. */\r
+                               if( WaitForSingleObject( pvInterruptAcknowledgeEvent, 0 ) != WAIT_TIMEOUT )\r
+                               {\r
+                                       /* This line is for a break point only. */\r
+                                       __asm { NOP };\r
+                               }\r
 \r
-                       /* The event handler needs to know to signal the interrupt acknowledge \r
-                       event the next time this task runs. */\r
-                       pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
-                       pxThreadState->lWaitingInterruptAck = pdTRUE;\r
+                               /* The event handler needs to know to signal the interrupt \r
+                               acknowledge event the next time this task runs. */\r
+                               pxThreadState = ( xThreadState * ) *( ( unsigned long * ) pxCurrentTCB );\r
+                               pxThreadState->lWaitingInterruptAck = pdTRUE;\r
 \r
-                       ReleaseMutex( pvInterruptEventMutex );\r
+                               SignalObjectAndWait( pvInterruptEventMutex, pvInterruptAcknowledgeEvent, INFINITE, FALSE );\r
+                               /* Give back the interrupt event mutex so the event can be processed. */\r
+//                             ReleaseMutex( pvInterruptEventMutex );\r
 \r
-                       vPortTrace( "vPortExitCritical:  Waiting interrupt ack\r\n" );\r
-                       WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
-                       vPortTrace( "vPortExitCritical: Interrupt acknowledged, leaving critical section code\r\n" );\r
+//                             vPortTrace( "vPortExitCritical:  Waiting interrupt ack\r\n" );\r
+//                             WaitForSingleObject( pvInterruptAcknowledgeEvent, INFINITE );\r
+                               vPortTrace( "vPortExitCritical: Interrupt acknowledged, leaving critical section code\r\n" );\r
+                       }\r
+                       else\r
+                       {\r
+                               /* Can't leave here without giving back the interrupt event\r
+                               mutex. */\r
+                               ReleaseMutex( pvInterruptEventMutex );\r
                        }\r
                }\r
+               else\r
+               {\r
+                       /* Tick interrupts will still not be processed as the critical\r
+                       nesting depth will not be zero. */\r
+                       ulCriticalNesting--;\r
+               }\r
        }\r
 }\r