From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 8/7] provide select(2) backend for PublicInbox::DS
Date: Tue, 12 Sep 2023 06:13:04 +0000 [thread overview]
Message-ID: <20230912061304.M916432@dcvr> (raw)
In-Reply-To: <20230912023402.M761405@dcvr>
This is safer than relying on an internal API of IO::Poll
and doesn't create extra references to IO globs like the
public one.
---
Not sure if this is redundant or not, or if perhaps dropping
DSPoll would be preferable, even...
MANIFEST | 2 ++
lib/PublicInbox/DS.pm | 6 +++---
lib/PublicInbox/Select.pm | 40 +++++++++++++++++++++++++++++++++++++++
t/select.t | 4 ++++
4 files changed, 49 insertions(+), 3 deletions(-)
create mode 100644 lib/PublicInbox/Select.pm
create mode 100644 t/select.t
diff --git a/MANIFEST b/MANIFEST
index d7a670b8..63287bad 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -331,6 +331,7 @@ lib/PublicInbox/SearchIdxShard.pm
lib/PublicInbox/SearchQuery.pm
lib/PublicInbox/SearchThread.pm
lib/PublicInbox/SearchView.pm
+lib/PublicInbox/Select.pm
lib/PublicInbox/SharedKV.pm
lib/PublicInbox/Sigfd.pm
lib/PublicInbox/Smsg.pm
@@ -578,6 +579,7 @@ t/run.perl
t/search-amsg.eml
t/search-thr-index.t
t/search.t
+t/select.t
t/sha.t
t/shared_kv.t
t/sigfd.t
diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index b3edc094..d47df491 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -31,7 +31,7 @@ use Scalar::Util qw(blessed);
use PublicInbox::Syscall qw(%SIGNUM
EPOLLIN EPOLLOUT EPOLLONESHOT EPOLLEXCLUSIVE);
use PublicInbox::Tmpfile;
-use PublicInbox::DSPoll;
+use PublicInbox::Select;
use Errno qw(EAGAIN EINVAL ECHILD EINTR);
use Carp qw(carp croak);
our @EXPORT_OK = qw(now msg_more awaitpid add_timer add_uniq_timer);
@@ -43,7 +43,7 @@ my $reap_armed;
my $ToClose; # sockets to close when event loop is done
our (
%DescriptorMap, # fd (num) -> PublicInbox::DS object
- $Poller, # global Epoll, DSPoll, or DSKQXS ref
+ $Poller, # global Select, Epoll, DSPoll, or DSKQXS ref
@post_loop_do, # subref + args to call at the end of each loop
@@ -83,7 +83,7 @@ sub Reset {
$reap_armed = undef;
$LoopTimeout = -1; # no timeout by default
- $Poller = PublicInbox::DSPoll->new;
+ $Poller = PublicInbox::Select->new;
}
=head2 C<< CLASS->SetLoopTimeout( $timeout ) >>
diff --git a/lib/PublicInbox/Select.pm b/lib/PublicInbox/Select.pm
new file mode 100644
index 00000000..9df3a6bd
--- /dev/null
+++ b/lib/PublicInbox/Select.pm
@@ -0,0 +1,40 @@
+# 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);
+
+sub new { bless {}, __PACKAGE__ } # fd => events
+
+sub ep_wait {
+ my ($self, $maxevents, $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 $n = select($rvec, $wvec, undef, $msec < 0 ? undef : ($msec/1000));
+ return 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;
diff --git a/t/select.t b/t/select.t
new file mode 100644
index 00000000..e8032c5a
--- /dev/null
+++ b/t/select.t
@@ -0,0 +1,4 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+use v5.12;
+local $ENV{TEST_IOPOLLER} = 'PublicInbox::Select';
+require './t/ds-poll.t';
next prev parent reply other threads:[~2023-09-12 6:13 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-11 9:41 [PATCH 0/7] system-related updates and cleanups Eric Wong
2023-09-11 9:41 ` [PATCH 1/7] tests: map CLOFORK->FD_CLOEXEC temporarily for `tail -f' Eric Wong
2023-09-11 9:41 ` [PATCH 2/7] daemon: depend on DS event_loop in master process, too Eric Wong
2023-09-11 9:41 ` [PATCH 3/7] ds: use object-oriented API for epoll Eric Wong
2023-09-11 9:41 ` [PATCH 4/7] favor poll(2) for most daemons Eric Wong
2023-09-11 9:41 ` [PATCH 5/7] dspoll: switch to the documented IO::Poll API Eric Wong
2023-09-12 2:34 ` Eric Wong
2023-09-12 6:13 ` Eric Wong [this message]
2023-09-11 9:41 ` [PATCH 6/7] ds: use constants for @UNBLOCKABLE list Eric Wong
2023-09-11 9:41 ` [PATCH 7/7] spawn: do not block ABRT/BUS/ILL/SEGV signals Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230912061304.M916432@dcvr \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).