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

filter: Statementstmt 97.6% 82 / 84 Branchbran 75.0% 24 / 32 Conditioncond 50.0% 3 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 27 / 27 Podpod 100% 12 / 12 totaltotal 90.8% 149 / 164 CC 21 SCAR 9.9
CC
21
Cov
89%
new
5.1
watch_idle
4.1
watch_io
4.0
CRAP 2.7
« 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-2026 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::EV 0.04;
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 BEGIN {
20 17 ▶ if( $^V ge v5.36 ) {
Subroutine BEGIN: called CC=3 SCAR=13.3
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 sub new ( $class, @args )
66 ▶{
Subroutine new: called CC=5 SCAR=16.4
Pod documented
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
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
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 sub loop_once ( $self, $timeout = undef )
82 87 ▶{
Subroutine loop_once: called CC=3 SCAR=11.0
Pod documented
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 sub watch_io ( $self, %params )
92 ▶{
Subroutine watch_io: called CC=4 SCAR=13.9
Pod documented
93 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
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 sub unwatch_io ( $self, %params )
105 ▶{
Subroutine unwatch_io: called CC=4 SCAR=13.9
Pod documented
106 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
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 sub watch_time ( $self, %params )
118 ▶{
Subroutine watch_time: called CC=3 SCAR=11.0
Pod documented
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 ( $self, $id )
133 ▶{
Subroutine unwatch_time: called CC=1 SCAR=0.0
Pod documented
134 17 delete $self->{watch_time}{$id};
135 }
136  
137 sub watch_signal ( $self, $signal, $code )
138 ▶{
Subroutine watch_signal: called CC=2 SCAR=6.9
Pod documented
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}'") 1 4 5
140  
141 4 $self->{watch_signal}{$signal} = EV::signal $signal, $code;
142 }
143  
144 sub unwatch_signal ( $self, $signal )
145 ▶{
Subroutine unwatch_signal: called CC=1 SCAR=0.0
Pod documented
146 2 delete $self->{watch_signal}{$signal};
147 }
148  
149 sub watch_idle ( $self, %params )
150 ▶{
Subroutine watch_idle: called CC=4 SCAR=14.1
Pod documented
151 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
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") 0 6 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'") 0 6 6
156  
157 6 my $key;
158 my $w = EV::idle sub {
159 5 ▶ delete $self->{watch_idle}{$key};
Subroutine __ANON__: called CC=1 SCAR=0.0
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 sub unwatch_idle ( $self, $id )
169 ▶{
Subroutine unwatch_idle: called CC=1 SCAR=0.0
Pod documented
170 1 delete $self->{watch_idle}{$id};
171 }
172  
173 sub watch_process ( $self, $pid, $code )
174 ▶{
Subroutine watch_process: called CC=1 SCAR=0.0
Pod documented
175 ▶ $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 sub unwatch_process ( $self, $pid )
181 ▶{
Subroutine unwatch_process: called CC=1 SCAR=0.0
Pod documented
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;
« Build ↑ Summary