public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: pipes: always signal select_sem if any bytes are read or written
Date: Tue, 14 Sep 2021 15:07:34 +0000 (GMT)	[thread overview]
Message-ID: <20210914150734.DB8063857832@sourceware.org> (raw)

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

commit ba4c58299f642b525de1a681dd8ad05b61dc8e49
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Thu Sep 9 11:35:54 2021 +0200

    Cygwin: pipes: always signal select_sem if any bytes are read or written
    
    Fold all code branches potentially having read or written data into
    a single if branch, so signalling select_sem catches all cases.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/fhandler_pipe.cc | 64 +++++++++++++++---------------------------
 1 file changed, 23 insertions(+), 41 deletions(-)

diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc
index 488e14be8..6994a5dce 100644
--- a/winsup/cygwin/fhandler_pipe.cc
+++ b/winsup/cygwin/fhandler_pipe.cc
@@ -303,20 +303,11 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
 	    waitret = WAIT_OBJECT_0;
 
 	  if (waitret == WAIT_CANCELED)
-	    {
-	      status = STATUS_THREAD_CANCELED;
-	      break;
-	    }
+	    status = STATUS_THREAD_CANCELED;
 	  else if (waitret == WAIT_SIGNALED)
-	    {
-	      status = STATUS_THREAD_SIGNALED;
-	      nbytes += io.Information;
-	      if (select_sem && io.Information > 0)
-		ReleaseSemaphore (select_sem,
-				  get_obj_handle_count (select_sem), NULL);
-	      break;
-	    }
-	  status = io.Status;
+	    status = STATUS_THREAD_SIGNALED;
+	  else
+	    status = io.Status;
 	}
       if (isclosed ())  /* A signal handler might have closed the fd. */
 	{
@@ -326,11 +317,17 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
 	    __seterrno ();
 	  nbytes = (size_t) -1;
 	}
-      else if (NT_SUCCESS (status))
+      else if (NT_SUCCESS (status)
+	       || status == STATUS_BUFFER_OVERFLOW
+	       || status == STATUS_THREAD_CANCELED
+	       || status == STATUS_THREAD_SIGNALED)
 	{
 	  nbytes_now = io.Information;
 	  ptr = ((char *) ptr) + nbytes_now;
 	  nbytes += nbytes_now;
+	  if (select_sem && nbytes_now > 0)
+	    ReleaseSemaphore (select_sem,
+			      get_obj_handle_count (select_sem), NULL);
 	}
       else
 	{
@@ -341,13 +338,6 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
 	    case STATUS_PIPE_BROKEN:
 	      /* This is really EOF.  */
 	      break;
-	    case STATUS_MORE_ENTRIES:
-	    case STATUS_BUFFER_OVERFLOW:
-	      /* `io.Information' is supposedly valid.  */
-	      nbytes_now = io.Information;
-	      ptr = ((char *) ptr) + nbytes_now;
-	      nbytes += nbytes_now;
-	      break;
 	    case STATUS_PIPE_LISTENING:
 	    case STATUS_PIPE_EMPTY:
 	      if (nbytes != 0)
@@ -366,10 +356,8 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
 	    }
 	}
 
-      if (nbytes_now == 0)
+      if (nbytes_now == 0 || status == STATUS_BUFFER_OVERFLOW)
 	break;
-      else if (select_sem)
-	ReleaseSemaphore (select_sem, get_obj_handle_count (select_sem), NULL);
     }
   ReleaseMutex (read_mtx);
   if (evt)
@@ -467,20 +455,11 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len)
 	    waitret = WAIT_OBJECT_0;
 
 	  if (waitret == WAIT_CANCELED)
-	    {
-	      status = STATUS_THREAD_CANCELED;
-	      break;
-	    }
+	    status = STATUS_THREAD_CANCELED;
 	  else if (waitret == WAIT_SIGNALED)
-	    {
-	      status = STATUS_THREAD_SIGNALED;
-	      nbytes += io.Information;
-	      if (select_sem && io.Information > 0)
-		ReleaseSemaphore (select_sem,
-				  get_obj_handle_count (select_sem), NULL);
-	      break;
-	    }
-	  status = io.Status;
+	    status = STATUS_THREAD_SIGNALED;
+	  else
+	    status = io.Status;
 	}
       if (isclosed ())  /* A signal handler might have closed the fd. */
 	{
@@ -489,13 +468,18 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len)
 	  else
 	    __seterrno ();
 	}
-      else if (NT_SUCCESS (status))
+      else if (NT_SUCCESS (status)
+	       || status == STATUS_THREAD_CANCELED
+	       || status == STATUS_THREAD_SIGNALED)
 	{
 	  nbytes_now = io.Information;
 	  ptr = ((char *) ptr) + nbytes_now;
 	  nbytes += nbytes_now;
+	  if (select_sem && nbytes_now > 0)
+	    ReleaseSemaphore (select_sem,
+			      get_obj_handle_count (select_sem), NULL);
 	  /* 0 bytes returned?  EAGAIN.  See above. */
-	  if (nbytes == 0)
+	  if (NT_SUCCESS (status) && nbytes == 0)
 	    set_errno (EAGAIN);
 	}
       else if (STATUS_PIPE_IS_CLOSED (status))
@@ -508,8 +492,6 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len)
 
       if (nbytes_now == 0)
 	break;
-      else if (select_sem)
-	ReleaseSemaphore (select_sem, get_obj_handle_count (select_sem), NULL);
     }
   if (evt)
     CloseHandle (evt);


                 reply	other threads:[~2021-09-14 15:07 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210914150734.DB8063857832@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).