MCE/Mutex/Channel2.pm

filter: Statementstmt 78.5% 66 / 84 Branchbran 33.9% 19 / 56 Conditioncond 22.2% 2 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 77.7% 14 / 18 Podpod 100% 6 / 6 totaltotal 60.3% 108 / 179 SCAR 45.5
CC
30
Cov
58%
timedwait2
110.0
synchronize2
10.7
new
6.2
CRAP 94.8
« MCE/Relay.pm ↑ Summary MCE/Channel/MutexFast.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## MCE::Mutex::Channel2 - Provides two mutexes using a single channel.
4 ##
5 ###############################################################################
6  
7 package MCE::Mutex::Channel2;
8  
9 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 ▶no warnings qw( threads recursion uninitialized once );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 our $VERSION = '1.889';
15  
16 17 ▶use if $^O eq 'MSWin32', 'threads';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use if $^O eq 'MSWin32', 'threads::shared';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 17 ▶use base 'MCE::Mutex::Channel';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 17 ▶use MCE::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 17 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use Time::HiRes 'alarm';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 17 ▶my $is_MSWin32 = ($^O eq 'MSWin32') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 17 17
25 17 ▶my $tid = $INC{'threads.pm'} ? threads->tid() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 17 17
26  
27 sub CLONE {
28 0 ▶ $tid = threads->tid() if $INC{'threads.pm'};
Subroutine CLONE: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 0 0
29 }
30  
31 sub MCE::Mutex::Channel2::_guard::DESTROY {
32 52 ▶ my ($pid, $obj) = @{ $_[0] };
Subroutine DESTROY: called CC=2 SCAR=7.0
33 52 ▶ CORE::syswrite($obj->{_r_sock}, '0'), $obj->{$pid.'b'} = 0 if $obj->{$pid.'b'};
Branch: 1 branch
BranchTrueFalseTotal
if $$obj{$pid . 'b'} 52 0 52
34  
35 52 return;
36 }
37  
38 ###############################################################################
39 ## ----------------------------------------------------------------------------
40 ## Public methods.
41 ##
42 ###############################################################################
43  
44 sub new {
45 30 ▶ my ($class, %obj) = (@_, impl => 'Channel2');
Subroutine new: called CC=6 SCAR=18.3
Pod documented
46 30 ▶ $obj{_init_pid} = $tid ? $$ .'.'. $tid : $$;
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 30 30
47 30 ▶ $obj{_t_lock} = threads::shared::share( my $t_lock ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 30 30
48 30 ▶ $obj{_t_lock2} = threads::shared::share( my $t_lock2 ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 30 30
49  
50 30 MCE::Util::_sock_pair(\%obj, qw(_r_sock _w_sock), undef, 1);
51  
52 30 CORE::syswrite($obj{_w_sock}, '0');
53 30 CORE::syswrite($obj{_r_sock}, '0');
54 30 bless \%obj, $class;
55  
56 30 ▶ if ( caller !~ /^MCE:?/ || caller(1) !~ /^MCE:?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((caller) =~ /^MCE:?/ and caller(1) =~ /^MCE:?/) 1 29 30
Condition66%2/3
(caller) =~ /^MCE:?/ and caller(1) =~ /^MCE:?/
!ll&&!rl&&r
0129
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
57 1 MCE::Mutex::Channel::_save_for_global_cleanup(\%obj);
58 }
59  
60 30 return \%obj;
61 }
62  
63 sub lock2 {
64 42 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift);
Subroutine lock2: called CC=4 SCAR=14.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 42 42
65  
66 42 ▶ unless ($obj->{ $pid.'b' }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$obj{$pid . 'b'}) 42 0 42
67 CORE::lock($obj->{_t_lock2}), MCE::Util::_sock_ready($obj->{_w_sock})
68 42 ▶ if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 42 42
69 42 MCE::Util::_sysread($obj->{_w_sock}, my($b), 1), $obj->{ $pid.'b' } = 1;
70 }
71  
72 42 return;
73 }
74  
75 sub guard_lock2 {
76 0 ▶ &lock2(@_);
Subroutine guard_lock2: not called CC=2 SCAR=17.9
Pod documented
77 0 ▶ bless([ $tid ? $$ .'.'. $tid : $$, $_[0] ], MCE::Mutex::Channel2::_guard::);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
78 }
79  
80 17 *lock_exclusive2 = \&lock2;
81 17 *lock_shared2 = \&lock2;
82  
83 sub unlock2 {
84 42 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift);
Subroutine unlock2: called CC=3 SCAR=11.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 42 42
85  
86 CORE::syswrite($obj->{_r_sock}, '0'), $obj->{ $pid.'b' } = 0
87 42 ▶ if $obj->{ $pid.'b' };
Branch: 1 branch
BranchTrueFalseTotal
if $$obj{$pid . 'b'} 42 0 42
88  
89 42 return;
90 }
91  
92 sub synchronize2 {
93 52 ▶ my ($pid, $obj, $code) = ($tid ? $$ .'.'. $tid : $$, shift, shift);
Subroutine synchronize2: called CC=8 SCAR=23.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 52 52
94 52 my (@ret, $b);
95  
96 52 ▶ return unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 52 52
97  
98 # lock, run, unlock - inlined for performance
99 52 my $guard = bless([ $pid, $obj ], MCE::Mutex::Channel2::_guard::);
100 52 ▶ unless ($obj->{ $pid.'b' }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$obj{$pid . 'b'}) 52 0 52
101 CORE::lock($obj->{_t_lock2}), MCE::Util::_sock_ready($obj->{_w_sock})
102 52 ▶ if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 52 52
103 52 MCE::Util::_sysread($obj->{_w_sock}, $b, 1), $obj->{ $pid.'b' } = 1;
104 }
105 (defined wantarray)
106 52 ▶ ? @ret = wantarray ? $code->(@_) : scalar $code->(@_)
Branch: 2 branches
BranchTrueFalseTotal
defined wantarray ? : 0 52 52
wantarray ? : 0 0 0
107 : $code->(@_);
108  
109 52 ▶ return wantarray ? @ret : $ret[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 52 52
110 }
111  
112 17 *enter2 = \&synchronize2;
113  
114 sub timedwait2 {
115 0 ▶ my ($obj, $timeout) = @_;
Subroutine timedwait2: not called CC=10 SCAR=47.0
Pod documented
116  
117 0 ▶ $timeout = 1 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
118 0 ▶ Carp::croak('MCE::Mutex::Channel2: timedwait2 (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
119 if (!looks_like_number($timeout) || $timeout < 0);
120  
121 0 ▶ $timeout = 0.0003 if $timeout < 0.0003;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < "0.0003" 0 0 0
122 0 local $@; my $ret = '';
123  
124 0 eval {
125 0 ▶ local $SIG{ALRM} = sub { alarm 0; die "alarm clock restart\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 ▶ alarm $timeout unless $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_MSWin32 0 0 0
127  
128 die "alarm clock restart\n"
129 0 ▶ if $is_MSWin32 && MCE::Util::_sock_ready($obj->{_w_sock}, $timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 and MCE::Util::_sock_ready($obj->{"_w_sock"}, $timeout) 0 0 0
Condition0%0/3
$is_MSWin32 and MCE::Util::_sock_ready($obj->{"_w_sock"}, $timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$is_MSWin32 and MCE::Util::_sock_ready($obj->{"_w_sock"}, $timeout)
$is_MSWin32 MCE::Util::_sock_ready($obj->{"_w_sock"}, $timeout)
Truth table0%0/3
A and B
A= $is_MSWin32B= MCE::Util::_sock_ready($obj->{"_w_sock"}, $timeout)
ABresult
0X0
100
111
130  
131 0 ▶ (!$is_MSWin32)
Branch: 1 branch
BranchTrueFalseTotal
!$is_MSWin32 ? : 0 0 0
132 ? ($obj->lock_exclusive2, $ret = 1, alarm(0))
133 : ($obj->lock_exclusive2, $ret = 1);
134 };
135  
136 0 ▶ alarm 0 unless $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_MSWin32 0 0 0
137  
138 0 $ret;
139 }
140  
141 17 1;
142  
143 __END__
« MCE/Relay.pm ↑ Summary MCE/Channel/MutexFast.pm »