| 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, 2021-2026 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | package Future::IO::Impl::UV 0.08; | ||
| 7 | |||
| 8 | 11 | ▶ | use v5.20; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 11 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 11 | ▶ | use base qw( Future::IO::ImplBase ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 11 | ▶ | use feature qw( postderef signatures ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 11 | ▶ | no warnings qw( experimental::postderef experimental::signatures ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 11 | ▶ | use Future::IO 0.20 qw( POLLIN POLLOUT POLLPRI POLLHUP POLLERR ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | 0 | ▶ | use UV; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | use UV::Poll 2.002; # fh class bugfix | ||
| 19 | use UV::Timer; | ||
| 20 | use UV::Signal; | ||
| 21 | |||
| 22 | use POSIX (); | ||
| 23 | |||
| 24 | use Struct::Dumb qw( readonly_struct ); | ||
| 25 | |||
| 26 | BEGIN { | ||
| 27 | if( $^V ge v5.36 ) { | ||
| 28 | builtin->import(qw( refaddr )); | ||
| 29 | warnings->unimport(qw( experimental::builtin )) if $^V lt v5.40; | ||
| 30 | } | ||
| 31 | else { | ||
| 32 | require Scalar::Util; | ||
| 33 | Scalar::Util->import(qw( refaddr )); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | __PACKAGE__->APPLY; | ||
| 38 | |||
| 39 | =head1 NAME | ||
| 40 | |||
| 41 | C<Future::IO::Impl::UV> - implement C<Future::IO> using C<UV> | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | =for highlighter language=perl | ||
| 46 | |||
| 47 | This module provides an implementation for L<Future::IO> which uses L<UV>. | ||
| 48 | |||
| 49 | There are no additional methods to use in this module; it simply has to be | ||
| 50 | loaded, and it will provide the C<Future::IO> implementation methods: | ||
| 51 | |||
| 52 | use Future::IO; | ||
| 53 | use Future::IO::Impl::UV; | ||
| 54 | |||
| 55 | my $f = Future::IO->sleep(5); | ||
| 56 | ... | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub sleep ( $, $secs ) | ||
| 61 | { | ||
| 62 | my $f = Future::IO::Impl::UV::_Future->new; | ||
| 63 | |||
| 64 | my $timer = UV::Timer->new; | ||
| 65 | $timer->start( $secs * 1000, 0, sub ( $ ) { $f->done; } ); | ||
| 66 | $f->on_cancel( sub ( $ ) { $timer->stop; } ); | ||
| 67 | |||
| 68 | return $f; | ||
| 69 | } | ||
| 70 | |||
| 71 | # libuv doesn't like having more than one uv_poll_t instance per filehandle, | ||
| 72 | # so we'll have to combine reads and writes | ||
| 73 | |||
| 74 | readonly_struct Poller => [qw( events f )]; | ||
| 75 | my %pollers_by_refaddr; | ||
| 76 | my %uvpoll_by_refaddr; | ||
| 77 | |||
| 78 | sub _update_poll ( $fh ) | ||
| 79 | { | ||
| 80 | my $refaddr = refaddr $fh; | ||
| 81 | |||
| 82 | my $poll = $uvpoll_by_refaddr{$refaddr} //= | ||
| 83 | UV::Poll->new( | ||
| 84 | fh => $fh, | ||
| 85 | on_poll => sub ( $poll, $status, $events ) { | ||
| 86 | my $revents = 0; | ||
| 87 | $revents |= POLLIN if $events & UV::Poll::UV_READABLE; | ||
| 88 | $revents |= POLLOUT if $events & UV::Poll::UV_WRITABLE; | ||
| 89 | $revents |= POLLPRI if $events & UV::Poll::UV_PRIORITIZED; | ||
| 90 | $revents |= POLLHUP if $events & UV::Poll::UV_DISCONNECT; | ||
| 91 | $revents |= POLLERR if $status; | ||
| 92 | |||
| 93 | my $pollers = $pollers_by_refaddr{$refaddr}; | ||
| 94 | foreach my $idx ( 0 .. $#$pollers ) { | ||
| 95 | $pollers->[$idx]->events & $revents or $revents & (POLLHUP|POLLERR) or | ||
| 96 | next; | ||
| 97 | |||
| 98 | my ( $p ) = splice @$pollers, $idx, 1, (); | ||
| 99 | |||
| 100 | $p and $p->f and $p->f->done( $revents ); | ||
| 101 | last; | ||
| 102 | } | ||
| 103 | |||
| 104 | _update_poll( $fh ); | ||
| 105 | }, | ||
| 106 | ); | ||
| 107 | |||
| 108 | my $want = 0; | ||
| 109 | foreach my $poller ( $pollers_by_refaddr{$refaddr}->@* ) { | ||
| 110 | $want |= UV::Poll::UV_READABLE if $poller->events & POLLIN; | ||
| 111 | $want |= UV::Poll::UV_WRITABLE if $poller->events & POLLOUT; | ||
| 112 | $want |= UV::Poll::UV_PRIORITIZED if $poller->events & POLLPRI; | ||
| 113 | $want |= UV::Poll::UV_DISCONNECT if $poller->events & POLLHUP; | ||
| 114 | } | ||
| 115 | |||
| 116 | if( $want ) { | ||
| 117 | $poll->start( $want ); | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | $poll->stop; | ||
| 121 | delete $uvpoll_by_refaddr{$refaddr}; | ||
| 122 | delete $pollers_by_refaddr{$refaddr}; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | sub poll ( $, $fh, $events ) | ||
| 127 | { | ||
| 128 | my $refaddr = refaddr $fh; | ||
| 129 | |||
| 130 | my $f = Future::IO::Impl::UV::_Future->new; | ||
| 131 | |||
| 132 | push $pollers_by_refaddr{$refaddr}->@*, Poller( $events, $f ); | ||
| 133 | |||
| 134 | _update_poll( $fh ); | ||
| 135 | return $f; | ||
| 136 | } | ||
| 137 | |||
| 138 | my $sigchld_watch; | ||
| 139 | my %futures_waitpid; # {$pid} => [@futures] | ||
| 140 | |||
| 141 | sub waitpid ( $, $pid ) | ||
| 142 | { | ||
| 143 | # libuv does not currently have a nice way to ask it to watch an existing | ||
| 144 | # PID that it didn't fork/exec itself. All we can do here is ask to be | ||
| 145 | # informed of SIGCHLD and then check if any of the processes we're keeping | ||
| 146 | # an eye on have exited yet. This is kindof sucky, because it means a | ||
| 147 | # linear scan on every signal. | ||
| 148 | # https://github.com/libuv/libuv/issues/3100 | ||
| 149 | $sigchld_watch ||= do { | ||
| 150 | my $w = UV::Signal->new( signal => POSIX::SIGCHLD ); | ||
| 151 | $w->start(sub ( $, $signum ) { | ||
| 152 | foreach my $pid ( keys %futures_waitpid ) { | ||
| 153 | next unless waitpid( $pid, POSIX::WNOHANG ) > 0; | ||
| 154 | my $wstatus = $?; | ||
| 155 | |||
| 156 | my $fs = delete $futures_waitpid{$pid}; | ||
| 157 | $_->done( $wstatus ) for @$fs; | ||
| 158 | } | ||
| 159 | }); | ||
| 160 | $w; | ||
| 161 | }; | ||
| 162 | |||
| 163 | my $f = Future::IO::Impl::UV::_Future->new; | ||
| 164 | |||
| 165 | if( waitpid( $pid, POSIX::WNOHANG ) > 0 ) { | ||
| 166 | my $wstatus = $?; | ||
| 167 | $f->done( $wstatus ); | ||
| 168 | return $f; | ||
| 169 | } | ||
| 170 | |||
| 171 | push $futures_waitpid{$pid}->@*, $f; | ||
| 172 | |||
| 173 | return $f; | ||
| 174 | } | ||
| 175 | |||
| 176 | package Future::IO::Impl::UV::_Future; | ||
| 177 | use base qw( Future ); | ||
| 178 | |||
| 179 | sub await ( $self ) | ||
| 180 | { | ||
| 181 | UV::loop->run( UV::Loop::UV_RUN_ONCE ) until $self->is_ready; | ||
| 182 | return $self; | ||
| 183 | } | ||
| 184 | |||
| 185 | =head1 AUTHOR | ||
| 186 | |||
| 187 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | 0x55AA; | ||