MCE/Channel/Simple.pm

filter: Statementstmt 92.4% 134 / 145 Branchbran 55.5% 70 / 126 Conditioncond 33.3% 6 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 15 / 15 Podpod 100% 11 / 11 totaltotal 72.7% 238 / 327 CC 69 SCAR 26.7
CC
69
Cov
73%
dequeue
26.2
recv_nb
21.6
dequeue_nb
19.3
CRAP 14.4
« MCE/Candy.pm ↑ Summary MCE/Channel/SimpleFast.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Channel tuned for one producer and one consumer involving no locking.
4 ##
5 ###############################################################################
6  
7 package MCE::Channel::Simple;
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶no warnings qw( uninitialized once );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '1.904';
15  
16 1 ▶use base 'MCE::Channel';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 my $LF = "\012"; Internals::SvREADONLY($LF, 1);
19 1 ▶my $is_MSWin32 = ( $^O eq 'MSWin32' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 1 1
20 1 my $freeze = MCE::Channel::_get_freeze();
21 1 my $thaw = MCE::Channel::_get_thaw();
22  
23 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
24 1 my ( $class, %obj ) = ( @_, impl => 'Simple' );
25  
26 1 $obj{init_pid} = MCE::Channel::_pid();
27 1 MCE::Util::_sock_pair( \%obj, 'p_sock', 'c_sock' );
28  
29 1 return bless \%obj, $class;
30 }
31  
32 ###############################################################################
33 ## ----------------------------------------------------------------------------
34 ## Queue-like methods.
35 ##
36 ###############################################################################
37  
38 1 ▶sub end {
Subroutine end: called CC=3 SCAR=11.2
Pod documented
39 1 my ( $self ) = @_;
40  
41 1 ▶ local $\ = undef if (defined $\);
Branch: 1 branch
BranchTrueFalseTotal
if defined $\ 0 1 1
42 1 ▶ MCE::Util::_sock_ready_w( $self->{p_sock} ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 1 1
43 1 print { $self->{p_sock} } pack('i', -1);
44  
45 1 $self->{ended} = 1;
46 }
47  
48 15 ▶sub enqueue {
Subroutine enqueue: called CC=5 SCAR=16.2
Pod documented
49 15 my $self = shift;
50 15 ▶ return MCE::Channel::_ended('enqueue') if $self->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ended"} 1 14 15
51  
52 14 ▶ local $\ = undef if (defined $\);
Branch: 1 branch
BranchTrueFalseTotal
if defined $\ 0 14 14
53 14 ▶ MCE::Util::_sock_ready_w( $self->{p_sock} ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 14 14
54  
55 14 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 24 14 38
56 24 my $data = $freeze->([ shift ]);
57 24 print { $self->{p_sock} } pack('i', length $data) . $data;
58 }
59  
60 14 return 1;
61 }
62  
63 9 ▶sub dequeue {
Subroutine dequeue: called CC=17 SCAR=32.6
Pod documented
64 9 my ( $self, $count ) = @_;
65 9 ▶ $count = 1 if ( !$count || $count < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if not $count or $count < 1 8 1 9
Condition66%2/3
not $count or $count < 1
l!l&&r!l&&!r
801
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
66  
67 9 ▶ local $/ = $LF if ( $/ ne $LF );
Branch: 1 branch
BranchTrueFalseTotal
if $/ ne $LF 0 9 9
68  
69 9 ▶ if ( $count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) { } 8 1 9
70 8 my ( $plen, $data );
71 8 ▶ MCE::Util::_sock_ready( $self->{c_sock} ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 8 8
72  
73 $is_MSWin32
74 ? sysread( $self->{c_sock}, $plen, 4 )
75 8 ▶ : read( $self->{c_sock}, $plen, 4 );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 8 8
76  
77 8 my $len = unpack('i', $plen);
78 8 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 0 8 8
79 0 $self->end;
80 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
81 }
82  
83 $is_MSWin32
84 ? MCE::Channel::_read( $self->{c_sock}, $data, $len )
85 8 ▶ : read( $self->{c_sock}, $data, $len );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 8 8
86  
87 0 ▶ wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 8 8
88 }
89 else {
90 1 my ( $plen, @ret );
91 1 ▶ MCE::Util::_sock_ready( $self->{c_sock} ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 1 1
92  
93 1 ▶ while ( $count-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 3 1 4
94 3 my $data;
95  
96 $is_MSWin32
97 ? sysread( $self->{c_sock}, $plen, 4 )
98 3 ▶ : read( $self->{c_sock}, $plen, 4 );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 3 3
99  
100 3 my $len = unpack('i', $plen);
101 3 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 0 3 3
102 0 $self->end;
103 0 last;
104 }
105  
106 $is_MSWin32
107 ? MCE::Channel::_read( $self->{c_sock}, $data, $len )
108 3 ▶ : read( $self->{c_sock}, $data, $len );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 3 3
109  
110 3 push @ret, @{ $thaw->($data) };
111 }
112  
113 1 ▶ wantarray ? @ret : $ret[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
114 }
115 }
116  
117 11 ▶sub dequeue_nb {
Subroutine dequeue_nb: called CC=13 SCAR=29.6
Pod documented
118 11 my ( $self, $count ) = @_;
119 11 ▶ $count = 1 if ( !$count || $count < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if not $count or $count < 1 10 1 11
Condition66%2/3
not $count or $count < 1
l!l&&r!l&&!r
1001
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
120  
121 11 my ( $plen, @ret );
122 11 ▶ local $/ = $LF if ( $/ ne $LF );
Branch: 1 branch
BranchTrueFalseTotal
if $/ ne $LF 0 11 11
123  
124 11 ▶ while ( $count-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 13 11 24
125 13 my $data;
126 13 MCE::Util::_nonblocking( $self->{c_sock}, 1 );
127  
128 $is_MSWin32
129 ? sysread( $self->{c_sock}, $plen, 4 )
130 13 ▶ : read( $self->{c_sock}, $plen, 4 );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 13 13
131  
132 13 MCE::Util::_nonblocking( $self->{c_sock}, 0 );
133  
134 13 ▶ my $len; $len = unpack('i', $plen) if $plen;
Branch: 1 branch
BranchTrueFalseTotal
if $plen 13 0 13
135 13 ▶ if ( !$len || $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $len or $len < 0) 0 13 13
Condition33%1/3
not $len or $len < 0
l!l&&r!l&&!r
0013
MC/DC0%0/2
not $len or $len < 0
not $len $len < 0
Truth table33%1/3
not(A) or B
A= not $lenB= $len < 0
ABresult
1X1
011
000
136 0 ▶ $self->end if defined $len && $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len and $len < 0 0 0 0
Condition0%0/3
defined $len and $len < 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $len and $len < 0
defined $len $len < 0
Truth table0%0/3
A and B
A= defined $lenB= $len < 0
ABresult
0X0
100
111
137 0 last;
138 }
139  
140 $is_MSWin32
141 ? MCE::Channel::_read( $self->{c_sock}, $data, $len )
142 13 ▶ : read( $self->{c_sock}, $data, $len );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 13 13
143  
144 13 push @ret, @{ $thaw->($data) };
145 }
146  
147 11 ▶ wantarray ? @ret : $ret[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 10 11
148 }
149  
150 ###############################################################################
151 ## ----------------------------------------------------------------------------
152 ## Methods for two-way communication; producer(s) to consumers.
153 ##
154 ###############################################################################
155  
156 11 ▶sub send {
Subroutine send: called CC=4 SCAR=14.0
Pod documented
157 11 my $self = shift;
158 11 ▶ return MCE::Channel::_ended('send') if $self->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ended"} 1 10 11
159  
160 10 my $data = $freeze->([ @_ ]);
161  
162 10 ▶ local $\ = undef if (defined $\);
Branch: 1 branch
BranchTrueFalseTotal
if defined $\ 0 10 10
163 10 ▶ MCE::Util::_sock_ready_w( $self->{p_sock} ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 10 10
164 10 print { $self->{p_sock} } pack('i', length $data) . $data;
165  
166 10 return 1;
167 }
168  
169 5 ▶sub recv {
Subroutine recv: called CC=8 SCAR=23.7
Pod documented
170 5 my ( $self ) = @_;
171 5 my ( $plen, $data );
172  
173 5 ▶ local $/ = $LF if ( $/ ne $LF );
Branch: 1 branch
BranchTrueFalseTotal
if $/ ne $LF 0 5 5
174 5 ▶ MCE::Util::_sock_ready( $self->{c_sock} ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 5 5
175  
176 $is_MSWin32
177 ? sysread( $self->{c_sock}, $plen, 4 )
178 5 ▶ : read( $self->{c_sock}, $plen, 4 );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 5 5
179  
180 5 my $len = unpack('i', $plen);
181 5 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 0 5 5
182 0 $self->end;
183 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
184 }
185  
186 $is_MSWin32
187 ? MCE::Channel::_read( $self->{c_sock}, $data, $len )
188 5 ▶ : read( $self->{c_sock}, $data, $len );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 5 5
189  
190 5 ▶ wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
191 }
192  
193 5 ▶sub recv_nb {
Subroutine recv_nb: called CC=11 SCAR=30.7
Pod documented
194 5 my ( $self ) = @_;
195 5 my ( $plen, $data );
196  
197 5 ▶ local $/ = $LF if ( $/ ne $LF );
Branch: 1 branch
BranchTrueFalseTotal
if $/ ne $LF 0 5 5
198 5 MCE::Util::_nonblocking( $self->{c_sock}, 1 );
199  
200 $is_MSWin32
201 ? sysread( $self->{c_sock}, $plen, 4 )
202 5 ▶ : read( $self->{c_sock}, $plen, 4 );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 5 5
203  
204 5 MCE::Util::_nonblocking( $self->{c_sock}, 0 );
205  
206 5 ▶ my $len; $len = unpack('i', $plen) if $plen;
Branch: 1 branch
BranchTrueFalseTotal
if $plen 5 0 5
207 5 ▶ if ( !$len || $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $len or $len < 0) 0 5 5
Condition33%1/3
not $len or $len < 0
l!l&&r!l&&!r
005
MC/DC0%0/2
not $len or $len < 0
not $len $len < 0
Truth table33%1/3
not(A) or B
A= not $lenB= $len < 0
ABresult
1X1
011
000
208 0 ▶ $self->end if defined $len && $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len and $len < 0 0 0 0
Condition0%0/3
defined $len and $len < 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $len and $len < 0
defined $len $len < 0
Truth table0%0/3
A and B
A= defined $lenB= $len < 0
ABresult
0X0
100
111
209 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
210 }
211  
212 $is_MSWin32
213 ? MCE::Channel::_read( $self->{c_sock}, $data, $len )
214 5 ▶ : read( $self->{c_sock}, $data, $len );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 5 5
215  
216 5 ▶ wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
217 }
218  
219 ###############################################################################
220 ## ----------------------------------------------------------------------------
221 ## Methods for two-way communication; consumers to producer(s).
222 ##
223 ###############################################################################
224  
225 10 ▶sub send2 {
Subroutine send2: called CC=3 SCAR=11.2
Pod documented
226 10 my $self = shift;
227 10 my $data = $freeze->([ @_ ]);
228  
229 10 ▶ local $\ = undef if (defined $\);
Branch: 1 branch
BranchTrueFalseTotal
if defined $\ 0 10 10
230 10 ▶ MCE::Util::_sock_ready_w( $self->{c_sock} ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 10 10
231 10 print { $self->{c_sock} } pack('i', length $data) . $data;
232  
233 10 return 1;
234 }
235  
236 5 ▶sub recv2 {
Subroutine recv2: called CC=6 SCAR=18.5
Pod documented
237 5 my ( $self ) = @_;
238 5 my ( $plen, $data );
239  
240 5 ▶ local $/ = $LF if ( $/ ne $LF );
Branch: 1 branch
BranchTrueFalseTotal
if $/ ne $LF 0 5 5
241 5 ▶ MCE::Util::_sock_ready( $self->{p_sock} ) if $is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $is_MSWin32 0 5 5
242  
243 $is_MSWin32
244 ? sysread( $self->{p_sock}, $plen, 4 )
245 5 ▶ : read( $self->{p_sock}, $plen, 4 );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 5 5
246  
247 5 my $len = unpack('i', $plen);
248  
249 $is_MSWin32
250 ? MCE::Channel::_read( $self->{p_sock}, $data, $len )
251 5 ▶ : read( $self->{p_sock}, $data, $len );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 5 5
252  
253 5 ▶ wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
254 }
255  
256 5 ▶sub recv2_nb {
Subroutine recv2_nb: called CC=8 SCAR=22.2
Pod documented
257 5 my ( $self ) = @_;
258 5 my ( $plen, $data );
259  
260 5 ▶ local $/ = $LF if ( $/ ne $LF );
Branch: 1 branch
BranchTrueFalseTotal
if $/ ne $LF 0 5 5
261 5 MCE::Util::_nonblocking( $self->{p_sock}, 1 );
262  
263 $is_MSWin32
264 ? sysread( $self->{p_sock}, $plen, 4 )
265 5 ▶ : read( $self->{p_sock}, $plen, 4 );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 5 5
266  
267 5 MCE::Util::_nonblocking( $self->{p_sock}, 0 );
268  
269 5 ▶ my $len; $len = unpack('i', $plen) if $plen;
Branch: 1 branch
BranchTrueFalseTotal
if $plen 5 0 5
270 5 ▶ return wantarray ? () : undef unless $len;
Branch: 2 branches
BranchTrueFalseTotal
unless $len 0 5 5
wantarray ? : 0 0 0
271  
272 $is_MSWin32
273 ? MCE::Channel::_read( $self->{p_sock}, $data, $len )
274 5 ▶ : read( $self->{p_sock}, $data, $len );
Branch: 1 branch
BranchTrueFalseTotal
$is_MSWin32 ? : 0 5 5
275  
276 5 ▶ wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
277 }
278  
279 1 1;
280  
281 __END__
« MCE/Candy.pm ↑ Summary MCE/Channel/SimpleFast.pm »