Generic ISR dispatch routine

anonymous wrote on Sunday, December 24, 2006:

In order to have a single generic ISR dispatch routine, I have added a function to tasks.c that returns the priority of the highest priority ready task.

unsigned portBASE_TYPE TopPriority(void)
    return uxTopReadyPriority;

My generic dispatch routine captures this priority before dispatching to the proper ISR. When the ISR returns, it tests whether the highest priority has changed. If so, it calls for a context switch, else it doesn’t.

void PortIRQDispatch(void)
    unsigned    portBASE_TYPE prevTopPriority = TopPriority();



I wonder if anyone has any comments on issues/problems this may raise.

One point is that this subverts time slicing between tasks of equal priority. Using <= rather than < in the final test would be similar to time slicing, although not quite the same.

nobody wrote on Sunday, December 24, 2006:

I’m not sure I follow the object of this code, what you are trying to achieve? 

If you want to only cause a switch to another task when there is a task of higher priority than the running task then you could add the code to vTaskSwitchContext in tasks.c to only execute the while loop if the top ready priority is greater than the running priority.

If this is just to have commonality in your IRQs and to prevent the application writer having to use the ENTER/EXIT macros then I think the code is good.  You of coarse have a few little expenses in an extra layer of indirection.  Also you are going to save and restore the context even when the task does not need it saved and restored.  Each IRQ in the system will save the context.

Different ports handle this in different ways.  Some only save the context when a switch is actually required which is really nice as this is the most optimal.  From the macros you quote I assume you are using the ARM port.  This itself uses two different techniques.  Looking at the STR750 (and I think the ARM9) ports you will see that there is an IRQ wrapper  written in assembler that saves the context before calling the IRQ C function, then restores it again prior to exiting the ASM wrapper (on completion of the interrupt).  The portEND_SWITCHING_ISR macro then becomes a simple call to vTaskSwitchContext() if a switch is required.  Take a look at the port page for STR750/RIDE at the section "Interrupt service routines".

anonymous wrote on Sunday, December 24, 2006:

I am using an LPC2xxx. The VIC priority vectored interrupts and the FIQ interrupt are handled separately and each in there own way. In my case, they all operate outside the realm of FreeRTOS, never triggering context switches.

My PortIRQDispatch() is the common routine entered off of the VIC default IRQ. All of these have to go through such a common handler. So it can perform operations common to all of the default vector ISRs. I was looking for a common way of determining if a context switch was required on exiting the ISR.

I really don’t like to tamper with FreeRTOS, so I look for ways to work with it with as few changes as possible. My TopPriority() function added into tasks.c seemed like a pretty inocuous change that gave me what I wanted. But I wonder if others might have tried a similar stunt and run into some subtle ‘gotcha’.

In this particular case, I’m not sure tampering vTaskSwitchContext() would be a good thing to do. I think it would mean that taskYIELD() wouldn’t perform as expected.

Thanks for the feedback.