public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Ken Brown <kbrown@cornell.edu>
To: cygwin-patches@cygwin.com
Subject: [PATCH 4/8] Cygwin: FIFO: reorganize some fifo_client_handler methods
Date: Tue,  4 Aug 2020 08:55:03 -0400	[thread overview]
Message-ID: <20200804125507.8842-5-kbrown@cornell.edu> (raw)
In-Reply-To: <20200804125507.8842-1-kbrown@cornell.edu>

Rename the existing set_state() to query_and_set_state() to reflect
what it really does.  (It queries the O/S for the pipe state.)  Add a
new set_state() method, which is a standard setter, and a
corresponding getter get_state().
---
 winsup/cygwin/fhandler.h       |  9 ++++--
 winsup/cygwin/fhandler_fifo.cc | 50 +++++++++++++++++++---------------
 winsup/cygwin/select.cc        | 28 +++++++++++--------
 3 files changed, 50 insertions(+), 37 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 5488327a2..f64eabda4 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1297,11 +1297,14 @@ enum fifo_client_connect_state
 struct fifo_client_handler
 {
   HANDLE h;
-  fifo_client_connect_state state;
+  fifo_client_connect_state _state;
   bool last_read;  /* true if our last successful read was from this client. */
-  fifo_client_handler () : h (NULL), state (fc_unknown), last_read (false) {}
+  fifo_client_handler () : h (NULL), _state (fc_unknown), last_read (false) {}
   void close () { NtClose (h); }
-  fifo_client_connect_state set_state ();
+  fifo_client_connect_state get_state () const { return _state; }
+  void set_state (fifo_client_connect_state s) { _state = s; }
+  /* Query O/S.  Return previous state. */
+  fifo_client_connect_state query_and_set_state ();
 };
 
 class fhandler_fifo;
diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc
index b8a47f27f..c816c692a 100644
--- a/winsup/cygwin/fhandler_fifo.cc
+++ b/winsup/cygwin/fhandler_fifo.cc
@@ -340,7 +340,7 @@ fhandler_fifo::add_client_handler (bool new_pipe_instance)
       if (!ph)
 	return -1;
       fc.h = ph;
-      fc.state = fc_listening;
+      fc.set_state (fc_listening);
     }
   fc_handler[nhandlers++] = fc;
   return 0;
@@ -365,7 +365,7 @@ fhandler_fifo::cleanup_handlers ()
 
   while (i < nhandlers)
     {
-      if (fc_handler[i].state < fc_closing)
+      if (fc_handler[i].get_state () < fc_closing)
 	delete_client_handler (i);
       else
 	i++;
@@ -377,7 +377,7 @@ void
 fhandler_fifo::record_connection (fifo_client_handler& fc,
 				  fifo_client_connect_state s)
 {
-  fc.state = s;
+  fc.set_state (s);
   set_pipe_non_blocking (fc.h, true);
 }
 
@@ -414,13 +414,13 @@ fhandler_fifo::update_my_handlers ()
 	{
 	  debug_printf ("Can't duplicate handle of previous owner, %E");
 	  __seterrno ();
-	  fc.state = fc_error;
+	  fc.set_state (fc_error);
 	  fc.last_read = false;
 	  ret = -1;
 	}
       else
 	{
-	  fc.state = shared_fc_handler[i].state;
+	  fc.set_state (shared_fc_handler[i].get_state ());
 	  fc.last_read = shared_fc_handler[i].last_read;
 	}
     }
@@ -614,7 +614,7 @@ owner_listen:
 		break;
 	      default:
 		debug_printf ("NtFsControlFile status %y after failing to connect bogus client or real client", io.Status);
-		fc.state = fc_unknown;
+		fc.set_state (fc_unknown);
 		break;
 	      }
 	  break;
@@ -1280,7 +1280,7 @@ fhandler_fifo::raw_read (void *in_ptr, size_t& len)
       for (j = 0; j < nhandlers; j++)
 	if (fc_handler[j].last_read)
 	  break;
-      if (j < nhandlers && fc_handler[j].state >= fc_closing)
+      if (j < nhandlers && fc_handler[j].get_state () >= fc_closing)
 	{
 	  NTSTATUS status;
 	  IO_STATUS_BLOCK io;
@@ -1303,11 +1303,11 @@ fhandler_fifo::raw_read (void *in_ptr, size_t& len)
 	    case STATUS_PIPE_EMPTY:
 	      break;
 	    case STATUS_PIPE_BROKEN:
-	      fc_handler[j].state = fc_disconnected;
+	      fc_handler[j].set_state (fc_disconnected);
 	      break;
 	    default:
 	      debug_printf ("NtReadFile status %y", status);
-	      fc_handler[j].state = fc_error;
+	      fc_handler[j].set_state (fc_error);
 	      break;
 	    }
 	  fc_handler[j].last_read = false;
@@ -1315,7 +1315,7 @@ fhandler_fifo::raw_read (void *in_ptr, size_t& len)
 
       /* Second pass. */
       for (int i = 0; i < nhandlers; i++)
