Mutex/Channel.pm

filter: Statementstmt 54.1% 46 / 85 Branchbran 14.5% 9 / 62 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 57.8% 11 / 19 Podpod 100% 6 / 6 totaltotal 39.0% 73 / 187 CC 32 SCAR 36.3
CC
32
Cov
36%
timedwait
110.0
synchronize
72.0
lock
20.0
CRAP 37.7
« Mutex/Util.pm ↑ Summary Mutex/Flock.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Mutex::Channel - Mutex locking via a pipe or socket.
4 ##
5 ###############################################################################
6  
7 package Mutex::Channel;
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶no warnings qw( threads recursion uninitialized once );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '1.011';
15  
16 2 ▶use if $^O eq 'MSWin32', 'threads';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use if $^O eq 'MSWin32', 'threads::shared';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use base 'Mutex';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Mutex::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Time::HiRes 'alarm';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶my $is_MSWin32 = ($^O eq 'MSWin32') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 2 2
25 2 ▶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
002
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 2 ▶my $tid = $INC{'threads.pm'} ? threads->tid : 0;
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 2 2
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 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 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=17.5
40 2 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, @_);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 2 2
41 2 ▶ CORE::syswrite($obj->{_w_sock}, '0'), $obj->{ $pid } = 0 if $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$pid} 0 2 2
42  
43 2 ▶ if ( $obj->{_init_pid} eq $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"_init_pid"} eq $pid) 2 0 2
44 2 ▶ $use_pipe
Branch: 1 branch
BranchTrueFalseTotal
$use_pipe ? : 2 0 2
45 ? Mutex::Util::destroy_pipes($obj, qw(_w_sock _r_sock))
46 : Mutex::Util::destroy_socks($obj, qw(_w_sock _r_sock));
47 }
48  
49 2 return;
50 }
51  
52 ###############################################################################
53 ## ----------------------------------------------------------------------------
54 ## Public methods.
55 ##
56 ###############################################################################
57  
58 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.5
Pod documented
59 2 my ($class, %obj) = (@_, impl => 'Channel');
60 2 ▶ $obj{_init_pid} = $tid ? $$ .'.'. $tid : $$;
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 2 2
61 2 ▶ $obj{_t_lock} = threads::shared::share( my $t_lock ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 2 2
62  
63 2 ▶ $use_pipe
Branch: 1 branch
BranchTrueFalseTotal
$use_pipe ? : 2 0 2
64 ? Mutex::Util::pipe_pair(\%obj, qw(_r_sock _w_sock))
65 : Mutex::Util::sock_pair(\%obj, qw(_r_sock _w_sock));
66  
67 2 CORE::syswrite($obj{_w_sock}, '0');
68  
69 2 return bless(\%obj, $class);
70 }
71  
72 0 ▶sub lock {
Subroutine lock: not called CC=4 SCAR=30.0
Pod documented
73 0 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
74  
75 0 ▶ unless ($obj->{ $pid }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{$pid}) 0 0 0
76 CORE::lock($obj->{_t_lock}), Mutex::Util::_sock_ready($obj->{_r_sock})
77 0 ▶ if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 0 0
78 0 Mutex::Util::_sysread($obj->{_r_sock}, my($b), 1), $obj->{ $pid } = 1;
79 }
80  
81 0 return;
82 }
83  
84 0 ▶sub guard_lock {
Subroutine guard_lock: not called CC=2 SCAR=17.9
Pod documented
85 0 &lock(@_);
86 0 ▶ bless([ $tid ? $$ .'.'. $tid : $$, $_[0] ], Mutex::Channel::_guard::);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
87 }
88  
89 2 *lock_exclusive = \&lock;
90 2 *lock_shared = \&lock;
91  
92 0 ▶sub unlock {
Subroutine unlock: not called CC=3 SCAR=24.8
Pod documented
93 0 ▶ my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
94  
95 CORE::syswrite($obj->{_w_sock}, '0'), $obj->{ $pid } = 0
96 0 ▶ if $obj->{ $pid };
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$pid} 0 0 0
97  
98 0 return;
99 }
100  
101 0 ▶sub synchronize {
Subroutine synchronize: not called CC=8 SCAR=42.8
Pod documented
102 0 ▶ my ($pid, $obj, $code) = ($tid ? $$ .'.'. $tid : $$, shift, shift);
Branch: 1 branch
BranchTrueFalseTotal
$tid ? : 0 0 0
103 0 my (@ret, $b);
104  
105 0 ▶ return unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
106  
107 # lock, run, unlock - inlined for performance
108 0 my $guard = bless([ $pid, $obj ], Mutex::Channel::_guard::);
109 0 ▶ unless ($obj->{ $pid }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{$pid}) 0 0 0
110 CORE::lock($obj->{_t_lock}), Mutex::Util::_sock_ready($obj->{_r_sock})
111 0 ▶ if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 0 0
112 0 Mutex::Util::_sysread($obj->{_r_sock}, $b, 1), $obj->{ $pid } = 1;
113 }
114 (defined wantarray)
115 0 ▶ ? @ret = wantarray ? $code->(@_) : scalar $code->(@_)
Branch: 2 branches
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
wantarray ? : 0 0 0
116 : $code->(@_);
117  
118 0 ▶ return wantarray ? @ret : $ret[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
119 }
120  
121 2 *enter = \&synchronize;
122  
123 0 ▶sub timedwait {
Subroutine timedwait: not called CC=10 SCAR=47.0
Pod documented
124 0 my ($obj, $timeout) = @_;
125  
126 0 ▶ $timeout = 1 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
127 0 ▶ Carp::croak('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
128 if (!looks_like_number($timeout) || $timeout < 0);
129  
130 0 ▶ $timeout = 0.0003 if $timeout < 0.0003;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < "0.0003" 0 0 0
131 0 local $@; my $ret = '';
132  
133 0 eval {
134 0 ▶ local $SIG{ALRM} = sub { alarm 0; die "alarm clock restart\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 ▶ alarm $timeout unless $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_MSWin32 0 0 0
136  
137 die "alarm clock restart\n"
138 0 ▶ if $is_MSWin32 && Mutex::Util::_sock_ready($obj->{_r_sock}, $timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 and Mutex::Util::_sock_ready($obj->{"_r_sock"}, $timeout) 0 0 0
Condition0%0/3
$is_MSWin32 and Mutex::Util::_sock_ready($obj->{"_r_sock"}, $timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$is_MSWin32 and Mutex::Util::_sock_ready($obj->{"_r_sock"}, $timeout)
$is_MSWin32 Mutex::Util::_sock_ready($obj->{"_r_sock"}, $timeout)
Truth table0%0/3
A and B
A= $is_MSWin32B= Mutex::Util::_sock_ready($obj->{"_r_sock"}, $timeout)
ABresult
0X0
100
111
139  
140 0 ▶ (!$is_MSWin32)
Branch: 1 branch
BranchTrueFalseTotal
!$is_MSWin32 ? : 0 0 0
141 ? ($obj->lock_exclusive, $ret = 1, alarm(0))
142 : ($obj->lock_exclusive, $ret = 1);
143 };
144  
145 0 ▶ alarm 0 unless $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_MSWin32 0 0 0
146  
147 0 $ret;
148 }
149  
150 2 1;
151  
152 __END__
« Mutex/Util.pm ↑ Summary Mutex/Flock.pm »