use parent qw(Exporter);
our @EXPORT_OK = qw(now msg_more write_in_full);
use warnings;
+use 5.010_001;
use PublicInbox::Syscall qw(:epoll);
use fields ('sock', # underlying socket
'wbuf', # arrayref of coderefs or GLOB refs
'wbuf_off', # offset into first element of wbuf to start writing at
- 'event_watch', # bitmask of events the client is interested in (POLLIN,OUT,etc.)
);
use Errno qw(EAGAIN EINVAL);
use Carp qw(croak confess);
use File::Temp qw(tempfile);
-use constant POLLIN => 1;
-use constant POLLOUT => 4;
-use constant POLLERR => 8;
-use constant POLLHUP => 16;
-use constant POLLNVAL => 32;
-
-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 {
return $keep_running;
}
+# map EPOLL* bits to kqueue EV_* flags for EV_SET
+sub kq_flag ($$) {
+ my ($bit, $ev) = @_;
+ if ($ev & $bit) {
+ my $fl = EV_ADD() | EV_ENABLE();
+ ($ev & EPOLLONESHOT) ? ($fl|EV_ONESHOT()) : $fl;
+ } else {
+ EV_DISABLE();
+ }
+}
+
#####################################################################
### PublicInbox::DS-the-object code
#####################################################################
=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} = POLLERR|POLLHUP|POLLNVAL;
-
_InitPoller();
if ($HaveEpoll) {
- if ($exclusive) {
- $ev = $self->{event_watch} = EPOLLIN|EPOLLERR|EPOLLHUP|$EPOLLEXCLUSIVE;
- }
retry:
if (epoll_ctl($Epoll, EPOLL_CTL_ADD, $fd, $ev)) {
- if ($! == EINVAL && ($ev & $EPOLLEXCLUSIVE)) {
- $EPOLLEXCLUSIVE = 0; # old kernel
- $ev = $self->{event_watch} = EPOLLIN|EPOLLERR|EPOLLHUP;
+ if ($! == EINVAL && ($ev & EPOLLEXCLUSIVE)) {
+ $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());
+ $KQueue->EV_SET($fd, EVFILT_READ(), EV_ADD() | kq_flag(EPOLLIN, $ev));
+ $KQueue->EV_SET($fd, EVFILT_WRITE(), EV_ADD() | kq_flag(EPOLLOUT, $ev));
}
Carp::cluck("PublicInbox::DS::new blowing away existing descriptor map for fd=$fd ($DescriptorMap{$fd})")
}
} elsif ($! == EAGAIN) {
$self->{wbuf_off} = $off;
- watch_write($self, 1);
+ watch($self, EPOLLOUT|EPOLLONESHOT);
return 0;
} else {
return $self->close;
} # while @$wbuf
delete $self->{wbuf};
- $self->watch_write(0);
1; # all done
}
return $self->close;
}
$self->{wbuf} = [ tmpbuf($bref, $written) ];
- watch_write($self, 1);
+ watch($self, EPOLLOUT|EPOLLONESHOT);
return 0;
}
}
# queue up the unwritten substring:
$self->{wbuf} = [ tmpbuf(\($_[1]), $n) ];
- watch_write($self, 1);
+ watch($self, EPOLLOUT|EPOLLONESHOT);
return 0;
}
}
$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 ($$) {
+ my ($self, $ev) = @_;
my $sock = $self->{sock} or return;
-
- my $val = shift;
- my $event = $self->{event_watch};
-
- $event &= ~POLLIN if ! $val;
- $event |= POLLIN if $val;
-
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, $ev) and
+ confess("EPOLL_CTL_MOD $!");
+ } elsif ($HaveKQueue) {
+ $KQueue->EV_SET($fd, EVFILT_READ(), kq_flag(EPOLLIN, $ev));
+ $KQueue->EV_SET($fd, EVFILT_WRITE(), kq_flag(EPOLLOUT, $ev));
}
}
-=head2 C<< $obj->watch_write( $boolean ) >>
-
-Turn 'writable' event notification on or off.
-
-=cut
-sub watch_write {
- my PublicInbox::DS $self = shift;
- my $sock = $self->{sock} or return;
-
- my $val = shift;
- my $event = $self->{event_watch};
-
- $event &= ~POLLOUT if ! $val;
- $event |= POLLOUT if $val;
- my $fd = fileno($sock);
-
- # 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;
- }
-}
+sub watch_in1 ($) { watch($_[0], EPOLLIN | EPOLLONESHOT) }
package PublicInbox::DS::Timer;
# [$abs_float_firetime, $coderef];