-	if (fc_handler[i].state >= fc_closing)
+	if (fc_handler[i].get_state () >= fc_closing)
 	  {
 	    NTSTATUS status;
 	    IO_STATUS_BLOCK io;
@@ -1339,12 +1339,12 @@ fhandler_fifo::raw_read (void *in_ptr, size_t& len)
 	      case STATUS_PIPE_EMPTY:
 		break;
 	      case STATUS_PIPE_BROKEN:
-		fc_handler[i].state = fc_disconnected;
+		fc_handler[i].set_state (fc_disconnected);
 		nconnected--;
 		break;
 	      default:
 		debug_printf ("NtReadFile status %y", status);
-		fc_handler[i].state = fc_error;
+		fc_handler[i].set_state (fc_error);
 		nconnected--;
 		break;
 	      }
@@ -1417,45 +1417,51 @@ fhandler_fifo::close_all_handlers ()
   fifo_client_unlock ();
 }
 
+/* Return previous state. */
 fifo_client_connect_state
-fifo_client_handler::set_state ()
+fifo_client_handler::query_and_set_state ()
 {
   IO_STATUS_BLOCK io;
   FILE_PIPE_LOCAL_INFORMATION fpli;
   NTSTATUS status;
+  fifo_client_connect_state prev_state = get_state ();
 
   if (!h)
-    return (state = fc_unknown);
+    {
+      set_state (fc_unknown);
+      goto out;
+    }
 
   status = NtQueryInformationFile (h, &io, &fpli,
 				   sizeof (fpli), FilePipeLocalInformation);
   if (!NT_SUCCESS (status))
     {
       debug_printf ("NtQueryInformationFile status %y", status);
-      state = fc_error;
+      set_state (fc_error);
     }
   else if (fpli.ReadDataAvailable > 0)
-    state = fc_input_avail;
+    set_state (fc_input_avail);
   else
     switch (fpli.NamedPipeState)
       {
       case FILE_PIPE_DISCONNECTED_STATE:
-	state = fc_disconnected;
+	set_state (fc_disconnected);
 	break;
       case FILE_PIPE_LISTENING_STATE:
-	state = fc_listening;
+	set_state (fc_listening);
 	break;
       case FILE_PIPE_CONNECTED_STATE:
-	state = fc_connected;
+	set_state (fc_connected);
 	break;
       case FILE_PIPE_CLOSING_STATE:
-	state = fc_closing;
+	set_state (fc_closing);
 	break;
       default:
-	state = fc_error;
+	set_state (fc_error);
 	break;
       }
-  return state;
+out:
+  return prev_state;
 }
 
 void
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index 1ba76c817..0c94f6c45 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -875,18 +875,22 @@ peek_fifo (select_record *s, bool from_select)
       fh->fifo_client_lock ();
       int nconnected = 0;
       for (int i = 0; i < fh->get_nhandlers (); i++)
-	if (fh->get_fc_handler (i).set_state () >= fc_closing)
-	  {
-	    nconnected++;
-	    if (fh->get_fc_handler (i).state == fc_input_avail)
-	      {
-		select_printf ("read: %s, ready for read", fh->get_name ());
-		fh->fifo_client_unlock ();
-		fh->reading_unlock ();
-		gotone += s->read_ready = true;
-		goto out;
-	      }
-	  }
+	{
+	  fifo_client_handler &fc = fh->get_fc_handler (i);
+	  fc.query_and_set_state ();
+	  if (fc.get_state () >= fc_closing)
+	    {
+	      nconnected++;
+	      if (fc.get_state () == fc_input_avail)
+		{
+		  select_printf ("read: %s, ready for read", fh->get_name ());
+		  fh->fifo_client_unlock ();
+		  fh->reading_unlock ();
+		  gotone += s->read_ready = true;
+		  goto out;
+		}
+	    }
+	}
       fh->fifo_client_unlock ();
       if (!nconnected && fh->hit_eof ())
 	{
-- 
2.28.0


  parent reply	other threads:[~2020-08-04 12:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-04 12:54 [PATCH 0/8] FIFO: bug fixes and small improvements Ken Brown
2020-08-04 12:55 ` [PATCH 1/8] Cygwin: FIFO: lock fixes Ken Brown
2020-08-04 12:55 ` [PATCH 2/8] Cygwin: FIFO: fix timing issue with owner change Ken Brown
2020-08-04 12:55 ` [PATCH 3/8] Cygwin: FIFO: add a timeout to take_ownership Ken Brown
2020-08-04 12:55 ` Ken Brown [this message]
2020-08-04 12:55 ` [PATCH 5/8] Cygwin: FIFO: don't read from pipes that are closing Ken Brown
2020-08-04 12:55 ` [PATCH 6/8] Cygwin: FIFO: synchronize the fifo_reader and fifosel threads Ken Brown
2020-08-04 12:55 ` [PATCH 7/8] Cygwin: FIFO: fix indentation Ken Brown
2020-08-04 12:55 ` [PATCH 8/8] Cygwin: FIFO: add a third pass to raw_read Ken Brown
2020-08-04 15:04 ` [PATCH 0/8] FIFO: bug fixes and small improvements Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200804125507.8842-5-kbrown@cornell.edu \
    --to=kbrown@cornell.edu \
    --cc=cygwin-patches@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).