user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 5/7] dspoll: switch to the documented IO::Poll API
Date: Mon, 11 Sep 2023 09:41:30 +0000	[thread overview]
Message-ID: <20230911094132.75792-6-e@80x24.org> (raw)
In-Reply-To: <20230911094132.75792-1-e@80x24.org>

IO::Poll::_poll has always been an undocumented API.  While it's
remained working so far (since the early 2000s with Danga::Socket),
I'm uncomfortable continuing with it moving forward since it's
not documented (the leading underscore typically means it's
not meant to be used by 3rd-parties).

So switch to the documented API and just learn to live with some
redundant object references and awkwardness in the API.
---
 lib/PublicInbox/DSPoll.pm | 43 +++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 22 deletions(-)

diff --git a/lib/PublicInbox/DSPoll.pm b/lib/PublicInbox/DSPoll.pm
index fc282de0..8ab5d19f 100644
--- a/lib/PublicInbox/DSPoll.pm
+++ b/lib/PublicInbox/DSPoll.pm
@@ -13,34 +13,33 @@ use v5.12;
 use IO::Poll;
 use PublicInbox::Syscall qw(EPOLLONESHOT EPOLLIN EPOLLOUT);
 
-sub new { bless {}, __PACKAGE__ } # fd => events
+sub new { bless { poll => IO::Poll->new }, __PACKAGE__ } # fd => events
 
 sub ep_wait {
 	my ($self, $maxevents, $timeout_msec, $events) = @_;
-	my @pset;
-	while (my ($fd, $events) = each %$self) {
-		my $pevents = $events & EPOLLIN ? POLLIN : 0;
-		$pevents |= $events & EPOLLOUT ? POLLOUT : 0;
-		push(@pset, $fd, $pevents);
-	}
-	@$events = ();
-	my $n = IO::Poll::_poll($timeout_msec, @pset);
-	if ($n >= 0) {
-		for (my $i = 0; $i < @pset; ) {
-			my $fd = $pset[$i++];
-			my $revents = $pset[$i++] or next;
-			delete($self->{$fd}) if $self->{$fd} & EPOLLONESHOT;
-			push @$events, $fd;
-		}
-		my $nevents = scalar @$events;
-		if ($n != $nevents) {
-			warn "BUG? poll() returned $n, but got $nevents";
-		}
+	$self->{poll}->poll($timeout_msec/1000) > 0 or return (@$events = ());
+	my @io = $self->{poll}->handles(POLLIN|POLLOUT);
+	@$events = map { fileno($_) } @io;
+	for (@$events) {
+		my $io = shift @io;
+		$self->{poll}->remove($io) if delete($self->{oneshot}->{$_});
 	}
 }
 
-sub ep_del { delete($_[0]->{fileno($_[1])}); 0 }
-sub ep_add { $_[0]->{fileno($_[1])} = $_[2]; 0 }
+sub ep_del {
+	my ($self, $io) = @_;
+	delete $self->{oneshot}->{fileno($io)};
+	$self->{poll}->remove($io);
+	0;
+}
+
+sub ep_add {
+	my ($self, $io, $ev) = @_;
+	$self->{oneshot}->{fileno($io)} = 1 if $ev & EPOLLONESHOT;
+	$self->{poll}->mask($io, ($ev & EPOLLIN ? POLLIN : 0) |
+				($ev & EPOLLOUT ? POLLOUT : 0));
+	0;
+}
 
 no warnings 'once';
 *ep_mod = \&ep_add;

  parent reply	other threads:[~2023-09-11  9:41 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 ` Eric Wong [this message]
2023-09-12  2:34   ` [PATCH 5/7] dspoll: switch to the documented IO::Poll API Eric Wong
2023-09-12  6:13     ` [PATCH 8/7] provide select(2) backend for PublicInbox::DS Eric Wong
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=20230911094132.75792-6-e@80x24.org \
    --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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

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).