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

filter: Statementstmt 97.6% 82 / 84 Branchbran 75.0% 24 / 32 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 27 / 27 Podpod 100% 12 / 12 totaltotal 89.6% 147 / 164 CC 21 SCAR 10.0
CC
21
Cov
89%
new
5.3
watch_idle
4.1
watch_io
4.0
CRAP 2.7
↑ 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-2026 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::EV 0.05;
7  
8 17 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 ▶use feature qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶no warnings qw( experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use constant API_VERSION => '0.76';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 17 ▶use base qw( IO::Async::Loop );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 IO::Async::Loop->VERSION( '0.49' );
18  
19 17 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=13.3
20 17 ▶ if( $^V ge v5.36 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^V ge v5.36) { } 17 0 17
21 17 builtin->import(qw( refaddr weaken ));
22 17 ▶ warnings->unimport(qw( experimental::builtin )) if $^V lt v5.40;
Branch: 1 branch
BranchTrueFalseTotal
if $^V lt v5.40 0 17 17
23 }
24 else {
25 0 require Scalar::Util;
26 0 Scalar::Util->import(qw( refaddr weaken ));
27 }
28 }
29  
30 17 ▶use IO::Async::Metrics '$METRICS';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 17 ▶use constant _CAN_SUBSECOND_ACCURATELY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 17 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 =head1 NAME
39  
40 C<IO::Async::Loop::EV> - use C<IO::Async> with C<EV>
41  
42 =head1 SYNOPSIS
43  
44 =for highlighter language=perl
45  
46 use IO::Async::Loop::EV;
47  
48 my $loop = IO::Async::Loop::EV->new();
49  
50 $loop->add( ... );
51  
52 $loop->add( IO::Async::Signal->new(
53 name => 'HUP',
54 on_receipt => sub { ... },
55 ) );
56  
57 $loop->run;
58  
59 =head1 DESCRIPTION
60  
61 This subclass of L<IO::Async::Loop> uses L<EV> to perform its work.
62  
63 =cut
64  
65 16 ▶sub new ( $class, @args )
Subroutine new: called CC=5 SCAR=16.7
Pod documented
66 {
67 16 my $self = $class->SUPER::__new( @args );
68  
69 16 $self->{$_} = {} for qw( watch_r watch_w watch_time watch_signal watch_idle watch_process );
70  
71 # Check it's actually active
72 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
Condition33%2/6
defined $IO::Async::Loop::EV::METRICS and $IO::Async::Loop::EV::METRICS->adapter and $IO::Async::Loop::EV::METRICS
!ll&&!rl&&r
0160
defined $IO::Async::Loop::EV::METRICS and $IO::Async::Loop::EV::METRICS->adapter
!ll&&!rl&&r
0016
MC/DC0%0/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 table0%0/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
73 12 weaken( my $weakself = $self );
74 12 ▶ $self->{watch_prepare} = EV::prepare sub (@) { $weakself->pre_wait };
Subroutine __ANON__: called CC=1 SCAR=0.0
75 12 ▶ $self->{watch_check} = EV::check sub (@) { $weakself->post_wait };
Subroutine __ANON__: called CC=1 SCAR=0.0
76 }
77  
78 16 return $self;
79 }
80  
81 87 ▶sub loop_once ( $self, $timeout = undef )
Subroutine loop_once: called CC=3 SCAR=11.0
Pod documented
82 87 {
83 87 my $timeout_w;
84 87 ▶ if( defined $timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 74 13 87
85 74 $timeout_w = EV::timer $timeout, 0, sub (@) {}; # simply to wake up RUN_ONCE
86 }
87  
88 87 EV::run( EV::RUN_ONCE );
89 }
90  
91 15 ▶sub watch_io ( $self, %params )
Subroutine watch_io: called CC=4 SCAR=13.9
Pod documented
92 {
93 15 ▶ my $handle = $params{handle} or die "Need a handle";
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $params{"handle"} or die "Need a handle" 15 0 15
94  
95 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
96 10 $self->{watch_r}{refaddr $handle} = EV::io( $handle, EV::READ, $on_read_ready );
97 }
98  
99 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
100 7 $self->{watch_w}{refaddr $handle} = EV::io( $handle, EV::WRITE, $on_write_ready );
101 }
102 }
103  
104 11 ▶sub unwatch_io ( $self, %params )
Subroutine unwatch_io: called CC=4 SCAR=13.9
Pod documented
105 {
106 11 ▶ my $handle = $params{handle} or die "Need a handle";
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $params{"handle"} or die "Need a handle" 11 0 11
107  
108 11 ▶ if( $params{on_read_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"on_read_ready"}) 8 3 11
109 8 delete $self->{watch_r}{refaddr $handle};
110 }
111  
112 11 ▶ if( $params{on_write_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"on_write_ready"}) 7 4 11
113 7 delete $self->{watch_w}{refaddr $handle};
114 }
115 }
116  
117 28 ▶sub watch_time ( $self, %params )
Subroutine watch_time: called CC=3 SCAR=11.0
Pod documented
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") 28 0 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 17 ▶sub unwatch_time ( $self, $id )
Subroutine unwatch_time: called CC=1 SCAR=0.0
Pod documented
133 {
134 17 delete $self->{watch_time}{$id};
135 }
136  
137 5 ▶sub watch_signal ( $self, $signal, $code )
Subroutine watch_signal: called CC=2 SCAR=6.9
Pod documented
138 {
139 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}'") 4 1 5
140  
141 4 $self->{watch_signal}{$signal} = EV::signal $signal, $code;
142 }
143  
144 2 ▶sub unwatch_signal ( $self, $signal )
Subroutine unwatch_signal: called CC=1 SCAR=0.0
Pod documented
145 {
146 2 delete $self->{watch_signal}{$signal};
147 }
148  
149 6 ▶sub watch_idle ( $self, %params )
Subroutine watch_idle: called CC=4 SCAR=14.1
Pod documented
150 {
151 6 ▶ my $when = delete $params{when} or croak "Expected 'when'";
Branch: 1 branch
BranchTrueFalseTotal
my $when = delete $params{"when"} or croak("Expected 'when'") 6 0 6
152  
153 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") 6 0 6
154  
155 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'") 6 0 6
156  
157 6 my $key;
158 5 ▶ my $w = EV::timer 0, 0, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 5 delete $self->{watch_idle}{$key};
160 5 goto &$code;
161 6 };
162  
163 6 $key = "$w";
164 6 $self->{watch_idle}{$key} = $w;
165 6 return $key;
166 }
167  
168 1 ▶sub unwatch_idle ( $self, $id )
Subroutine unwatch_idle: called CC=1 SCAR=0.0
Pod documented
169 {
170 1 delete $self->{watch_idle}{$id};
171 }
172  
173 17 ▶sub watch_process ( $self, $pid, $code )
Subroutine watch_process: called CC=1 SCAR=0.0
Pod documented
174 {
175 19 ▶ $self->{watch_process}{$pid} = EV::child $pid, 0, sub ( $w, @ ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
176 19 $code->( $w->rpid, $w->rstatus );
177 17 };
178 }
179  
180 1 ▶sub unwatch_process ( $self, $pid )
Subroutine unwatch_process: called CC=1 SCAR=0.0
Pod documented
181 {
182 1 delete $self->{watch_process}{$pid};
183 }
184  
185 =head1 AUTHOR
186  
187 Paul Evans <leonerd@leonerd.org.uk>
188  
189 =cut
190  
191 17 0x55AA;
↑ Summary