4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-02-01 03:50:28 +08:00

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>
This commit is contained in:
Corinna Vinschen 2020-03-17 17:24:56 +01:00 committed by Ken Brown
parent 93c653165b
commit 84eae151cb

View File

@ -1440,23 +1440,23 @@ static int
peek_serial (select_record *s, bool) peek_serial (select_record *s, bool)
{ {
COMSTAT st; COMSTAT st;
DWORD event, io_err;
fhandler_serial *fh = (fhandler_serial *) s->fh; 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; return s->read_ready = true;
select_printf ("fh->overlapped_armed %d", fh->overlapped_armed); select_printf ("fh->overlapped_armed %d", fh->overlapped_armed);
HANDLE h; HANDLE h;
set_handle_or_return_if_not_open (h, s); 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"); select_printf ("already ready");
ready = 1; return true;
goto out;
} }
/* This is apparently necessary for the com0com driver. /* This is apparently necessary for the com0com driver.
@ -1471,59 +1471,54 @@ peek_serial (select_record *s, bool)
ResetEvent (fh->io_status.hEvent); 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; goto err;
} }
else if (st.cbInQue) if (st.cbInQue)
return s->read_ready = true; 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; return s->read_ready = true;
else if (GetLastError () == ERROR_IO_PENDING) if (GetLastError () != ERROR_IO_PENDING)
{
debug_printf ("WaitCommEvent %E");
goto err;
}
fh->overlapped_armed = 1; fh->overlapped_armed = 1;
else
{
debug_printf ("WaitCommEvent");
goto err;
}
} }
switch (WaitForSingleObject (fh->io_status.hEvent, 10L)) switch (WaitForSingleObject (fh->io_status.hEvent, 10L))
{ {
case WAIT_OBJECT_0: 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; goto err;
} }
else if (!st.cbInQue) if (st.cbInQue)
Sleep (10L);
else
{ {
return s->read_ready = true;
select_printf ("got something"); select_printf ("got something");
return s->read_ready = true;
} }
break; break;
case WAIT_TIMEOUT: case WAIT_TIMEOUT:
break; break;
default: default:
debug_printf ("WaitForMultipleObjects"); debug_printf ("WaitForMultipleObjects %E");
goto err; goto err;
} }
out: return 0;
return ready;
err: err:
if (GetLastError () == ERROR_OPERATION_ABORTED) if (GetLastError () == ERROR_OPERATION_ABORTED)
{ {
select_printf ("operation aborted"); select_printf ("operation aborted");
return ready; return 0;
} }
s->set_select_errno (); s->set_select_errno ();
select_printf ("error %E");
return -1; return -1;
} }