unbuffered fread() deadlock

Andre Heider a.heider@gmx.de
Sun Jan 11 11:05:00 GMT 2009


Hi list,

i have the following situation:

- 2 threads
- each thread has its own, opened FILE*
- buffering has been disabled for both via setvbuf

Two concurrent fread() calls can now result in a deadlock:

thread 1:
#5  0x80014bc4 in __sfp_lock_acquire ()
#6  0x80016a40 in _fwalk (ptr=<value optimized out>, function=<value 
optimized out>)
#7  0x8001b9ac in __srefill_r (ptr=0x80c5ade8, fp=0x80bec394)
#8  0x80015460 in _fread_r (ptr=0x80c5ade8, buf=<value optimized out>,
     size=1, count=1024, fp=0x80bec394)

thread 2:
#5  0x80029a94 in __flockfile (fp=<value optimized out>)
#6  0x80016ac0 in _fwalk (ptr=<value optimized out>, function=0x8001ba20 
<lflush>)
#7  0x8001b9ac in __srefill_r (ptr=0x807d2520, fp=0x80900e58)
#8  0x80015460 in _fread_r (ptr=0x807d2520, buf=<value optimized out>, 
size=1, count=1, fp=0x80900e58)


refill.c contains the following comment just above the _fwalk call:

   /*
    * Before reading from a line buffered or unbuffered file,
    * flush all line buffered output files, per the ANSI C
    * standard.
    */

But it doesn't do that, it flushes every file, no matter what fp->_flags 
is set to (well, except zero). Attached you'll find a patch which adds a 
new argument to the fwalk calls (a simple bitmask which is compared to 
fp->_flags). __srefill_r() now simply passes __SLBF there. I'm not sure 
if this is the best way to resolve this (i'm new to newlib), i just 
synced the code with the comment ;)

This patch is against 1.16.0, but it seems CVS has the same issue.

Regards,
Andre
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: newlib-fwalk.diff
URL: <http://sourceware.org/pipermail/newlib/attachments/20090111/370054c9/attachment.ksh>


More information about the Newlib mailing list