public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob face8edc3b3ae8ff2378e9f1bf6c79f414ba4018 1404 bytes (raw)
$ git show HEAD:lib/PublicInbox/Select.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
 
# Copyright (C) all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
#
# This makes select(2) look like epoll to simplify the code in DS.pm.
# Unlike IO::Select, it does NOT hold references to IO handles.
# This is NOT meant to be an all encompassing emulation of epoll
# via select, but only to support cases we care about.
package PublicInbox::Select;
use v5.12;
use PublicInbox::Syscall qw(EPOLLONESHOT EPOLLIN EPOLLOUT);
use Errno;

sub new { bless {}, __PACKAGE__ } # fd => events

sub ep_wait {
	my ($self, $msec, $events) = @_;
	my ($rvec, $wvec) = ('', ''); # we don't use EPOLLERR
	while (my ($fd, $ev) = each %$self) {
		vec($rvec, $fd, 1) = 1 if $ev & EPOLLIN;
		vec($wvec, $fd, 1) = 1 if $ev & EPOLLOUT;
	}
	@$events = ();
	my $to = $msec < 0 ? undef : ($msec/1000);
	my $n = select $rvec, $wvec, undef, $to or return; # timeout expired
	return if $n < 0 && $! == Errno::EINTR; # caller recalculates timeout
	die "select: $!" if $n < 0;
	while (my ($fd, $ev) = each %$self) {
		if (vec($rvec, $fd, 1) || vec($wvec, $fd, 1)) {
			delete($self->{$fd}) if $ev & EPOLLONESHOT;
			push @$events, $fd;
		}
	}
	$n == scalar(@$events) or
		warn "BUG? select() returned $n, but got ".scalar(@$events);
}

sub ep_del { delete($_[0]->{fileno($_[1])}); 0 }
sub ep_add { $_[0]->{fileno($_[1])} = $_[2]; 0 }

no warnings 'once';
*ep_mod = \&ep_add;

1;

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