[newlib-cygwin] Cygwin: FIFO: improve EOF detection

Corinna Vinschen corinna@sourceware.org
Wed Mar 27 13:02:00 GMT 2019


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c6e221c03668909f6c3fb399d8888629353b0191

commit c6e221c03668909f6c3fb399d8888629353b0191
Author: Ken Brown <kbrown@cornell.edu>
Date:   Fri Mar 22 19:30:38 2019 +0000

    Cygwin: FIFO: improve EOF detection
    
    Add a hit_eof method that tries to detect whether any clients are
    connected.  Before concluding that there are none, it gives the
    listen_client thread time to update the client data.

Diff:
---
 winsup/cygwin/fhandler.h       |  1 +
 winsup/cygwin/fhandler_fifo.cc | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 997dc0b..af5f500 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1277,6 +1277,7 @@ class fhandler_fifo: public fhandler_base
   bool listen_client ();
 public:
   fhandler_fifo ();
+  bool hit_eof ();
   PUNICODE_STRING get_pipe_name ();
   DWORD listen_client_thread ();
   void fifo_client_lock () { _fifo_client_lock.lock (); }
diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc
index b0016ee..1dcb3b3 100644
--- a/winsup/cygwin/fhandler_fifo.cc
+++ b/winsup/cygwin/fhandler_fifo.cc
@@ -654,6 +654,25 @@ fhandler_fifo::raw_write (const void *ptr, size_t len)
   return ret;
 }
 
+/* A FIFO open for reading is at EOF if no process has it open for
+   writing.  We test this by checking nconnected.  But we must take
+   account of the possible delay from the time of connection to the
+   time the connection is recorded by the listen_client thread. */
+bool
+fhandler_fifo::hit_eof ()
+{
+  fifo_client_lock ();
+  bool eof = (nconnected == 0);
+  fifo_client_unlock ();
+  if (eof)
+    {
+      /* Give the listen_client thread time to catch up, then recheck. */
+      Sleep (1);
+      eof = (nconnected == 0);
+    }
+  return eof;
+}
+
 void __reg3
 fhandler_fifo::raw_read (void *in_ptr, size_t& len)
 {
@@ -665,7 +684,7 @@ fhandler_fifo::raw_read (void *in_ptr, size_t& len)
 
   while (1)
     {
-      if (nconnected == 0)	/* EOF */
+      if (hit_eof ())
 	{
 	  len = 0;
 	  return;



More information about the Cygwin-cvs mailing list