]> git.sur5r.net Git - freertos/commitdiff
Win32 port:
authorrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Mon, 22 Nov 2010 10:38:43 +0000 (10:38 +0000)
committerrichardbarry <richardbarry@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Mon, 22 Nov 2010 10:38:43 +0000 (10:38 +0000)
Separate out the interrupt handlers into separate functions so they can be installed into the array of interrupt handlers.
Reverse the priorities of the pseudo interrupt handler and tick generate threads so the extra hand shaking event can be removed.

git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@1160 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

Source/portable/MSVC-MingW/port.c
Source/portable/MSVC-MingW/portmacro.h

index e3fde1e9b9e63ea8cd21f344f3e2541e0d428108..24b4577af5142040f2054e68069e70fc132f7913 100644 (file)
@@ -73,6 +73,14 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter );
  */\r
 static void prvProcessPseudoInterrupts( void );\r
 \r
+/*\r
+ * Interrupt handlers used by the kernel itself.  These are executed from the\r
+ * pseudo interrupt handler thread.\r
+ */\r
+static unsigned long prvProcessDeleteThreadInterrupt( void );\r
+static unsigned long prvProcessYieldInterrupt( void );\r
+static unsigned long prvProcessTickInterrupt( void );\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 /* The WIN32 simulator runs each task in a thread.  The context switching is\r
@@ -100,9 +108,6 @@ static void *pvInterruptEvent = NULL;
 by multiple threads. */\r
 static void *pvInterruptEventMutex = NULL;\r
 \r
-/* Events used to manage sequencing. */\r
-static void *pvTickAcknowledgeEvent = NULL;\r
-\r
 /* The critical nesting count for the currently executing task.  This is \r
 initialised to a non-zero value so interrupts do not become enabled during \r
 the initialisation phase.  As each task has its own critical nesting value \r
@@ -115,7 +120,7 @@ static unsigned long ulCriticalNesting = 9999UL;
 /* 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
 all the other interrupts can be user defined. */\r
-static void (*vIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };\r
+static unsigned long (*ulIsrHandler[ portMAX_INTERRUPTS ])( void ) = { 0 };\r
 \r
 /* Pointer to the TCB of the currently executing task. */\r
 extern void *pxCurrentTCB;\r
@@ -147,10 +152,8 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
                SetEvent( pvInterruptEvent );\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
-               SignalObjectAndWait( pvInterruptEventMutex, pvTickAcknowledgeEvent, INFINITE, FALSE );\r
+               and can access the interrupt handler variables. */\r
+               ReleaseMutex( pvInterruptEventMutex );\r
        }\r
 \r
        #ifdef __GNUC__\r
@@ -189,13 +192,17 @@ void *pvHandle;
 long lSuccess = pdPASS;\r
 xThreadState *pxThreadState;\r
 \r
+       /* Install the interrupt handlers used by the scheduler itself. */\r
+       vPortSetInterruptHandler( portINTERRUPT_YIELD, prvProcessYieldInterrupt );\r
+       vPortSetInterruptHandler( portINTERRUPT_TICK, prvProcessTickInterrupt );\r
+       vPortSetInterruptHandler( portINTERRUPT_DELETE_THREAD, prvProcessDeleteThreadInterrupt );\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
 \r
-       if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) || ( pvTickAcknowledgeEvent == NULL ) )\r
+       if( ( pvInterruptEventMutex == NULL ) || ( pvInterruptEvent == NULL ) )\r
        {\r
                lSuccess = pdFAIL;\r
        }\r
@@ -211,7 +218,7 @@ xThreadState *pxThreadState;
        \r
        if( lSuccess == pdPASS )\r
        {\r
-               if( SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL ) == 0 )\r
+               if( SetThreadPriority( pvHandle, THREAD_PRIORITY_NORMAL ) == 0 )\r
                {\r
                        lSuccess = pdFAIL;\r
                }\r
