What does gcc -D_REENTRANT really do?

I write Java bindings for the C library and therefore work with JNI. Oracle indicates that it is reasonable that native libraries for compilation with Java should be compiled using multi-threaded compilers.

The JNI docs provide a concrete example that for gcc this multithreading requirement must be met by defining one of the _REENTRANT or _POSIX_C_SOURCE . It seems strange to me. _REENTRANT and _POSIX_C_SOURCE are macros for testing functions. The GCC and POSIX documentation describes their effects in terms of defining characters and creating visible declarations, just as I would expect for any function test macro.

If I don't need extra characters or functions, then do these macros really do anything useful to me? Can one or both of them invoke gcc to generate different code than otherwise? Maybe they can cause my code calls to standard library functions to be associated with various implementations? Or is Oracle just talking from its lower regions?

Edit: Also, it occurs to me that re-hosting is a separate consideration from stream processing. Non-reentrancy can be a problem even for single-threaded programs, so Oracleโ€™s suggestion that the _REENTRANT definition makes gcc multithreaded now seems even more dubious.

+5
source share
1 answer

The Oracle recommendation was written for Solaris, not Linux.

On Solaris, if you compiled .so without _REENTRANT and ended up loaded with a multi-threaded application, then very bad things can happen (for example, corrupted random data from internal libc components). This happened because, without a definition, by default you got unlocked versions of some routines.

This was the case when I first read this documentation, which was perhaps 15 years ago, the mention of the -mt flag for the sun studio compiler was added after the last time I read this document.

This is no longer the case. You always get the same routine, regardless of whether you compile the _REENTRANT flag; now it is just a macro of functions, not a macro of behavior.

+2
source

All Articles