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

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 36 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 184 SCAR 53.5
CC
14
Cov
0%
watch_io
56.0
loop_once
20.0
unwatch_io
20.0
CRAP 210.0
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, 2013 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Loop::UV;
7  
8 use strict;
9 use warnings;
10  
11 use Carp;
12  
13 our $VERSION = '0.01';
14 use constant API_VERSION => '0.49';
15  
16 use base qw( IO::Async::Loop );
17 IO::Async::Loop->VERSION( '0.49' );
18  
19 use UV;
20  
21 =head1 NAME
22  
23 C<IO::Async::Loop::UV> - use C<IO::Async> with C<UV>
24  
25 =head1 SYNOPSIS
26  
27 use IO::Async::Loop::UV;
28  
29 my $loop = IO::Async::Loop::UV->new();
30  
31 $loop->add( ... );
32  
33 $loop->add( IO::Async::Signal->new(
34 name => 'HUP',
35 on_receipt => sub { ... },
36 ) );
37  
38 $loop->loop_forever();
39  
40 =head1 DESCRIPTION
41  
42 This subclass of L<IO::Async::Loop> uses L<UV> to perform its work.
43  
44 As both C<UV> and the underlying F<libuv> are quite new, this module currently
45 has a few shortcomings and limitations. See the L</BUGS> section.
46  
47 =cut
48  
49 sub new
50 {
51 my $class = shift;
52 my $self = $class->SUPER::__new( @_ );
53  
54 return $self;
55 }
56  
57 sub loop_once
58 {
59 my $self = shift;
60 my ( $timeout ) = @_;
61  
62 $timeout = 0 if keys %{ $self->{idles} };
63  
64 my $timer;
65 if( defined $timeout ) {
66 $timer = UV::timer_init();
67 UV::timer_start( $timer, $timeout * 1000, 0, sub {} );
68 }
69  
70 UV::run( UV::RUN_ONCE );
71  
72 UV::timer_stop( $timer ) if $timer;
73 }
74  
75 sub watch_io
76 {
77 my $self = shift;
78 my %params = @_;
79  
80 my $handle = $params{handle} or die "Need a handle";
81 my $state = $self->{fh}{$handle} ||= {
82 mask => 0,
83 };
84  
85 # UV has a thing it calls "handles". They're not filehandles, they're
86 # something else
87 my $uvh = $state->{uvh} ||= UV::poll_init( $handle->fileno );
88  
89 if( my $on_read_ready = $params{on_read_ready} ) {
90 $state->{on_read_ready} = $on_read_ready;
91 $state->{mask} |= UV::READABLE;
92 }
93  
94 if( my $on_write_ready = $params{on_write_ready} ) {
95 $state->{on_write_ready} = $on_write_ready;
96 $state->{mask} |= UV::WRITABLE;
97 }
98  
99 my $cb = $self->{fh_cb}{$handle} ||= sub {
100 my ( $status, $events ) = @_;
101 if( my $cb = $state->{on_read_ready} ) {
102 $cb->() if $events & UV::READABLE or $status == -1;
103 }
104 if( my $cb = $state->{on_write_ready} ) {
105 $cb->() if $events & UV::WRITABLE or $status == -1;
106 }
107 };
108  
109 UV::poll_start( $uvh, $state->{mask}, $cb );
110 }
111  
112 sub unwatch_io
113 {
114 my $self = shift;
115 my %params = @_;
116  
117 my $handle = $params{handle} or die "Need a handle";
118 my $state = $self->{fh}{$handle} or return;
119 my $uvh = $state->{uvh};
120  
121 if( $params{on_read_ready} ) {
122 $state->{mask} &= ~UV::READABLE;
123 undef $state->{on_read_ready};
124 }
125  
126 if( $params{on_write_ready} ) {
127 $state->{mask} &= ~UV::WRITABLE;
128 undef $state->{on_write_ready};
129 }
130  
131 if( $state->{mask} ) {
132 UV::poll_start( $uvh, $state->{mask}, $self->{fh_cb}{$handle} );
133 }
134 else {
135 delete $self->{fh}{$handle};
136 delete $self->{fh_cb}{$handle};
137 UV::close( $uvh );
138 }
139 }
140  
141 sub watch_time
142 {
143 my $self = shift;
144 my %params = @_;
145  
146 my $code = $params{code} or croak "Expected 'code' as CODE ref";
147 my $now = $params{now} || $self->time;
148 my $delay = $params{after} || ( $params{at} - $now );
149  
150 $delay = 0 if $delay < 0;
151  
152 my $timer = UV::timer_init;
153 UV::timer_start( $timer, $delay * 1000, 0, $code );
154  
155 return $timer;
156 }
157  
158 sub unwatch_time
159 {
160 my $self = shift;
161 my ( $timer ) = @_;
162  
163 UV::close( $timer );
164 }
165  
166 sub watch_idle
167 {
168 my $self = shift;
169 my %params = @_;
170  
171 my $when = delete $params{when} or croak "Expected 'when'";
172  
173 my $code = delete $params{code} or croak "Expected 'code' as a CODE ref";
174  
175 $when eq "later" or croak "Expected 'when' to be 'later'";
176  
177 my $idles = $self->{idles} ||= {};
178 my $idle = UV::idle_init();
179 $idles->{$idle}++;
180 UV::idle_start( $idle, sub {
181 UV::close( $idle );
182 delete $idles->{$idle};
183 $code->();
184 } );
185  
186 return $idle;
187 }
188  
189 sub unwatch_idle
190 {
191 my $self = shift;
192 my ( $idle ) = @_;
193  
194 UV::close( $idle );
195 delete $self->{idles}{$idle};
196 }
197  
198 =head1 BUGS
199  
200 =over 2
201  
202 =item *
203  
204 F<libuv> does not provide a way to inspect the C<POLLUP> status bit, so some
205 types of file descriptor cannot provide EOF condition. This causes a unit-test
206 failure.
207  
208 =item *
209  
210 F<libuv> attempts to invoke a close callback when closing watch handles, even
211 if one is not defined. This causes the next C<UV::run_once()> call after a
212 handle has been closed to always return immediately. This should not cause a
213 problem in practice, but does cause a unit-test failure.
214  
215 =item *
216  
217 L<UV> does not wrap signal or child-process watch abilities of F<libuv>, so
218 these are currently emulated by the Loop's built-in signal-pipe mechanism.
219 Because of this, signal or child-process watching cannot be shared by both
220 C<IO::Async> and C<UV>-using code at the same time.
221  
222 =back
223  
224 =head1 AUTHOR
225  
226 Paul Evans <leonerd@leonerd.org.uk>
227  
228 =cut
229  
230 0x55AA;