| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 4 | ▶ | if (ref $obj->{$p} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | for my $i (0 .. @{ $obj->{$p} } - 1) { | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | next unless (defined $obj->{$p}[$i]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | close $obj->{$p}[$i] if (fileno $obj->{$p}[$i]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | undef $obj->{$p}[$i]; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | else { | ||||||||||||||||||||||||||||
| 54 | 4 | ▶ | close $obj->{$p} if (fileno $obj->{$p}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if (ref $obj->{$p} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | for my $i (0 .. @{ $obj->{$p} } - 1) { | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | next unless (defined $obj->{$p}[$i]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if (fileno $obj->{$p}[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | syswrite($obj->{$p}[$i], '0') if $is_winenv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | syswrite($obj->{$p}, '0') if $is_winenv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 116 | # remove tainted'ness | ||||||||||||||||||||||||||||
| 117 | 0 | ($i) = $i =~ /(.*)/; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | socketpair( $obj->{$r_sock}[$i], $obj->{$w_sock}[$i], | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
not defined $timeout and $sock_ready{"$socket"} > 1
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
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my ($val_bytes, $delay, $start) = (pack('L', 0), 0, time); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if (!defined $timeout) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | $sock_ready{"$socket"}++; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $timeout = undef if $timeout < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | $timeout += $start if $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | return 1 if $timeout && time > $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$timeout and time > $timeout
MC/DC0%0/2
$timeout and time > $timeout
$timeout
time > $timeout
Truth table0%0/3
A and B
A= $timeoutB= time > $timeout
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # delay after a while to not consume a CPU core | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | sleep(0.015), next if $delay; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | $delay = 1 if time - $start > 0.030; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | goto \&_sysread if ($! == Errno::EINTR()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | }; | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 2 | 1; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||||||||||