Re: [PATCH v2] Cygwin: console: Share readahead buffer within the same process.

2020-01-28 Thread Corinna Vinschen
On Jan 27 21:14, Takashi Yano wrote:
> - The cause of the problem reported in
>   https://www.cygwin.com/ml/cygwin/2020-01/msg00220.html is that the
>   chars input before dup() cannot be read from the new file descriptor.
>   This is because the readahead buffer (rabuf) in the console is newly
>   created by dup(), and does not inherit from the parent. This patch
>   fixes the issue.
> ---
>  winsup/cygwin/fhandler.cc | 56 +++
>  winsup/cygwin/fhandler.h  | 33 +-
>  winsup/cygwin/fhandler_console.cc | 40 +-
>  winsup/cygwin/fhandler_termios.cc | 35 +--
>  winsup/cygwin/fhandler_tty.cc |  2 +-
>  5 files changed, 111 insertions(+), 55 deletions(-)

Pushed.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer


signature.asc
Description: PGP signature


[PATCH v2] Cygwin: console: Share readahead buffer within the same process.

2020-01-27 Thread Takashi Yano
- The cause of the problem reported in
  https://www.cygwin.com/ml/cygwin/2020-01/msg00220.html is that the
  chars input before dup() cannot be read from the new file descriptor.
  This is because the readahead buffer (rabuf) in the console is newly
  created by dup(), and does not inherit from the parent. This patch
  fixes the issue.
---
 winsup/cygwin/fhandler.cc | 56 +++
 winsup/cygwin/fhandler.h  | 33 +-
 winsup/cygwin/fhandler_console.cc | 40 +-
 winsup/cygwin/fhandler_termios.cc | 35 +--
 winsup/cygwin/fhandler_tty.cc |  2 +-
 5 files changed, 111 insertions(+), 55 deletions(-)

diff --git a/winsup/cygwin/fhandler.cc b/winsup/cygwin/fhandler.cc
index aeee8fe4d..4210510be 100644
--- a/winsup/cygwin/fhandler.cc
+++ b/winsup/cygwin/fhandler.cc
@@ -44,11 +44,11 @@ void
 fhandler_base::reset (const fhandler_base *from)
 {
   pc << from->pc;
-  rabuf = NULL;
-  ralen = 0;
-  raixget = 0;
-  raixput = 0;
-  rabuflen = 0;
+  ra.rabuf = NULL;
+  ra.ralen = 0;
+  ra.raixget = 0;
+  ra.raixput = 0;
+  ra.rabuflen = 0;
   _refcnt = 0;
 }
 
@@ -66,15 +66,15 @@ int
 fhandler_base::put_readahead (char value)
 {
   char *newrabuf;
-  if (raixput < rabuflen)
+  if (raixput () < rabuflen ())
 /* Nothing to do */;
-  else if ((newrabuf = (char *) realloc (rabuf, rabuflen += 32)))
-rabuf = newrabuf;
+  else if ((newrabuf = (char *) realloc (rabuf (), rabuflen () += 32)))
+rabuf () = newrabuf;
   else
 return 0;
 
-  rabuf[raixput++] = value;
-  ralen++;
+  rabuf ()[raixput ()++] = value;
+  ralen ()++;
   return 1;
 }
 
@@ -82,11 +82,11 @@ int
 fhandler_base::get_readahead ()
 {
   int chret = -1;
-  if (raixget < ralen)
-chret = ((unsigned char) rabuf[raixget++]) & 0xff;
+  if (raixget () < ralen ())
+chret = ((unsigned char) rabuf ()[raixget ()++]) & 0xff;
   /* FIXME - not thread safe */
-  if (raixget >= ralen)
-raixget = raixput = ralen = 0;
+  if (raixget () >= ralen ())
+raixget () = raixput () = ralen () = 0;
   return chret;
 }
 
@@ -94,10 +94,10 @@ int
 fhandler_base::peek_readahead (int queryput)
 {
   int chret = -1;
-  if (!queryput && raixget < ralen)
-chret = ((unsigned char) rabuf[raixget]) & 0xff;
-  else if (queryput && raixput > 0)
-chret = ((unsigned char) rabuf[raixput - 1]) & 0xff;
+  if (!queryput && raixget () < ralen ())
+chret = ((unsigned char) rabuf ()[raixget ()]) & 0xff;
+  else if (queryput && raixput () > 0)
+chret = ((unsigned char) rabuf ()[raixput () - 1]) & 0xff;
   return chret;
 }
 
@@ -105,7 +105,7 @@ void
 fhandler_base::set_readahead_valid (int val, int ch)
 {
   if (!val)
-ralen = raixget = raixput = 0;
+ralen () = raixget () = raixput () = 0;
   if (ch != -1)
 put_readahead (ch);
 }
@@ -1092,7 +1092,7 @@ fhandler_base::lseek (off_t offset, int whence)
   if (whence != SEEK_CUR || offset != 0)
 {
   if (whence == SEEK_CUR)
-   offset -= ralen - raixget;
+   offset -= ralen () - raixget ();
   set_readahead_valid (0);
 }
 
@@ -1142,7 +1142,7 @@ fhandler_base::lseek (off_t offset, int whence)
  readahead that we have to take into account when calculating
  the actual position for the application.  */
   if (whence == SEEK_CUR)
-res -= ralen - raixget;
+res -= ralen () - raixget ();
 
   return res;
 }
@@ -1565,23 +1565,23 @@ fhandler_base::fhandler_base () :
   ino (0),
   _refcnt (0),
   openflags (0),
-  rabuf (NULL),
-  ralen (0),
-  raixget (0),
-  raixput (0),
-  rabuflen (0),
   unique_id (0),
   archetype (NULL),
   usecount (0)
 {
+  ra.rabuf = NULL;
+  ra.ralen = 0;
+  ra.raixget = 0;
+  ra.raixput = 0;
+  ra.rabuflen = 0;
   isclosed (false);
 }
 
 /* Normal I/O destructor */
 fhandler_base::~fhandler_base ()
 {
-  if (rabuf)
-free (rabuf);
+  if (ra.rabuf)
+free (ra.rabuf);
 }
 
 /**/
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index c0d56b4da..b4a5638fe 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -202,16 +202,21 @@ class fhandler_base
 
   ino_t ino;   /* file ID or hashed filename, depends on FS. */
   LONG _refcnt;
+ public:
+  struct rabuf_t
+  {
+char *rabuf;   /* used for crlf conversion in text files */
+size_t ralen;
+size_t raixget;
+size_t raixput;
+size_t rabuflen;
+  };
 
  protected:
   /* File open flags from open () and fcntl () calls */
   int openflags;
 
-  char *rabuf; /* used for crlf conversion in text files */
-  size_t ralen;
-  size_t raixget;
-  size_t raixput;
-  size_t rabuflen;
+  struct rabuf_t ra;
 
   /* Used for advisory file locking.  See flock.cc.  */
   int64_t unique_id;
@@ -315,7 +320,13 @@ class fhandler_base
 ReleaseSemaphore (read_state, n, NULL);
   }
 
-  bool get_readahead_valid () { return raixget < ralen; }
+  virtual char *&rabuf () { return ra.rabuf; };
+  vir