Mutex/Util.pm

filter: Statementstmt 40.8% 38 / 93 Branchbran 10.0% 6 / 60 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 64.2% 9 / 14 Podpod 100% 4 / 4 totaltotal 31.4% 57 / 181 CC 37 SCAR 40.5
CC
37
Cov
28%
_sock_ready
132.0
destroy_socks
110.0
sock_pair
20.0
CRAP 57.5
↑ Summary Mutex/Channel.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Utility functions for Mutex.
4 ##
5 ###############################################################################
6  
7 package Mutex::Util;
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 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '1.011';
15  
16 ## no critic (BuiltinFunctions::ProhibitStringyEval)
17  
18 2 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Socket qw( AF_UNIX );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 my ($is_winenv, $zero_bytes, %sock_ready);
23  
24 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
25 2 ▶ $is_winenv = ( $^O =~ /mswin|mingw|msys|cygwin/i ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /mswin|mingw|msys|cygwin/i ? : 0 2 2
26 2 $zero_bytes = pack('L', 0);
27 }
28  
29 0 ▶sub CLONE {
Subroutine CLONE: not called CC=1 SCAR=6.9
30 0 %sock_ready = ();
31 }
32  
33 ###############################################################################
34 ## ----------------------------------------------------------------------------
35 ## Public functions.
36 ##
37 ###############################################################################
38  
39 2 ▶sub destroy_pipes {
Subroutine destroy_pipes: called CC=8 SCAR=27.7
Pod documented
40 2 my ($obj, @params) = @_;
41 2 local ($!,$?); local $SIG{__DIE__};
42  
43 2 for my $p (@params) {
44 4 ▶ next unless (defined $obj->{$p});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{$p} 0 4 4
45  
46 4 ▶ if (ref $obj->{$p} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{$p} eq 'ARRAY') { } 0 4 4
47 0 for my $i (0 .. @{ $obj->{$p} } - 1) {
48 0 ▶ next unless (defined $obj->{$p}[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{$p}[$i] 0 0 0
49 0 ▶ close $obj->{$p}[$i] if (fileno $obj->{$p}[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if fileno $obj->{$p}[$i] 0 0 0
50 0 undef $obj->{$p}[$i];
51 }
52 }
53 else {
54 4 ▶ close $obj->{$p} if (fileno $obj->{$p});
Branch: 1 branch
BranchTrueFalseTotal
if fileno $obj->{$p} 4 0 4
55 4 undef $obj->{$p};
56 }
57 }
58  
59 2 return;
60 }
61  
62 0 ▶sub destroy_socks {
Subroutine destroy_socks: not called CC=10 SCAR=47.0
Pod documented
63 0 my ($obj, @params) = @_;
64 0 local ($!,$?,$@); local $SIG{__DIE__};
65  
66 0 for my $p (@params) {
67 0 ▶ next unless (defined $obj->{$p});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{$p} 0 0 0
68  
69 0 ▶ if (ref $obj->{$p} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{$p} eq 'ARRAY') { } 0 0 0
70 0 for my $i (0 .. @{ $obj->{$p} } - 1) {
71 0 ▶ next unless (defined $obj->{$p}[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{$p}[$i] 0 0 0
72 0 ▶ if (fileno $obj->{$p}[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno $obj->{$p}[$i]) 0 0 0
73 0 ▶ syswrite($obj->{$p}[$i], '0') if $is_winenv;
Branch: 1 branch
BranchTrueFalseTotal
if $is_winenv 0 0 0
74 0 eval q{ CORE::shutdown($obj->{$p}[$i], 2) };
75 0 close $obj->{$p}[$i];
76 }
77 0 undef $obj->{$p}[$i];
78 }
79 }
80 else {
81 0 ▶ if (fileno $obj->{$p}) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno $obj->{$p}) 0 0 0
82 0 ▶ syswrite($obj->{$p}, '0') if $is_winenv;
Branch: 1 branch
BranchTrueFalseTotal
if $is_winenv 0 0 0
83 0 eval q{ CORE::shutdown($obj->{$p}, 2) };
84 0 close $obj->{$p};
85 }
86 0 undef $obj->{$p};
87 }
88 }
89  
90 0 return;
91 }
92  
93 2 ▶sub pipe_pair {
Subroutine pipe_pair: called CC=4 SCAR=17.3
Pod documented
94 2 my ($obj, $r_sock, $w_sock, $i) = @_;
95 2 local $!;
96  
97 2 ▶ if (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) { } 0 2 2
98 # remove tainted'ness
99 0 ($i) = $i =~ /(.*)/;
100 0 ▶ pipe($obj->{$r_sock}[$i], $obj->{$w_sock}[$i]) or die "pipe: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
pipe $obj->{$r_sock}[$i], $obj->{$w_sock}[$i] or die "pipe: $!\n" 0 0 0
101 0 $obj->{$w_sock}[$i]->autoflush(1);
102 }
103 else {
104 2 ▶ pipe($obj->{$r_sock}, $obj->{$w_sock}) or die "pipe: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
pipe $obj->{$r_sock}, $obj->{$w_sock} or die "pipe: $!\n" 2 0 2
105 2 $obj->{$w_sock}->autoflush(1);
106 }
107  
108 2 return;
109 }
110  
111 0 ▶sub sock_pair {
Subroutine sock_pair: not called CC=4 SCAR=30.0
Pod documented
112 0 my ($obj, $r_sock, $w_sock, $i) = @_;
113 0 local $!;
114  
115 0 ▶ if (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) { } 0 0 0
116 # remove tainted'ness
117 0 ($i) = $i =~ /(.*)/;
118 0 ▶ socketpair( $obj->{$r_sock}[$i], $obj->{$w_sock}[$i],
Branch: 1 branch
BranchTrueFalseTotal
socketpair $obj->{$r_sock}[$i], $obj->{$w_sock}[$i], 1, 1, 0 or die "socketpair: $!\n" 0 0 0
119 AF_UNIX, Socket::SOCK_STREAM(), 0 ) or die "socketpair: $!\n";
120 0 $obj->{$r_sock}[$i]->autoflush(1);
121 0 $obj->{$w_sock}[$i]->autoflush(1);
122 }
123 else {
124 0 ▶ socketpair( $obj->{$r_sock}, $obj->{$w_sock},
Branch: 1 branch
BranchTrueFalseTotal
socketpair $obj->{$r_sock}, $obj->{$w_sock}, 1, 1, 0 or die "socketpair: $!\n" 0 0 0
125 AF_UNIX, Socket::SOCK_STREAM(), 0 ) or die "socketpair: $!\n";
126 0 $obj->{$r_sock}->autoflush(1);
127 0 $obj->{$w_sock}->autoflush(1);
128 }
129  
130 0 return;
131 }
132  
133 0 ▶sub _sock_ready {
Subroutine _sock_ready: not called CC=11 SCAR=48.8
134 0 my ($socket, $timeout) = @_;
135 0 ▶ return '' if !defined $timeout && $sock_ready{"$socket"} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $timeout and $sock_ready{"$socket"} > 1 0 0 0
Condition0%0/3
not defined $timeout and $sock_ready{"$socket"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $timeout and $sock_ready{"$socket"} > 1
not defined $timeout $sock_ready{"$socket"} > 1
Truth table0%0/3
not(A) and B
A= not defined $timeoutB= $sock_ready{"$socket"} > 1
ABresult
0X0
100
111
136  
137 0 my ($val_bytes, $delay, $start) = (pack('L', 0), 0, time);
138  
139 0 ▶ if (!defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $timeout) { } 0 0 0
140 0 $sock_ready{"$socket"}++;
141 }
142 else {
143 0 ▶ $timeout = undef if $timeout < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 0 0 0 0
144 0 ▶ $timeout += $start if $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout 0 0 0
145 }
146  
147 0 while (1) {
148 # MSWin32 FIONREAD - from winsock2.h macro
149 0 ioctl($socket, 0x4004667f, $val_bytes);
150  
151 0 ▶ return '' if $val_bytes ne $zero_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $val_bytes ne $zero_bytes 0 0 0
152 0 ▶ return 1 if $timeout && time > $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout and time > $timeout 0 0 0
Condition0%0/3
$timeout and time > $timeout
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout and time > $timeout
$timeout time > $timeout
Truth table0%0/3
A and B
A= $timeoutB= time > $timeout
ABresult
0X0
100
111
153  
154 # delay after a while to not consume a CPU core
155 0 ▶ sleep(0.015), next if $delay;
Branch: 1 branch
BranchTrueFalseTotal
if $delay 0 0 0
156 0 ▶ $delay = 1 if time - $start > 0.030;
Branch: 1 branch
BranchTrueFalseTotal
if time - $start > "0.03" 0 0 0
157 }
158 }
159  
160 0 ▶sub _sysread {
Subroutine _sysread: not called CC=4 SCAR=30.0
161 ( @_ == 3
162 ? CORE::sysread($_[0], $_[1], $_[2])
163 : CORE::sysread($_[0], $_[1], $_[2], $_[3])
164 )
165 0 ▶ or do {
Branch: 2 branches
BranchTrueFalseTotal
@_ == 3 ? sysread($_[0], $_[1], $_[2]) : sysread($_[0], $_[1], $_[2], $_[3]) or do { goto \&Mutex::Util::_sysread if $! == 4 } 0 0 0
@_ == 3 ? : 0 0 0
166 0 ▶ goto \&_sysread if ($! == Errno::EINTR());
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
167 };
168 }
169  
170 2 1;
171  
172 __END__
↑ Summary Mutex/Channel.pm »