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

filter: Statementstmt 100% 13 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 18 / 18 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ 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, 2011-2020 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::AnyEvent;
7  
8 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 our $VERSION = '0.04';
12 10 ▶use constant API_VERSION => '0.76';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Force AnyEvent to detect a suitable model now, before we load
15 # IO::Async::Loop. Otherwise, AnyEvent will use AnyEvent::Impl::IOAsync
16 # which causes a circular dependency at runtime, leading to such problems as:
17 #
18 # Deep recursion on subroutine "AnyEvent::Impl::IOAsync::io" at ...
19  
20 10 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶BEGIN { AnyEvent::detect() }
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # use base qw( IO::Async::Loop );
24 # Except, 'use base' breaks here if IO::Async::Loop is already loaded, causing
25 # SUPER:: lookups not to work properly.
26 BEGIN { require IO::Async::Loop; unshift our @ISA, qw( IO::Async::Loop ); }
27 IO::Async::Loop->VERSION( '0.49' );
28  
29 use Carp;
30  
31 use constant ANYEVENT_6 => $AnyEvent::VERSION >= 6;
32  
33 =head1 NAME
34  
35 C<IO::Async::Loop::AnyEvent> - use C<IO::Async> with C<AnyEvent>
36  
37 =head1 SYNOPSIS
38  
39 use IO::Async::Loop::AnyEvent;
40  
41 my $loop = IO::Async::Loop::AnyEvent->new();
42  
43 $loop->add( ... );
44  
45 $loop->add( IO::Async::Signal->new(
46 name => 'HUP',
47 on_receipt => sub { ... },
48 ) );
49  
50 $loop->run;
51  
52 =head1 DESCRIPTION
53  
54 This subclass of L<IO::Async::Loop> uses L<AnyEvent> to perform its work.
55  
56 =head1 CONSTRUCTOR
57  
58 =cut
59  
60 =head2 new
61  
62 $loop = IO::Async::Loop::AnyEvent->new
63  
64 This function returns a new instance of a C<IO::Async::Loop::AnyEvent> object.
65  
66 =cut
67  
68 sub new
69 {
70 my $class = shift;
71 my ( %args ) = @_;
72  
73 my $self = $class->SUPER::__new( %args );
74  
75 $self->{$_} = {} for qw( watch_r watch_w watch_time watch_signal watch_idle watch_child );
76  
77 return $self;
78 }
79  
80 sub loop_once
81 {
82 my $self = shift;
83 my ( $timeout ) = @_;
84  
85 my $cv = AnyEvent->condvar;
86 my $w;
87  
88 if( defined $timeout ) {
89 $w = AnyEvent->timer( after => $timeout, cb => sub { $cv->send } );
90 }
91  
92 if( ANYEVENT_6 ) {
93 # This method isn't technically documented by AnyEvent
94 AnyEvent->_poll;
95 }
96 else {
97 # This method isn't technically documented by AnyEvent
98 AnyEvent->one_event;
99 }
100 }
101  
102 sub loop_forever
103 {
104 my $self = shift;
105  
106 ( local $self->{loop_forever_cv} = AnyEvent->condvar )->recv;
107 }
108  
109 sub loop_stop
110 {
111 my $self = shift;
112  
113 $self->{loop_forever_cv}->send;
114 }
115  
116 sub watch_io
117 {
118 my $self = shift;
119 my %params = @_;
120  
121 my $handle = $params{handle} or die "Need a handle";
122  
123 if( my $on_read_ready = $params{on_read_ready} ) {
124 $self->{watch_r}{$handle} = AnyEvent->io(
125 fh => $handle,
126 poll => "r",
127 cb => $on_read_ready,
128 );
129 }
130  
131 if( my $on_write_ready = $params{on_write_ready} ) {
132 $self->{watch_w}{$handle} = AnyEvent->io(
133 fh => $handle,
134 poll => "w",
135 cb => $on_write_ready,
136 );
137 }
138 }
139  
140 sub unwatch_io
141 {
142 my $self = shift;
143 my %params = @_;
144  
145 my $handle = $params{handle} or die "Need a handle";
146  
147 if( $params{on_read_ready} ) {
148 delete $self->{watch_r}{$handle};
149 }
150  
151 if( $params{on_write_ready} ) {
152 delete $self->{watch_w}{$handle};
153 }
154 }
155  
156 sub watch_time
157 {
158 my $self = shift;
159 my %params = @_;
160  
161 my $code = $params{code} or croak "Expected 'code' as CODE ref";
162  
163 my $w;
164 if( defined $params{at} ) {
165 $w = AnyEvent->timer( after => $params{at} - $self->time, cb => $code );
166 }
167 elsif( defined $params{after} ) {
168 $w = AnyEvent->timer( after => $params{after}, cb => $code );
169 }
170 else {
171 croak "Expected one of 'at' or 'after'";
172 }
173  
174 return $self->{watch_time}{$w} = $w;
175 }
176  
177 sub unwatch_time
178 {
179 my $self = shift;
180 my ( $id ) = @_;
181  
182 delete $self->{watch_time}{$id};
183 }
184  
185 sub watch_signal
186 {
187 my $self = shift;
188 my ( $signal, $code ) = @_;
189  
190 $self->{watch_signal}{$signal} = AnyEvent->signal(
191 signal => $signal,
192 cb => $code,
193 );
194 }
195  
196 sub unwatch_signal
197 {
198 my $self = shift;
199 my ( $signal ) = @_;
200  
201 delete $self->{watch_signal}{$signal};
202 }
203  
204 sub watch_idle
205 {
206 my $self = shift;
207 my %params = @_;
208  
209 my $when = delete $params{when} or croak "Expected 'when'";
210  
211 my $code = delete $params{code} or croak "Expected 'code' as a CODE ref";
212  
213 $when eq "later" or croak "Expected 'when' to be 'later'";
214  
215 my $key;
216 my $w = AnyEvent->timer(
217 after => 0,
218 cb => sub {
219 delete $self->{watch_idle}{$key};
220 goto &$code;
221 },
222 );
223  
224 $key = "$w";
225 $self->{watch_idle}{$key} = $w;
226 return $key;
227 }
228  
229 sub unwatch_idle
230 {
231 my $self = shift;
232 my ( $id ) = @_;
233  
234 delete $self->{watch_idle}{$id};
235 }
236  
237 sub watch_process
238 {
239 my $self = shift;
240 my ( $pid, $code ) = @_;
241  
242 $self->{watch_child}{$pid} = AnyEvent->child( pid => $pid, cb => $code );
243 }
244  
245 sub unwatch_process
246 {
247 my $self = shift;
248 my ( $pid ) = @_;
249  
250 delete $self->{watch_child}{$pid};
251 }
252  
253 =head1 BUGS
254  
255 =over 4
256  
257 =item *
258  
259 C<watch_idle> and C<unwatch_idle> don't work properly against
260 C<AnyEvent::Impl::IOAsync>. At least, the unit tests fail, and some scheduled
261 CODErefs never get executed, and sit in the internal queue of the inner-nested
262 C<IO::Async::Loop> that C<AnyEvent::Impl::IOAsync> itself constructed. An easy
263 workaround here is simply to pick another AnyEvent model, by using the
264 C<PERL_ANYEVENT_MODEL> environment variable.
265  
266 That all said, I am honestly surprised this is the only thing that breaks,
267 when C<IO::Async> is nested upon C<AnyEvent> itself running atop another
268 C<IO::Async>.
269  
270 =item *
271  
272 The implementation of the C<loop_once> method requires the use of an
273 undocumented C<AnyEvent> method (C<one_event> before version 6, C<_poll>
274 thereafter). This happens to work at the time of writing, but as it is
275 undocumented it may be subject to change.
276  
277 The C<loop_forever> method does not rely on this undocumented method, so
278 should be safe from upstream changes. Furthremore, if C<AnyEvent> rather than
279 C<IO::Async> remains ultimately in control of the runtime, by waiting on
280 condvars, this should not be problematic.
281  
282 =back
283  
284 =cut
285  
286 =head1 AUTHOR
287  
288 Paul Evans <leonerd@leonerd.org.uk>
289  
290 =cut
291  
292 0x55AA;
↑ Summary