From a455ae9a0ed871e5f1e9ab5cf89ffdcbe34a49db Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Tue, 7 Sep 2021 09:02:55 +0900
Subject: [PATCH] Cygwin: select: Introduce select_evt event for pipe.

- This patch reverts "Cygwin: select: Improve select/poll response",
  and introduces select_evt event which notifies pipe status change.
---
 winsup/cygwin/fhandler.cc      |  1 +
 winsup/cygwin/fhandler.h       |  3 +++
 winsup/cygwin/fhandler_pipe.cc | 28 +++++++++++++++++++++++
 winsup/cygwin/select.cc        | 41 +++++++++-------------------------
 4 files changed, 42 insertions(+), 31 deletions(-)

diff --git a/winsup/cygwin/fhandler.cc b/winsup/cygwin/fhandler.cc
index f0c1b68f1..265e8ee59 100644
--- a/winsup/cygwin/fhandler.cc
+++ b/winsup/cygwin/fhandler.cc
@@ -1464,6 +1464,7 @@ fhandler_base::fhandler_base () :
   _refcnt (0),
   openflags (0),
   unique_id (0),
+  select_evt (NULL),
   archetype (NULL),
   usecount (0)
 {
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index bb7eb09ce..9022aa09c 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -217,6 +217,7 @@ class fhandler_base
   void set_ino (ino_t i) { ino = i; }
 
   HANDLE read_state;
+  HANDLE select_evt;
 
  public:
   LONG inc_refcnt () {return InterlockedIncrement (&_refcnt);}
@@ -520,6 +521,8 @@ public:
     fh->copy_from (this);
     return fh;
   }
+
+  HANDLE get_select_evt () { return select_evt; }
 };
 
 struct wsa_event
diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc
index 83efb8296..7cce4564c 100644
--- a/winsup/cygwin/fhandler_pipe.cc
+++ b/winsup/cygwin/fhandler_pipe.cc
@@ -367,6 +367,9 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
       CancelIo (get_handle ());
       pthread::static_cancel_self ();
     }
+  if (select_evt && nbytes)
+    for (ULONG i = 0; i < get_obj_handle_count (select_evt); i++)
+      SetEvent (select_evt);
   len = nbytes;
 }
 
@@ -489,6 +492,9 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len)
       CancelIo (get_handle ());
       pthread::static_cancel_self ();
     }
+  if (select_evt && nbytes)
+    for (ULONG i = 0; i < get_obj_handle_count (select_evt); i++)
+      SetEvent (select_evt);
   return nbytes ?: -1;
 }
 
@@ -497,6 +503,8 @@ fhandler_pipe::fixup_after_fork (HANDLE parent)
 {
   if (read_mtx)
     fork_fixup (parent, read_mtx, "read_mtx");
+  if (select_evt)
+    fork_fixup (parent, select_evt, "select_evt");
   fhandler_base::fixup_after_fork (parent);
 }
 
@@ -518,6 +526,15 @@ fhandler_pipe::dup (fhandler_base *child, int flags)
       ftp->close ();
       res = -1;
     }
+  else if (select_evt &&
+	   !DuplicateHandle (GetCurrentProcess (), select_evt,
+			    GetCurrentProcess (), &ftp->select_evt,
+			    0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
+    {
+      __seterrno ();
+      ftp->close ();
+      res = -1;
+    }
 
   debug_printf ("res %d", res);
   return res;
@@ -528,6 +545,12 @@ fhandler_pipe::close ()
 {
   if (read_mtx)
     CloseHandle (read_mtx);
+  if (select_evt)
+    {
+      for (ULONG i = 0; i < get_obj_handle_count (select_evt); i++)
+	SetEvent (select_evt);
+      CloseHandle (select_evt);
+    }
   return fhandler_base::close ();
 }
 
@@ -747,6 +770,11 @@ fhandler_pipe::create (fhandler_pipe *fhs[2], unsigned psize, int mode)
 	  fhs[0]->set_read_mutex (mtx);
 	  res = 0;
 	}
+      fhs[0]->select_evt = CreateEvent (&sa, FALSE, FALSE, NULL);
+      if (fhs[0]->select_evt)
+	DuplicateHandle (GetCurrentProcess (), fhs[0]->select_evt,
+			 GetCurrentProcess (), &fhs[1]->select_evt,
+			 0, 1, DUPLICATE_SAME_ACCESS);
     }
 
   debug_printf ("%R = pipe([%p, %p], %d, %y)", res, fhs[0], fhs[1], psize, mode);
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index ac2fd227e..19efe9e95 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -734,7 +734,6 @@ thread_pipe (void *arg)
   select_pipe_info *pi = (select_pipe_info *) arg;
   DWORD sleep_time = 0;
   bool looping = true;
