This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: sscanf() vs. fgetc()


Robin Farine wrote:
> 
> The problem comes from the conjunction of Cyg_StdioStream::refill_read_buffer()
> and cyg_libc_stdio_flush_all_but(). The former routine locks '*this' and calls
> cyg_stdio_read() which finally blocks on a condition variable, waiting for
> characters coming from the underlying device. Then, another thread with a higher
> priority (lower value) calls sscanf() => cyg_libc_stdio_flush_all_but() which
> spins trying to flush the other (locked) stream and thus does not give the CPU
> back to the lower priority thread in order to unlock the stream (provided the
> stream has some characters to consume).

Hah. I'd just debugged this myself this far. You are entirely correct. It
shouldn't wait at all for a device that's currently reading - nothing needs
to be flushed. So.....

Index: include/stream.hxx
===================================================================
RCS file:
/home/cvs/ecc/ecc/language/c/libc/stdio/current/include/stream.hxx,v
retrieving revision 1.4
diff -u -5 -p -r1.4 stream.hxx
--- include/stream.hxx	2001/03/16 09:04:43	1.4
+++ include/stream.hxx	2001/07/12 18:54:31
@@ -62,13 +62,19 @@
 #include <cyg/kernel/mutex.hxx>    // Cyg_Mutex
 #endif
 
 // TYPE DEFINITIONS
 
+class Cyg_StdioStream;
+__externC Cyg_ErrNo
+cyg_libc_stdio_flush_all_but( Cyg_StdioStream * );
+
 class Cyg_StdioStream
 {
     friend int setvbuf( FILE *, char *, int, size_t );
+    friend Cyg_ErrNo
+    cyg_libc_stdio_flush_all_but( Cyg_StdioStream * );
 
 private:
 
     // error status for this file
     Cyg_ErrNo error;
Index: src/common/fflush.cxx
===================================================================
RCS file:
/home/cvs/ecc/ecc/language/c/libc/stdio/current/src/common/fflush.cxx,v
retrieving revision 1.2
diff -u -5 -p -r1.2 fflush.cxx
--- src/common/fflush.cxx	2000/07/25 14:54:38	1.2
+++ src/common/fflush.cxx	2001/07/12 18:54:31
@@ -84,18 +84,20 @@ cyg_libc_stdio_flush_all_but( Cyg_StdioS
                     
                     files_flushed[i] = true;
                 } // if
                 else {
                     // valid stream
-                    
-                    if ( stream->trylock_me() ) {
-                        err = stream->flush_output_unlocked();
-                        stream->unlock_me();
-                        files_flushed[i] = true;
-                    } // if
-                    else
-                        loop_again = true;
+                    // only buffers which we've written to need flushing
+                    if ( !stream->flags.last_buffer_op_was_read) {
+                        if ( stream->trylock_me() ) {
+                            err = stream->flush_output_unlocked();
+                            stream->unlock_me();
+                            files_flushed[i] = true;
+                        } // if
+                        else
+                            loop_again = true;
+                    }
                 } // else
             } // if
         } // for
     } // do
     while(loop_again && !err);


Jifl
-- 
Red Hat, Rustat House, Clifton Road, Cambridge, UK. Tel: +44 (1223) 271062
Maybe this world is another planet's Hell -Aldous Huxley || Opinions==mine
Come to the Red Hat TechWorld open source conference in Brussels!
Keynotes, techie talks and exhibitions    http://www.redhat-techworld.com/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]