From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id F18511FD5A for ; Sun, 12 Jan 2020 21:17:58 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 11/11] sigfd: simplify loop and improve documentation Date: Sun, 12 Jan 2020 21:17:56 +0000 Message-Id: <20200112211756.23100-12-e@yhbt.net> In-Reply-To: <20200112211756.23100-1-e@yhbt.net> References: <20200112211756.23100-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We can use the return value of sysread to bound our loop instead of repeatedly shortening the string. Furthermore add some comments which can be easily checked against the signalfd(2) manpage. --- lib/PublicInbox/Sigfd.pm | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/PublicInbox/Sigfd.pm b/lib/PublicInbox/Sigfd.pm index ec5d7145..15dedb10 100644 --- a/lib/PublicInbox/Sigfd.pm +++ b/lib/PublicInbox/Sigfd.pm @@ -42,14 +42,15 @@ sub new { # PublicInbox::Daemon in master main loop (blocking) sub wait_once ($) { my ($self) = @_; + # 128 == sizeof(struct signalfd_siginfo) my $r = sysread($self->{sock}, my $buf, 128 * 64); if (defined($r)) { - while (1) { - my $sig = unpack('L', $buf); - my $cb = $self->{sig}->{$sig}; - $cb->($sig) if $cb ne 'IGNORE'; - return $r if length($buf) == 128; - $buf = substr($buf, 128); + my $nr = $r / 128 - 1; # $nr may be -1 + for my $off (0..$nr) { + # the first uint32_t of signalfd_siginfo: ssi_signo + my $signo = unpack('L', substr($buf, 128 * $off, 4)); + my $cb = $self->{sig}->{$signo}; + $cb->($signo) if $cb ne 'IGNORE'; } } $r;