MCE/Channel/ThreadsFast.pm untested

filter: Statementstmt 0.0% 0 / 177 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 395 CC 55 SCAR 40.1
CC
55
Cov
0%
dequeue
132.0
recv2_nb
90.0
dequeue_nb
56.0
CRAP 55.1
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Channel for producer(s) and many consumers supporting threads only.
4 ##
5 ###############################################################################
6  
7 package MCE::Channel::ThreadsFast;
8  
9 use strict;
10 use warnings;
11  
12 no warnings qw( uninitialized once );
13  
14 our $VERSION = '1.904';
15  
16 use threads;
17 use threads::shared;
18  
19 use base 'MCE::Channel';
20  
21 my $LF = "\012"; Internals::SvREADONLY($LF, 1);
22 my $is_MSWin32 = ( $^O eq 'MSWin32' ) ? 1 : 0;
23  
24 sub new {
25 my ( $class, %obj ) = ( @_, impl => 'ThreadsFast' );
26  
27 $obj{init_pid} = MCE::Channel::_pid();
28 MCE::Util::_sock_pair( \%obj, 'p_sock', 'c_sock' );
29 MCE::Util::_sock_pair( \%obj, 'p2_sock', 'c2_sock' ) if $is_MSWin32;
30  
31 # locking for the consumer side of the channel
32 $obj{cr_mutex} = threads::shared::share( my $cr_mutex );
33 $obj{cw_mutex} = threads::shared::share( my $cw_mutex );
34  
35 # optionally, support many-producers writing and reading
36 $obj{pr_mutex} = threads::shared::share( my $pr_mutex ) if $obj{mp};
37 $obj{pw_mutex} = threads::shared::share( my $pw_mutex ) if $obj{mp};
38  
39 return bless \%obj, $class;
40 }
41  
42 ###############################################################################
43 ## ----------------------------------------------------------------------------
44 ## Queue-like methods.
45 ##
46 ###############################################################################
47  
48 sub end {
49 my ( $self ) = @_;
50  
51 local $\ = undef if (defined $\);
52 MCE::Util::_sock_ready_w( $self->{p_sock} ) if $is_MSWin32;
53 print { $self->{p_sock} } pack('i', -1);
54  
55 $self->{ended} = 1;
56 }
57  
58 sub enqueue {
59 my $self = shift;
60 return MCE::Channel::_ended('enqueue') if $self->{ended};
61  
62 local $\ = undef if (defined $\);
63  
64 {
65 CORE::lock $self->{pw_mutex} if $self->{pw_mutex};
66 MCE::Util::_sock_ready_w( $self->{p_sock} ) if $is_MSWin32;
67  
68 while ( @_ ) {
69 my $data = ''.shift;
70 print { $self->{p_sock} } pack('i', length $data), $data;
71 }
72 }
73  
74 return 1;
75 }
76  
77 sub dequeue {
78 my ( $self, $count ) = @_;
79 $count = 1 if ( !$count || $count < 1 );
80  
81 if ( $count == 1 ) {
82 my ( $plen, $data );
83  
84 {
85 CORE::lock $self->{cr_mutex};
86 MCE::Util::_sock_ready( $self->{c_sock} ) if $is_MSWin32;
87 MCE::Util::_sysread( $self->{c_sock}, $plen, 4 );
88  
89 my $len = unpack('i', $plen);
90 if ( $len < 0 ) {
91 $self->end;
92 return wantarray ? () : undef;
93 }
94  
95 return '' unless $len;
96 MCE::Channel::_read( $self->{c_sock}, $data, $len );
97 }
98  
99 $data;
100 }
101 else {
102 my ( $plen, @ret );
103  
104 {
105 CORE::lock $self->{cr_mutex};
106 MCE::Util::_sock_ready( $self->{c_sock} ) if $is_MSWin32;
107  
108 while ( $count-- ) {
109 MCE::Util::_sysread( $self->{c_sock}, $plen, 4 );
110  
111 my $len = unpack('i', $plen);
112 if ( $len < 0 ) {
113 $self->end;
114 last;
115 }
116  
117 push(@ret, ''), next unless $len;
118 MCE::Channel::_read( $self->{c_sock}, my($data), $len );
119 push @ret, $data;
120 }
121 }
122  
123 wantarray ? @ret : $ret[-1];
124 }
125 }
126  
127 sub dequeue_nb {
128 my ( $self, $count ) = @_;
129 $count = 1 if ( !$count || $count < 1 );
130  
131 my ( $plen, @ret );
132  
133 {
134 CORE::lock $self->{cr_mutex};
135  
136 while ( $count-- ) {
137 MCE::Util::_nonblocking( $self->{c_sock}, 1 );
138 MCE::Util::_sysread( $self->{c_sock}, $plen, 4 );
139 MCE::Util::_nonblocking( $self->{c_sock}, 0 );
140  
141 my $len; $len = unpack('i', $plen) if $plen;
142 if ( !$len || $len < 0 ) {
143 $self->end if defined $len && $len < 0;
144 push @ret, '' if defined $len && $len == 0;
145 last;
146 }
147  
148 MCE::Channel::_read( $self->{c_sock}, my($data), $len );
149 push @ret, $data;
150 }
151 }
152  
153 wantarray ? @ret : $ret[-1];
154 }
155  
156 ###############################################################################
157 ## ----------------------------------------------------------------------------
158 ## Methods for two-way communication; producer(s) to consumers.
159 ##
160 ###############################################################################
161  
162 sub send {
163 my $self = shift;
164 return MCE::Channel::_ended('send') if $self->{ended};
165  
166 my $data = ''.shift;
167  
168 local $\ = undef if (defined $\);
169  
170 {
171 CORE::lock $self->{pw_mutex} if $self->{pw_mutex};
172 MCE::Util::_sock_ready_w( $self->{p_sock} ) if $is_MSWin32;
173 print { $self->{p_sock} } pack('i', length $data), $data;
174 }
175  
176 return 1;
177 }
178  
179 sub recv {
180 my ( $self ) = @_;
181 my ( $plen, $data );
182  
183 {
184 CORE::lock $self->{cr_mutex};
185 MCE::Util::_sock_ready( $self->{c_sock} ) if $is_MSWin32;
186 MCE::Util::_sysread( $self->{c_sock}, $plen, 4 );
187  
188 my $len = unpack('i', $plen);
189 if ( $len < 0 ) {
190 $self->end;
191 return wantarray ? () : undef;
192 }
193  
194 return '' unless $len;
195  
196 MCE::Channel::_read( $self->{c_sock}, $data, $len );
197 }
198  
199 $data;
200 }
201  
202 sub recv_nb {
203 my ( $self ) = @_;
204 my ( $plen, $data );
205  
206 {
207 CORE::lock $self->{cr_mutex};
208 MCE::Util::_nonblocking( $self->{c_sock}, 1 );
209 MCE::Util::_sysread( $self->{c_sock}, $plen, 4 );
210 MCE::Util::_nonblocking( $self->{c_sock}, 0 );
211  
212 my $len; $len = unpack('i', $plen) if $plen;
213 if ( !$len || $len < 0 ) {
214 $self->end if defined $len && $len < 0;
215 return '' if defined $len && $len == 0;
216 return wantarray ? () : undef;
217 }
218  
219 MCE::Channel::_read( $self->{c_sock}, $data, $len );
220 }
221  
222 $data;
223 }
224  
225 ###############################################################################
226 ## ----------------------------------------------------------------------------
227 ## Methods for two-way communication; consumers to producer(s).
228 ##
229 ###############################################################################
230  
231 sub send2 {
232 my $self = shift;
233 my $data = ''.shift;
234  
235 local $\ = undef if (defined $\);
236  
237 {
238 my $c_sock = $self->{c2_sock} || $self->{c_sock};
239  
240 CORE::lock $self->{cw_mutex};
241 MCE::Util::_sock_ready_w( $c_sock ) if $is_MSWin32;
242 print { $c_sock } pack('i', length $data), $data;
243 }
244  
245 return 1;
246 }
247  
248 sub recv2 {
249 my ( $self ) = @_;
250 my ( $plen, $data );
251  
252 local $/ = $LF if ( $/ ne $LF );
253  
254 {
255 my $p_sock = $self->{p2_sock} || $self->{p_sock};
256 my $pr_mutex = $self->{pr_mutex};
257  
258 CORE::lock $pr_mutex if $pr_mutex;
259 MCE::Util::_sock_ready( $p_sock ) if $is_MSWin32;
260  
261 ( $pr_mutex || $is_MSWin32 )
262 ? MCE::Util::_sysread( $p_sock, $plen, 4 )
263 : read( $p_sock, $plen, 4 );
264  
265 my $len = unpack('i', $plen);
266 return '' unless $len;
267  
268 ( $pr_mutex || $is_MSWin32 )
269 ? MCE::Channel::_read( $p_sock, $data, $len )
270 : read( $p_sock, $data, $len );
271 }
272  
273 $data;
274 }
275  
276 sub recv2_nb {
277 my ( $self ) = @_;
278 my ( $plen, $data );
279  
280 local $/ = $LF if ( $/ ne $LF );
281  
282 {
283 my $p_sock = $self->{p2_sock} || $self->{p_sock};
284 my $pr_mutex = $self->{pr_mutex};
285  
286 CORE::lock $pr_mutex if $pr_mutex;
287 MCE::Util::_nonblocking( $p_sock, 1 );
288  
289 ( $pr_mutex || $is_MSWin32 )
290 ? MCE::Util::_sysread( $p_sock, $plen, 4 )
291 : read( $p_sock, $plen, 4 );
292  
293 MCE::Util::_nonblocking( $p_sock, 0 );
294  
295 my $len; $len = unpack('i', $plen) if $plen;
296  
297 return '' if defined $len && $len == 0;
298 return wantarray ? () : undef unless $len;
299  
300 ( $pr_mutex || $is_MSWin32 )
301 ? MCE::Channel::_read( $p_sock, $data, $len )
302 : read( $p_sock, $data, $len );
303 }
304  
305 $data;
306 }
307  
308 1;
309  
310 __END__