public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Ken Brown <kbrown@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: FIFO: improve taking ownership in fifo_reader_thread
Date: Thu, 16 Jul 2020 20:00:30 +0000 (GMT)	[thread overview]
Message-ID: <20200716200030.DFDC23857C58@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=6b8a8294966316f6e044e78f76579a18a3e9c173

commit 6b8a8294966316f6e044e78f76579a18a3e9c173
Author: Ken Brown <kbrown@cornell.edu>
Date:   Sat Jul 11 14:52:55 2020 -0400

    Cygwin: FIFO: improve taking ownership in fifo_reader_thread
    
    When a reader takes ownership in fifo_reader_thread, it now goes
    directly to the part of the main loop that listens for a connection.
    Previously it went back to the beginning of the loop.
    
    Also, if the reader has to delay taking ownership because the previous
    owner has not finished updating the shared fifo_client handlers, it
    now checks to see if cancel_evt has been set.  Previously it might
    have had to spin its wheels unnecessarily only to eventually find that
    its thread had been canceled.

Diff:
---
 winsup/cygwin/fhandler_fifo.cc | 44 ++++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 21 deletions(-)

diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc
index afe21a468..1fb319fcf 100644
--- a/winsup/cygwin/fhandler_fifo.cc
+++ b/winsup/cygwin/fhandler_fifo.cc
@@ -462,12 +462,30 @@ fhandler_fifo::fifo_reader_thread_func ()
 	take_ownership = true;
       else if (cur_owner != me)
 	idle = true;
-      if (take_ownership)
+      else
+	/* I'm the owner. */
+	goto owner_listen;
+      if (idle)
+	{
+	  owner_unlock ();
+	  HANDLE w[2] = { owner_needed_evt, cancel_evt };
+	  switch (WaitForMultipleObjects (2, w, false, INFINITE))
+	    {
+	    case WAIT_OBJECT_0:
+	      continue;
+	    case WAIT_OBJECT_0 + 1:
+	      goto canceled;
+	    default:
+	      api_fatal ("WFMO failed, %E");
+	    }
+	}
+      else if (take_ownership)
 	{
 	  if (!shared_fc_handler_updated ())
 	    {
 	      owner_unlock ();
-	      yield ();
+	      if (IsEventSignalled (cancel_evt))
+		goto canceled;
 	      continue;
 	    }
 	  else
@@ -478,26 +496,11 @@ fhandler_fifo::fifo_reader_thread_func ()
 		api_fatal ("Can't update my handlers, %E");
 	      owner_found ();
 	      owner_unlock ();
-	      continue;
+	      /* Fall through to owner_listen. */
 	    }
 	}
-      else if (idle)
-	{
-	  owner_unlock ();
-	  HANDLE w[2] = { owner_needed_evt, cancel_evt };
-	  switch (WaitForMultipleObjects (2, w, false, INFINITE))
-	    {
-	    case WAIT_OBJECT_0:
-	      continue;
-	    case WAIT_OBJECT_0 + 1:
-	      goto canceled;
-	    default:
-	      api_fatal ("WFMO failed, %E");
-	    }
-	}
-      else
-	{
-	  /* I'm the owner */
+
+owner_listen:
 	  fifo_client_lock ();
 	  cleanup_handlers ();
 	  if (add_client_handler () < 0)
@@ -590,7 +593,6 @@ fhandler_fifo::fifo_reader_thread_func ()
 	  fifo_client_unlock ();
 	  if (cancel)
 	    goto canceled;
-	}
     }
 canceled:
   if (conn_evt)


                 reply	other threads:[~2020-07-16 20:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20200716200030.DFDC23857C58@sourceware.org \
    --to=kbrown@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /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).