about summary refs log tree commit homepage
path: root/lib/PublicInbox/PktOp.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-03-28 09:01:13 +0000
committerEric Wong <e@80x24.org>2021-03-28 23:01:36 +0000
commit954581b8e575966a8bddc35e3b23d81d16a52833 (patch)
treed5e87f75313f827411796d82871fd6b1d5388e9c /lib/PublicInbox/PktOp.pm
parent29792d70a5d8305f68521664a7fa2e0fe54ff291 (diff)
downloadpublic-inbox-954581b8e575966a8bddc35e3b23d81d16a52833.tar.gz
Provide a consistent ->op_wait_event method instead of
forcing callers to loop (or not) at each callsite.
This also avoid a leak possibility by avoiding circular
references.
Diffstat (limited to 'lib/PublicInbox/PktOp.pm')
-rw-r--r--lib/PublicInbox/PktOp.pm20
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/PublicInbox/PktOp.pm b/lib/PublicInbox/PktOp.pm
index 5d8e78ea..c3221735 100644
--- a/lib/PublicInbox/PktOp.pm
+++ b/lib/PublicInbox/PktOp.pm
@@ -16,21 +16,23 @@ use PublicInbox::IPC qw(ipc_freeze ipc_thaw);
 our @EXPORT_OK = qw(pkt_do);
 
 sub new {
-        my ($cls, $r, $ops) = @_;
-        my $self = bless { sock => $r, ops => $ops }, $cls;
+        my ($cls, $r) = @_;
+        my $self = bless { sock => $r }, $cls;
         if ($PublicInbox::DS::in_loop) { # iff using DS->EventLoop
                 $r->blocking(0);
                 $self->SUPER::new($r, EPOLLIN|EPOLLET);
+        } else {
+                $self->{blocking} = 1;
         }
         $self;
 }
 
 # returns a blessed object as the consumer, and a GLOB/IO for the producer
 sub pair {
-        my ($cls, $ops) = @_;
+        my ($cls) = @_;
         my ($c, $p);
         socketpair($c, $p, AF_UNIX, SOCK_SEQPACKET, 0) or die "socketpair: $!";
-        (new($cls, $c, $ops), $p);
+        (new($cls, $c), $p);
 }
 
 sub pkt_do { # for the producer to trigger event_step in consumer
@@ -41,7 +43,7 @@ sub pkt_do { # for the producer to trigger event_step in consumer
 sub close {
         my ($self) = @_;
         my $c = $self->{sock} or return;
-        $c->blocking ? delete($self->{sock}) : $self->SUPER::close;
+        $self->{blocking} ? delete($self->{sock}) : $self->SUPER::close;
 }
 
 sub event_step {
@@ -73,4 +75,12 @@ sub event_step {
         }
 }
 
+# call this when we're ready to wait on events,
+# returns immediately if non-blocking
+sub op_wait_event {
+        my ($self, $ops) = @_;
+        $self->{ops} = $ops;
+        while ($self->{blocking} && $self->{sock}) { event_step($self) }
+}
+
 1;