public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 1c3b970b7a332d3fd681fd885157005c70fea504 2445 bytes (raw)
$ git show p516-leak:lib/PublicInbox/DSKQXS.pm	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
# Copyright (C) 2019 all contributors <meta@public-inbox.org>
# Licensed the same as Danga::Socket (and Perl5)
# License: GPL-1.0+ or Artistic-1.0-Perl
#  <https://www.gnu.org/licenses/gpl-1.0.txt>
#  <https://dev.perl.org/licenses/artistic.html>
#
# kqueue support via IO::KQueue XS module.  This makes kqueue look
# like epoll to simplify the code in DS.pm.  This is NOT meant to be
# an all encompassing emulation of epoll via IO::KQueue, but just to
# support cases public-inbox-nntpd/httpd care about.
# A pure-Perl version using syscall() is planned, and it should be
# faster due to the lack of syscall overhead.
package PublicInbox::DSKQXS;
use strict;
use warnings;
use parent qw(IO::KQueue);
use parent qw(Exporter);
use IO::KQueue;
use PublicInbox::Syscall qw(EPOLLONESHOT EPOLLIN EPOLLOUT EPOLLET
	EPOLL_CTL_ADD EPOLL_CTL_MOD EPOLL_CTL_DEL);
our @EXPORT_OK = qw(epoll_ctl epoll_wait);
my $owner_pid = -1; # kqueue is close-on-fork (yes, fork, not exec)

# map EPOLL* bits to kqueue EV_* flags for EV_SET
sub kq_flag ($$) {
	my ($bit, $ev) = @_;
	if ($ev & $bit) {
		my $fl = EV_ENABLE;
		$fl |= EV_CLEAR if $fl & EPOLLET;

		# EV_DISPATCH matches EPOLLONESHOT semantics more closely
		# than EV_ONESHOT, in that EV_ADD is not required to
		# re-enable a disabled watch.
		($ev & EPOLLONESHOT) ? ($fl | EV_DISPATCH) : $fl;
	} else {
		EV_DISABLE;
	}
}

sub new {
	my ($class) = @_;
	die 'non-singleton use not supported' if $owner_pid == $$;
	$owner_pid = $$;
	$class->SUPER::new;
}

sub epoll_ctl {
	my ($self, $op, $fd, $ev) = @_;
	if ($op == EPOLL_CTL_MOD) {
		$self->EV_SET($fd, EVFILT_READ, kq_flag(EPOLLIN, $ev));
		$self->EV_SET($fd, EVFILT_WRITE, kq_flag(EPOLLOUT, $ev));
	} elsif ($op == EPOLL_CTL_DEL) {
		$self->EV_SET($fd, EVFILT_READ, EV_DISABLE);
		$self->EV_SET($fd, EVFILT_WRITE, EV_DISABLE);
	} else {
		$self->EV_SET($fd, EVFILT_READ, EV_ADD|kq_flag(EPOLLIN, $ev));
		$self->EV_SET($fd, EVFILT_WRITE, EV_ADD|kq_flag(EPOLLOUT, $ev));
	}
	0;
}

sub epoll_wait {
	my ($self, $maxevents, $timeout_msec, $events) = @_;
	@$events = eval { $self->kevent($timeout_msec) };
	if (my $err = $@) {
		# workaround https://rt.cpan.org/Ticket/Display.html?id=116615
		if ($err =~ /Interrupted system call/) {
			@$events = ();
		} else {
			die $err;
		}
	}
	# caller only cares for $events[$i]->[0]
	scalar(@$events);
}

sub DESTROY {
	my ($self) = @_;
	if ($owner_pid == $$) {
		POSIX::close($$self);
		$owner_pid = -1;
	}
}

1;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git