Shared/Condvar.pm

filter: Statementstmt 47.0% 87 / 185 Branchbran 20.6% 12 / 58 Conditioncond 6.0% 2 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 44.6% 21 / 47 Podpod 100% 18 / 18 totaltotal 38.7% 140 / 361 CC 43 SCAR 36.2
CC
43
Cov
37%
timedwait
210.0
broadcast
20.0
__ANON__
12.0
CRAP 37.4
« Shared/Base.pm ↑ Summary Shared/Array.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Condvar helper class.
4 ##
5 ###############################################################################
6  
7 package MCE::Shared::Condvar;
8  
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶no warnings qw( threads recursion uninitialized numeric );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 our $VERSION = '1.894';
17  
18 3 ▶use MCE::Shared::Base ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use MCE::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use MCE::Mutex ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 q("") => \&MCE::Shared::Base::_stringify,
24 q(0+) => \&MCE::Shared::Base::_numify,
25 fallback => 1
26 3 );
27  
28 3 my $LF = "\012"; Internals::SvREADONLY($LF, 1);
29 3 ▶my $_tid = $INC{'threads.pm'} ? threads->tid() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 3 3
30 3 my $_reset_flg = 1;
31  
32 0 ▶sub CLONE {
Subroutine CLONE: not called CC=2 SCAR=17.9
33 0 ▶ $_tid = threads->tid() if $INC{'threads.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 0 0
34 }
35  
36 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.3
37 3 my ($_cv) = @_;
38 3 ▶ my $_pid = $_tid ? $$ .'.'. $_tid : $$;
Branch: 1 branch
BranchTrueFalseTotal
$_tid ? : 0 3 3
39  
40 3 ▶ if ($_cv->{_init_pid} eq $_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_cv->{"_init_pid"} eq $_pid) 3 0 3
41 3 MCE::Util::_destroy_socks($_cv, qw(_cw_sock _cr_sock));
42 }
43  
44 3 return;
45 }
46  
47 ###############################################################################
48 ## ----------------------------------------------------------------------------
49 ## Public methods.
50 ##
51 ###############################################################################
52  
53 3 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod documented
54 3 my ($_class, $_cv) = (shift, {});
55  
56 3 ▶ $_cv->{_init_pid} = $_tid ? $$ .'.'. $_tid : $$;
Branch: 1 branch
BranchTrueFalseTotal
$_tid ? : 0 3 3
57 3 ▶ $_cv->{_value} = shift || 0;
Condition50%1/2
shift() || 0
l!l
03
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
58 3 $_cv->{_count} = 0;
59  
60 3 MCE::Util::_sock_pair($_cv, qw(_cr_sock _cw_sock), undef, 1);
61  
62 MCE::Shared::Object::_reset(), $_reset_flg = ''
63 3 ▶ if $_reset_flg && $INC{'MCE/Shared/Server.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $_reset_flg and $INC{"MCE/Shared/Server.pm"} 3 0 3
Condition33%1/3
$_reset_flg and $INC{"MCE/Shared/Server.pm"}
!ll&&!rl&&r
003
MC/DC0%0/2
$_reset_flg and $INC{"MCE/Shared/Server.pm"}
$_reset_flg $INC{"MCE/Shared/Server.pm"}
Truth table33%1/3
A and B
A= $_reset_flgB= $INC{"MCE/Shared/Server.pm"}
ABresult
0X0
100
111
64  
65 3 bless $_cv, $_class;
66 }
67  
68 0 ▶sub get { $_[0]->{_value} }
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
69 0 ▶sub set { $_[0]->{_value} = $_[1] }
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
70  
71 # The following methods applies to shared-context only and handled by
72 # MCE::Shared::Object.
73  
74 0 ▶sub lock { }
Subroutine lock: not called CC=1 SCAR=0.0
Pod documented
75 0 ▶sub unlock { }
Subroutine unlock: not called CC=1 SCAR=0.0
Pod documented
76  
77 0 ▶sub broadcast { }
Subroutine broadcast: not called CC=1 SCAR=0.0
Pod documented
78 0 ▶sub signal { }
Subroutine signal: not called CC=1 SCAR=0.0
Pod documented
79 0 ▶sub timedwait { }
Subroutine timedwait: not called CC=1 SCAR=0.0
Pod documented
80 0 ▶sub wait { }
Subroutine wait: not called CC=1 SCAR=0.0
Pod documented
81  
82 ###############################################################################
83 ## ----------------------------------------------------------------------------
84 ## Sugar API, mostly resembles https://redis.io/commands#string primitives.
85 ##
86 ###############################################################################
87  
88 # append ( string )
89  
90 0 ▶sub append {
Subroutine append: not called CC=2 SCAR=17.9
Pod documented
91 0 ▶ length( $_[0]->{_value} .= $_[1] // '' );
Condition0%0/2
$_[1] // ""
l!l
00
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
92 }
93  
94 # decr
95 # decrby ( number )
96 # incr
97 # incrby ( number )
98 # getdecr
99 # getincr
100  
101 0 ▶sub decr { --$_[0]->{_value} }
Subroutine decr: not called CC=1 SCAR=6.9
Pod documented
102 0 ▶sub decrby { $_[0]->{_value} -= $_[1] || 0 }
Subroutine decrby: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[1] || 0
l!l
00
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
103 0 ▶sub incr { ++$_[0]->{_value} }
Subroutine incr: not called CC=1 SCAR=6.9
Pod documented
104 0 ▶sub incrby { $_[0]->{_value} += $_[1] || 0 }
Subroutine incrby: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[1] || 0
l!l
00
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
105 0 ▶sub getdecr { $_[0]->{_value}-- // 0 }
Subroutine getdecr: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{"_value"}-- // 0
l!l
00
MC/DC0%0/1
$_[0]{"_value"}-- // 0
$_[0]{"_value"}--
Truth table0%0/2
A
A= $_[0]{"_value"}--
Aresult
11
00
106 0 ▶sub getincr { $_[0]->{_value}++ // 0 }
Subroutine getincr: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{"_value"}++ // 0
l!l
00
MC/DC0%0/1
$_[0]{"_value"}++ // 0
$_[0]{"_value"}++
Truth table0%0/2
A
A= $_[0]{"_value"}++
Aresult
11
00
107  
108 # getset ( value )
109  
110 0 ▶sub getset {
Subroutine getset: not called CC=1 SCAR=6.9
Pod documented
111 0 my $old = $_[0]->{_value};
112 0 $_[0]->{_value} = $_[1];
113  
114 0 $old;
115 }
116  
117 # len ( )
118  
119 0 ▶sub len {
Subroutine len: not called CC=1 SCAR=6.9
Pod documented
120 0 length $_[0]->{_value};
121 }
122  
123 ###############################################################################
124 ## ----------------------------------------------------------------------------
125 ## Server functions.
126 ##
127 ###############################################################################
128  
129 {
130 3 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 3 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
133 3 SHR_O_CVB => 'O~CVB', # Condvar broadcast
134 SHR_O_CVS => 'O~CVS', # Condvar signal
135 SHR_O_CVT => 'O~CVT', # Condvar timedwait
136 SHR_O_CVW => 'O~CVW', # Condvar wait
137 3 };
138  
139 3 my ( $_DAU_R_SOCK_REF, $_DAU_R_SOCK, $_obj, $_id );
140  
141 my %_output_function = (
142  
143 0 ▶ SHR_O_CVB.$LF => sub { # Condvar broadcast
Subroutine __ANON__: not called CC=3 SCAR=24.8
144 0 $_DAU_R_SOCK = ${ $_DAU_R_SOCK_REF };
145 0 chomp($_id = <$_DAU_R_SOCK>);
146  
147 0 ▶ my $_var = $_obj->{ $_id } || do {
Condition0%0/3
$_obj->{$_id} || do { print {$_DAU_R_SOCK;} $LF; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
$_obj->{$_id} || do { print {$_DAU_R_SOCK;} $LF; return }
$_obj->{$_id} do { print {$_DAU_R_SOCK;} $LF; return }
Truth table0%0/3
A || B
A= $_obj->{$_id}B= do { print {$_DAU_R_SOCK;} $LF; return }
ABresult
1X1
011
000
148 0 print {$_DAU_R_SOCK} $LF;
149 0 return;
150 };
151 0 for my $_i (1 .. $_var->{_count}) {
152 0 syswrite($_var->{_cw_sock}, $LF);
153 }
154  
155 0 $_var->{_count} = 0;
156 0 print {$_DAU_R_SOCK} $LF;
157  
158 0 return;
159 },
160  
161 0 ▶ SHR_O_CVS.$LF => sub { # Condvar signal
Subroutine __ANON__: not called CC=3 SCAR=24.8
162 0 $_DAU_R_SOCK = ${ $_DAU_R_SOCK_REF };
163 0 chomp($_id = <$_DAU_R_SOCK>);
164  
165 0 ▶ my $_var = $_obj->{ $_id } || do {
Condition0%0/3
$_obj->{$_id} || do { print {$_DAU_R_SOCK;} $LF; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
$_obj->{$_id} || do { print {$_DAU_R_SOCK;} $LF; return }
$_obj->{$_id} do { print {$_DAU_R_SOCK;} $LF; return }
Truth table0%0/3
A || B
A= $_obj->{$_id}B= do { print {$_DAU_R_SOCK;} $LF; return }
ABresult
1X1
011
000
166 0 print {$_DAU_R_SOCK} $LF;
167 0 return;
168 };
169 0 ▶ if ( $_var->{_count} >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_var->{"_count"} >= 0) 0 0 0
170 0 syswrite($_var->{_cw_sock}, $LF);
171 0 $_var->{_count} -= 1;
172 }
173  
174 0 print {$_DAU_R_SOCK} $LF;
175  
176 0 return;
177 },
178  
179 0 ▶ SHR_O_CVT.$LF => sub { # Condvar timedwait
Subroutine __ANON__: not called CC=2 SCAR=17.9
180 0 $_DAU_R_SOCK = ${ $_DAU_R_SOCK_REF };
181 0 chomp($_id = <$_DAU_R_SOCK>);
182  
183 0 ▶ my $_var = $_obj->{ $_id } || do {
Condition0%0/3
$_obj->{$_id} || do { print {$_DAU_R_SOCK;} $LF; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
$_obj->{$_id} || do { print {$_DAU_R_SOCK;} $LF; return }
$_obj->{$_id} do { print {$_DAU_R_SOCK;} $LF; return }
Truth table0%0/3
A || B
A= $_obj->{$_id}B= do { print {$_DAU_R_SOCK;} $LF; return }
ABresult
1X1
011
000
184 0 print {$_DAU_R_SOCK} $LF;
185 0 return;
186 };
187  
188 0 $_var->{_count} -= 1;
189 0 print {$_DAU_R_SOCK} $LF;
190  
191 0 return;
192 },
193  
194 0 ▶ SHR_O_CVW.$LF => sub { # Condvar wait
Subroutine __ANON__: not called CC=2 SCAR=17.9
195 0 $_DAU_R_SOCK = ${ $_DAU_R_SOCK_REF };
196 0 chomp($_id = <$_DAU_R_SOCK>);
197  
198 0 ▶ my $_var = $_obj->{ $_id } || do {
Condition0%0/3
$_obj->{$_id} || do { print {$_DAU_R_SOCK;} $LF; return }
l!l&&r!l&&!r
000
MC/DC0%0/2
$_obj->{$_id} || do { print {$_DAU_R_SOCK;} $LF; return }
$_obj->{$_id} do { print {$_DAU_R_SOCK;} $LF; return }
Truth table0%0/3
A || B
A= $_obj->{$_id}B= do { print {$_DAU_R_SOCK;} $LF; return }
ABresult
1X1
011
000
199 0 print {$_DAU_R_SOCK} $LF;
200 0 return;
201 };
202  
203 0 $_var->{_count} += 1;
204 0 print {$_DAU_R_SOCK} $LF;
205  
206 0 return;
207 },
208  
209 3 );
210  
211 0 ▶ sub _init_mgr {
Subroutine _init_mgr: not called CC=3 SCAR=24.8
212 0 my $_function;
213 0 ( $_DAU_R_SOCK_REF, $_obj, $_function ) = @_;
214  
215 0 for my $key ( keys %_output_function ) {
216 0 ▶ last if exists($_function->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_function->{$key} 0 0 0
217 0 $_function->{$key} = $_output_function{$key};
218 }
219  
220 0 return;
221 }
222 }
223  
224 ###############################################################################
225 ## ----------------------------------------------------------------------------
226 ## Object package.
227 ##
228 ###############################################################################
229  
230 ## Items below are folded into MCE::Shared::Object.
231  
232 package # hide from rpm
233 3 MCE::Shared::Object;
234  
235 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
237  
238 3 ▶no warnings qw( threads recursion uninitialized numeric once );
Subroutine BEGIN: called CC=1 SCAR=0.0
239  
240 3 ▶use Time::HiRes qw( alarm sleep );
Subroutine BEGIN: called CC=1 SCAR=0.0
241 3 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
242  
243 3 ▶no overloading;
Subroutine BEGIN: called CC=1 SCAR=0.0
244  
245 3 ▶my $_is_MSWin32 = ($^O eq 'MSWin32') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 3 3
246  
247 3 my ($_DAT_LOCK, $_DAT_W_SOCK, $_DAU_W_SOCK, $_dat_ex, $_dat_un, $_chn, $_obj);
248  
249 8 ▶sub _init_condvar {
Subroutine _init_condvar: called CC=1 SCAR=0.0
250 8 ($_DAT_LOCK, $_DAT_W_SOCK, $_DAU_W_SOCK, $_dat_ex, $_dat_un, $_chn, $_obj) = @_;
251  
252 8 return;
253 }
254  
255 # broadcast ( floating_seconds )
256 # broadcast ( )
257  
258 0 ▶sub broadcast {
Subroutine broadcast: not called CC=4 SCAR=30.0
259 0 my $_id = $_[0]->[0];
260 0 ▶ return unless ( my $_CV = $_obj->{ $_id } );
Branch: 1 branch
BranchTrueFalseTotal
unless my $_CV = $_obj->{$_id} 0 0 0
261 0 ▶ return unless ( exists $_CV->{_cr_sock} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_CV->{"_cr_sock"} 0 0 0
262  
263 0 ▶ sleep($_[1]) if defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
264  
265 0 _req1('O~CVB', $_id.$LF);
266 0 $_[0]->[6]->unlock();
267  
268 0 sleep(0);
269 }
270  
271 # signal ( floating_seconds )
272 # signal ( )
273  
274 1 ▶sub signal {
Subroutine signal: called CC=4 SCAR=14.3
275 1 my $_id = $_[0]->[0];
276 1 ▶ return unless ( my $_CV = $_obj->{ $_id } );
Branch: 1 branch
BranchTrueFalseTotal
unless my $_CV = $_obj->{$_id} 0 1 1
277 1 ▶ return unless ( exists $_CV->{_cr_sock} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_CV->{"_cr_sock"} 0 1 1
278  
279 1 ▶ sleep($_[1]) if defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 1 1
280  
281 1 _req1('O~CVS', $_id.$LF);
282 1 $_[0]->[6]->unlock();
283  
284 1 sleep(0);
285 }
286  
287 # timedwait ( floating_seconds )
288  
289 0 ▶sub timedwait {
Subroutine timedwait: not called CC=14 SCAR=53.5
290 0 my $_id = $_[0]->[0];
291 0 my $_timeout = $_[1];
292  
293 0 ▶ return unless ( my $_CV = $_obj->{ $_id } );
Branch: 1 branch
BranchTrueFalseTotal
unless my $_CV = $_obj->{$_id} 0 0 0
294 0 ▶ return unless ( exists $_CV->{_cr_sock} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_CV->{"_cr_sock"} 0 0 0
295 0 ▶ return $_[0]->wait() unless $_timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless $_timeout 0 0 0
296  
297 0 ▶ _croak('Condvar: timedwait (timeout) is not valid')
Branch: 1 branch
BranchTrueFalseTotal
if not &looks_like_number($_timeout) or $_timeout < 0 0 0 0
Condition0%0/3
not &looks_like_number($_timeout) or $_timeout < 0
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
298 if (!looks_like_number($_timeout) || $_timeout < 0);
299  
300 0 _req1('O~CVW', $_id.$LF);
301 0 $_[0]->[6]->unlock();
302  
303 0 ▶ $_timeout = 0.0003 if $_timeout < 0.0003;
Branch: 1 branch
BranchTrueFalseTotal
if $_timeout < "0.0003" 0 0 0
304  
305 0 local $@; eval {
306 0 ▶ local $SIG{ALRM} = sub { alarm 0; die "alarm clock restart\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
307 0 ▶ alarm $_timeout unless $_is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
unless $_is_MSWin32 0 0 0
308  
309 die "alarm clock restart\n"
310 0 ▶ if $_is_MSWin32 && MCE::Util::_sock_ready($_CV->{_cr_sock}, $_timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $_is_MSWin32 and MCE::Util::_sock_ready($_CV->{"_cr_sock"}, $_timeout) 0 0 0
Condition0%0/3
$_is_MSWin32 and MCE::Util::_sock_ready($_CV->{"_cr_sock"}, $_timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$_is_MSWin32 and MCE::Util::_sock_ready($_CV->{"_cr_sock"}, $_timeout)
$_is_MSWin32 MCE::Util::_sock_ready($_CV->{"_cr_sock"}, $_timeout)
Truth table0%0/3
A and B
A= $_is_MSWin32B= MCE::Util::_sock_ready($_CV->{"_cr_sock"}, $_timeout)
ABresult
0X0
100
111
311  
312 (!$_is_MSWin32)
313 ? (MCE::Util::_sysread($_CV->{_cr_sock}, my($_b1), 1), alarm(0))
314 0 ▶ : (MCE::Util::_sysread($_CV->{_cr_sock}, my($_b2), 1));
Branch: 1 branch
BranchTrueFalseTotal
!$_is_MSWin32 ? : 0 0 0
315 };
316  
317 0 ▶ alarm 0 unless $_is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
unless $_is_MSWin32 0 0 0
318  
319 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
320 0 ▶ chomp($@), _croak($@) unless $@ eq "alarm clock restart\n";
Branch: 1 branch
BranchTrueFalseTotal
unless $@ eq "alarm clock restart\n" 0 0 0
321 0 _req1('O~CVT', $_id.$LF);
322  
323 0 return '';
324 }
325  
326 0 return 1;
327 }
328  
329 # wait ( )
330  
331 2 ▶sub wait {
Subroutine wait: called CC=4 SCAR=14.2
332 2 my $_id = $_[0]->[0];
333 2 ▶ return unless ( my $_CV = $_obj->{ $_id } );
Branch: 1 branch
BranchTrueFalseTotal
unless my $_CV = $_obj->{$_id} 0 2 2
334 2 ▶ return unless ( exists $_CV->{_cr_sock} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_CV->{"_cr_sock"} 0 2 2
335  
336 2 _req1('O~CVW', $_id.$LF);
337 2 $_[0]->[6]->unlock();
338  
339 2 ▶ MCE::Util::_sock_ready($_CV->{_cr_sock}) if $_is_MSWin32;
Branch: 1 branch
BranchTrueFalseTotal
if $_is_MSWin32 0 2 2
340 2 MCE::Util::_sysread($_CV->{_cr_sock}, my($_b), 1);
341  
342 2 return 1;
343 }
344  
345 3 1;
346  
347 __END__
« Shared/Base.pm ↑ Summary Shared/Array.pm »