| line | stmt | bran | cond | sub | pod | time | code | 
| 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 -- leonerd@leonerd.org.uk | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | package IO::Async::Loop::Event; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 11 |  |  | 11 |  | 13311 | use strict; | 
|  | 11 |  |  |  |  | 17 |  | 
|  | 11 |  |  |  |  | 432 |  | 
| 9 | 11 |  |  | 11 |  | 70 | use warnings; | 
|  | 11 |  |  |  |  | 21 |  | 
|  | 11 |  |  |  |  | 668 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '0.02'; | 
| 12 | 11 |  |  | 11 |  | 66 | use constant API_VERSION => '0.49'; | 
|  | 11 |  |  |  |  | 16 |  | 
|  | 11 |  |  |  |  | 806 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 11 |  |  | 11 |  | 59 | use base qw( IO::Async::Loop ); | 
|  | 11 |  |  |  |  | 15 |  | 
|  | 11 |  |  |  |  | 13564 |  | 
| 15 |  |  |  |  |  |  | IO::Async::Loop->VERSION( '0.49' ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 11 |  |  | 11 |  | 150691 | use constant _CAN_SUBSECOND_ACCURATELY => 0; | 
|  | 11 |  |  |  |  | 34 |  | 
|  | 11 |  |  |  |  | 682 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 11 |  |  | 11 |  | 58 | use Carp; | 
|  | 11 |  |  |  |  | 23 |  | 
|  | 11 |  |  |  |  | 754 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 11 |  |  | 11 |  | 9229 | use Event; | 
|  | 11 |  |  |  |  | 144432 |  | 
|  | 11 |  |  |  |  | 81 |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =head1 NAME | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | C - use C with C | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | use IO::Async::Loop::Event; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | my $loop = IO::Async::Loop::Event->new(); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | $loop->add( ... ); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | $loop->run; | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | This subclass of L uses L to perform its work. | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | =cut | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | sub new | 
| 44 |  |  |  |  |  |  | { | 
| 45 | 10 |  |  | 10 | 1 | 168 | my $class = shift; | 
| 46 | 10 |  |  |  |  | 104 | my $self = $class->SUPER::__new( @_ ); | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 10 |  |  |  |  | 677 | $self->{$_} = {} for qw( watch_io watch_sig watch_idle ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 10 |  |  |  |  | 38 | return $self; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub loop_once | 
| 54 |  |  |  |  |  |  | { | 
| 55 | 52 |  |  | 52 | 1 | 23981 | my $self = shift; | 
| 56 | 52 |  |  |  |  | 99 | my ( $timeout ) = @_; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 52 | 100 |  |  |  | 219 | if( defined $timeout ) { | 
| 59 | 51 |  |  |  |  | 18426277 | Event::one_event( $timeout ); | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  | else { | 
| 62 | 1 |  |  |  |  | 18 | Event::one_event; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub run | 
| 67 |  |  |  |  |  |  | { | 
| 68 | 5 |  |  | 5 | 1 | 127 | my $self = shift; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 5 |  |  |  |  | 30 | my $result = Event::loop(); | 
| 71 | 5 | 100 |  |  |  | 125 | wantarray ? @$result : $result->[0]; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | sub stop | 
| 75 |  |  |  |  |  |  | { | 
| 76 | 5 |  |  | 5 | 1 | 394928 | my $self = shift; | 
| 77 | 5 |  |  |  |  | 26 | my @result = @_; | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 5 |  |  |  |  | 217 | Event::unloop( \@result ); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | sub watch_io | 
| 83 |  |  |  |  |  |  | { | 
| 84 | 7 |  |  | 7 | 1 | 5967 | my $self = shift; | 
| 85 | 7 |  |  |  |  | 29 | my %params = @_; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 7 | 50 |  |  |  | 24 | my $handle = $params{handle} or die "Need a handle"; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 7 |  | 100 |  |  | 59 | my $w = $self->{watch_io}{$handle} ||= []; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 7 | 100 |  |  |  | 27 | if( my $on_read_ready = $params{on_read_ready} ) { | 
| 92 | 6 |  |  |  |  | 14 | $w->[1] = $on_read_ready; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 7 | 100 |  |  |  | 23 | if( my $on_write_ready = $params{on_write_ready} ) { | 
| 96 | 3 |  |  |  |  | 5 | $w->[2] = $on_write_ready; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 7 | 50 |  |  |  | 30 | my $poll = ( $w->[1] ? "r" : "" ) . ( $w->[2] ? "w" : "" ); | 
|  |  | 100 |  |  |  |  |  | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 7 | 100 |  |  |  | 24 | if( $w->[0] ) { | 
| 102 | 1 |  |  |  |  | 30 | $w->[0]->poll( $poll ); | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  | else { | 
| 105 |  |  |  |  |  |  | $w->[0] = Event->io( | 
| 106 |  |  |  |  |  |  | poll => $poll, | 
| 107 |  |  |  |  |  |  | fd => $handle, | 
| 108 |  |  |  |  |  |  | cb => sub { | 
| 109 | 8 |  |  | 8 |  | 15 | my $e = shift; | 
| 110 | 8 | 100 | 66 |  |  | 88 | if( $e->got =~ m/r/ and $w->[1] ) { | 
| 111 | 7 |  |  |  |  | 24 | $w->[1]->(); | 
| 112 |  |  |  |  |  |  | } | 
| 113 | 8 | 100 | 100 |  |  | 82 | if( $e->got =~ m/w/ and $w->[2] ) { | 
| 114 | 2 |  |  |  |  | 8 | $w->[2]->(); | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  | } | 
| 117 | 6 |  |  |  |  | 64 | ); | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | sub unwatch_io | 
| 122 |  |  |  |  |  |  | { | 
| 123 | 7 |  |  | 7 | 1 | 15161 | my $self = shift; | 
| 124 | 7 |  |  |  |  | 28 | my %params = @_; | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 7 | 50 |  |  |  | 29 | my $handle = $params{handle} or die "Need a handle"; | 
| 127 |  |  |  |  |  |  |  | 
| 128 | 7 | 50 |  |  |  | 32 | my $w = $self->{watch_io}{$handle} or return; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 7 | 100 |  |  |  | 21 | if( $params{on_read_ready} ) { | 
| 131 | 6 |  |  |  |  | 15 | undef $w->[1]; | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 7 | 100 |  |  |  | 32 | if( $params{on_write_ready} ) { | 
| 135 | 3 |  |  |  |  | 5 | undef $w->[2]; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 7 | 100 |  |  |  | 37 | my $poll = ( $w->[1] ? "r" : "" ) . ( $w->[2] ? "w" : "" ); | 
|  |  | 50 |  |  |  |  |  | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 7 | 100 |  |  |  | 20 | if( length $poll ) { | 
| 141 | 1 |  |  |  |  | 9 | $w->[0]->poll( $poll ); | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  | else { | 
| 144 | 6 |  |  |  |  | 94 | $w->[0]->cancel; | 
| 145 | 6 |  |  |  |  | 58 | delete $self->{watch_io}{$handle}; | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | sub watch_time | 
| 150 |  |  |  |  |  |  | { | 
| 151 | 26 |  |  | 26 | 1 | 143818 | my $self = shift; | 
| 152 | 26 |  |  |  |  | 311 | my %params = @_; | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 26 | 50 |  |  |  | 145 | my $code = $params{code} or croak "Expected 'code' as CODE ref"; | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 26 |  |  |  |  | 76 | my $w; | 
| 157 | 26 | 100 |  |  |  | 162 | if( defined $params{after} ) { | 
| 158 | 25 |  |  |  |  | 49 | my $delay = $params{after}; | 
| 159 | 25 | 100 |  |  |  | 212 | $delay = 0 if $delay < 0; | 
| 160 | 25 |  |  |  |  | 317 | $w = Event->timer( after => $delay, cb => $code ); | 
| 161 |  |  |  |  |  |  | } | 
| 162 |  |  |  |  |  |  | else { | 
| 163 | 1 |  |  |  |  | 8 | $w = Event->timer( at => $params{at}, cb => $code ); | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  |  | 
| 166 | 26 |  |  |  |  | 10790 | return $w; | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | sub unwatch_time | 
| 170 |  |  |  |  |  |  | { | 
| 171 | 15 |  |  | 15 | 1 | 1524 | my $self = shift; | 
| 172 | 15 |  |  |  |  | 47 | my ( $w ) = @_; | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 15 |  |  |  |  | 272 | $w->cancel; | 
| 175 |  |  |  |  |  |  | } | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | sub watch_signal | 
| 178 |  |  |  |  |  |  | { | 
| 179 | 7 |  |  | 7 | 1 | 17194 | my $self = shift; | 
| 180 | 7 |  |  |  |  | 87 | my ( $name, $code ) = @_; | 
| 181 |  |  |  |  |  |  |  | 
| 182 | 7 | 100 |  |  |  | 359 | exists $SIG{$name} or croak "Unrecognised signal name $name"; | 
| 183 |  |  |  |  |  |  |  | 
| 184 | 6 |  |  |  |  | 174 | my $w = Event->signal( signal => $name, cb => $code ); | 
| 185 | 6 |  |  |  |  | 3108 | $self->{watch_sig}{$name} = $w; | 
| 186 |  |  |  |  |  |  | } | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | sub unwatch_signal | 
| 189 |  |  |  |  |  |  | { | 
| 190 | 2 |  |  | 2 | 1 | 2795 | my $self = shift; | 
| 191 | 2 |  |  |  |  | 7 | my ( $name ) = @_; | 
| 192 |  |  |  |  |  |  |  | 
| 193 | 2 |  |  |  |  | 40 | ( delete $self->{watch_sig}{$name} )->cancel; | 
| 194 |  |  |  |  |  |  | } | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | sub watch_idle | 
| 197 |  |  |  |  |  |  | { | 
| 198 | 6 |  |  | 6 | 1 | 4757 | my $self = shift; | 
| 199 | 6 |  |  |  |  | 22 | my %params = @_; | 
| 200 |  |  |  |  |  |  |  | 
| 201 | 6 | 50 |  |  |  | 22 | my $when = delete $params{when} or croak "Expected 'when'"; | 
| 202 |  |  |  |  |  |  |  | 
| 203 | 6 | 50 |  |  |  | 21 | my $code = delete $params{code} or croak "Expected 'code' as a CODE ref"; | 
| 204 |  |  |  |  |  |  |  | 
| 205 | 6 | 50 |  |  |  | 18 | $when eq "later" or croak "Expected 'when' to be 'later'"; | 
| 206 |  |  |  |  |  |  |  | 
| 207 | 6 |  |  |  |  | 8 | my $key; | 
| 208 | 6 |  |  |  |  | 10 | my $idles = $self->{watch_idle}; | 
| 209 | 6 |  |  |  |  | 30 | my $w = Event->timer( | 
| 210 |  |  |  |  |  |  | after => 0, | 
| 211 |  |  |  |  |  |  | cb => $code, | 
| 212 |  |  |  |  |  |  | prio => -1, | 
| 213 |  |  |  |  |  |  | ); | 
| 214 |  |  |  |  |  |  |  | 
| 215 | 6 |  |  |  |  | 825 | $key = "$w"; | 
| 216 | 6 |  |  |  |  | 15 | $idles->{$key} = $w; | 
| 217 | 6 |  |  |  |  | 21 | return $key; | 
| 218 |  |  |  |  |  |  | } | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | sub unwatch_idle | 
| 221 |  |  |  |  |  |  | { | 
| 222 | 1 |  |  | 1 | 1 | 5 | my $self = shift; | 
| 223 | 1 |  |  |  |  | 3 | my ( $id ) = @_; | 
| 224 |  |  |  |  |  |  |  | 
| 225 | 1 |  |  |  |  | 12 | ( delete $self->{watch_idle}{$id} )->cancel; | 
| 226 |  |  |  |  |  |  | } | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | =head1 AUTHOR | 
| 229 |  |  |  |  |  |  |  | 
| 230 |  |  |  |  |  |  | Paul Evans | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | =cut | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | 0x55AA; |