MCE/Channel/MutexFast.pm

filter: Statementstmt 94.9% 150 / 158 Branchbran 76.3% 87 / 114 Conditioncond 48.1% 13 / 27 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 100% 18 / 18 Podpod 100% 11 / 11 totaltotal 81.7% 283 / 346 SCAR 44.6
CC
67
Cov
84%
dequeue
13.7
dequeue_nb
13.2
recv2_nb
13.2
CRAP 86.8
« MCE/Mutex/Channel2.pm ↑ Summary MCE/Core/Input/Request.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Channel for producer(s) and many consumers supporting processes and threads.
4 ##
5 ###############################################################################
6  
7 package MCE::Channel::MutexFast;
8  
9 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 ▶no warnings qw( uninitialized once );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 our $VERSION = '1.889';
15  
16 13 ▶use base 'MCE::Channel';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use MCE::Mutex ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 13 my $LF = "\012"; Internals::SvREADONLY($LF, 1);
20  
21 sub new {
22 13 ▶ my ( $class, %obj ) = ( @_, impl => 'MutexFast' );
Subroutine new: called CC=3 SCAR=11.0
Pod documented
23  
24 13 $obj{init_pid} = MCE::Channel::_pid();
25 13 MCE::Util::_sock_pair( \%obj, 'p_sock', 'c_sock' );
26  
27 # locking for the consumer side of the channel
28 13 $obj{c_mutex} = MCE::Mutex->new( impl => 'Channel2' );
29  
30 # optionally, support many-producers writing and reading
31 13 ▶ $obj{p_mutex} = MCE::Mutex->new( impl => 'Channel2' ) if $obj{mp};
Branch: 1 branch
BranchTrueFalseTotal
if $obj{"mp"} 1 12 13
32  
33 13 bless \%obj, $class;
34  
35 13 MCE::Mutex::Channel::_save_for_global_cleanup($obj{c_mutex});
36 13 ▶ MCE::Mutex::Channel::_save_for_global_cleanup($obj{p_mutex}) if $obj{mp};
Branch: 1 branch
BranchTrueFalseTotal
if $obj{"mp"} 1 12 13
37  
38 13 return \%obj;
39 }
40  
41 END {
42 13 ▶ MCE::Child->finish('MCE') if $INC{'MCE/Child.pm'};
Subroutine END: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"MCE/Child.pm"} 11 2 13
43 }
44  
45 ###############################################################################
46 ## ----------------------------------------------------------------------------
47 ## Queue-like methods.
48 ##
49 ###############################################################################
50  
51 sub end {
52 2 ▶ my ( $self ) = @_;
Subroutine end: called CC=2 SCAR=7.0
Pod documented
53  
54 2 ▶ local $\ = undef if (defined $\);
Branch: 1 branch
BranchTrueFalseTotal
if defined $\ 0 2 2
55 2 print { $self->{p_sock} } pack('i', -1);
56  
57 2 $self->{ended} = 1;
58 }
59  
60 sub enqueue {
61 26 ▶ my $self = shift;
Subroutine enqueue: called CC=6 SCAR=17.9
Pod documented
62 26 ▶ return MCE::Channel::_ended('enqueue') if $self->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ended"} 2 24 26
63  
64 24 ▶ local $\ = undef if (defined $\);
Branch: 1 branch
BranchTrueFalseTotal
if defined $\ 0 24 24
65 24 my $p_mutex = $self->{p_mutex};
66 24 ▶ $p_mutex->lock2 if $p_mutex;
Branch: 1 branch
BranchTrueFalseTotal
if $p_mutex 12 12 24
67  
68 24 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 44 24 68
69 44 my $data = ''.shift;
70 44 print { $self->{p_sock} } pack('i', length $data), $data;
71 }
72  
73 24 ▶ $p_mutex->unlock2 if $p_mutex;
Branch: 1 branch
BranchTrueFalseTotal
if $p_mutex 12 12 24
74  
75 24 return 1;
76 }
77  
78 sub dequeue {
79 16 ▶ my ( $self, $count ) = @_;
Subroutine dequeue: called CC=12 SCAR=26.2
Pod documented
80 16 ▶ $count = 1 if ( !$count || $count < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if not $count or $count < 1 14 2 16
Condition66%2/3
not $count or $count < 1
l!l&&r!l&&!r
1402
MC/DC50%1/2
not $count or $count < 1
not $count $count < 1
Truth table66%2/3
not(A) or B
A= not $countB= $count < 1
ABresult
1X1
011
000
81  
82 16 ▶ if ( $count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) { } 14 2 16
83 14 ( my $c_mutex = $self->{c_mutex} )->lock;
84 14 MCE::Util::_sysread( $self->{c_sock}, my($plen), 4 );
85  
86 14 my $len = unpack('i', $plen);
87 14 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 0 14 14
88 0 $self->end, $c_mutex->unlock;
89 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
90 }
91  
92 14 ▶ MCE::Channel::_read( $self->{c_sock}, my($data), $len ) if $len;
Branch: 1 branch
BranchTrueFalseTotal
if $len 10 4 14
93 14 $c_mutex->unlock;
94  
95 14 ▶ $len ? $data : '';
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 10 4 14
96 }
97 else {
98 2 my ( $plen, @ret );
99  
100 2 ( my $c_mutex = $self->{c_mutex} )->lock;
101  
102 2 ▶ while ( $count-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 6 2 8
103 6 MCE::Util::_sysread( $self->{c_sock}, $plen, 4 );
104  
105 6 my $len = unpack('i', $plen);
106 6 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 0 6 6
107 0 $self->end;
108 0 last;
109 }
110  
111 6 ▶ push(@ret, ''), next unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 6 6
112 6 MCE::Channel::_read( $self->{c_sock}, my($data), $len );
113 6 push @ret, $data;
114 }
115  
116 2 $c_mutex->unlock;
117  
118 2 ▶ wantarray ? @ret : $ret[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
119 }
120 }
121  
122 sub dequeue_nb {
123 20 ▶ my ( $self, $count ) = @_;
Subroutine dequeue_nb: called CC=12 SCAR=25.8
Pod documented
124 20 ▶ $count = 1 if ( !$count || $count < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if not $count or $count < 1 18 2 20
Condition66%2/3
not $count or $count < 1
l!l&&r!l&&!r
1802
MC/DC50%1/2
not $count or $count < 1
not $count $count < 1
Truth table66%2/3
not(A) or B
A= not $countB= $count < 1
ABresult
1X1
011
000
125  
126 20 my ( $plen, @ret );
127 20 ( my $c_mutex = $self->{c_mutex} )->lock;
128  
129 20 ▶ while ( $count-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 24 16 40
130 24 MCE::Util::_nonblocking( $self->{c_sock}, 1 );
131 24 MCE::Util::_sysread( $self->{c_sock}, $plen, 4 );
132 24 MCE::Util::_nonblocking( $self->{c_sock}, 0 );
133  
134 24 ▶ my $len; $len = unpack('i', $plen) if $plen;
Branch: 1 branch
BranchTrueFalseTotal
if $plen 24 0 24
135 24 ▶ if ( !$len || $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $len or $len < 0) 4 20 24
Condition66%2/3
not $len or $len < 0
l!l&&r!l&&!r
4020
MC/DC50%1/2
not $len or $len < 0
not $len $len < 0
Truth table66%2/3
not(A) or B
A= not $lenB= $len < 0
ABresult
1X1
011
000
136 4 ▶ $self->end if defined $len && $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len and $len < 0 0 4 4
Condition33%1/3
defined $len and $len < 0
!ll&&!rl&&r
040
MC/DC0%0/2
defined $len and $len < 0
defined $len $len < 0
Truth table33%1/3
A and B
A= defined $lenB= $len < 0
ABresult
0X0
100
111
137 4 ▶ push @ret, '' if defined $len && $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len and $len == 0 4 0 4
Condition33%1/3
defined $len and $len == 0
!ll&&!rl&&r
004
MC/DC0%0/2
defined $len and $len == 0
defined $len $len == 0
Truth table33%1/3
A and B
A= defined $lenB= $len == 0
ABresult
0X0
100
111
138 4 last;
139 }
140  
141 20 MCE::Channel::_read( $self->{c_sock}, my($data), $len );
142 20 push @ret, $data;
143 }
144  
145 20 $c_mutex->unlock;
146  
147 20 ▶ wantarray ? @ret : $ret[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 18 20
148 }
149  
150 ###############################################################################
151 ## ----------------------------------------------------------------------------
152 ## Methods for two-way communication; producer to consumer.
153 ##
154 ###############################################################################
155  
156 sub send {
157 38 ▶ my $self = shift;
Subroutine send: called CC=5 SCAR=16.1
Pod documented
158 38 ▶ return MCE::Channel::_ended('send') if $self->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ended"} 2 36 38
159  
160 36 my $data = ''.shift;
161  
162 36 ▶ local $\ = undef if (defined $\);
Branch: 1 branch
BranchTrueFalseTotal
if defined $\ 0 36 36
163 36 my $p_mutex = $self->{p_mutex};
164 36 ▶ $p_mutex->lock2 if $p_mutex;
Branch: 1 branch
BranchTrueFalseTotal
if $p_mutex 6 30 36
165  
166 36 print { $self->{p_sock} } pack('i', length $data), $data;
167 36 ▶ $p_mutex->unlock2 if $p_mutex;
Branch: 1 branch
BranchTrueFalseTotal
if $p_mutex 6 30 36
168  
169 36 return 1;
170 }
171  
172 sub recv {
173 9 ▶ my ( $self ) = @_;
Subroutine recv: called CC=5 SCAR=17.1
Pod documented
174  
175 9 ( my $c_mutex = $self->{c_mutex} )->lock;
176 9 MCE::Util::_sysread( $self->{c_sock}, my($plen), 4 );
177  
178 9 my $len = unpack('i', $plen);
179 9 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 0 9 9
180 0 $self->end, $c_mutex->unlock;
181 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
182 }
183  
184 9 ▶ MCE::Channel::_read( $self->{c_sock}, my($data), $len ) if $len;
Branch: 1 branch
BranchTrueFalseTotal
if $len 2 7 9
185 9 $c_mutex->unlock;
186  
187 9 ▶ $len ? $data : '';
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 2 7 9
188 }
189  
190 sub recv_nb {
191 6 ▶ my ( $self ) = @_;
Subroutine recv_nb: called CC=9 SCAR=24.6
Pod documented
192  
193 6 ( my $c_mutex = $self->{c_mutex} )->lock;
194 6 MCE::Util::_nonblocking( $self->{c_sock}, 1 );
195 6 MCE::Util::_sysread( $self->{c_sock}, my($plen), 4 );
196 6 MCE::Util::_nonblocking( $self->{c_sock}, 0 );
197  
198 6 ▶ my $len; $len = unpack('i', $plen) if $plen;
Branch: 1 branch
BranchTrueFalseTotal
if $plen 6 0 6
199 6 ▶ if ( !$len || $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $len or $len < 0) 4 2 6
Condition66%2/3
not $len or $len < 0
l!l&&r!l&&!r
402
MC/DC50%1/2
not $len or $len < 0
not $len $len < 0
Truth table66%2/3
not(A) or B
A= not $lenB= $len < 0
ABresult
1X1
011
000
200 4 ▶ $self->end if defined $len && $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len and $len < 0 0 4 4
Condition33%1/3
defined $len and $len < 0
!ll&&!rl&&r
040
MC/DC0%0/2
defined $len and $len < 0
defined $len $len < 0
Truth table33%1/3
A and B
A= defined $lenB= $len < 0
ABresult
0X0
100
111
201 4 $c_mutex->unlock;
202 4 ▶ return '' if defined $len && $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len and $len == 0 4 0 4
Condition33%1/3
defined $len and $len == 0
!ll&&!rl&&r
004
MC/DC0%0/2
defined $len and $len == 0
defined $len $len == 0
Truth table33%1/3
A and B
A= defined $lenB= $len == 0
ABresult
0X0
100
111
203 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
204 }
205  
206 2 MCE::Channel::_read( $self->{c_sock}, my($data), $len );
207 2 $c_mutex->unlock;
208  
209 2 $data;
210 }
211  
212 ###############################################################################
213 ## ----------------------------------------------------------------------------
214 ## Methods for two-way communication; consumer to producer.
215 ##
216 ###############################################################################
217  
218 sub send2 {
219 12 ▶ my $self = shift;
Subroutine send2: called CC=2 SCAR=7.0
Pod documented
220 12 my $data = ''.shift;
221  
222 12 ▶ local $\ = undef if (defined $\);
Branch: 1 branch
BranchTrueFalseTotal
if defined $\ 0 12 12
223 $self->{c_mutex}->synchronize2( sub {
224 12 ▶ print { $self->{c_sock} } pack('i', length $data), $data;
Subroutine __ANON__: called CC=1 SCAR=0.0
225 12 });
226  
227 12 return 1;
228 }
229  
230 sub recv2 {
231 6 ▶ my ( $self ) = @_;
Subroutine recv2: called CC=8 SCAR=20.8
Pod documented
232 6 my ( $plen, $data );
233  
234 6 ▶ local $/ = $LF if ( $/ ne $LF );
Branch: 1 branch
BranchTrueFalseTotal
if $/ ne $LF 0 6 6
235 6 my $p_mutex = $self->{p_mutex};
236 6 ▶ $p_mutex->lock if $p_mutex;
Branch: 1 branch
BranchTrueFalseTotal
if $p_mutex 3 3 6
237  
238 ( $p_mutex )
239 ? MCE::Util::_sysread( $self->{p_sock}, $plen, 4 )
240 6 ▶ : read( $self->{p_sock}, $plen, 4 );
Branch: 1 branch
BranchTrueFalseTotal
$p_mutex ? : 3 3 6
241  
242 6 my $len = unpack('i', $plen);
243  
244 6 ▶ if ( $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 2 4 6
245 ( $p_mutex )
246 ? MCE::Channel::_read( $self->{p_sock}, $data, $len )
247 2 ▶ : read( $self->{p_sock}, $data, $len );
Branch: 1 branch
BranchTrueFalseTotal
$p_mutex ? : 1 1 2
248 }
249  
250 6 ▶ $p_mutex->unlock if $p_mutex;
Branch: 1 branch
BranchTrueFalseTotal
if $p_mutex 3 3 6
251  
252 6 ▶ $len ? $data : '';
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 2 4 6
253 }
254  
255 sub recv2_nb {
256 6 ▶ my ( $self ) = @_;
Subroutine recv2_nb: called CC=12 SCAR=25.8
Pod documented
257 6 my ( $plen, $data );
258  
259 6 ▶ local $/ = $LF if ( $/ ne $LF );
Branch: 1 branch
BranchTrueFalseTotal
if $/ ne $LF 0 6 6
260 6 my $p_mutex = $self->{p_mutex};
261 6 ▶ $p_mutex->lock if $p_mutex;
Branch: 1 branch
BranchTrueFalseTotal
if $p_mutex 3 3 6
262  
263 6 MCE::Util::_nonblocking( $self->{p_sock}, 1 );
264  
265 ( $p_mutex )
266 ? MCE::Util::_sysread( $self->{p_sock}, $plen, 4 )
267 6 ▶ : read( $self->{p_sock}, $plen, 4 );
Branch: 1 branch
BranchTrueFalseTotal
$p_mutex ? : 3 3 6
268  
269 6 MCE::Util::_nonblocking( $self->{p_sock}, 0 );
270  
271 6 ▶ my $len; $len = unpack('i', $plen) if $plen;
Branch: 1 branch
BranchTrueFalseTotal
if $plen 6 0 6
272 6 ▶ if ( !$len ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($len) 4 2 6
273 4 ▶ $p_mutex->unlock if $p_mutex;
Branch: 1 branch
BranchTrueFalseTotal
if $p_mutex 2 2 4
274 4 ▶ return '' if defined $len && $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len and $len == 0 4 0 4
Condition33%1/3
defined $len and $len == 0
!ll&&!rl&&r
004
MC/DC0%0/2
defined $len and $len == 0
defined $len $len == 0
Truth table33%1/3
A and B
A= defined $lenB= $len == 0
ABresult
0X0
100
111
275 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
276 }
277  
278 ( $p_mutex )
279 ? MCE::Channel::_read( $self->{p_sock}, $data, $len )
280 2 ▶ : read( $self->{p_sock}, $data, $len );
Branch: 1 branch
BranchTrueFalseTotal
$p_mutex ? : 1 1 2
281  
282 2 ▶ $p_mutex->unlock if $p_mutex;
Branch: 1 branch
BranchTrueFalseTotal
if $p_mutex 1 1 2
283  
284 2 $data;
285 }
286  
287 13 1;
288  
289 __END__
« MCE/Mutex/Channel2.pm ↑ Summary MCE/Core/Input/Request.pm »