@@ -222,11 +229,13 @@ xThreadState *pxThreadState;
        if( lSuccess == pdPASS )\r
        {\r
                /* Start the thread that simulates the timer peripheral to generate\r
-               tick interrupts. */\r
+               tick interrupts.  The priority is set below that of the pseudo \r
+               interrupt handler so the interrupt event mutex is used for the\r
+               handshake / overrun protection. */\r
                pvHandle = CreateThread( NULL, 0, prvSimulatedPeripheralTimer, NULL, 0, NULL );\r
                if( pvHandle != NULL )\r
                {\r
-                       SetThreadPriority( pvHandle, THREAD_PRIORITY_NORMAL );\r
+                       SetThreadPriority( pvHandle, THREAD_PRIORITY_BELOW_NORMAL );\r
                        SetThreadPriorityBoost( pvHandle, TRUE );\r
                        SetThreadAffinityMask( pvHandle, 0x01 );\r
                }\r
@@ -252,12 +261,45 @@ xThreadState *pxThreadState;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+static unsigned long prvProcessDeleteThreadInterrupt( void )\r
+{\r
+       return pdTRUE;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static unsigned long prvProcessYieldInterrupt( void )\r
+{\r
+       return pdTRUE;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static unsigned long prvProcessTickInterrupt( void )\r
+{\r
+unsigned long ulSwitchRequired;\r
+\r
+       /* Process the tick itself. */\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
+               ulSwitchRequired = pdTRUE;\r
+       }\r
+       #else\r
+       {\r
+               ulSwitchRequired = pdFALSE;\r
+       }\r
+       #endif\r
+\r
+       return ulSwitchRequired;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 static void prvProcessPseudoInterrupts( void )\r
 {\r
-long lSwitchRequired, lCurrentTaskBeingDeleted;\r
+unsigned long ulSwitchRequired, i;\r
 xThreadState *pxThreadState;\r
 void *pvObjectList[ 2 ];\r
-unsigned long i;\r
 \r
        /* Going to block on the mutex that ensured exclusive access to the pseudo \r
        interrupt objects, and the event that signals that a pseudo interrupt\r
@@ -271,8 +313,7 @@ unsigned long i;
 \r
                /* Used to indicate whether the pseudo interrupt processing has\r
                necessitated a context switch to another task/thread. */\r
-               lSwitchRequired = pdFALSE;\r
-               lCurrentTaskBeingDeleted = pdFALSE;\r
+               ulSwitchRequired = pdFALSE;\r
 \r
                /* For each interrupt we are interested in processing, each of which is\r
                represented by a bit in the 32bit ulPendingInterrupts variable. */\r
@@ -281,65 +322,22 @@ unsigned long i;
                        /* Is the pseudo interrupt pending? */\r
                        if( ulPendingInterrupts & ( 1UL << i ) )\r
                        {\r
-                               switch( i )\r
+                               /* Is a handler installed? */\r
+                               if( ulIsrHandler[ i ] != NULL )\r
                                {\r
-                                       case portINTERRUPT_YIELD:\r
-\r
-                                               lSwitchRequired = pdTRUE;\r
-\r
-                                               /* Clear the interrupt pending bit. */\r
-                                               ulPendingInterrupts &= ~( 1UL << portINTERRUPT_YIELD );\r
-                                               break;\r
-\r
-                                       case portINTERRUPT_TICK:\r
-                                       \r
-                                               /* Process the tick itself. */\r
-                                               vTaskIncrementTick();\r
-                                               #if( configUSE_PREEMPTION != 0 )\r
-                                               {\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
-                                               /* Clear the interrupt pending bit. */\r
-                                               ulPendingInterrupts &= ~( 1UL << portINTERRUPT_TICK );\r
-                                               SetEvent( pvTickAcknowledgeEvent );\r
-                                               break;\r
-\r
-                                       case portINTERRUPT_DELETE_THREAD:\r
-\r
-                                               lCurrentTaskBeingDeleted = pdTRUE;\r
-\r
-                                               /* Clear the interrupt pending bit. */\r
-                                               ulPendingInterrupts &= ~( 1UL << portINTERRUPT_DELETE_THREAD );\r
-                                               break;\r
-\r
-                                       default:\r
-\r
-                                               /* Is a handler installed? */\r
-                                               if( vIsrHandler[ i ] != NULL )\r
-                                               {\r
-                                                       lSwitchRequired = pdTRUE;\r
-\r
-                                                       /* Run the actual handler. */\r
-                                                       vIsrHandler[ i ]();\r
-\r
-                                                       /* Clear the interrupt pending bit. */\r
-                                                       ulPendingInterrupts &= ~( 1UL << i );\r
-\r
-                                                       /* TODO:  Need to have some sort of handshake \r
-                                                       event here for non-tick and none yield \r
-                                                       interrupts. */\r
-                                               }\r
-                                               break;\r
+                                       /* Run the actual handler. */\r
+                                       if( ulIsrHandler[ i ]() != pdFALSE )\r
+                                       {\r
+                                               ulSwitchRequired |= ( 1 << i );\r
+                                       }\r
                                }\r
+\r
+                               /* Clear the interrupt pending bit. */\r
+                               ulPendingInterrupts &= ~( 1UL << i );\r
                        }\r
                }\r
 \r
-               if( ( lSwitchRequired != pdFALSE ) || ( lCurrentTaskBeingDeleted != pdFALSE ) )\r
+               if( ulSwitchRequired != pdFALSE )\r
                {\r
                        void *pvOldCurrentTCB;\r
 \r
@@ -355,7 +353,7 @@ unsigned long i;
                                /* Suspend the old thread. */\r
                                pxThreadState = ( xThreadState *) *( ( unsigned long * ) pvOldCurrentTCB );\r
 \r
-                               if( lCurrentTaskBeingDeleted != pdFALSE )\r
+                               if( ( ulSwitchRequired & ( 1 << portINTERRUPT_DELETE_THREAD ) ) != pdFALSE )\r
                                {\r
                                        TerminateThread( pxThreadState->pvThread, 0 );\r
                                }\r
@@ -433,19 +431,19 @@ xThreadState *pxThreadState;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-void vPortSetInterruptHandler( unsigned long ulInterruptNumber, void (*pvHandler)( void ) )\r
+void vPortSetInterruptHandler( unsigned long ulInterruptNumber, unsigned long (*pvHandler)( void ) )\r
 {\r
        if( ulInterruptNumber < portMAX_INTERRUPTS )\r
        {\r
                if( pvInterruptEventMutex != NULL )\r
                {\r
                        WaitForSingleObject( pvInterruptEventMutex, INFINITE );\r
-                       vIsrHandler[ ulInterruptNumber ] = pvHandler;\r
+                       ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
                        ReleaseMutex( pvInterruptEventMutex );\r
                }\r
                else\r
                {\r
-                       vIsrHandler[ ulInterruptNumber ] = pvHandler;\r
+                       ulIsrHandler[ ulInterruptNumber ] = pvHandler;\r
                }\r
        }\r
 }\r
index 7dfdf166f4ac8e4d492bfb7e50a57bd1aff93f9f..a83fbd17a59068479d6c3c45fffcd5e4280c1510 100644 (file)
@@ -111,5 +111,15 @@ void vPortExitCritical( void );
 */\r
 void vPortGeneratePseudoInterrupt( unsigned long ulInterruptNumber );\r
 \r
+/*\r
+ * Install an interrupt handler to be called by the pseudo interrupt handler \r
+ * thread.  The interrupt number must be above any used by the kernel itself\r
+ * (at the time of writing the kernel was using interrupt numbers 0, 1, and 2\r
+ * as defined above).  The number must also be lower than 32. \r
+ *\r
+ * Interrupt handler functions must return a non-zero value if executing the\r
+ * handler resulted in a task switch being required. \r
+ */\r
+void vPortSetInterruptHandler( unsigned long ulInterruptNumber, unsigned long (*pvHandler)( void ) );\r
 \r
 #endif\r