| 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, 2016-2021 -- 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.02'; | ||
| 14 | use constant API_VERSION => '0.49'; | ||
| 15 | |||
| 16 | # We cannot support ->watch_process( 0 => ... ) because libuv does not give us | ||
| 17 | # a hook | ||
| 18 | use constant _CAN_WATCH_ALL_PIDS => 0; | ||
| 19 | |||
| 20 | use base qw( IO::Async::Loop ); | ||
| 21 | IO::Async::Loop->VERSION( '0.49' ); | ||
| 22 | |||
| 23 | use UV; | ||
| 24 | use UV::Idle; | ||
| 25 | use UV::Loop; | ||
| 26 | use UV::Poll; | ||
| 27 | use UV::Signal; | ||
| 28 | use UV::Timer; | ||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | C<IO::Async::Loop::UV> - use C<IO::Async> with C<UV> | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | use IO::Async::Loop::UV; | ||
| 37 | |||
| 38 | my $loop = IO::Async::Loop::UV->new(); | ||
| 39 | |||
| 40 | $loop->add( ... ); | ||
| 41 | |||
| 42 | $loop->add( IO::Async::Signal->new( | ||
| 43 | name => 'HUP', | ||
| 44 | on_receipt => sub { ... }, | ||
| 45 | ) ); | ||
| 46 | |||
| 47 | $loop->loop_forever(); | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | This subclass of L<IO::Async::Loop> uses L<UV> to perform its work. | ||
| 52 | |||
| 53 | As both C<UV> and the underlying F<libuv> are quite new, this module currently | ||
| 54 | has a few shortcomings and limitations. See the L</BUGS> section. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub new | ||
| 59 | { | ||
| 60 | my $class = shift; | ||
| 61 | my $self = $class->SUPER::__new( @_ ); | ||
| 62 | |||
| 63 | $self->{fakefiles} = {}; | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub loop_once | ||
| 69 | { | ||
| 70 | my $self = shift; | ||
| 71 | my ( $timeout ) = @_; | ||
| 72 | |||
| 73 | $timeout = 0 if keys %{ $self->{idles} }; | ||
| 74 | |||
| 75 | my $timer; | ||
| 76 | my $mode = UV::Loop::UV_RUN_ONCE; | ||
| 77 | if( defined $timeout and $timeout == 0 ) { | ||
| 78 | $mode = UV::Loop::UV_RUN_NOWAIT; | ||
| 79 | } | ||
| 80 | elsif( defined $timeout and $timeout > 0 ) { | ||
| 81 | $timer = UV::Timer->new; | ||
| 82 | $timer->start( $timeout * 1000, 0, sub {} ); | ||
| 83 | } | ||
| 84 | |||
| 85 | UV::loop->run( $mode ); | ||
| 86 | |||
| 87 | $timer->stop if $timer; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub watch_io | ||
| 91 | { | ||
| 92 | my $self = shift; | ||
| 93 | my %params = @_; | ||
| 94 | |||
| 95 | my $handle = $params{handle} or die "Need a handle"; | ||
| 96 | my $state = $self->{fh}{$handle} ||= { | ||
| 97 | mask => 0, | ||
| 98 | }; | ||
| 99 | |||
| 100 | if( my $on_read_ready = $params{on_read_ready} ) { | ||
| 101 | $state->{on_read_ready} = $on_read_ready; | ||
| 102 | $state->{mask} |= UV::Poll::UV_READABLE; | ||
| 103 | } | ||
| 104 | |||
| 105 | if( my $on_write_ready = $params{on_write_ready} ) { | ||
| 106 | $state->{on_write_ready} = $on_write_ready; | ||
| 107 | $state->{mask} |= UV::Poll::UV_WRITABLE; | ||
| 108 | } | ||
| 109 | |||
| 110 | if( ( stat $handle ) and -f _ ) { | ||
| 111 | my $fakefiles = $self->{fakefiles}; | ||
| 112 | ( $self->{fakefiles_idle} //= UV::Idle->new( | ||
| 113 | on_idle => sub { | ||
| 114 | foreach my $fake ( values %$fakefiles ) { | ||
| 115 | $fake->{on_read_ready}->() if $fake->{on_read_ready}; | ||
| 116 | $fake->{on_write_ready}->() if $fake->{on_write_ready}; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | ) )->start; | ||
| 120 | |||
| 121 | $self->{fakefiles}{$handle} = $state; | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | # UV has a thing it calls "handles". They're not filehandles, they're | ||
| 125 | # something else | ||
| 126 | my $uvh = $state->{uvh} ||= UV::Poll->new( fd => $handle->fileno ); | ||
| 127 | my $cb = $self->{fh_cb}{$handle} ||= sub { | ||
| 128 | my ( undef, $status, $events ) = @_; | ||
| 129 | if( my $cb = $state->{on_read_ready} ) { | ||
| 130 | $cb->() if $events & UV::Poll::UV_READABLE or $status < 0; | ||
| 131 | } | ||
| 132 | if( my $cb = $state->{on_write_ready} ) { | ||
| 133 | $cb->() if $events & UV::Poll::UV_WRITABLE or $status < 0; | ||
| 134 | } | ||
| 135 | }; | ||
| 136 | |||
| 137 | $uvh->start( $state->{mask}, $cb ); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | sub unwatch_io | ||
| 142 | { | ||
| 143 | my $self = shift; | ||
| 144 | my %params = @_; | ||
| 145 | |||
| 146 | my $handle = $params{handle} or die "Need a handle"; | ||
| 147 | my $state = $self->{fh}{$handle} or return; | ||
| 148 | my $uvh = $state->{uvh}; | ||
| 149 | |||
| 150 | if( $params{on_read_ready} ) { | ||
| 151 | $state->{mask} &= ~UV::Poll::UV_READABLE; | ||
| 152 | undef $state->{on_read_ready}; | ||
| 153 | } | ||
| 154 | |||
| 155 | if( $params{on_write_ready} ) { | ||
| 156 | $state->{mask} &= ~UV::Poll::UV_WRITABLE; | ||
| 157 | undef $state->{on_write_ready}; | ||
| 158 | } | ||
| 159 | |||
| 160 | if( !$uvh ) { | ||
| 161 | if( $state->{mask} ) { | ||
| 162 | # do nothing | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | delete $self->{fakefiles}{$handle}; | ||
| 166 | $self->{fakefiles_idle}->stop unless keys %{ $self->{fakefiles} }; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | else { | ||
| 170 | if( $state->{mask} ) { | ||
| 171 | $uvh->start( $state->{mask}, $self->{fh_cb}{$handle} ); | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | delete $self->{fh}{$handle}; | ||
| 175 | delete $self->{fh_cb}{$handle}; | ||
| 176 | $uvh->stop; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | sub watch_time | ||
| 182 | { | ||
| 183 | my $self = shift; | ||
| 184 | my %params = @_; | ||
| 185 | |||
| 186 | my $code = $params{code} or croak "Expected 'code' as CODE ref"; | ||
| 187 | my $now = $params{now} || $self->time; | ||
| 188 | my $delay = $params{after} || ( $params{at} - $now ); | ||
| 189 | |||
| 190 | $delay = 0 if $delay < 0; | ||
| 191 | |||
| 192 | my $timer = UV::Timer->new; | ||
| 193 | # Ugly selfcycle to avoid it going out of scope and getting DESTROY'ed | ||
| 194 | $timer->start( $delay * 1000, 0, sub { | ||
| 195 | undef $timer; | ||
| 196 | $code->(); | ||
| 197 | }); | ||
| 198 | |||
| 199 | return $timer; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub unwatch_time | ||
| 203 | { | ||
| 204 | my $self = shift; | ||
| 205 | my ( $timer ) = @_; | ||
| 206 | |||
| 207 | $timer->stop; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub watch_idle | ||
| 211 | { | ||
| 212 | my $self = shift; | ||
| 213 | my %params = @_; | ||
| 214 | |||
| 215 | my $when = delete $params{when} or croak "Expected 'when'"; | ||
| 216 | |||
| 217 | my $code = delete $params{code} or croak "Expected 'code' as a CODE ref"; | ||
| 218 | |||
| 219 | $when eq "later" or croak "Expected 'when' to be 'later'"; | ||
| 220 | |||
| 221 | my $idles = $self->{idles} ||= {}; | ||
| 222 | my $idle = UV::Idle->new; | ||
| 223 | $idles->{$idle}++; | ||
| 224 | $idle->start( sub { | ||
| 225 | $idle->stop; | ||
| 226 | delete $idles->{$idle}; | ||
| 227 | $code->(); | ||
| 228 | } ); | ||
| 229 | |||
| 230 | return $idle; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub unwatch_idle | ||
| 234 | { | ||
| 235 | my $self = shift; | ||
| 236 | my ( $idle ) = @_; | ||
| 237 | |||
| 238 | $idle->stop; | ||
| 239 | delete $self->{idles}{$idle}; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub watch_signal | ||
| 243 | { | ||
| 244 | my $self = shift; | ||
| 245 | my ( $signame, $code ) = @_; | ||
| 246 | |||
| 247 | defined( my $signum = IO::Async::OS->signame2num( $signame ) ) or | ||
| 248 | croak "Unrecognised signal $signame"; | ||
| 249 | |||
| 250 | my $signals = $self->{signals} ||= {}; | ||
| 251 | my $signal = UV::Signal->new( signal => $signum ); | ||
| 252 | $signal->start( $code ); | ||
| 253 | |||
| 254 | $signals->{$signame} = $signal; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub unwatch_signal | ||
| 258 | { | ||
| 259 | my $self = shift; | ||
| 260 | my ( $signame ) = @_; | ||
| 261 | |||
| 262 | ( delete $self->{signals}{$signame} )->stop; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub watch_process | ||
| 266 | { | ||
| 267 | my $self = shift; | ||
| 268 | my ( $pid, $code ) = @_; | ||
| 269 | |||
| 270 | defined $pid or croak "Require a PID for ->watch_process"; | ||
| 271 | $pid or croak "Require a PID for ->watch_process (cannot watch for all processes by PID=0)"; | ||
| 272 | |||
| 273 | my $pids = $self->{pids} ||= {}; | ||
| 274 | # libuv does not currently have a nice way to ask it to watch an existing | ||
| 275 | # PID that it didn't fork/exec itself. All we can do here is ask to be | ||
| 276 | # informed of SIGCHLD and then check if any of the processes we're keeping | ||
| 277 | # an eye on have exited yet. This is kindof sucky, because it means a | ||
| 278 | # linear scan on every signal. | ||
| 279 | # https://github.com/libuv/libuv/issues/3100 | ||
| 280 | $self->{sigchld_watch} ||= do { | ||
| 281 | my $sigchld_watch = UV::Signal->new( signal => POSIX::SIGCHLD ); | ||
| 282 | $sigchld_watch->start(sub { | ||
| 283 | foreach my $pid ( keys %$pids ) { | ||
| 284 | my $zid = waitpid( $pid, POSIX::WNOHANG ) or next; | ||
| 285 | next if $zid == -1; | ||
| 286 | my $status = $?; | ||
| 287 | |||
| 288 | ( delete $pids->{$pid} )->( $zid, $status ); | ||
| 289 | } | ||
| 290 | }); | ||
| 291 | $sigchld_watch; | ||
| 292 | }; | ||
| 293 | |||
| 294 | # There's a chance the process already exit()ed before we watched for | ||
| 295 | # SIGCHLD | ||
| 296 | my $zid; | ||
| 297 | if( $zid = waitpid( $pid, POSIX::WNOHANG ) and $zid != -1 ) { | ||
| 298 | my $status = $?; | ||
| 299 | $code->( $zid, $status ); | ||
| 300 | |||
| 301 | return; | ||
| 302 | } | ||
| 303 | |||
| 304 | $pids->{$pid} = $code; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub unwatch_process | ||
| 308 | { | ||
| 309 | my $self = shift; | ||
| 310 | my ( $pid ) = @_; | ||
| 311 | |||
| 312 | delete $self->{pids}{$pid}; | ||
| 313 | } | ||
| 314 | |||
| 315 | =head1 BUGS | ||
| 316 | |||
| 317 | =over 2 | ||
| 318 | |||
| 319 | =item * | ||
| 320 | |||
| 321 | F<libuv> does not provide a way to inspect the C<POLLHUP> status bit, so some | ||
| 322 | types of file descriptor cannot provide EOF condition. This causes a unit-test | ||
| 323 | failure. (RT#113010) | ||
| 324 | |||
| 325 | =item * | ||
| 326 | |||
| 327 | L<libuv> does not provide a way to be informed of any SIGCHLD/waitpid() | ||
| 328 | status. Because of this there is no ability to watch for all processes by | ||
| 329 | |||
| 330 | $loop->watch_process( 0 => sub { ... } ); | ||
| 331 | |||
| 332 | Attempts to do this will throw an exception. | ||
| 333 | |||
| 334 | =back | ||
| 335 | |||
| 336 | =head1 AUTHOR | ||
| 337 | |||
| 338 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 339 | |||
| 340 | =cut | ||
| 341 | |||
| 342 | 0x55AA; |