line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Proc::PidChange; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: execute callbacks when PID changes |
4
|
|
|
|
|
|
|
our $VERSION = '0.002'; # VERSION |
5
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:MELO'; # AUTHORITY |
6
|
|
|
|
|
|
|
|
7
|
5
|
|
|
5
|
|
198444
|
use strict; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
181
|
|
8
|
5
|
|
|
5
|
|
28
|
use warnings; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
142
|
|
9
|
5
|
|
|
5
|
|
4436
|
use parent 'Exporter'; |
|
5
|
|
|
|
|
1622
|
|
|
5
|
|
|
|
|
28
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
### Export setup |
12
|
|
|
|
|
|
|
our @EXPORT = 'check_current_pid'; |
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw( check_current_pid register_pid_change_callback unregister_pid_change_callback ); |
14
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
15
|
|
|
|
|
|
|
all => \@EXPORT_OK, |
16
|
|
|
|
|
|
|
registry => [qw(register_pid_change_callback unregister_pid_change_callback)], |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
## Flag: if true, no need to pool for changes |
20
|
|
|
|
|
|
|
our $RT = 0; |
21
|
|
|
|
|
|
|
unless ($ENV{PROC_PIDCHANGE_NO_RT}) { |
22
|
|
|
|
|
|
|
eval { |
23
|
|
|
|
|
|
|
require POSIX::AtFork; |
24
|
|
|
|
|
|
|
POSIX::AtFork->import(); |
25
|
|
|
|
|
|
|
POSIX::AtFork->add_to_child(\&check_current_pid); |
26
|
|
|
|
|
|
|
$RT++; |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
### Our implementation |
32
|
|
|
|
|
|
|
{ |
33
|
|
|
|
|
|
|
### Our state |
34
|
|
|
|
|
|
|
our $last_checked_pid; |
35
|
5
|
|
|
5
|
|
2187
|
BEGIN { $last_checked_pid = $$ } |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
our @callbacks; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
### Check for pid changes |
40
|
|
|
|
|
|
|
sub check_current_pid { |
41
|
7
|
100
|
|
7
|
1
|
52752
|
return if $last_checked_pid == $$; |
42
|
2
|
|
|
|
|
105
|
$last_checked_pid = $$; |
43
|
2
|
|
|
|
|
144
|
return _call_all_callbacks(); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub _call_all_callbacks { |
47
|
7
|
|
|
7
|
|
218
|
$_->() for @callbacks; |
48
|
7
|
|
|
|
|
337
|
return; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
### Callback registry API |
52
|
|
|
|
|
|
|
sub register_pid_change_callback { |
53
|
6
|
|
|
6
|
1
|
18418
|
push @callbacks, grep { ref($_) eq 'CODE' } @_; |
|
20
|
|
|
|
|
54
|
|
54
|
6
|
|
|
|
|
15
|
return; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub unregister_pid_change_callback { |
58
|
2
|
|
|
2
|
1
|
3032
|
my %targets = map { $_ => 1 } grep { ref($_) eq 'CODE' } @_; |
|
4
|
|
|
|
|
17
|
|
|
4
|
|
|
|
|
13
|
|
59
|
2
|
|
|
|
|
6
|
@callbacks = grep { !$targets{$_} } @callbacks; |
|
7
|
|
|
|
|
20
|
|
60
|
|
|
|
|
|
|
|
61
|
2
|
|
|
|
|
7
|
return; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
1; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |