dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] lei: simplify startq/au_done wakeup notifications
Date: Wed, 18 Oct 2023 21:47:10 +0000	[thread overview]
Message-ID: <20231018214710.1816130-1-e@80x24.org> (raw)

We only need to write one byte at MUA start instead of one byte
for every LeiXSearch worker.  Make sure it succeeds by enabling
autodie for syswrite.

When reading, we can rely on `:perlio' layer `read' semantics
to retry on EINTR, to avoid looping and other error checking.
---
 lib/PublicInbox/LEI.pm        |  9 +++++----
 lib/PublicInbox/LeiXSearch.pm | 29 +++++++++++------------------
 2 files changed, 16 insertions(+), 22 deletions(-)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 3ccdd4f7..56e4c001 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -9,7 +9,7 @@ package PublicInbox::LEI;
 use v5.12;
 use parent qw(PublicInbox::DS PublicInbox::LeiExternal
 	PublicInbox::LeiQuery);
-use autodie qw(bind chdir fork open socket socketpair unlink);
+use autodie qw(bind chdir fork open socket socketpair syswrite unlink);
 use Getopt::Long ();
 use Socket qw(AF_UNIX SOCK_SEQPACKET pack_sockaddr_un);
 use Errno qw(EPIPE EAGAIN ECONNREFUSED ENOENT ECONNRESET);
@@ -1031,9 +1031,10 @@ sub start_mua {
 		$io->[0] = $self->{1} if $self->{opt}->{stdin} && -t $self->{1};
 		send_exec_cmd($self, $io, \@cmd, {});
 	}
-	if ($self->{lxs} && $self->{au_done}) { # kick wait_startq
-		syswrite($self->{au_done}, 'q' x ($self->{lxs}->{jobs} // 0));
-	}
+
+	# kick wait_startq:
+	syswrite($self->{au_done}, 'q') if $self->{lxs} && $self->{au_done};
+
 	return unless -t $self->{2}; # XXX how to determine non-TUI MUAs?
 	$self->{opt}->{quiet} = 1;
 	delete $self->{-progress};
diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm
index 25b66b3b..f9a995ef 100644
--- a/lib/PublicInbox/LeiXSearch.pm
+++ b/lib/PublicInbox/LeiXSearch.pm
@@ -122,25 +122,18 @@ sub _mset_more ($$) {
 	$size >= $mo->{limit} && (($mo->{offset} += $size) < $mo->{total});
 }
 
-# $startq will EOF when do_augment is done augmenting and allow
+# $startq will see `q' in do_post_augment -> start_mua if spawning MUA.
+# Otherwise $startq will EOF when do_augment is done augmenting and allow
 # query_combined_mset and query_thread_mset to proceed.
 sub wait_startq ($) {
 	my ($lei) = @_;
-	my $startq = delete $lei->{startq} or return;
-	while (1) {
-		my $n = sysread($startq, my $do_augment_done, 1);
-		if (defined $n) {
-			return if $n == 0; # no MUA
-			if ($do_augment_done eq 'q') {
-				$lei->{opt}->{quiet} = 1;
-				delete $lei->{opt}->{verbose};
-				delete $lei->{-progress};
-			} else {
-				die "BUG: do_augment_done=`$do_augment_done'";
-			}
-			return;
-		}
-		die "wait_startq: $!" unless $!{EINTR};
+	read(delete($lei->{startq}) // return, my $buf, 1) or return; # EOF
+	if ($buf eq 'q') {
+		$lei->{opt}->{quiet} = 1;
+		delete $lei->{opt}->{verbose};
+		delete $lei->{-progress};
+	} else {
+		die "BUG: wrote `$buf' to au_done";
 	}
 }
 
@@ -451,10 +444,10 @@ sub do_post_augment {
 		$lei->fail("$err");
 	}
 	if (!$err && delete $lei->{early_mua}) { # non-augment case
-		eval { $lei->start_mua };
+		eval { $lei->start_mua }; # may trigger wait_startq
 		$lei->fail($@) if $@;
 	}
-	close(delete $lei->{au_done}); # triggers wait_startq in lei_xsearch
+	close(delete $lei->{au_done}); # triggers wait_startq start_mua didn't
 }
 
 sub incr_post_augment { # called whenever an l2m shard finishes augment

                 reply	other threads:[~2023-10-18 21:47 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=20231018214710.1816130-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.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).