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,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 BEC671F4BF for ; Mon, 24 Jun 2019 02:54:17 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 19/57] ds: share watch_chg between watch_read/watch_write Date: Mon, 24 Jun 2019 02:52:20 +0000 Message-Id: <20190624025258.25592-20-e@80x24.org> In-Reply-To: <20190624025258.25592-1-e@80x24.org> References: <20190624025258.25592-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: There was much duplicate logic between watch_read and watch_write. Share that logic, and give us room to enable edge-triggered or one-shot notifications in the future. --- lib/PublicInbox/DS.pm | 73 +++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 48 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index 00e2e5c6..943e30b5 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -622,65 +622,42 @@ sub msg_more ($$) { $self->write(\($_[1])); } -=head2 C<< $obj->watch_read( $boolean ) >> - -Turn 'readable' event notification on or off. - -=cut -sub watch_read { - my PublicInbox::DS $self = shift; +sub watch_chg ($$$) { + my ($self, $bits, $set) = @_; my $sock = $self->{sock} or return; - - my $val = shift; - my $event = $self->{event_watch}; - - $event &= ~EPOLLIN if ! $val; - $event |= EPOLLIN if $val; - + my $cur = $self->{event_watch}; + my $changes = $cur; + if ($set) { + $changes |= $bits; + } else { + $changes &= ~$bits; + } + return if $changes == $cur; my $fd = fileno($sock); - # If it changed, set it - if ($event != $self->{event_watch}) { - if ($HaveKQueue) { - $KQueue->EV_SET($fd, EVFILT_READ(), - $val ? EV_ENABLE() : EV_DISABLE()); - } - elsif ($HaveEpoll) { - epoll_ctl($Epoll, EPOLL_CTL_MOD, $fd, $event) and - confess("EPOLL_CTL_MOD: $!"); - } - $self->{event_watch} = $event; + if ($HaveEpoll) { + epoll_ctl($Epoll, EPOLL_CTL_MOD, $fd, $changes) and + confess("EPOLL_CTL_MOD $!"); + } elsif ($HaveKQueue) { + my $flag = $set ? EV_ENABLE() : EV_DISABLE(); + $KQueue->EV_SET($fd, EVFILT_READ(), $flag) if $bits & EPOLLIN; + $KQueue->EV_SET($fd, EVFILT_WRITE(), $flag) if $bits & EPOLLOUT; } + $self->{event_watch} = $changes; } -=head2 C<< $obj->watch_write( $boolean ) >> +=head2 C<< $obj->watch_read( $boolean ) >> -Turn 'writable' event notification on or off. +Turn 'readable' event notification on or off. =cut -sub watch_write { - my PublicInbox::DS $self = shift; - my $sock = $self->{sock} or return; +sub watch_read ($$) { watch_chg($_[0], EPOLLIN, $_[1]) }; - my $val = shift; - my $event = $self->{event_watch}; +=head2 C<< $obj->watch_write( $boolean ) >> - $event &= ~EPOLLOUT if ! $val; - $event |= EPOLLOUT if $val; - my $fd = fileno($sock); +Turn 'writable' event notification on or off. - # If it changed, set it - if ($event != $self->{event_watch}) { - if ($HaveKQueue) { - $KQueue->EV_SET($fd, EVFILT_WRITE(), - $val ? EV_ENABLE() : EV_DISABLE()); - } - elsif ($HaveEpoll) { - epoll_ctl($Epoll, EPOLL_CTL_MOD, $fd, $event) and - confess "EPOLL_CTL_MOD: $!"; - } - $self->{event_watch} = $event; - } -} +=cut +sub watch_write ($$) { watch_chg($_[0], EPOLLOUT, $_[1]) }; package PublicInbox::DS::Timer; # [$abs_float_firetime, $coderef]; -- EW