about summary refs log tree commit homepage
path: root/lib/PublicInbox/DS.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-12-12 21:16:47 +0000
committerEric Wong <e@80x24.org>2019-12-14 21:13:48 +0000
commit0541761b0558317be1825710c1545f6e718103f2 (patch)
treeccf61ca0cf7bf988adcc7990e92592be74ea1f46 /lib/PublicInbox/DS.pm
parent8e02ffaf89afe2b39bf9618657de29bbff65c890 (diff)
downloadpublic-inbox-0541761b0558317be1825710c1545f6e718103f2.tar.gz
EvCleanup only existed since Danga::Socket was a separate
component, and cleanup code belongs with the event loop.
Diffstat (limited to 'lib/PublicInbox/DS.pm')
-rw-r--r--lib/PublicInbox/DS.pm27
1 files changed, 22 insertions, 5 deletions
diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index 301ec057..7eb0aada 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -41,7 +41,8 @@ require File::Spec;
 
 my $nextq; # queue for next_tick
 my $WaitPids; # list of [ pid, callback, callback_arg ]
-my $reap_timer;
+my $later_queue; # callbacks
+my ($later_timer, $reap_timer);
 our (
      %DescriptorMap,             # fd (num) -> PublicInbox::DS object
      $Epoll,                     # Global epoll fd (or DSKQXS ref)
@@ -71,7 +72,8 @@ sub Reset {
     %DescriptorMap = ();
     $nextq = [];
     $WaitPids = [];
-    $reap_timer = undef;
+    $later_queue = [];
+    $reap_timer = $later_timer = undef;
     @ToClose = ();
     $LoopTimeout = -1;  # no timeout by default
     @Timers = ();
@@ -250,9 +252,11 @@ sub reap_pids {
 # reentrant SIGCHLD handler (since reap_pids is not reentrant)
 sub enqueue_reap ($) { push @$nextq, \&reap_pids };
 
+sub running () { ($SIG{CHLD} // '') eq \&enqueue_reap }
+
 sub EpollEventLoop {
     local $in_loop = 1;
-    while (1) {
+    do {
         my @events;
         my $i;
         my $timeout = RunTimers();
@@ -266,8 +270,8 @@ sub EpollEventLoop {
             # in that event.
             $DescriptorMap{$events[$i]->[0]}->event_step;
         }
-        return unless PostEventLoop();
-    }
+    } while (PostEventLoop());
+    _run_later();
 }
 
 =head2 C<< CLASS->SetPostLoopCallback( CODEREF ) >>
@@ -640,6 +644,19 @@ sub dwaitpid ($$$) {
     }
 }
 
+sub _run_later () {
+    my $run = $later_queue;
+    $later_timer = undef;
+    $later_queue = [];
+    $_->() for @$run;
+}
+
+sub later ($) {
+    my ($cb) = @_;
+    push @$later_queue, $cb;
+    $later_timer //= AddTimer(undef, 60, \&_run_later);
+}
+
 package PublicInbox::DS::Timer;
 # [$abs_float_firetime, $coderef];
 sub cancel {