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: serial: select: simplify peek_serial
Date: Thu, 26 Mar 2020 11:26:11 +0000 (GMT)	[thread overview]
Message-ID: <20200326112611.8923C385E01A@sourceware.org> (raw)

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

commit 9e106db0adb74cda86e69a3405f20955e6fc5602
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Tue Mar 17 17:24:56 2020 +0100

    Cygwin: serial: select: simplify peek_serial
    
    - Don't use ev member for ClearCommError and WaitCommEvent.
      Both returned values are different (error value vs. event
      code).  The values are not used elsewhere so it doesn't make
      sense to store them in the object.
    
    - Drop local variable ready which is used inconsequentially.
    
    - Since WFSO already waits 10 ms, don't wait again if no char
      is in the inbound queue.
    
    - Avoid else if chains.
    
    - Only print one line of debug output on error.
    
    - Drop overlapped_armed < 0 check.  This value is only set in
      fhandler_serial::raw_read if VTIME > 0, and even then it's only
      set to be immediately reset to 0 before calling ReadFile.  So
      overlapped_armed is never actually < 0 when calling select.
    
    - Fix a screwed up statement order.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/select.cc | 43 +++++++++++++++++++------------------------
 1 file changed, 19 insertions(+), 24 deletions(-)

diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index 3965cfbb4..6ffc97b37 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -1440,23 +1440,23 @@ static int
 peek_serial (select_record *s, bool)
 {
   COMSTAT st;
+  DWORD event, io_err;
 
   fhandler_serial *fh = (fhandler_serial *) s->fh;
 
-  if (fh->get_readahead_valid () || fh->overlapped_armed < 0)
+  if (fh->get_readahead_valid ())
     return s->read_ready = true;
 
   select_printf ("fh->overlapped_armed %d", fh->overlapped_armed);
 
   HANDLE h;
   set_handle_or_return_if_not_open (h, s);
-  int ready = 0;
 
-  if ((s->read_selected && s->read_ready) || (s->write_selected && s->write_ready))
+  if ((s->read_selected && s->read_ready)
+      || (s->write_selected && s->write_ready))
     {
       select_printf ("already ready");
-      ready = 1;
-      goto out;
+      return true;
     }
 
   /* This is apparently necessary for the com0com driver.
@@ -1471,59 +1471,54 @@ peek_serial (select_record *s, bool)
 
       ResetEvent (fh->io_status.hEvent);
 
-      if (!ClearCommError (h, &fh->ev, &st))
+      if (!ClearCommError (h, &io_err, &st))
 	{
-	  debug_printf ("ClearCommError");
+	  debug_printf ("ClearCommError %E");
 	  goto err;
 	}
-      else if (st.cbInQue)
+      if (st.cbInQue)
 	return s->read_ready = true;
-      else if (WaitCommEvent (h, &fh->ev, &fh->io_status))
+      if (WaitCommEvent (h, &event, &fh->io_status))
 	return s->read_ready = true;
-      else if (GetLastError () == ERROR_IO_PENDING)
-	fh->overlapped_armed = 1;
-      else
+      if (GetLastError () != ERROR_IO_PENDING)
 	{
-	  debug_printf ("WaitCommEvent");
+	  debug_printf ("WaitCommEvent %E");
 	  goto err;
 	}
+      fh->overlapped_armed = 1;
     }
 
   switch (WaitForSingleObject (fh->io_status.hEvent, 10L))
     {
     case WAIT_OBJECT_0:
-      if (!ClearCommError (h, &fh->ev, &st))
+      if (!ClearCommError (h, &io_err, &st))
 	{
-	  debug_printf ("ClearCommError");
+	  debug_printf ("ClearCommError %E");
 	  goto err;
 	}
-      else if (!st.cbInQue)
-	Sleep (10L);
-      else
+      if (st.cbInQue)
 	{
-	  return s->read_ready = true;
 	  select_printf ("got something");
+	  return s->read_ready = true;
 	}
       break;
     case WAIT_TIMEOUT:
       break;
     default:
-      debug_printf ("WaitForMultipleObjects");
+      debug_printf ("WaitForMultipleObjects %E");
       goto err;
     }
 
-out:
-  return ready;
+  return 0;
 
 err:
   if (GetLastError () == ERROR_OPERATION_ABORTED)
     {
       select_printf ("operation aborted");
-      return ready;
+      return 0;
     }
 
   s->set_select_errno ();
-  select_printf ("error %E");
   return -1;
 }


                 reply	other threads:[~2020-03-26 11:26 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=20200326112611.8923C385E01A@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).