-  DWORD t0 = GetTickCount ();
 
   while (looping)
     {
@@ -754,12 +753,7 @@ thread_pipe (void *arg)
 	break;
       cygwait (pi->bye, sleep_time >> 3);
       if (sleep_time < 80)
-	{
-	  DWORD t1 = GetTickCount ();
-	  if (t0 != t1)
-	    ++sleep_time;
-	  t0 = t1;
-	}
+	++sleep_time;
       if (pi->stop_thread)
 	break;
     }
@@ -776,7 +770,9 @@ start_thread_pipe (select_record *me, select_stuff *stuff)
     {
       pi->start = &stuff->start;
       pi->stop_thread = false;
-      pi->bye = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
+      pi->bye = me->fh->get_select_evt ();
+      if (pi->bye == NULL)
+	pi->bye = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
       pi->thread = new cygthread (thread_pipe, pi, "pipesel");
       me->h = *pi->thread;
       if (!me->h)
@@ -786,7 +782,7 @@ start_thread_pipe (select_record *me, select_stuff *stuff)
 }
 
 static void
-pipe_cleanup (select_record *, select_stuff *stuff)
+pipe_cleanup (select_record *me, select_stuff *stuff)
 {
   select_pipe_info *pi = (select_pipe_info *) stuff->device_specific_pipe;
   if (!pi)
@@ -796,7 +792,8 @@ pipe_cleanup (select_record *, select_stuff *stuff)
       pi->stop_thread = true;
       SetEvent (pi->bye);
       pi->thread->detach ();
-      CloseHandle (pi->bye);
+      if (me->fh->get_select_evt () == NULL)
+	CloseHandle (pi->bye);
     }
   delete pi;
   stuff->device_specific_pipe = NULL;
@@ -935,7 +932,6 @@ thread_fifo (void *arg)
   select_fifo_info *pi = (select_fifo_info *) arg;
   DWORD sleep_time = 0;
   bool looping = true;
-  DWORD t0 = GetTickCount ();
 
   while (looping)
     {
@@ -955,12 +951,7 @@ thread_fifo (void *arg)
 	break;
       cygwait (pi->bye, sleep_time >> 3);
       if (sleep_time < 80)
-	{
-	  DWORD t1 = GetTickCount ();
-	  if (t0 != t1)
-	    ++sleep_time;
-	  t0 = t1;
-	}
+	++sleep_time;
       if (pi->stop_thread)
 	break;
     }
@@ -1136,7 +1127,6 @@ thread_console (void *arg)
   select_console_info *ci = (select_console_info *) arg;
   DWORD sleep_time = 0;
   bool looping = true;
-  DWORD t0 = GetTickCount ();
 
   while (looping)
     {
@@ -1156,12 +1146,7 @@ thread_console (void *arg)
 	break;
       cygwait (ci->bye, sleep_time >> 3);
       if (sleep_time < 80)
-	{
-	  DWORD t1 = GetTickCount ();
-	  if (t0 != t1)
-	    ++sleep_time;
-	  t0 = t1;
-	}
+	++sleep_time;
       if (ci->stop_thread)
 	break;
     }
@@ -1381,7 +1366,6 @@ thread_pty_slave (void *arg)
   select_pipe_info *pi = (select_pipe_info *) arg;
   DWORD sleep_time = 0;
   bool looping = true;
-  DWORD t0 = GetTickCount ();
 
   while (looping)
     {
@@ -1401,12 +1385,7 @@ thread_pty_slave (void *arg)
 	break;
       cygwait (pi->bye, sleep_time >> 3);
       if (sleep_time < 80)
-	{
-	  DWORD t1 = GetTickCount ();
-	  if (t0 != t1)
-	    ++sleep_time;
-	  t0 = t1;
-	}
+	++sleep_time;
       if (pi->stop_thread)
 	break;
     }
-- 
2.33.0

