Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to make reentrant functions?

Status
Not open for further replies.

sacrpio

Member level 3
Joined
May 24, 2004
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
500
How We can make our function reentrant. If I have a static variable in my function & I want to make this function reentrant. What I have to do?
 

keil recursive reentrant

I would try to avoid reentrant functions if you are using a small microcontroller like 8051 or PIC.

Most of the time you can recode a function to not use reentrancy ...

This is from Keil C51 manual:
3
Reentrant Functions
A reentrant function can be shared by several processes at the same time. When
a reentrant function is executing, another process can interrupt the execution and
then begin to execute that same reentrant function. Normally, functions in Cx51
cannot be called recursively or in a fashion which causes reentrancy. The reason
for this limitation is that function arguments and local variables are stored in
fixed memory locations. The reentrant function attribute allows you to declare
functions that may be reentrant and, therefore, may be called recursively. For
example:
int calc (char i, int b) reentrant {
int x;
x = table ;
return (x * b);
}
Reentrant functions can be called recursively and can be called simultaneously
by two or more processes. Reentrant functions are often required in real-time
applications or in situations where interrupt code and non-interrupt code must
share a function.
As in the above example, you may selectively define (using the reentrant
attribute) functions as being reentrant. For each reentrant function, a reentrant
stack area is simulated in internal or external memory depending upon the
memory model used, as follows:
Small model reentrant functions simulate the reentrant stack in idata
memory.
Compact model reentrant functions simulate the reentrant stack in pdata
memory.
Large model reentrant functions simulate the reentrant stack in xdata
memory.

best regards
 

static variable reentrant

sacrpio said:
How We can make our function reentrant. If I have a static variable in my function & I want to make this function reentrant. What I have to do?

The hard answer is to somehow create a separate static variable for each task.
The easy answer is to get rid of the static variable.

Perhaps tell us why your function needs a static variable, and someone here may have more specific suggestions.
 

reentrant functions for embedded systems

If you use - rtos mutex can help to get variable access. Insert wait on mutex in your reentrant function before accessing variable .

For nonrtos based program - just disable all interrupts before accessing static variable and enable interrupts when you are no longer interrested in that variable .
All this has to be done within your reentrant
function . But you need this only if your fucntion can be accessed from main loop and from ISR at the same time .
 

can a function be recursive reentrant

artem said:
If you use - rtos mutex can help to get variable access. Insert wait on mutex in your reentrant function before accessing variable .

How about recursive functions? In this case mutex is a direct way to get dead-lock.
Stack variables is the only way to get real reentrant function.
 

mutex reentrant

Avoid writing recursive function - it looks pretty at first look , but end ups with satck overflow if system is not properly dimensioned .
 

Re: Reentrant Functions

artem said:
Avoid writing recursive function - it looks pretty at first look , but end ups with satck overflow if system is not properly dimensioned .

I fully agree, but sometime recursive function is the most elegant way to implement some feature. For example, LCD menu engine.
 

Re: Reentrant Functions

an introduction to reentrancy from embedded.ocm **broken link removed**
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top