Reentrant Functions

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Reentrant Functions

2,265 次查看
SecondTechCo
Contributor IV

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.

标签 (1)
标记 (2)
6 回复数

1,833 次查看
BlackNight
NXP Employee
NXP Employee

'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.

0 项奖励
回复

1,833 次查看
SecondTechCo
Contributor IV

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 ?

0 项奖励
回复

1,833 次查看
SecondTechCo
Contributor IV

IE while I am in the middle of the function and interrupt calls the same function

0 项奖励
回复

1,833 次查看
JimDon
Senior Contributor III

If you declare it static, then it will basically a global variable, there will be only one copy of it and the function will NOT be re-entrant. The way you it show it now is correct.

1,833 次查看
SecondTechCo
Contributor IV

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.

0 项奖励
回复

1,833 次查看
JimDon
Senior Contributor III

Yes. Well, all 3 would be on the stack, but at different locations on the stack.