]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/DS.pm
ds: remove IO::Poll support (for now)
[public-inbox.git] / lib / PublicInbox / DS.pm
index 2e0aa1e042b7ab2e50dd44a7e3b974de82cdc749..9c801214b150290a907733d3b9bf3e255c4a7300 100644 (file)
@@ -37,7 +37,7 @@ use Errno  qw(EAGAIN EINVAL);
 use Carp   qw(croak confess);
 use File::Temp qw(tempfile);
 
-our $HAVE_KQUEUE = eval { require IO::KQueue; 1 };
+our $HAVE_KQUEUE = eval { require IO::KQueue; IO::KQueue->import; 1 };
 
 our (
      $HaveEpoll,                 # Flag -- is epoll available?  initially undefined.
@@ -168,11 +168,6 @@ sub _InitPoller
             *EventLoop = *EpollEventLoop;
         }
     }
-
-    if (!$HaveEpoll && !$HaveKQueue) {
-        require IO::Poll;
-        *EventLoop = *PollEventLoop;
-    }
 }
 
 =head2 C<< CLASS->EventLoop() >>
@@ -190,8 +185,6 @@ sub FirstTimeEventLoop {
         EpollEventLoop($class);
     } elsif ($HaveKQueue) {
         KQueueEventLoop($class);
-    } else {
-        PollEventLoop($class);
     }
 }
 
@@ -250,51 +243,6 @@ sub EpollEventLoop {
     exit 0;
 }
 
-### The fallback IO::Poll-based event loop. Gets installed as EventLoop if
-### IO::Epoll fails to load.
-sub PollEventLoop {
-    my $class = shift;
-
-    my PublicInbox::DS $pob;
-
-    while (1) {
-        my $timeout = RunTimers();
-
-        # the following sets up @poll as a series of ($poll,$event_mask)
-        # items, then uses IO::Poll::_poll, implemented in XS, which
-        # modifies the array in place with the even elements being
-        # replaced with the event masks that occured.
-        my @poll;
-        while ( my ($fd, $sock) = each %DescriptorMap ) {
-            push @poll, $fd, $sock->{event_watch};
-        }
-
-        # if nothing to poll, either end immediately (if no timeout)
-        # or just keep calling the callback
-        unless (@poll) {
-            select undef, undef, undef, ($timeout / 1000);
-            return unless PostEventLoop();
-            next;
-        }
-
-        my $count = IO::Poll::_poll($timeout, @poll);
-        unless ($count >= 0) {
-            return unless PostEventLoop();
-            next;
-        }
-
-        # Fetch handles with read events
-        while (@poll) {
-            my ($fd, $state) = splice(@poll, 0, 2);
-            $DescriptorMap{$fd}->event_step if $state;
-        }
-
-        return unless PostEventLoop();
-    }
-
-    exit 0;
-}
-
 ### The kqueue-based event loop. Gets installed as EventLoop if IO::KQueue works
 ### okay.
 sub KQueueEventLoop {
@@ -411,14 +359,10 @@ retry:
         }
     }
     elsif ($HaveKQueue) {
-        my $f = $ev & EPOLLIN ? IO::KQueue::EV_ENABLE()
-                              : IO::KQueue::EV_DISABLE();
-        $KQueue->EV_SET($fd, IO::KQueue::EVFILT_READ(),
-                        IO::KQueue::EV_ADD() | $f);
-        $f = $ev & EPOLLOUT ? IO::KQueue::EV_ENABLE()
-                            : IO::KQueue::EV_DISABLE();
-        $KQueue->EV_SET($fd, IO::KQueue::EVFILT_WRITE(),
-                        IO::KQueue::EV_ADD() | $f);
+        my $f = $ev & EPOLLIN ? EV_ENABLE() : EV_DISABLE();
+        $KQueue->EV_SET($fd, EVFILT_READ(), EV_ADD() | $f);
+        $f = $ev & EPOLLOUT ? EV_ENABLE() : EV_DISABLE();
+        $KQueue->EV_SET($fd, EVFILT_WRITE(), EV_ADD() | $f);
     }
 
     Carp::cluck("PublicInbox::DS::new blowing away existing descriptor map for fd=$fd ($DescriptorMap{$fd})")
@@ -626,65 +570,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, IO::KQueue::EVFILT_READ(),
-                            $val ? IO::KQueue::EV_ENABLE() : IO::KQueue::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, IO::KQueue::EVFILT_WRITE(),
-                            $val ? IO::KQueue::EV_ENABLE() : IO::KQueue::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];