views:

285

answers:

5

Does GCC generate reentrant code for all scenarios ?

+4  A: 

no, you must write reentrant code.

Gary
But, is there any variant of gcc that generate re-entrant code ?
S.Man
A: 

No, GCC cannot possibly guarantee re-entrant code that you write.

However, on the major platforms, the compiler produced or included code, such as math intrinsics or function calls, are re-entrant. As GCC doesn't support platforms where non-reentrant function calls are common, such as the 8051, there is little risk in having a compiler issue with reentrancy.

There are GCC ports which have bugs and issues, such as the MSP430 version.

Yann Ramin
Do you mean to convey that there are GCC toolchains with support for re-entrant code various platforms but with some issues ?
S.Man
+1  A: 

Re-entrancy is not something that the compiler has any control over - it's up to the programmer to write re-entrant code. To do this you need to avoid all the obvious pitfalls, e.g. globals (including local static variables), shared resources, threads, calls to other non-reentrant functions, etc.

Having said that, some cross-compilers for small embedded systems, e.g. 8051, may not generate reentrant code by default, and you may have to request reentrant code for specific functions via e.g. a #pragma.

Paul R
THis query was due to an info that I came across that conveyed for developing of applications related with uC/OS-II RTOS, it is required to have a C compiler that generates reentrant code.
S.Man
+2  A: 

Reentrancy is something that ISO C and C++ are capable of by design, so that includes GCC. It is still your responsibility to code the function for reentrancy.

A C compiler that does not generate reentrant code even when a function is coded correctly for reentrancy would be the exception rather than the rule, and would be for reasons of architectural constraint (such as having insufficient resources to support stack, so generating static frames). In these situations the compiler documentation should make this clear.

Some articles you might read:

Clifford
+1  A: 

GCC generates reentrant code on at least the majority of platforms it compiles for (especially if you avoid passing or returning structures by value) but it is possible that a particular language or platform ABI might dictate otherwise. You'll need to be much more specific for any more conclusive statement to be made; I know it's certainly basically reentrant on desktop processors if the code being compiled is itself basically reentrant (weird global state tricks can get you into trouble on any platform, of course).

Donal Fellows
To be exact, I've no problems at all using gcc to compile code that is reentrant on x86, arm and sparc.
Donal Fellows