[PATCH] Cygwin: pty: Revise code waiting for forwarding by master_fwd_thread.

Takashi Yano takashi.yano@nifty.ne.jp
Mon Jan 20 02:51:00 GMT 2020


- 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.
---
 winsup/cygwin/fhandler.h      |  1 +
 winsup/cygwin/fhandler_tty.cc | 16 +++++++++++++---
 winsup/cygwin/tty.cc          |  1 +
 winsup/cygwin/tty.h           |  1 +
 4 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index c0d56b4da..f3301b059 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2201,6 +2201,7 @@ class fhandler_pty_slave: public fhandler_pty_common
   }
   void setup_locale (void);
   void set_freeconsole_on_close (bool val);
+  void wait_forwarding (void);
 };
 
 #define __ptsname(buf, unit) __small_sprintf ((buf), "/dev/pty%d", (unit))
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index a5db0967b..c839b9806 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1109,7 +1109,7 @@ skip_console_setting:
     }
   else if ((fd == 1 || fd == 2) && !get_ttyp ()->switch_to_pcon_out)
     {
-      Sleep (20);
+      wait_forwarding ();
       if (get_ttyp ()->pcon_pid == 0 ||
 	  kill (get_ttyp ()->pcon_pid, 0) != 0)
 	get_ttyp ()->pcon_pid = myself->pid;
@@ -1151,7 +1151,7 @@ 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_forwarding (); /* Wait for pty_master_fwd_thread() */
   get_ttyp ()->pcon_pid = 0;
   get_ttyp ()->switch_to_pcon_in = false;
   get_ttyp ()->switch_to_pcon_out = false;
@@ -2718,7 +2718,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);
+		wait_forwarding ();
 	      if (get_ttyp ()->pcon_pid == 0 ||
 		  kill (get_ttyp ()->pcon_pid, 0) != 0)
 		get_ttyp ()->pcon_pid = myself->pid;
@@ -2991,6 +2991,15 @@ pty_master_thread (VOID *arg)
   return ((fhandler_pty_master *) arg)->pty_master_thread ();
 }
 
+void
+fhandler_pty_slave::wait_forwarding (void)
+{
+  const DWORD time_to_wait = 40;
+  DWORD elasped = GetTickCount () - get_ttyp ()->last_fwd_time;
+  if (elasped < time_to_wait)
+    Sleep (time_to_wait - elasped);
+}
+
 DWORD
 fhandler_pty_master::pty_master_fwd_thread ()
 {
@@ -3000,6 +3009,7 @@ fhandler_pty_master::pty_master_fwd_thread ()
   termios_printf ("Started.");
   for (;;)
     {
+      get_ttyp ()->last_fwd_time = GetTickCount ();
       if (!ReadFile (from_slave, outbuf, sizeof outbuf, &rlen, NULL))
 	{
 	  termios_printf ("ReadFile for forwarding failed, %E");
diff --git a/winsup/cygwin/tty.cc b/winsup/cygwin/tty.cc
index 695ce91f1..095838f1b 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;
+  last_fwd_time = GetTickCount ();
 }
 
 HANDLE
diff --git a/winsup/cygwin/tty.h b/winsup/cygwin/tty.h
index cd4c0ed44..d6b22359a 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;
+  DWORD last_fwd_time;
 
 public:
   HANDLE from_master () const { return _from_master; }
-- 
2.21.0



More information about the Cygwin-patches mailing list