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

filter: Statementstmt 92.0% 116 / 126 Branchbran 65.6% 21 / 32 Conditioncond 66.6% 6 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 92.3% 24 / 26 Podpod 85.7% 12 / 14 totaltotal 84.5% 180 / 213 SCAR 30.5
CC
20
Cov
86%
unwatch_io
7.1
unwatch_child
6.0
watch_io
5.0
CRAP 21.2
↑ Summary Build »
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, 2010 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::IO::Async;
7  
8 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 our $VERSION = '0.02';
12 12 ▶use constant API_VERSION => '0.33';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 ▶use base qw( IO::Async::Loop );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 12 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 12 ▶use IO::Async::Notifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use IO::Async::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use IO::Async::Timer::Absolute;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 ▶use IO::Async::Signal;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 12 ▶use IO::Async::PID;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 NAME
27  
28 L<IO::Async::Loop::IO::Async> - use C<IO::Async> with C<IO::Async>
29  
30 =head1 SYNOPSIS
31  
32 use IO::Async::Loop::IO::Async;
33  
34 my $loop = IO::Async::Loop::IO::Async->new();
35  
36 $loop->add( ... );
37  
38 $loop->add( IO::Async::Signal->new(
39 name => 'HUP',
40 on_receipt => sub { ... },
41 ) );
42  
43 $loop->loop_forever();
44  
45 =head1 DESCRIPTION
46  
47 This subclass of L<IO::Async::Loop> uses another instance of an
48 C<IO::Async::Loop> object as its underlying implementation. While this at
49 first appears to be pointless, this module distribution is not primarily
50 intended to serve a useful purpose for end-users. Rather, it stands as a real
51 code example, for authors of other modules to use for reference.
52  
53 =head2 For C<IO::Async::Loop::*> Authors
54  
55 Authors of other subclasses to implement C<IO::Async::Loop> subclasses may
56 find this distribution useful as a template. By copying the code and replacing
57 the contents of the various C<watch_*> and C<unwatch_*> methods, a Loop
58 implementation can be built making use of some other event system or
59 underlying kernel blocking primative.
60  
61 =head2 For Authors of Other Event Systems
62  
63 Authors of implementations in other event systems wishing to support running
64 their event system on top of L<IO::Async> may find this distribution useful to
65 read a way to implement the various underlying behaviours, such as watching
66 filehandles and timers. Examples in each of the C<watch_*> and C<unwatch_*>
67 methods may be useful to demonstrate the sort of code that might be required
68 to attach some other event system on top of C<IO::Async>.
69  
70 =head1 CONSTRUCTOR
71  
72 =cut
73  
74 =head2 $loop = IO::Async::Loop::IO::Async->new()
75  
76 This function returns a new instance of a C<IO::Async::Loop::IO::Async> object.
77  
78 =cut
79  
80 sub new
81 {
82 11 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
83 11 my ( %args ) = @_;
84  
85 11 my $self = $class->SUPER::__new( %args );
86  
87 11 $self->{root_notifier} = IO::Async::Notifier->new;
88  
89 11 return $self;
90 }
91  
92 =head1 METHODS
93  
94 =cut
95  
96 =head2 $loop->parent_loop( $parent )
97  
98 =head2 $parent = $loop->parent_loop
99  
100 Accessor for the underlying C<IO::Async::Loop> that this loop will use. If one
101 is not provided by the time that C<loop_once> is first invoked, one will be
102 constructed using the normal C<< IO::Async::Loop->new >> constructor. This
103 method may be used to access it after that.
104  
105 =cut
106  
107 sub parent_loop
108 {
109 62 ▶ my $self = shift;
Subroutine parent_loop: called CC=3 SCAR=11.1
Pod documented
110 62 my ( $loop ) = @_;
111  
112 62 ▶ $self->{parent_loop} = $loop if $loop;
Branch: 1 branch
BranchTrueFalseTotal
if $loop 0 62 62
113  
114 62 ▶ $self->{parent_loop} ||= do {
Condition66%2/3
$self->{"parent_loop"} ||= do { my $loop = "IO::Async::Loop"->new; $loop->add($self->{"root_notifier"}); $loop }
l!l&&r!l&&!r
52100
MC/DC0%0/2
$self->{"parent_loop"} ||= do { my $loop = "IO::Async::Loop"->new; $loop->add($self->{"root_notifier"}); $loop }
$self->{"parent_loop"} do { my $loop = "IO::Async::Loop"->new; $loop->add($self->{"root_notifier"}); $loop }
Truth table66%2/3
A ||= B
A= $self->{"parent_loop"}B= do { my $loop = "IO::Async::Loop"->new; $loop->add($self->{"root_notifier"}); $loop }
ABresult
1X1
011
000
115 10 my $loop = IO::Async::Loop->new;
116 10 $loop->add( $self->{root_notifier} );
117 10 $loop;
118 };
119  
120 62 return $self->{parent_loop};
121 }
122  
123 sub loop_once
124 {
125 50 ▶ my $self = shift;
Subroutine loop_once: called CC=1 SCAR=0.0
Pod documented
126 50 my ( $timeout ) = @_;
127  
128 50 $self->parent_loop->loop_once( $timeout );
129 }
130  
131 sub watch_io
132 {
133 3 ▶ my $self = shift;
Subroutine watch_io: called CC=5 SCAR=16.1
Pod documented
134 3 my %params = @_;
135  
136 3 ▶ my $handle = $params{handle} or die "Need a handle";
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $params{"handle"} or die "Need a handle" 0 3 3
137  
138 3 ▶ my $ioa_handle = $self->{handles}{$handle} ||= do {
Condition66%2/3
$self->{"handles"}{$handle} ||= do { my $h = "IO::Async::Handle"->new; $self->{"root_notifier"}->add_child($h); $h }
l!l&&r!l&&!r
210
MC/DC0%0/2
$self->{"handles"}{$handle} ||= do { my $h = "IO::Async::Handle"->new; $self->{"root_notifier"}->add_child($h); $h }
$self->{"handles"}{$handle} do { my $h = "IO::Async::Handle"->new; $self->{"root_notifier"}->add_child($h); $h }
Truth table66%2/3
A ||= B
A= $self->{"handles"}{$handle}B= do { my $h = "IO::Async::Handle"->new; $self->{"root_notifier"}->add_child($h); $h }
ABresult
1X1
011
000
139 1 my $h = IO::Async::Handle->new;
140 1 $self->{root_notifier}->add_child( $h );
141 1 $h;
142 };
143  
144 3 ▶ if( my $on_read_ready = $params{on_read_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_read_ready = $params{"on_read_ready"}) 2 1 3
145 2 $ioa_handle->configure(
146 read_handle => $handle,
147 on_read_ready => $on_read_ready,
148 );
149 2 $ioa_handle->want_readready( 1 );
150 }
151  
152 3 ▶ if( my $on_write_ready = $params{on_write_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_write_ready = $params{"on_write_ready"}) 1 2 3
153 1 $ioa_handle->configure(
154 write_handle => $handle,
155 on_write_ready => $on_write_ready,
156 );
157 1 $ioa_handle->want_writeready( 1 );
158 }
159 }
160  
161 sub unwatch_io
162 {
163 3 ▶ my $self = shift;
Subroutine unwatch_io: called CC=7 SCAR=19.6
Pod documented
164 3 my %params = @_;
165  
166 3 ▶ my $handle = $params{handle} or die "Need a handle";
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $params{"handle"} or die "Need a handle" 0 3 3
167  
168 3 ▶ my $ioa_handle = $self->{handles}{$handle} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ioa_handle = $self->{"handles"}{$handle} or return 0 3 3
169  
170 3 ▶ if( $params{on_read_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"on_read_ready"}) 2 1 3
171 2 $ioa_handle->want_readready( 0 );
172 2 $ioa_handle->configure(
173 read_handle => undef,
174 on_read_ready => undef,
175 );
176 }
177  
178 3 ▶ if( $params{on_write_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"on_write_ready"}) 1 2 3
179 1 $ioa_handle->want_writeready( 0 );
180 1 $ioa_handle->configure(
181 write_handle => undef,
182 on_write_ready => undef,
183 );
184 }
185  
186 3 ▶ if( !$ioa_handle->want_readready and !$ioa_handle->want_writeready ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ioa_handle->want_readready || $ioa_handle->want_writeready) 2 1 3
Condition66%2/3
$ioa_handle->want_readready || $ioa_handle->want_writeready
l!l&&r!l&&!r
102
MC/DC50%1/2
$ioa_handle->want_readready || $ioa_handle->want_writeready
$ioa_handle->want_readready $ioa_handle->want_writeready
Truth table66%2/3
A || B
A= $ioa_handle->want_readreadyB= $ioa_handle->want_writeready
ABresult
1X1
011
000
187 2 $self->{root_notifier}->remove_child( $ioa_handle );
188 }
189 }
190  
191 sub enqueue_timer
192 {
193 27 ▶ my $self = shift;
Subroutine enqueue_timer: called CC=2 SCAR=7.0
Pod documented
194 27 my %params = @_;
195  
196 27 my $time = $self->_build_time( %params );
197  
198 27 ▶ 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 27 27
199  
200 27 my $timer = IO::Async::Timer::Absolute->new(
201 time => $time,
202 on_expire => $code,
203 );
204  
205 27 $self->{root_notifier}->add_child( $timer );
206  
207 27 return $timer;
208 }
209  
210 sub cancel_timer
211 {
212 17 ▶ my $self = shift;
Subroutine cancel_timer: called CC=2 SCAR=7.0
Pod documented
213 17 my ( $timer ) = @_;
214  
215 17 ▶ $timer->stop if $timer->get_loop;
Branch: 1 branch
BranchTrueFalseTotal
if $timer->get_loop 17 0 17
216 17 $self->{root_notifier}->remove_child( $timer );
217 }
218  
219 sub requeue_timer
220 {
221 0 ▶ my $self = shift;
Subroutine requeue_timer: not called CC=1 SCAR=6.9
Pod documented
222 0 my ( $timer, %params ) = @_;
223  
224 0 my $time = $self->_build_time( %params );
225  
226 0 $timer->configure( time => $time );
227  
228 0 return $timer;
229 }
230  
231 sub watch_signal
232 {
233 4 ▶ my $self = shift;
Subroutine watch_signal: called CC=1 SCAR=0.0
Pod documented
234 4 my ( $signal, $code ) = @_;
235  
236 4 my $ioa_signal = IO::Async::Signal->new(
237 name => $signal,
238 on_receipt => $code,
239 );
240  
241 4 $self->{signals}{$signal} = $ioa_signal;
242  
243 4 $self->{root_notifier}->add_child( $ioa_signal );
244 }
245  
246 sub unwatch_signal
247 {
248 2 ▶ my $self = shift;
Subroutine unwatch_signal: called CC=1 SCAR=0.0
Pod documented
249 2 my ( $signal ) = @_;
250  
251 2 $self->{root_notifier}->remove_child( delete $self->{signals}{$signal} );
252 }
253  
254 sub watch_idle
255 {
256 6 ▶ my $self = shift;
Subroutine watch_idle: called CC=4 SCAR=14.5
Pod documented
257 6 my %params = @_;
258  
259 6 ▶ 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 6 6
260  
261 6 ▶ my $when = $params{when} or croak "Expected 'when'";
Branch: 1 branch
BranchTrueFalseTotal
my $when = $params{"when"} or croak("Expected 'when'") 0 6 6
262  
263 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
264  
265 # TODO: Find a nice way to do this that isn't cheating
266 6 return $self->parent_loop->watch_idle(
267 when => "later",
268 code => $code,
269 );
270 }
271  
272 sub unwatch_idle
273 {
274 1 ▶ my $self = shift;
Subroutine unwatch_idle: called CC=1 SCAR=0.0
Pod documented
275 1 my ( $id ) = @_;
276  
277 1 $self->parent_loop->unwatch_idle( $id );
278 }
279  
280 sub watch_child
281 {
282 16 ▶ my $self = shift;
Subroutine watch_child: called CC=2 SCAR=6.9
Pod undocumented
283 16 my ( $pid, $code ) = @_;
284  
285 # Some more cheating
286 16 ▶ if( $pid == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 5 11 16
287 5 $self->parent_loop->watch_child( 0, $code );
288 5 return;
289 }
290  
291 11 weaken( my $weakself = $self );
292  
293 my $ioa_pid = IO::Async::PID->new(
294 pid => $pid,
295 on_exit => sub {
296 11 ▶ my ( undef, $exitstatus ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
297  
298 11 $code->( $pid, $exitstatus );
299  
300 11 delete $weakself->{pids}{$pid};
301 }
302 11 );
303  
304 11 $self->{pids}{$pid} = $ioa_pid;
305  
306 11 $self->{root_notifier}->add_child( $ioa_pid );
307 }
308  
309 sub unwatch_child
310 {
311 0 ▶ my $self = shift;
Subroutine unwatch_child: not called CC=2 SCAR=17.9
Pod undocumented
312 0 my ( $pid ) = @_;
313  
314 0 ▶ if( $pid == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
315 0 $self->parent_loop->unwatch_child( 0 );
316 }
317  
318 0 $self->{root_notifier}->remove_child( delete $self->{pids}{$pid} );
319 }
320  
321 # Keep perl happy; keep Britain tidy
322 12 1;
323  
324 __END__
↑ Summary Build »