]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/EvCleanup.pm
bundle Danga::Socket and Sys::Syscall
[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 use fields qw(rd);
10
11 my $ENABLED;
12 sub enabled { $ENABLED }
13 sub enable { $ENABLED = 1 }
14 my $singleton;
15 my $asapq = [ [], undef ];
16 my $nextq = [ [], undef ];
17 my $laterq = [ [], undef ];
18
19 sub once_init () {
20         my $self = fields::new('PublicInbox::EvCleanup');
21         my ($r, $w);
22
23         # This is a dummy pipe which is always writable so it can always
24         # fires in the next event loop iteration.
25         pipe($r, $w) or die "pipe: $!";
26         fcntl($w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
27         $self->SUPER::new($w);
28         $self->{rd} = $r; # never read, since we never write..
29         $self;
30 }
31
32 sub _run_all ($) {
33         my ($q) = @_;
34
35         my $run = $q->[0];
36         $q->[0] = [];
37         $q->[1] = undef;
38         $_->() foreach @$run;
39 }
40
41 # ensure PublicInbox::DS::ToClose fires after timers fire
42 sub _asap_close () { $asapq->[1] ||= _asap_timer() }
43
44 sub _run_asap () { _run_all($asapq) }
45 sub _run_next () {
46         _run_all($nextq);
47         _asap_close();
48 }
49
50 sub _run_later () {
51         _run_all($laterq);
52         _asap_close();
53 }
54
55 # Called by PublicInbox::DS
56 sub event_write {
57         my ($self) = @_;
58         $self->watch_write(0);
59         _run_asap();
60 }
61
62 sub _asap_timer () {
63         $singleton ||= once_init();
64         $singleton->watch_write(1);
65         1;
66 }
67
68 sub asap ($) {
69         my ($cb) = @_;
70         push @{$asapq->[0]}, $cb;
71         $asapq->[1] ||= _asap_timer();
72 }
73
74 sub next_tick ($) {
75         my ($cb) = @_;
76         push @{$nextq->[0]}, $cb;
77         $nextq->[1] ||= PublicInbox::DS->AddTimer(0, *_run_next);
78 }
79
80 sub later ($) {
81         my ($cb) = @_;
82         push @{$laterq->[0]}, $cb;
83         $laterq->[1] ||= PublicInbox::DS->AddTimer(60, *_run_later);
84 }
85
86 END {
87         _run_asap();
88         _run_all($nextq);
89         _run_all($laterq);
90 }
91
92 1;