From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 49790 invoked by alias); 21 Jan 2020 09:37:16 -0000 Mailing-List: contact cygwin-cvs-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: cygwin-cvs-owner@cygwin.com Received: (qmail 49749 invoked by uid 9078); 21 Jan 2020 09:37:16 -0000 Date: Tue, 21 Jan 2020 09:37:00 -0000 Message-ID: <20200121093716.49747.qmail@sourceware.org> Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: pty: Revise code waiting for forwarding by master_fwd_thread. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: 5377a847764461493c620644f6530892344d1cdd X-Git-Newrev: 6cc299f0e20e4b76f7dbab5ea8c296ffa4859b62 X-SW-Source: 2020-q1/txt/msg00021.txt https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=6cc299f0e20e4b76f7dbab5ea8c296ffa4859b62 commit 6cc299f0e20e4b76f7dbab5ea8c296ffa4859b62 Author: Takashi Yano Date: Tue Jan 21 11:22:02 2020 +0900 Cygwin: pty: Revise code waiting for forwarding by master_fwd_thread. - Though this rarely happens, sometimes the first printing of non- cygwin process does not displayed correctly. To fix this issue, the code for waiting for forwarding by master_fwd_thread is revised. Diff: --- winsup/cygwin/fhandler_tty.cc | 15 +++++++++++---- winsup/cygwin/tty.cc | 1 + winsup/cygwin/tty.h | 1 + 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index fff5beb..8ed1f8a 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -648,7 +648,7 @@ fhandler_pty_master::process_slave_output (char *buf, size_t len, int pktmode_on /* If echo pipe has data (something has been typed or pasted), prefer it over slave output. */ if (echo_cnt > 0) - { + { if (!ReadFile (echo_r, outbuf, rlen, &n, NULL)) { termios_printf ("ReadFile on echo pipe failed, %E"); @@ -1109,7 +1109,7 @@ skip_console_setting: } else if ((fd == 1 || fd == 2) && !get_ttyp ()->switch_to_pcon_out) { - Sleep (20); + cygwait (get_ttyp ()->fwd_done, INFINITE); if (get_ttyp ()->pcon_pid == 0 || kill (get_ttyp ()->pcon_pid, 0) != 0) get_ttyp ()->pcon_pid = myself->pid; @@ -1151,7 +1151,8 @@ fhandler_pty_slave::reset_switch_to_pcon (void) SetConsoleMode (get_handle (), mode & ~ENABLE_ECHO_INPUT); } if (get_ttyp ()->switch_to_pcon_out) - Sleep (20); /* Wait for pty_master_fwd_thread() */ + /* Wait for pty_master_fwd_thread() */ + cygwait (get_ttyp ()->fwd_done, INFINITE); get_ttyp ()->pcon_pid = 0; get_ttyp ()->switch_to_pcon_in = false; get_ttyp ()->switch_to_pcon_out = false; @@ -2202,6 +2203,8 @@ fhandler_pty_master::close () } release_output_mutex (); master_fwd_thread->terminate_thread (); + CloseHandle (get_ttyp ()->fwd_done); + get_ttyp ()->fwd_done = NULL; } } @@ -2718,7 +2721,7 @@ fhandler_pty_slave::fixup_after_attach (bool native_maybe, int fd_set) DWORD mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT; SetConsoleMode (get_output_handle (), mode); if (!get_ttyp ()->switch_to_pcon_out) - Sleep (20); + cygwait (get_ttyp ()->fwd_done, INFINITE); if (get_ttyp ()->pcon_pid == 0 || kill (get_ttyp ()->pcon_pid, 0) != 0) get_ttyp ()->pcon_pid = myself->pid; @@ -3000,11 +3003,14 @@ fhandler_pty_master::pty_master_fwd_thread () termios_printf ("Started."); for (;;) { + if (::bytes_available (rlen, from_slave) && rlen == 0) + SetEvent (get_ttyp ()->fwd_done); if (!ReadFile (from_slave, outbuf, sizeof outbuf, &rlen, NULL)) { termios_printf ("ReadFile for forwarding failed, %E"); break; } + ResetEvent (get_ttyp ()->fwd_done); ssize_t wlen = rlen; char *ptr = outbuf; if (get_pseudo_console ()) @@ -3365,6 +3371,7 @@ fhandler_pty_master::setup () errstr = "pty master forwarding thread"; goto err; } + get_ttyp ()->fwd_done = CreateEvent (&sec_none, true, false, NULL); setup_pseudoconsole (); diff --git a/winsup/cygwin/tty.cc b/winsup/cygwin/tty.cc index 695ce91..ef9bbc1 100644 --- a/winsup/cygwin/tty.cc +++ b/winsup/cygwin/tty.cc @@ -245,6 +245,7 @@ tty::init () num_pcon_attached_slaves = 0; term_code_page = 0; need_redraw_screen = false; + fwd_done = NULL; } HANDLE diff --git a/winsup/cygwin/tty.h b/winsup/cygwin/tty.h index cd4c0ed..b291fd3 100644 --- a/winsup/cygwin/tty.h +++ b/winsup/cygwin/tty.h @@ -106,6 +106,7 @@ private: int num_pcon_attached_slaves; UINT term_code_page; bool need_redraw_screen; + HANDLE fwd_done; public: HANDLE from_master () const { return _from_master; }