From c81ceaefec39454f5f76f91be2885d577a668527 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Sun, 28 Jun 2009 19:23:13 +0000 Subject: [PATCH] * fhandler.h (fhandler_base::has_ongoing_io): Declare new function. * fhandler.cc (fhandler_base::has_ongoing_io): Define new function. (fhandler_base::read_overlapped): Use has_ongoing_io to avoid writing when handle has not completed last I/O. (fhandler_base::write_overlapped): Ditto. * select.cc (peek_pipe): Be more careful about accessing hEvent field from get_overlapped(). --- winsup/cygwin/ChangeLog | 18 ++++++++++++ winsup/cygwin/fhandler.cc | 58 +++++++++++++++++++++++++++------------ winsup/cygwin/fhandler.h | 1 + winsup/cygwin/select.cc | 2 +- 4 files changed, 60 insertions(+), 19 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index b5197b6de..24a66bef0 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,13 @@ +2009-06-28 Christopher Faylor + + * fhandler.h (fhandler_base::has_ongoing_io): Declare new function. + * fhandler.cc (fhandler_base::has_ongoing_io): Define new function. + (fhandler_base::read_overlapped): Use has_ongoing_io to avoid writing + when handle has not completed last I/O. + (fhandler_base::write_overlapped): Ditto. + * select.cc (peek_pipe): Be more careful about accessing hEvent field + from get_overlapped(). + 2009-06-28 Christopher Faylor * gendef (cleanup): Rename from 'nocr'. Remove comments and trailing @@ -5,6 +15,14 @@ * cygwin.din: Add long-needed comment describing what dll_crt0__FP11per_process demangles to. + * select.cc (peek_pipe): Flag handle as not ready for write if event is + not signalled. + * fhandler.cc (fhandler_base::setup_overlapped): Establish event as + already signalled. + (fhandler_base::wait_overlapped): Don't reset event after we've + successfully waited. MSDN documentation says that this happens + automatically after a WriteFileEx/ReadFileEx. + 2009-06-26 Corinna Vinschen * wincap.h (wincaps::has_broken_alloc_console): New element. diff --git a/winsup/cygwin/fhandler.cc b/winsup/cygwin/fhandler.cc index da656ce28..650cca9e9 100644 --- a/winsup/cygwin/fhandler.cc +++ b/winsup/cygwin/fhandler.cc @@ -1763,33 +1763,55 @@ fhandler_base::wait_overlapped (bool inres, bool writing, DWORD *bytes, DWORD le return res; } -void -fhandler_base::read_overlapped (void *ptr, size_t& len) +bool __stdcall +fhandler_base::has_ongoing_io () { - DWORD bytes_read; - while (1) + if (get_overlapped () && get_overlapped ()->hEvent + && WaitForSingleObject (get_overlapped ()->hEvent, 0) != WAIT_OBJECT_0) { - bool res = ReadFile (get_handle (), ptr, len, &bytes_read, - get_overlapped ()); - int wres = wait_overlapped (res, false, &bytes_read); - if (wres || !_my_tls.call_signal_handler ()) - break; + set_errno (EAGAIN); + return true; } - len = (size_t) bytes_read; + return false; } -int -fhandler_base::write_overlapped (const void *ptr, size_t len) +void __stdcall +fhandler_base::read_overlapped (void *ptr, size_t& len) { - DWORD bytes_written; + DWORD nbytes; while (1) { - bool res = WriteFile (get_output_handle (), ptr, len, &bytes_written, - get_overlapped ()); - int wres = wait_overlapped (res, true, &bytes_written, (size_t) len); + if (has_ongoing_io ()) + { + nbytes = (DWORD) -1; + break; + } + bool res = ReadFile (get_handle (), ptr, len, &nbytes, + get_overlapped ()); + int wres = wait_overlapped (res, false, &nbytes); if (wres || !_my_tls.call_signal_handler ()) break; } - debug_printf ("returning %u", bytes_written); - return bytes_written; + len = (size_t) nbytes; +} + +int __stdcall +fhandler_base::write_overlapped (const void *ptr, size_t len) +{ + DWORD nbytes; + while (1) + { + if (has_ongoing_io ()) + { + nbytes = (DWORD) -1; + break; + } + bool res = WriteFile (get_output_handle (), ptr, len, &nbytes, + get_overlapped ()); + int wres = wait_overlapped (res, true, &nbytes, (size_t) len); + if (wres || !_my_tls.call_signal_handler ()) + break; + } + debug_printf ("returning %u", nbytes); + return nbytes; } diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index 1efd8fd58..a4cc50f56 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -302,6 +302,7 @@ class fhandler_base virtual char const *ttyname () { return get_name (); } virtual void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3))); virtual void __stdcall read_overlapped (void *ptr, size_t& len) __attribute__ ((regparm (3))); + virtual bool __stdcall has_ongoing_io () __attribute__ ((regparm (1))); virtual int write (const void *ptr, size_t len); virtual int __stdcall write_overlapped (const void *ptr, size_t len); virtual ssize_t readv (const struct iovec *, int iovcnt, ssize_t tot = -1); diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc index c3dc0055b..26cb3b40e 100644 --- a/winsup/cygwin/select.cc +++ b/winsup/cygwin/select.cc @@ -513,7 +513,7 @@ out: else if (fh->get_device () == FH_PIPER) select_printf ("%s, select for write on read end of pipe", fh->get_name ()); - else if (fh->get_overlapped ()->hEvent + else if (fh->get_overlapped () && fh->get_overlapped ()->hEvent && WaitForSingleObject (fh->get_overlapped ()->hEvent, 0) != WAIT_OBJECT_0) s->write_ready = false;