1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # Licensed the same as Danga::Socket (and Perl5)
3 # License: GPL-1.0+ or Artistic-1.0-Perl
4 # <https://www.gnu.org/licenses/gpl-1.0.txt>
5 # <https://dev.perl.org/licenses/artistic.html>
7 # kqueue support via IO::KQueue XS module. This makes kqueue look
8 # like epoll to simplify the code in DS.pm. This is NOT meant to be
9 # an all encompassing emulation of epoll via IO::KQueue, but just to
10 # support cases public-inbox-nntpd/httpd care about.
11 # A pure-Perl version using syscall() is planned, and it should be
12 # faster due to the lack of syscall overhead.
13 package PublicInbox::DSKQXS;
16 use parent qw(IO::KQueue);
17 use parent qw(Exporter);
19 use PublicInbox::Syscall qw(EPOLLONESHOT EPOLLIN EPOLLOUT EPOLL_CTL_DEL);
20 our @EXPORT = qw(epoll_ctl epoll_wait);
21 my $owner_pid = -1; # kqueue is close-on-fork (yes, fork, not exec)
23 # map EPOLL* bits to kqueue EV_* flags for EV_SET
27 my $fl = EV_ADD | EV_ENABLE;
28 ($ev & EPOLLONESHOT) ? ($fl | EV_ONESHOT) : $fl;
36 die 'non-singleton use not supported' if $owner_pid == $$;
42 my ($self, $op, $fd, $ev) = @_;
43 if ($op != EPOLL_CTL_DEL) {
44 $self->EV_SET($fd, EVFILT_READ, kq_flag(EPOLLIN, $ev));
45 $self->EV_SET($fd, EVFILT_WRITE, kq_flag(EPOLLOUT, $ev));
51 my ($self, $maxevents, $timeout_msec, $events) = @_;
52 @$events = eval { $self->kevent($timeout_msec) };
54 # workaround https://rt.cpan.org/Ticket/Display.html?id=116615
55 if ($err =~ /Interrupted system call/) {
61 # caller only cares for $events[$i]->[0]
67 if ($owner_pid == $$) {