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