public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 4301edffc543becf45adb2bbef3001c33135d431 696 bytes (raw)
$ git show HEAD:lib/PublicInbox/OnDestroy.pm	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
# Copyright (C) all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

package PublicInbox::OnDestroy;
use v5.12;
use parent qw(Exporter);
use autodie qw(fork);
our @EXPORT = qw(on_destroy);
our $fork_gen = 0;

# either parent or child is expected to exit or exec shortly after this:
sub fork_tmp () {
	my $pid = fork;
	++$fork_gen if $pid == 0;
	$pid;
}

# all children
sub all (@) { bless [ undef, @_ ], __PACKAGE__ }

# same process
sub on_destroy (@) { bless [ $fork_gen, @_ ], __PACKAGE__ }

sub cancel { @{$_[0]} = () }

sub DESTROY {
	my ($fgen, $cb, @args) = @{$_[0]};
	$cb->(@args) if ($cb && ($fgen // $fork_gen) == $fork_gen);
}

1;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git