From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id ABC0A1F91B for ; Sat, 27 Jun 2020 10:04:03 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 18/34] ds: add_timer: allow passing arg to callback. Date: Sat, 27 Jun 2020 10:03:44 +0000 Message-Id: <20200627100400.9871-19-e@yhbt.net> In-Reply-To: <20200627100400.9871-1-e@yhbt.net> References: <20200627100400.9871-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This allows callers to avoid creating expensive closures. We no longer pass the `$now' value to callers, as none of the callers used it. --- lib/PublicInbox/DS.pm | 10 +++++----- lib/PublicInbox/FakeInotify.pm | 10 ++++------ lib/PublicInbox/WatchMaildir.pm | 15 ++++++--------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index c46b20cba27..a3f2e76c16a 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -95,18 +95,18 @@ sub SetLoopTimeout { return $LoopTimeout = $_[1] + 0; } -=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef ) >> +=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef, $arg) >> Add a timer to occur $seconds from now. $seconds may be fractional, but timers are not guaranteed to fire at the exact time you ask for. =cut -sub add_timer ($$) { - my ($secs, $coderef) = @_; +sub add_timer ($$;$) { + my ($secs, $coderef, $arg) = @_; my $fire_time = now() + $secs; - my $timer = [$fire_time, $coderef]; + my $timer = [$fire_time, $coderef, $arg]; if (!@Timers || $fire_time >= $Timers[-1][0]) { push @Timers, $timer; @@ -198,7 +198,7 @@ sub RunTimers { # Run expired timers while (@Timers && $Timers[0][0] <= $now) { my $to_run = shift(@Timers); - $to_run->[1]->($now) if $to_run->[1]; + $to_run->[1]->($to_run->[2]); } # timers may enqueue into nextq: diff --git a/lib/PublicInbox/FakeInotify.pm b/lib/PublicInbox/FakeInotify.pm index df63173f083..debd2d39ae5 100644 --- a/lib/PublicInbox/FakeInotify.pm +++ b/lib/PublicInbox/FakeInotify.pm @@ -16,16 +16,14 @@ my $poll_intvl = 2; # same as Filesys::Notify::Simple sub poll_once { my ($self) = @_; - sub { - eval { $self->poll }; - warn "E: FakeInotify->poll: $@\n" if $@; - PublicInbox::DS::add_timer($poll_intvl, poll_once($self)); - }; + eval { $self->poll }; + warn "E: FakeInotify->poll: $@\n" if $@; + PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $self); } sub new { my $self = bless { watch => {} }, __PACKAGE__; - PublicInbox::DS::add_timer($poll_intvl, poll_once($self)); + PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $self); $self; } diff --git a/lib/PublicInbox/WatchMaildir.pm b/lib/PublicInbox/WatchMaildir.pm index b82b51025e6..f36aa20aa33 100644 --- a/lib/PublicInbox/WatchMaildir.pm +++ b/lib/PublicInbox/WatchMaildir.pm @@ -549,8 +549,8 @@ sub watch_imap_fetch_all ($$) { } } -sub imap_fetch_fork ($$$) { - my ($self, $intvl, $uris) = @_; +sub imap_fetch_fork ($) { # DS::add_timer callback + my ($self, $intvl, $uris) = @{$_[0]}; return if $self->{quit}; watch_atfork_parent($self); defined(my $pid = fork) or die "fork: $!"; @@ -563,11 +563,6 @@ sub imap_fetch_fork ($$$) { PublicInbox::DS::dwaitpid($pid, \&imap_fetch_reap, $self); } -sub imap_fetch_cb ($$$) { - my ($self, $intvl, $uris) = @_; - sub { imap_fetch_fork($self, $intvl, $uris) }; -} - sub imap_fetch_reap { # PublicInbox::DS::dwaitpid callback my ($self, $pid) = @_; my $intvl_uris = delete $self->{poll_pids}->{$pid} or @@ -579,7 +574,8 @@ sub imap_fetch_reap { # PublicInbox::DS::dwaitpid callback map { $_->as_string."\n" } @$uris; } warn('I: will check ', $_->as_string, " in ${intvl}s\n") for @$uris; - PublicInbox::DS::add_timer($intvl, imap_fetch_cb($self, $intvl, $uris)); + PublicInbox::DS::add_timer($intvl, \&imap_fetch_fork, + [$self, $intvl, $uris]); } sub watch_imap_init ($) { @@ -625,7 +621,8 @@ sub watch_imap_init ($) { # poll all URIs for a given interval sequentially while (my ($intvl, $uris) = each %$poll) { - PublicInbox::DS::requeue(imap_fetch_cb($self, $intvl, $uris)); + PublicInbox::DS::add_timer(0, \&imap_fetch_fork, + [$self, $intvl, $uris]); } }