I have search a lot and cannot find virtually anything on reentrant functions in the documentation. Eventually I came across this statement.
When local variables are allocated at fixed addresses, the resulting code is not reentrant. Each function
must be called only once. Take special care with interrupt functions: they must not call any function which might
be active at the interrupt time. To be on the safe side, interrupt functions usually use a different set of functions
than non-interrupt functions.
Is this the way all functions work in codewarrior c or is there a way to get codewarrior to support functions which put their local variables on the stack ( MCF51 and codewarrior 10+ but am not sure if this matters ) ??
Some languages have the keyword reentrant for this.
If there isnt another way, is the only way do this to add flag to the code to prevent entry into a function while another part of the code is using the function ? This sounds messy.
'local variables allocated at fixed addresses' refers here to 'static locals', like
void foo(void) {
static int local; /* static local variable, local scope, but is a normal 'static' variable with a fixed memory address */
in above case, the local variable is not on the stack, but it treated like any static variable (static or external).
So what the documentation says is just what is normal in the C language.
And you do not need to use any special keyword/etc: CodeWarrior will allocate 'true' local variables on the stack, as everybody would expect.
I hope this helps.
if i have this function:
void foo(void) {
int localvar;
and i am running it from my main and I call it from an interrupt, the variable localvar gets overwritten. Doesn't the same thing happen if I declare it static ?
IE while I am in the middle of the function and interrupt calls the same function
Thanks ! I must have an interrupt priority problem then. So for clarification, If I have 3 interrupts each calling the same function at once, all of the local ( non static) variables in that function would be separate. IE you would have 3 different copies of the variable and two of those copies would be automatically on the stack at any one point while in all 3. As always, Thank you guys for the help.