How to use __DYNAMIC_REENT__ correctly in multithreaded applications ?
Thomas Pfaff
tpfaff@gmx.net
Mon Aug 18 18:42:00 GMT 2003
Hello,
some time ago i have added a __DYNAMIC_REENT__ to the cygwin part of
config.h. The main purpose was to get thread safe errnos (without this
define errno is stored process global in _impure_ptr).
Now the reent structure for every thread is thread local and will be
destroyed when the thread has terminated. A __getreent function was
implemented to get the thread local reent.
This leads to following problem:
Each FILE structure contains a pointer to the reent structure of the
thread that initially opened the file. If the thread terminates than the
reent pointer will point to an invalid address.
Here is a simple testcase to illustrate this probleme:
#include <stdio.h>
#include <pthread.h>
static FILE *fp;
static void *threadfunc (void *parm)
{
fp = fopen ("/tmp/testreent", "w");
return NULL;
}
int main(void)
{
pthread_t thread;
pthread_create (&thread, NULL, threadfunc, NULL);
pthread_join (thread, NULL);
fprintf (fp, "test");
fclose (fp);
return 0;
}
A thread opens a file and terminates immediately. The mainthread will
try to write to the opened file and segfaults in the CHECK_INIT macro in
fwrite since the data part of the FILE pointer is no longer valid.
I am a bit clueless now:
How was DYNAMIC_REENT stuff meant to work ?
IMHO the only way to get this working is to remove the reent part in the
FILE structure, but there might be a better solution.
TIA,
Thomas
More information about the Newlib
mailing list