What do you exactly mean by Reentrant function?

I guess this should be right place to ask this question.

What I know is at the higher level where the variables in a function should not be shared so that they dont get corrupted when the function is interrupted. I wanted a much more clear and basic view and a much more detiled explanation of the reentrancy concept.

Also I've heard people use reentrant function and reentrant code differently. Is there any difference between them? I believe they should be same.

Also can somebody throw light on the DO's and DON'Ts for embedded system programming in C. Well I understand that the list might be long and not necessarily exhaustive, but anything as long as it makes sense :) would be appreciated.

Thanx in advance.

--Taran

Reply to
Taran
Loading thread data ...

As one coming from the Forth stables I consider that the terms "re-entrant code" and "re-entrant function" are synonymous. As for the variables, if you consider that they need to operate just like a stack then you should get an idea of how such functions can be made efficient. Of course, it is something Forthists hardly think about as Forth is built around virtual or real stack machines.

Start with a copy of the MISRA C Guidelines and tune your Lint-alike tool to enforce as much of that as possible. Then it is mostly review and test frequently and thoroughly and keep your configuration management tight and well-audited.

Of course, you could always consider coming over to Forth ;>

--
********************************************************************
Paul E. Bennett ....................
Forth based HIDECS Consultancy .....
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-811095
Going Forth Safely ....EBA. http://www.electric-boat-association.org.uk/********************************************************************
Reply to
Paul E. Bennett

In article , Taran writes

Re-entrant functions are those that can be called by themselves or may be called by a function they call. thus there is more than one instance of the function running at any one time.

What is the processor? On many 8 bit systems the compilers do not do re- entrancy unless you explicitly tell them to. This is to save space. Both in the stack and other memory. This is because you need a duplicate set of data for themas well as stack space. In large systems it does not matter.

Probably they are the same... I would have thought so but.. never say die. There is always some one turning up some highly implausible code that no one in their right mind would have in a real system.

Have a look at

formatting link
(or
formatting link
I am inthe middle of re-siting my web site) You need the QuEST series of papers. A bit long in the tooth and under a re-write but basically they are OK.

Regards chris

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/\ /\/\/ snipped-for-privacy@phaedsys.org

formatting link
\/\/ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Reply to
Chris Hills

There are two things: reenterable code and reentrant code.

A reentrant function is one that can be called from multiple threads simultaneously. Specifically, the execution of the function can be interrupted, and it can be called again (e.g.) from another thread, without affecting the operation of the function for either thread. Reentrant functions are also reenterable.

A reenterable function is one which can be called more than one time and produce the same results from the same data. This definition has to be bent for those functions which are *supposed* to return a result based on history (such as returning the number of times it has been called).

A reenterable function that is not reentrant is one that requires initialization of static or global data.

A non-reenterable function is one that, for example, doesn't (properly) initialize itself (relying on static start-up values), or that destroys its initial data.

--
#include 
 _
Kevin D Quitt  USA 91387-4454         96.37% of all statistics are made up
  Per the FCA, this address may not be added to any commercial mail list
Reply to
Kevin D. Quitt

They are the same. A re-entrant function is simply one that can be called more than once at a time and still work properly. The classic example of this is a recursive function which calls itself. The other common example is one a multi-tasking system where it can be called by more than one task at the same time.

Do take time to thoroughly understand the underlying hardware and microprocessor/controller

Do learn the assembly language of the micro

Do think long and hard about your algorithms before you code them in C because you do not have unlimited resources (either of memory or execution time).

Don't use floating point unless it is absolutely necessary.

Do make sure you have a proper requirements specification and if anything in it is not clear ASK.

Do document the design as it proceeds. Don't leave it till the end.

Do comment you code fully and sensibly.

That'll do for starters.

Ian

--
Ian Bell
Reply to
Ian Bell

simultaneously. Specifically,

called again (e.g.) from another

thread. Reentrant functions are

and produce the same results

functions which are *supposed* to

times it has been called).

initialization of static or

Till now I assumed that reentrant and reentrable functions are same.

This is confusing. Based on your definition of re-entrable function:

-- All functions which use non static local variables and with arguments passed by value would produce the same result.

-- All functions which use static variables, which not constitutes history, are reentrable.

-- If they use pointers, then they also become part of history and are again reentrable.

I can't figure out of a non-reentrable function.

(properly) initialize itself (relying

Can you please give an example.

[snip]

Thanks in advance

-- Taran

Reply to
Taran

simultaneously. Specifically,

called again (e.g.) from another

thread. Reentrant functions are

and produce the same results

functions which are *supposed* to

times it has been called).

initialization of static or

Till now I assumed that reentrant and reentrable functions are same.

This is confusing. Based on your definition of re-entrable function:

-- All functions which use non static local variables and with arguments passed by value would produce the same result.

