Re: Making a function safe for use in an ISR



joshc <josh.curtz@xxxxxxxxx> wrote:
So during an interview for an embedded software position, I was asked
to write any function I wanted in C. I chose an iterative factorial
function like this:

int iterative_factorial(int i) {
int f;
f = 1;

while (i != 0) {
f = i * f;
i--;
}
return(f);
}

I was then asked what I would have to worry about or fix to make the
function safe to use in an ISR. I really could not think of anything
wrong with this function as it seems reentrant and I don't see any
concurrency issues.

You're thinking like a high-level language programmer. ;)

Can anyone think of anything that might make this
function unsafe to use in an ISR?

I'll not give you an answer, but some pointers to think about.

Consider what the most complex operation in that function is, and how
you'd implement it on a CPU that lacks native support for that
operation. Look at code-generation strategies for compilers on 'baby
micros' and you'll probably start to see why this function will probably
be reentrant on a 'big' CPU but (unless the compiler-writers have been
competent and generous to you) not on some small ones.

I certainly know of (and have debugged customer code, and yelled at
compiler vendors for failing to document things properly!) on C
implementations where that code would break if reentered.

Also, from a determinism point of view, I'd bounce any code with O(N)
runtime that someone tried to put in an ISR. Activate a task to do the
computation and get the hell out of the ISR ASAP!

pete
--
pete@xxxxxxxxxxx "it made about as much sense as a polythene sandwich"
.



Relevant Pages

  • Re: Making a function safe for use in an ISR
    ... int iterative_factorial{ ... function unsafe to use in an ISR? ... Look at code-generation strategies for compilers on 'baby ... immediately take you out of the baby micro area. ...
    (comp.arch.embedded)
  • Re: a little mistake
    ... Also the use of "inline" for that function is redundant. ... compilers will do it anyways and it isn't portable across all compilers ... int main ... Calling function would be part of the users code. ...
    (comp.lang.c)
  • Re: It works without stdio.h !!
    ... most C compilers will assume that a function that has no prototype is an ... extern int, and since you only specify functions that live in the C library, an unresolved ... never declare a variable of type char unless you well and truly believe you need to be ... strcpy, strcat, sprintf etc. with it. ...
    (microsoft.public.vc.mfc)
  • Memory allocation modifications in ibm_newemac driver
    ... The problem is this - when I enable a large MTU and run data through the EMAC and also am reading and writing data to a disk, memory becomes so fragmented that allocating a new SKB fails. ... static int emac_reset ... goto bail; ... if (isr & EMAC4_ISR_TXPE) ...
    (Linux-Kernel)
  • Re: Why ISR contains no arguments and return type is void
    ... If an interrupt can be caused by 3 causes, A, B, or C then the condition ... In addition, when you write an ISR, ... Check Ralph Brown's INT list. ... > in a register before returning from your ISR. ...
    (sci.electronics.design)