]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/EvCleanup.pm
d60ac2cc8c88d3de63288c2d51d517b796e65c1d
[public-inbox.git] / lib / PublicInbox / EvCleanup.pm
1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # event cleanups (currently for PublicInbox::DS)
5 package PublicInbox::EvCleanup;
6 use strict;
7 use warnings;
8 use base qw(PublicInbox::DS);
9
10 my $ENABLED;
11 sub enabled { $ENABLED }
12 sub enable { $ENABLED = 1 }
13 my $singleton;
14 my $asapq = [ [], undef ];
15 my $nextq = [ [], undef ];
16 my $laterq = [ [], undef ];
17
18 sub once_init () {
19         my $self = fields::new('PublicInbox::EvCleanup');
20         my ($r, $w);
21
22         # This is a dummy pipe which is always writable so it can always
23         # fires in the next event loop iteration.
24         pipe($r, $w) or die "pipe: $!";
25         fcntl($w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
26         $self->SUPER::new($w, 0);
27
28         # always writable, since PublicInbox::EvCleanup::event_step
29         # never drains wbuf.  We can avoid wasting a hash slot by
30         # stuffing the read-end of the pipe into the never-to-be-touched
31         # wbuf
32         $self->{wbuf} = $r;
33         $self;
34 }
35
36 sub _run_all ($) {
37         my ($q) = @_;
38
39         my $run = $q->[0];
40         $q->[0] = [];
41         $q->[1] = undef;
42         $_->() foreach @$run;
43 }
44
45 # ensure PublicInbox::DS::ToClose processing after timers fire
46 sub _asap_close () { $asapq->[1] ||= _asap_timer() }
47
48 sub _run_asap () { _run_all($asapq) }
49 sub _run_next () {
50         _run_all($nextq);
51         _asap_close();
52 }
53
54 sub _run_later () {
55         _run_all($laterq);
56         _asap_close();
57 }
58
59 # Called by PublicInbox::DS
60 sub event_step {
61         my ($self) = @_;
62         $self->watch_write(0);
63         _run_asap();
64 }
65
66 sub _asap_timer () {
67         $singleton ||= once_init();
68         $singleton->watch_write(1);
69         1;
70 }
71
72 sub asap ($) {
73         my ($cb) = @_;
74         push @{$asapq->[0]}, $cb;
75         $asapq->[1] ||= _asap_timer();
76 }
77
78 sub next_tick ($) {
79         my ($cb) = @_;
80         push @{$nextq->[0]}, $cb;
81         $nextq->[1] ||= PublicInbox::DS->AddTimer(0, *_run_next);
82 }
83
84 sub later ($) {
85         my ($cb) = @_;
86         push @{$laterq->[0]}, $cb;
87         $laterq->[1] ||= PublicInbox::DS->AddTimer(60, *_run_later);
88 }
89
90 END {
91         _run_asap();
92         _run_all($nextq);
93         _run_all($laterq);
94 }
95
96 1;