-- All functions which use static variables, which not constitutes history, are reentrable.

-- If they use pointers, then they also become part of history and are again reentrable.

I can't figure out of a non-reentrable function

(properly) initialize itself (relying

Can you please give an example.

[snip]

Thanks in advance

-- Taran

Reply to
Taran

Well, in C, you have to go out of your way to generate a non-reenterable function. In assembly, it's a lot easier: self-modifying code that is designed to work once, and that modifies an instruction by incrementing it. A disk-use analyzer that only reads the usage bitmap in from the disk if its internal buffer is all zeroes.

Good programmers have a very hard time writing non-reenterable code. Only 'clever' programmers have an easy time of it.

--
#include 
 _
Kevin D Quitt  USA 91387-4454         96.37% of all statistics are made up
  Per the FCA, this address may not be added to any commercial mail list
Reply to
Kevin D. Quitt

non-reenterable function.

By your definition, a function implementing a state diagram would be non reenterable, is that right ?

Reply to
Lanarcam

function.

You mean, like using a standard library function?

Best regards, Spehro Pefhany

--
"it's the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

Clearly, my definition is suspect. How about:

A function that operates correctly when asynchronously reentered is 'reentrant'.

A function that operates correctly when serially reentered is 'reenterable'.

--
#include 
 _
Kevin D Quitt  USA 91387-4454         96.37% of all statistics are made up
  Per the FCA, this address may not be added to any commercial mail list
Reply to
Kevin D. Quitt

function.

The standard C library functions are re-enterable.

If a thread shares all the same environment as its part, and a task has its own environment, then the for tasks, the C library is re-entrant for tasks, but not for threads. There only needs to be one copy of the C library in memory to accommodate all tasks.

--
#include 
 _
Kevin D Quitt  USA 91387-4454         96.37% of all statistics are made up
  Per the FCA, this address may not be added to any commercial mail list
Reply to
Kevin D. Quitt

function.

If you use the right version of the library. Consider e.g. errno - when errno is stored as static variable (and not local for a thread or a task) just doing a simple operation and checking errno can result in a mess.

Andreas

--
It's not the things you don't know what gets you into trouble. It's
the things you do know that just ain't so.
- Will Rogers
Reply to
Andreas Hadler

rand(), strtok(), tmpnam(), ctime(), localtime() ?

Reply to
Vadim Borshchev

Here's an example of a function that is not reentrant:

// A function to decode a structure (for output), and return // a pointer for use in printf.

struct foo { int a; int b; };

// used, e.g.: // struct foo *p; // ... // printf("foo=[%s]\n", decode_foo(p));

char * decode_foo(const struct foo *p) { static char buf[40];

sprintf(buf, "a=%d, b=%d", p->a, p->b); return buf; }

The function is not reentrant as it uses a static buffer. If the thread of execution changes and a new thread itself calls decode_foo, then the buffer will be overwritten with the new contents. On return to the original thread, the contents will be incorrect.

Here's the reentrant version:

// used, e.g.: // struct foo *p; // char buf[80]; // ... // printf("foo=[%s]\n", decode_foo(buf, p));

char * decode_foo(char *buf, const struct foo *p) { sprintf(buf, "a=%d, b=%d", p->a, p->b); return buf; }

Here, the caller is providing the buffer. This function, using no static storage, can be called simultaneously from multiple threads and generate the correct result (assuming sprintf is reentrant).

Regards, Paul.

Reply to
Paul Marciano

'reentrant'.

'reenterable'.

When you say serially, I suppose you mean that a call occurs only when all others calls have returned.

Consider a function that is called whenever an event occurs, serially and that implements a state diagram. Presumably, the state will change at each function call, and a successive call with the same arguments will not yield the same result. Well, the function still operates correctly, but I don't see your point.

Reply to
Lanarcam

function.

The C99 standard specifically states that they are not guaranteed to be reentrant.

Best regards, Spehro Pefhany

--
"it's the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Reply to
Spehro Pefhany

In oder to save its state between calls this function must use static or global variables to save its state. It cannot therefore be re-entrant.

Ian

Reply to
Ian Bell

This does not contradict what I wrote. C is designed against an abstract machine, and has no concept of threads or interrupts; therefore it cannot and does not address those issues.

--
#include 
 _
Kevin D Quitt  USA 91387-4454         96.37% of all statistics are made up
  Per the FCA, this address may not be added to any commercial mail list
Reply to
Kevin D. Quitt

serially

or

re-entrant.

The point was not about re-entrant functions but about re-enterable functions according to the terminology introduced here:

What does mean data here?

Presumably not history

The clarification,

is still not clear for me. Clearly the points 1 and 3 are contradictory. *Correctly* would require a more precise definition.

Reply to
Lanarcam

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.