| 1 | # Async child handling for RPC::Switch::Client::Tiny | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | package RPC::Switch::Client::Tiny::Async; | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 23 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 23 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 23 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 23 | ▶ | use POSIX ":sys_wait_h"; # WNOHANG | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 23 | our $VERSION = 1.20; | |||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 69 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 69 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 69 | return bless { | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | %args, | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | jobqueue => [], # queued async messages | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | jobs => {}, # active async childs | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | finished => {}, # finished childs to reap | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 152 | ▶ | sub child_stop { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
child_stop: called CC=9 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 152 | my ($self, $pid, $status) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 152 | my $child = $self->{finished}{$pid}; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | 152 | my ($rc, $sig, $core) = ($status >> 8, $status & 127, $status & 128); | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 152 | my $stoptime = sprintf "%.02f", time() - $child->{start}; | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 152 | ▶ | my %runtime = (exists $child->{runtime}) ? (runtime => $child->{runtime}) : (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 152 | ▶ | my %id = (exists $child->{id}) ? (id => $child->{id}) : (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 152 | my %reason = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 152 | ▶ | if ($^O eq 'MSWin32') { # cpantester: strawberry perl does not support WIF calls | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | %reason = (reason => $child->{state}); | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | } elsif (WIFSTOPPED($status)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | warn "worker child $pid stopped\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | } elsif (WIFSIGNALED($status)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 4 | %reason = (reason => "killed by signal $sig"); | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | ▶ | } elsif (WIFEXITED($status) && $rc) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
WIFEXITED($status) and $rc
MC/DC50%1/2
WIFEXITED($status) and $rc
WIFEXITED($status)
$rc
Truth table66%2/3
A and B
A= WIFEXITED($status)B= $rc
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 14 | %reason = (reason => "exited with status $rc"); | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 134 | %reason = (reason => $child->{state}); | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 152 | ▶ | $self->{trace_cb}->('END', {pid => $pid, %id, %runtime, stoptime => $stoptime, %reason}) if $self->{trace_cb}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 152 | delete $self->{finished}{$pid}; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 152 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 621 | ▶ | sub childs_reap { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
childs_reap: called CC=13 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 621 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 621 | ▶ | my $flags = $opts{nonblock} ? WNOHANG : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # A child is moved to the finished state in these cases: | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # 1) The child sent a valid reply on the pipe (done) | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # 2) The child sent an invalid reply or the pipe closed (error) | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # 3) The worker closes the pipe after rpcswitch.channel_gone (gone) | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # 4) The worker closes the pipe after rpcswitch socket closed (stopped) | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 621 | foreach my $child (values %{$self->{finished}}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 530 | my $res = waitpid($child->{pid}, $flags); | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 530 | ▶ | if ($res == 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 378 | my $waittime = time() - $child->{start}; | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # First try to send maskable signal to give child a | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # chance to cleanup resources, and send nonmaskable | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # sigkill to terminate child only after that. | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # (note: some nfs-syscalls might block nevertheless) | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # (note: sigkill on windows might deadlock - see below) | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 378 | ▶ | if (($waittime > 2) && ($child->{state} ne 'kill') && ($^O ne 'MSWin32')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$waittime > 2 and $child->{"state"} ne "kill" and $^O ne "MSWin32"
$waittime > 2 and $child->{"state"} ne "kill"
MC/DC0%0/3
$waittime > 2 and $child->{"state"} ne "kill" and $^O ne "MSWin32"
$waittime > 2
$child->{"state"} ne "kill"
$^O ne "MSWin32"
Truth table25%1/4
A and B and C
A= $waittime > 2B= $child->{"state"} ne "kill"C= $^O ne "MSWin32"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | warn "worker child $child->{pid}: still running after $child->{state} - send kill\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | kill 'KILL', $child->{pid}; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $child->{state} = 'kill'; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | ▶ | } elsif (($waittime > 1) && ($child->{state} ne 'term')) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$waittime > 1 and $child->{"state"} ne "term"
MC/DC0%0/2
$waittime > 1 and $child->{"state"} ne "term"
$waittime > 1
$child->{"state"} ne "term"
Truth table33%1/3
A and B
A= $waittime > 1B= $child->{"state"} ne "term"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | warn "worker child $child->{pid}: still running after $child->{state} - send term\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | kill 'TERM', $child->{pid}; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | $child->{state} = 'term'; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } elsif ($res < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return 0 if (($flags == 0) && $!{EINTR}); # blocking waitpid might be interrupted by signal | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$flags == 0 and $!{"EINTR"}
MC/DC0%0/2
$flags == 0 and $!{"EINTR"}
$flags == 0
$!{"EINTR"}
Truth table0%0/3
A and B
A= $flags == 0B= $!{"EINTR"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | warn "worker child $child->{pid}: disappeared" unless ($^O eq 'MSWin32'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $self->child_stop($child->{pid}, 0); | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 152 | $self->child_stop($child->{pid}, $?); | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 621 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # For perl on windows the perlfork emulation for threading systems | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # will be used internally (see: https://perldoc.perl.org/perlfork). | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # The description of kill() notes that using sig-KILL the process | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # which implements the pseudo-processes can be blocked and the perl | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # interpreter hangs. | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # -> this behavior is seen even on current win10/2023 systems with | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # an interpreter like strawberry-perl-5.32.1-64bit (cpan testers). | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # This should not happen for sig-TERM, where the perlfork-emulation | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # will not try to wait on childs and leak resources to avoid deadlocks. | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # So the Async module should be avoided on windows, because it will | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # not work as desired. | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub _childs_term { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_childs_term: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | foreach my $child (values %{$self->{finished}}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($child->{state} ne 'term') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | warn "worker child $child->{pid}: still running after $child->{state} - force term\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | kill 'TERM', $child->{pid}; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $child->{state} = 'term'; | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 96 | ▶ | sub childs_kill { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
childs_kill: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 96 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 96 | ▶ | return $self->_childs_term() if ($^O eq 'MSWin32'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 96 | foreach my $child (values %{$self->{finished}}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | ▶ | if ($child->{state} ne 'kill') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 4 | kill 'KILL', $child->{pid}; # terminate non maskable | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 4 | $child->{state} = 'kill'; | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 96 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 182 | ▶ | sub job_add { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
job_add: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 182 | my ($self, $child, $msg_id, $meta) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 182 | $child->{id} = $msg_id; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | 182 | $child->{start} = time(); | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 182 | @$child{keys %$meta} = values %$meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | 182 | $self->{jobs}{$child->{reader}->fileno} = $child; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | 182 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 173 | ▶ | sub job_rem { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
job_rem: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 173 | my ($self, $child) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 173 | $child->{runtime} = sprintf "%.02f", time() - $child->{start}; | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 173 | $child->{start} = time(); | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | 173 | delete $self->{jobs}{$child->{reader}->fileno}; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | 173 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 167 | ▶ | sub child_finish { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
child_finish: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 167 | my ($self, $child, $state) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # The pipe close raises a sigpipe in the child | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # when the child is still alive and tries to write. | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 167 | ▶ | shutdown($child->{reader}, 2) or warn "worker child $child->{pid}: shutdown pipe failed: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 167 | ▶ | close($child->{reader}) or warn "worker child $child->{pid}: close pipe failed: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 167 | delete $child->{reader}; | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 167 | $child->{state} = $state; | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 167 | $child->{start} = time(); | |||||||||||||||||||||||||||||||||||||||||||||
| 164 | 167 | $self->{finished}{$child->{pid}} = $child; | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | 167 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 195 | ▶ | sub child_start { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
child_start: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 195 | my ($self, $worker, $msg_id, $msg_vci) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 195 | ▶ | my %id = (defined $msg_id) ? (id => $msg_id) : (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 195 | ▶ | my %vci = (defined $msg_vci) ? (vci => $msg_vci) : (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # Handle waitpid() explicitly instead of using open('-|'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # see: https://perldoc.perl.org/perlipc#Safe-Pipe-Opens | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 195 | ▶ | socketpair(my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair failed: $!"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 195 | my $pid = fork(); | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | 195 | ▶ | die "failed to fork: $!" unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 195 | ▶ | if ($pid != 0) { # parent | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 176 | my $child = {reader => $rd, pid => $pid, start => time(), %id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 183 | 176 | ▶ | $self->{trace_cb}->('RUN', {pid => $pid, %id, %vci}) if $self->{trace_cb}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 176 | close($wr); | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | 176 | return $child; | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 19 | close($rd); | |||||||||||||||||||||||||||||||||||||||||||||
| 188 | 19 | $self->{jobqueue} = []; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | 19 | $self->{jobs} = {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 190 | 19 | $self->{finished} = {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 19 | local $SIG{TERM} = 'DEFAULT'; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # The child_handler returns an error code or dies. | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 19 | my $ret = $worker->child_handler($wr); | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 18 | exit $ret; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 206 | ▶ | sub msg_enqueue { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
msg_enqueue: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 206 | my ($self, $msg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # queue message and start child from rpc_handler | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 206 | push(@{$self->{jobqueue}}, $msg); | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | 206 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 771 | ▶ | sub msg_dequeue { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
msg_dequeue: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 771 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 771 | ▶ | unless (keys %{$self->{jobs}} < $self->{max_async}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 137 | return; # wait until job slot is avail | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 634 | return shift(@{$self->{jobqueue}}); | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 49 | ▶ | sub jobs_terminate { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
jobs_terminate: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 49 | my ($self, $state, $filter) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # Terminate active jobs & queued jobs | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # (no error reply since channel is closed) | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 49 | my @childs = grep { $filter->($_) } values %{$self->{jobs}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 223 | 49 | foreach my $child (@childs) { | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | 4 | $self->job_rem($child); | |||||||||||||||||||||||||||||||||||||||||||||
| 225 | 4 | $self->child_finish($child, $state); | |||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 49 | my @msgs = grep { $filter->($_) } @{$self->{jobqueue}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | 49 | ▶ | if (@msgs) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 1 | $self->{jobqueue} = [grep { !$filter->($_) } @{$self->{jobqueue}}]; | |||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 49 | return (\@childs, \@msgs); | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||