blib/lib/IO/Async/Loop/EV.pm

filter: Statementstmt 100% 101 / 101 Branchbran 78.5% 22 / 28 Conditioncond 50.0% 3 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 26 / 26 Podpod 100% 12 / 12 totaltotal 93.7% 165 / 176 CC 18 SCAR 9.4
CC
18
Cov
93%
new
5.1
watch_idle
4.1
watch_io
4.0
CRAP 2.6
« Build ↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2012-2020 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::EV;
7  
8 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 our $VERSION = '0.03';
12 17 ▶use constant API_VERSION => '0.76';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use base qw( IO::Async::Loop );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 IO::Async::Loop->VERSION( '0.49' );
16  
17 17 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 17 ▶use IO::Async::Metrics '$METRICS';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 17 ▶use constant _CAN_SUBSECOND_ACCURATELY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 17 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =head1 NAME
28  
29 C<IO::Async::Loop::EV> - use C<IO::Async> with C<EV>
30  
31 =head1 SYNOPSIS
32  
33 use IO::Async::Loop::EV;
34  
35 my $loop = IO::Async::Loop::EV->new();
36  
37 $loop->add( ... );
38  
39 $loop->add( IO::Async::Signal->new(
40 name => 'HUP',
41 on_receipt => sub { ... },
42 ) );
43  
44 $loop->run;
45  
46 =head1 DESCRIPTION
47  
48 This subclass of L<IO::Async::Loop> uses L<EV> to perform its work.
49  
50 =cut
51  
52 sub new
53 {
54 16 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.3
Pod documented
55 16 my $self = $class->SUPER::__new( @_ );
56  
57 16 $self->{$_} = {} for qw( watch_r watch_w watch_time watch_signal watch_idle watch_process );
58  
59 # Check it's actually active
60 16 ▶ if( defined $METRICS and $METRICS->adapter and $METRICS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $IO::Async::Loop::EV::METRICS and $IO::Async::Loop::EV::METRICS->adapter and $IO::Async::Loop::EV::METRICS) 12 4 16
Condition50%3/6
defined $IO::Async::Loop::EV::METRICS and $IO::Async::Loop::EV::METRICS->adapter and $IO::Async::Loop::EV::METRICS
!ll&&!rl&&r
0412
defined $IO::Async::Loop::EV::METRICS and $IO::Async::Loop::EV::METRICS->adapter
!ll&&!rl&&r
0016
MC/DC33%1/3
defined $IO::Async::Loop::EV::METRICS and $IO::Async::Loop::EV::METRICS->adapter and $IO::Async::Loop::EV::METRICS
defined $IO::Async::Loop::EV::METRICS $IO::Async::Loop::EV::METRICS->adapter $IO::Async::Loop::EV::METRICS
Truth table50%2/4
A and B and C
A= defined $IO::Async::Loop::EV::METRICSB= $IO::Async::Loop::EV::METRICS->adapterC= $IO::Async::Loop::EV::METRICS
ABCresult
0XX0
10X0
1100
1111
61 12 weaken( my $weakself = $self );
62 12 ▶ $self->{watch_prepare} = EV::prepare sub { $weakself->pre_wait };
Subroutine __ANON__: called CC=1 SCAR=0.0
63 12 ▶ $self->{watch_check} = EV::check sub { $weakself->post_wait };
Subroutine __ANON__: called CC=1 SCAR=0.0
64 }
65  
66 16 return $self;
67 }
68  
69 sub loop_once
70 {
71 87 ▶ my $self = shift;
Subroutine loop_once: called CC=2 SCAR=6.9
Pod documented
72 87 my ( $timeout ) = @_;
73  
74 87 my $timeout_w;
75 87 ▶ if( defined $timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 74 13 87
76 74 ▶ $timeout_w = EV::timer $timeout, 0, sub {}; # simply to wake up RUN_ONCE
Subroutine __ANON__: called CC=1 SCAR=0.0
77 }
78  
79 87 EV::run( EV::RUN_ONCE );
80 }
81  
82 sub watch_io
83 {
84 15 ▶ my $self = shift;
Subroutine watch_io: called CC=4 SCAR=13.9
Pod documented
85 15 my %params = @_;
86  
87 15 ▶ my $handle = $params{handle} or die "Need a handle";
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $params{"handle"} or die "Need a handle" 0 15 15
88  
89 15 ▶ if( my $on_read_ready = $params{on_read_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_read_ready = $params{"on_read_ready"}) 10 5 15
90 10 $self->{watch_r}{$handle} = EV::io( $handle, EV::READ, $on_read_ready );
91 }
92  
93 15 ▶ if( my $on_write_ready = $params{on_write_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_write_ready = $params{"on_write_ready"}) 7 8 15
94 7 $self->{watch_w}{$handle} = EV::io( $handle, EV::WRITE, $on_write_ready );
95 }
96 }
97  
98 sub unwatch_io
99 {
100 11 ▶ my $self = shift;
Subroutine unwatch_io: called CC=4 SCAR=13.9
Pod documented
101 11 my %params = @_;
102  
103 11 ▶ my $handle = $params{handle} or die "Need a handle";
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $params{"handle"} or die "Need a handle" 0 11 11
104  
105 11 ▶ if( $params{on_read_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"on_read_ready"}) 8 3 11
106 8 delete $self->{watch_r}{$handle};
107 }
108  
109 11 ▶ if( $params{on_write_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"on_write_ready"}) 7 4 11
110 7 delete $self->{watch_w}{$handle};
111 }
112 }
113  
114 sub watch_time
115 {
116 28 ▶ my $self = shift;
Subroutine watch_time: called CC=3 SCAR=11.0
Pod documented
117 28 my %params = @_;
118  
119 28 ▶ my $code = $params{code} or croak "Expected 'code' as CODE ref";
Branch: 1 branch
BranchTrueFalseTotal
my $code = $params{"code"} or croak("Expected 'code' as CODE ref") 0 28 28
120  
121 28 my $w;
122 28 ▶ if( defined $params{after} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'after'}) { } 27 1 28
123 27 $w = EV::timer $params{after}, 0, $code;
124 }
125 else {
126 1 $w = EV::periodic $params{at}, 0, 0, $code;
127 }
128  
129 28 return $self->{watch_time}{$w} = $w;
130 }
131  
132 sub unwatch_time
133 {
134 17 ▶ my $self = shift;
Subroutine unwatch_time: called CC=1 SCAR=0.0
Pod documented
135 17 my ( $id ) = @_;
136  
137 17 delete $self->{watch_time}{$id};
138 }
139  
140 sub watch_signal
141 {
142 5 ▶ my $self = shift;
Subroutine watch_signal: called CC=2 SCAR=6.9
Pod documented
143 5 my ( $signal, $code ) = @_;
144  
145 5 ▶ defined $self->signame2num( $signal ) or croak "No such signal '$signal'";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->signame2num($signal) or croak("No such signal '${signal}'") 1 4 5
146  
147 4 $self->{watch_signal}{$signal} = EV::signal $signal, $code;
148 }
149  
150 sub unwatch_signal
151 {
152 2 ▶ my $self = shift;
Subroutine unwatch_signal: called CC=1 SCAR=0.0
Pod documented
153 2 my ( $signal ) = @_;
154  
155 2 delete $self->{watch_signal}{$signal};
156 }
157  
158 sub watch_idle
159 {
160 6 ▶ my $self = shift;
Subroutine watch_idle: called CC=4 SCAR=14.0
Pod documented
161 6 my %params = @_;
162  
163 6 ▶ my $when = delete $params{when} or croak "Expected 'when'";
Branch: 1 branch
BranchTrueFalseTotal
my $when = delete $params{"when"} or croak("Expected 'when'") 0 6 6
164  
165 6 ▶ my $code = delete $params{code} or croak "Expected 'code' as a CODE ref";
Branch: 1 branch
BranchTrueFalseTotal
my $code = delete $params{"code"} or croak("Expected 'code' as a CODE ref") 0 6 6
166  
167 6 ▶ $when eq "later" or croak "Expected 'when' to be 'later'";
Branch: 1 branch
BranchTrueFalseTotal
$when eq "later" or croak("Expected 'when' to be 'later'") 0 6 6
168  
169 6 my $key;
170 my $w = EV::idle sub {
171 5 ▶ delete $self->{watch_idle}{$key};
Subroutine __ANON__: called CC=1 SCAR=0.0
172 5 goto &$code;
173 6 };
174  
175 6 $key = "$w";
176 6 $self->{watch_idle}{$key} = $w;
177 6 return $key;
178 }
179  
180 sub unwatch_idle
181 {
182 1 ▶ my $self = shift;
Subroutine unwatch_idle: called CC=1 SCAR=0.0
Pod documented
183 1 my ( $id ) = @_;
184  
185 1 delete $self->{watch_idle}{$id};
186 }
187  
188 sub watch_process
189 {
190 17 ▶ my $self = shift;
Subroutine watch_process: called CC=1 SCAR=0.0
Pod documented
191 17 my ( $pid, $code ) = @_;
192  
193 $self->{watch_process}{$pid} = EV::child $pid, 0, sub {
194 19 ▶ my $w = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
195 19 $code->( $w->rpid, $w->rstatus );
196 17 };
197 }
198  
199 sub unwatch_process
200 {
201 1 ▶ my $self = shift;
Subroutine unwatch_process: called CC=1 SCAR=0.0
Pod documented
202 1 my ( $pid ) = @_;
203  
204 1 delete $self->{watch_process}{$pid};
205 }
206  
207 =head1 AUTHOR
208  
209 Paul Evans <leonerd@leonerd.org.uk>
210  
211 =cut
212  
213 17 0x55AA;
« Build ↑ Summary