Re: Making a function safe for use in an ISR
- From: Pete Fenelon <pete@xxxxxxxxxxx>
- Date: Thu, 25 Jan 2007 04:46:35 +0000
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"
.
- Follow-Ups:
- Re: Making a function safe for use in an ISR
- From: Mark Borgerson
- Re: Making a function safe for use in an ISR
- References:
- Making a function safe for use in an ISR
- From: joshc
- Making a function safe for use in an ISR
- Prev by Date: Re: Making a function safe for use in an ISR
- Next by Date: Re: OT? Subversion anyone?
- Previous by thread: Re: Making a function safe for use in an ISR
- Next by thread: Re: Making a function safe for use in an ISR
- Index(es):
Relevant Pages
|