MCE/Mutex/Channel.pm

filter: Statementstmt 63.1% 60 / 95 Branchbran 26.3% 19 / 72 Conditioncond 33.3% 5 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 66.6% 14 / 21 Podpod 100% 6 / 6 totaltotal 48.4% 106 / 219 CC 40 SCAR 35.0
CC
40
Cov
46%
timedwait
110.0
synchronize
72.0
_destroy
30.0
CRAP 33.2
« MCE/Relay.pm ↑ Summary MCE/Mutex/Channel2.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## MCE::Mutex::Channel - Mutex locking via a pipe or socket.
4 ##
5 ###############################################################################
6  
7 package MCE::Mutex::Channel;
8  
9 108 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 108 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 108 ▶no warnings qw( threads recursion uninitialized once );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 108 our $VERSION = '1.904';
15  
16 108 ▶use if $^O eq 'MSWin32', 'threads';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 108 ▶use if $^O eq 'MSWin32', 'threads::shared';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 108 ▶use base 'MCE::Mutex';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 108 ▶use MCE::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 108 ▶use Scalar::Util qw(looks_like_number weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 108 ▶use Time::HiRes 'alarm';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 108 ▶my $is_MSWin32 = ($^O eq 'MSWin32') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 108 108
25 108 ▶my $use_pipe = ($^O !~ /mswin|mingw|msys|cygwin/i && $] gt '5.010000');
Condition33%1/3
!($^O =~ /mswin|mingw|msys|cygwin/i) && $] gt '5.010000'
!ll&&!rl&&r
00108
MC/DC0%0/2
!($^O =~ /mswin|mingw|msys|cygwin/i) && $] gt '5.010000'
!($^O =~ /mswin|mingw|msys|cygwin/i) $] gt '5.010000'
Truth table33%1/3
not(A) && B
A= !($^O =~ /mswin|mingw|msys|cygwin/i)B= $] gt '5.010000'
ABresult
0X0
100
111
26 108 ▶my $tid = $INC{'threads.pm'} ? threads->tid : 0;
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 108 108
27  
28 0 ▶sub CLONE {
Subroutine CLONE: not called CC=2 SCAR=17.9
29 0 ▶ $tid = threads->tid if $INC{'threads.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 0 0
30 }
31  
32 0 ▶sub MCE::Mutex::Channel::_guard::DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
33 0 my ($pid, $obj) = @{ $_[0] };
34 0 ▶ CORE::syswrite($obj->{_w_sock}, '0'), $obj->{ $pid } = 0 if $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$pid} 0 0 0
35  
36 0 return;
37 }
38  
39 283 ▶sub DESTROY {
Subroutine DESTROY: called CC=7 SCAR=20.1
40 283 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, @_);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 283 283
41 283 ▶ CORE::syswrite($obj->{_w_sock}, '0'), $obj->{$pid } = 0 if $obj->{$pid };
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$pid} 0 283 283
42 283 ▶ CORE::syswrite($obj->{_r_sock}, '0'), $obj->{$pid.'b'} = 0 if $obj->{$pid.'b'};
Branch: 1 branch
BranchTrueFalseTotal
if $$obj{$pid . 'b'} 0 283 283
43  
44 283 ▶ if ( $obj->{_init_pid} eq $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"_init_pid"} eq $pid) 225 58 283
45 225 ▶ (!$use_pipe || $obj->{impl} eq 'Channel2')
Branch: 1 branch
BranchTrueFalseTotal
!$use_pipe || $obj->{'impl'} eq 'Channel2' ? : 14 211 225
Condition66%2/3
not $use_pipe or $obj->{"impl"} eq "Channel2"
l!l&&r!l&&!r
014211
MC/DC50%1/2
not $use_pipe or $obj->{"impl"} eq "Channel2"
not $use_pipe $obj->{"impl"} eq "Channel2"
Truth table66%2/3
not(A) or B
A= not $use_pipeB= $obj->{"impl"} eq "Channel2"
ABresult
1X1
011
000
46 ? MCE::Util::_destroy_socks($obj, qw(_w_sock _r_sock))
47 : MCE::Util::_destroy_pipes($obj, qw(_w_sock _r_sock));
48 }
49  
50 283 return;
51 }
52  
53 108 my @mutex;
54  
55 0 ▶sub _destroy {
Subroutine _destroy: not called CC=5 SCAR=34.0
56 0 ▶ my $pid = $tid ? $$ .'.'. $tid : $$;
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
57  
58 # Called by { MCE, MCE::Child, and MCE::Hobo }::_exit
59 0 for my $i ( 0 .. @mutex - 1 ) {
60 CORE::syswrite($mutex[$i]->{_w_sock}, '0'), $mutex[$i]->{$pid} = 0
61 0 ▶ if ( $mutex[$i]->{$pid} );
Branch: 1 branch
BranchTrueFalseTotal
if $mutex[$i]{$pid} 0 0 0
62 CORE::syswrite($mutex[$i]->{_r_sock}, '0'), $mutex[$i]->{$pid.'b'} = 0
63 0 ▶ if ( $mutex[$i]->{$pid.'b'} );
Branch: 1 branch
BranchTrueFalseTotal
if $mutex[$i]{$pid . 'b'} 0 0 0
64 }
65 }
66  
67 26 ▶sub _save_for_global_cleanup {
Subroutine _save_for_global_cleanup: called CC=1 SCAR=0.0
68 26 push(@mutex, $_[0]), weaken($mutex[-1]);
69 }
70  
71 ###############################################################################
72 ## ----------------------------------------------------------------------------
73 ## Public methods.
74 ##
75 ###############################################################################
76  
77 473 ▶sub new {
Subroutine new: called CC=6 SCAR=18.4
Pod documented
78 473 my ($class, %obj) = (@_, impl => 'Channel');
79 473 ▶ $obj{_init_pid} = $tid ? $$ .'.'. $tid : $$;
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 473 473
80 473 ▶ $obj{_t_lock} = threads::shared::share( my $t_lock ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 473 473
81  
82 473 ▶ $use_pipe
Branch: 1 branch
BranchTrueFalseTotal
$use_pipe ? : 473 0 473
83 ? MCE::Util::_pipe_pair(\%obj, qw(_r_sock _w_sock))
84 : MCE::Util::_sock_pair(\%obj, qw(_r_sock _w_sock));
85  
86 473 CORE::syswrite($obj{_w_sock}, '0');
87 473 bless \%obj, $class;
88  
89 473 ▶ if ( caller !~ /^MCE:?/ || caller(1) !~ /^MCE:?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((caller) =~ /^MCE:?/ and caller(1) =~ /^MCE:?/) 2 471 473
Condition66%2/3
(caller) =~ /^MCE:?/ and caller(1) =~ /^MCE:?/
!ll&&!rl&&r
02471
MC/DC50%1/2
(caller) =~ /^MCE:?/ and caller(1) =~ /^MCE:?/
(caller) =~ /^MCE:?/ caller(1) =~ /^MCE:?/
Truth table66%2/3
A and B
A= (caller) =~ /^MCE:?/B= caller(1) =~ /^MCE:?/
ABresult
0X0
100
111
90 2 MCE::Mutex::Channel::_save_for_global_cleanup(\%obj);
91 }
92  
93 473 return \%obj;
94 }
95  
96 371 ▶sub lock {
Subroutine lock: called CC=4 SCAR=14.6
Pod documented
97 371 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 371 371
98  
99 371 ▶ unless ($obj->{ $pid }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{$pid}) 371 0 371
100 CORE::lock($obj->{_t_lock}), MCE::Util::_sock_ready($obj->{_r_sock})
101 371 ▶ if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 371 371
102 371 MCE::Util::_sysread($obj->{_r_sock}, my($b), 1), $obj->{ $pid } = 1;
103 }
104  
105 371 return;
106 }
107  
108 0 ▶sub guard_lock {
Subroutine guard_lock: not called CC=2 SCAR=17.9
Pod documented
109 0 &lock(@_);
110 0 ▶ bless([ $tid ? $$ .'.'. $tid : $$, $_[0] ], MCE::Mutex::Channel::_guard::);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
111 }
112  
113 108 *lock_exclusive = \&lock;
114 108 *lock_shared = \&lock;
115  
116 371 ▶sub unlock {
Subroutine unlock: called CC=3 SCAR=11.7
Pod documented
117 371 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 371 371
118  
119 CORE::syswrite($obj->{_w_sock}, '0'), $obj->{ $pid } = 0
120 371 ▶ if $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$pid} 371 0 371
121  
122 371 return;
123 }
124  
125 0 ▶sub synchronize {
Subroutine synchronize: not called CC=8 SCAR=42.8
Pod documented
126 0 ▶ my ($pid, $obj, $code) = ($tid ? $$ .'.'. $tid : $$, shift, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
127 0 my (@ret, $b);
128  
129 0 ▶ return unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
130  
131 # lock, run, unlock - inlined for performance
132 0 my $guard = bless([ $pid, $obj ], MCE::Mutex::Channel::_guard::);
133 0 ▶ unless ($obj->{ $pid }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{$pid}) 0 0 0
134 CORE::lock($obj->{_t_lock}), MCE::Util::_sock_ready($obj->{_r_sock})
135 0 ▶ if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 0 0
136 0 MCE::Util::_sysread($obj->{_r_sock}, $b, 1), $obj->{ $pid } = 1;
137 }
138 (defined wantarray)
139 0 ▶ ? @ret = wantarray ? $code->(@_) : scalar $code->(@_)
Branch: 2 branches
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
wantarray ? : 0 0 0
140 : $code->(@_);
141  
142 0 ▶ return wantarray ? @ret : $ret[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
143 }
144  
145 108 *enter = \&synchronize;
146  
147 0 ▶sub timedwait {
Subroutine timedwait: not called CC=10 SCAR=47.0
Pod documented
148 0 my ($obj, $timeout) = @_;
149  
150 0 ▶ $timeout = 1 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
151 0 ▶ Carp::croak('MCE::Mutex::Channel: timedwait (timeout) is not valid')
Branch: 1 branch
BranchTrueFalseTotal
if not &looks_like_number($timeout) or $timeout < 0 0 0 0
Condition0%0/3
not &looks_like_number($timeout) or $timeout < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not &looks_like_number($timeout) or $timeout < 0
not &looks_like_number($timeout) $timeout < 0
Truth table0%0/3
not(A) or B
A= not &looks_like_number($timeout)B= $timeout < 0
ABresult
1X1
011
000
152 if (!looks_like_number($timeout) || $timeout < 0);
153  
154 0 ▶ $timeout = 0.0003 if $timeout < 0.0003;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < "0.0003" 0 0 0
155 0 local $@; my $ret = '';
156  
157 0 eval {
158 0 ▶ local $SIG{ALRM} = sub { alarm 0; die "alarm clock restart\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
159 0 ▶ alarm $timeout unless $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_MSWin32 0 0 0
160  
161 die "alarm clock restart\n"
162 0 ▶ if $is_MSWin32 && MCE::Util::_sock_ready($obj->{_r_sock}, $timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 and MCE::Util::_sock_ready($obj->{"_r_sock"}, $timeout) 0 0 0
Condition0%0/3
$is_MSWin32 and MCE::Util::_sock_ready($obj->{"_r_sock"}, $timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$is_MSWin32 and MCE::Util::_sock_ready($obj->{"_r_sock"}, $timeout)
$is_MSWin32 MCE::Util::_sock_ready($obj->{"_r_sock"}, $timeout)
Truth table0%0/3
A and B
A= $is_MSWin32B= MCE::Util::_sock_ready($obj->{"_r_sock"}, $timeout)
ABresult
0X0
100
111
163  
164 0 ▶ (!$is_MSWin32)
Branch: 1 branch
BranchTrueFalseTotal
!$is_MSWin32 ? : 0 0 0
165 ? ($obj->lock_exclusive, $ret = 1, alarm(0))
166 : ($obj->lock_exclusive, $ret = 1);
167 };
168  
169 0 ▶ alarm 0 unless $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_MSWin32 0 0 0
170  
171 0 $ret;
172 }
173  
174 108 1;
175  
176 __END__
« MCE/Relay.pm ↑ Summary MCE/Mutex/Channel2.pm »