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.
@Timers, # timers
);
-# this may be set to zero with old kernels
-our $EPOLLEXCLUSIVE = EPOLLEXCLUSIVE;
Reset();
#####################################################################
*EventLoop = *EpollEventLoop;
}
}
-
- if (!$HaveEpoll && !$HaveKQueue) {
- require IO::Poll;
- *EventLoop = *PollEventLoop;
- }
}
=head2 C<< CLASS->EventLoop() >>
EpollEventLoop($class);
} elsif ($HaveKQueue) {
KQueueEventLoop($class);
- } else {
- PollEventLoop($class);
}
}
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 {
=cut
sub new {
- my ($self, $sock, $exclusive) = @_;
+ my ($self, $sock, $ev) = @_;
$self = fields::new($self) unless ref $self;
$self->{sock} = $sock;
Carp::cluck("undef sock and/or fd in PublicInbox::DS->new. sock=" . ($sock || "") . ", fd=" . ($fd || ""))
unless $sock && $fd;
- my $ev = $self->{event_watch} = 0;
+ $self->{event_watch} = $ev;
_InitPoller();
if ($HaveEpoll) {
- if ($exclusive) {
- $ev = $self->{event_watch} = EPOLLIN|$EPOLLEXCLUSIVE;
- }
retry:
if (epoll_ctl($Epoll, EPOLL_CTL_ADD, $fd, $ev)) {
- if ($! == EINVAL && ($ev & $EPOLLEXCLUSIVE)) {
- $EPOLLEXCLUSIVE = 0; # old kernel
- $ev = $self->{event_watch} = EPOLLIN;
+ if ($! == EINVAL && ($ev & EPOLLEXCLUSIVE)) {
+ $self->{event_watch} = ($ev &= ~EPOLLEXCLUSIVE);
goto retry;
}
die "couldn't add epoll watch for $fd: $!\n";
}
}
elsif ($HaveKQueue) {
- # Add them to the queue but disabled for now
- $KQueue->EV_SET($fd, IO::KQueue::EVFILT_READ(),
- IO::KQueue::EV_ADD() | IO::KQueue::EV_DISABLE());
- $KQueue->EV_SET($fd, IO::KQueue::EVFILT_WRITE(),
- IO::KQueue::EV_ADD() | IO::KQueue::EV_DISABLE());
+ 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})")
$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];