From 0541761b0558317be1825710c1545f6e718103f2 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 12 Dec 2019 21:16:47 +0000 Subject: ds: move EvCleanup code into DS EvCleanup only existed since Danga::Socket was a separate component, and cleanup code belongs with the event loop. --- lib/PublicInbox/DS.pm | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'lib/PublicInbox/DS.pm') 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 { -- cgit v1.2.3-24-ge0c7