| 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, 2026 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | package Future::IO::Impl::KQueue 0.03; | ||
| 7 | |||
| 8 | 1 | ▶ | use v5.20; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use base qw( Future::IO::ImplBase ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | use Future::IO qw( POLLIN POLLOUT POLLPRI POLLHUP POLLERR POLLNVAL ); | ||
| 12 | |||
| 13 | Future::IO::ImplBase->VERSION( '0.22' ); | ||
| 14 | |||
| 15 | use feature qw( postderef signatures ); | ||
| 16 | no warnings qw( experimental::postderef experimental::signatures ); | ||
| 17 | |||
| 18 | use Carp; | ||
| 19 | our @CARP_NOT = qw( Future::IO::Impl::KQueue::_Future ); | ||
| 20 | |||
| 21 | use IO::KQueue; | ||
| 22 | use POSIX (); | ||
| 23 | use Struct::Dumb qw( readonly_struct ); | ||
| 24 | use Time::HiRes qw( time ); | ||
| 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::KQueue> - implement C<Future::IO> using C<kqueue(2)> | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | =for highlighter language=perl | ||
| 46 | |||
| 47 | This module provides an implementation for L<Future::IO> which uses the | ||
| 48 | C<kqueue(2)> and C<kevents(2)> system calls, via L<IO::KQueue>. | ||
| 49 | |||
| 50 | There are no additional methods to use in this module; it simply has to be | ||
| 51 | loaded, and it will provide the C<Future::IO> implementation methods: | ||
| 52 | |||
| 53 | use Future::IO; | ||
| 54 | use Future::IO::Impl::KQueue; | ||
| 55 | |||
| 56 | my $f = Future::IO->sleep(5); | ||
| 57 | ... | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | readonly_struct Poller => [qw( events f )]; | ||
| 62 | my %pollers_by_refaddr; | ||
| 63 | my @deferred_epipe; | ||
| 64 | |||
| 65 | my %waitpids_by_pid; | ||
| 66 | |||
| 67 | my $kq; | ||
| 68 | sub kqueue () { $kq //= IO::KQueue->new; } | ||
| 69 | |||
| 70 | sub _new_future ( $ ) { return Future::IO::Impl::KQueue::_Future->new } | ||
| 71 | |||
| 72 | sub _tick ( $class ) | ||
| 73 | { | ||
| 74 | kqueue(); | ||
| 75 | |||
| 76 | my $timeout = $class->_timeout; | ||
| 77 | |||
| 78 | $timeout = 0 if @deferred_epipe; | ||
| 79 | |||
| 80 | # $timeout in msec | ||
| 81 | my @events = $kq->kevent( defined $timeout ? $timeout * 1000 : undef ); | ||
| 82 | |||
| 83 | foreach my $e ( @events ) { | ||
| 84 | my $filter = $e->[KQ_FILTER]; | ||
| 85 | |||
| 86 | if( $filter == EVFILT_READ or $filter == EVFILT_WRITE ) { | ||
| 87 | my $refaddr = $e->[KQ_UDATA]; | ||
| 88 | |||
| 89 | my $revents = ( $filter == EVFILT_READ ) ? POLLIN : | ||
| 90 | ( $filter == EVFILT_WRITE ) ? POLLOUT : 0; | ||
| 91 | $revents |= POLLHUP if $e->[KQ_FLAGS] & EV_EOF; | ||
| 92 | |||
| 93 | my $pollers = $pollers_by_refaddr{$refaddr} or next; | ||
| 94 | # TODO: if nobody cared, maybe we should remove it? | ||
| 95 | |||
| 96 | # Find the next poller which cares about at least one of these events | ||
| 97 | foreach my $idx ( 0 .. $#$pollers ) { | ||
| 98 | my $want_revents = $revents & ( $pollers->[$idx]->events | POLLHUP|POLLERR|POLLNVAL ) | ||
| 99 | or next; | ||
| 100 | |||
| 101 | my ( $poller ) = splice @$pollers, $idx, 1, (); | ||
| 102 | |||
| 103 | $poller and $poller->f and $poller->f->done( $want_revents ); | ||
| 104 | last; | ||
| 105 | } | ||
| 106 | |||
| 107 | my $mask = 0; | ||
| 108 | $mask |= $_->events for ( $pollers_by_refaddr{$refaddr} // [] )->@*; | ||
| 109 | |||
| 110 | if( $filter == EVFILT_READ and not $mask & (POLLIN|POLLPRI) ) { | ||
| 111 | $kq->EV_SET( $e->[KQ_IDENT], EVFILT_READ, EV_DELETE ); | ||
| 112 | } | ||
| 113 | if( $filter == EVFILT_WRITE and not $mask & POLLOUT ) { | ||
| 114 | $kq->EV_SET( $e->[KQ_IDENT], EVFILT_WRITE, EV_DELETE ); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | elsif( $filter == EVFILT_SIGNAL ) { | ||
| 118 | my $signum = $e->[KQ_IDENT]; | ||
| 119 | if( $signum == POSIX::SIGCHLD ) { | ||
| 120 | foreach my $pid ( keys %waitpids_by_pid ) { | ||
| 121 | next unless waitpid( $pid, POSIX::WNOHANG ) > 0; | ||
| 122 | my $wstatus = $?; | ||
| 123 | |||
| 124 | my $fs = delete $waitpids_by_pid{$pid}; | ||
| 125 | $_->done( $wstatus ) for @$fs; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | die "Oopsie event filter=$filter <@$e>"; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | $class->_manage_timers; | ||
| 135 | |||
| 136 | if( @deferred_epipe ) { | ||
| 137 | my @f = @deferred_epipe; @deferred_epipe = (); | ||
| 138 | foreach my $f ( @f ) { | ||
| 139 | $f->done( POLLHUP ); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | # We can't use KQueue's EVFILT_TIMER for sleep() and alarm() because timeout | ||
| 145 | # values would have to be unique. It can't support multiple separate timers | ||
| 146 | # with the exact same expiry time :( | ||
| 147 | # Instead we just use the timer queue supplied by Future::IO::ImplBase 0.22 | ||
| 148 | |||
| 149 | sub poll ( $, $fh, $events ) | ||
| 150 | { | ||
| 151 | defined $fh or | ||
| 152 | croak "Expected a defined filehandle for ->poll"; | ||
| 153 | |||
| 154 | kqueue(); | ||
| 155 | |||
| 156 | my $refaddr = refaddr $fh; | ||
| 157 | |||
| 158 | my $pollers = $pollers_by_refaddr{$refaddr} //= []; | ||
| 159 | |||
| 160 | my $f = Future::IO::Impl::KQueue::_Future->new; | ||
| 161 | |||
| 162 | defined( my $fileno = $fh->fileno ) or | ||
| 163 | carp "Filehandle $fh lost its fileno (was closed?) during poll"; | ||
| 164 | |||
| 165 | my $mask = $events; | ||
| 166 | $mask |= $_->events for ( $pollers_by_refaddr{$refaddr} // [] )->@*; | ||
| 167 | |||
| 168 | my $got_epipe; | ||
| 169 | |||
| 170 | # Don't have to ask for POLLHUP; it comes for free | ||
| 171 | # We should always use the _READ filter if looking for HUP though | ||
| 172 | if( $mask & POLLHUP ) { | ||
| 173 | $mask |= POLLIN; | ||
| 174 | $mask &= ~POLLHUP; | ||
| 175 | } | ||
| 176 | |||
| 177 | if( $mask & (POLLIN|POLLPRI) ) { | ||
| 178 | # POLLPRI doesn't have a direct KQueue equivalent | ||
| 179 | eval { $kq->EV_SET( $fileno, EVFILT_READ, EV_ADD, 0, 0, $refaddr ); 1 } or | ||
| 180 | ( $! == POSIX::EPIPE and ++$got_epipe ) or | ||
| 181 | die $@; | ||
| 182 | } | ||
| 183 | if( $mask & POLLOUT ) { | ||
| 184 | eval { $kq->EV_SET( $fileno, EVFILT_WRITE, EV_ADD, 0, 0, $refaddr ); 1 } or | ||
| 185 | ( $! == POSIX::EPIPE and ++$got_epipe ) or | ||
| 186 | die $@; | ||
| 187 | } | ||
| 188 | if( $mask & ~(POLLIN|POLLOUT|POLLPRI) ) { | ||
| 189 | croak sprintf "ARGH unrecognised event flags mask %02X", $mask; | ||
| 190 | } | ||
| 191 | |||
| 192 | if( $got_epipe ) { | ||
| 193 | # EV_SET yielded EPIPE synchronously. We need to defer this reporting | ||
| 194 | # until the next call to _tick. | ||
| 195 | push @deferred_epipe, $f; | ||
| 196 | } | ||
| 197 | else { | ||
| 198 | push @$pollers, Poller( $events, $f ); | ||
| 199 | } | ||
| 200 | |||
| 201 | return $f; | ||
| 202 | } | ||
| 203 | |||
| 204 | # We can't use IO::KQueue's EVFILT_PROC here because of a small bug. KQueue.xs | ||
| 205 | # defines the `fflags` field as u_short rather than u_int, which means it | ||
| 206 | # truncates the `NOTE_EXIT` flag which we'd need to have access to. | ||
| 207 | # I would report that upstream, but the repo is currently marked readonly and | ||
| 208 | # seems abandoned. :( | ||
| 209 | # | ||
| 210 | # For now we'll just use the usual SIGCHLD wrapper trick from Ppoll. | ||
| 211 | |||
| 212 | my $captured_sigchld; | ||
| 213 | |||
| 214 | sub waitpid ( $, $pid ) | ||
| 215 | { | ||
| 216 | unless( $captured_sigchld ) { | ||
| 217 | $SIG{CHLD} and $SIG{CHLD} ne "IGNORE" and | ||
| 218 | warn "Future::IO::Impl::KQueue is replacing \$SIG{CHLD}"; | ||
| 219 | |||
| 220 | kqueue(); | ||
| 221 | |||
| 222 | $kq->EV_SET( POSIX::SIGCHLD, EVFILT_SIGNAL, EV_ADD ); | ||
| 223 | |||
| 224 | $captured_sigchld = 1; | ||
| 225 | } | ||
| 226 | |||
| 227 | my $f = Future::IO::Impl::KQueue::_Future->new; | ||
| 228 | |||
| 229 | if( waitpid( $pid, POSIX::WNOHANG ) > 0 ) { | ||
| 230 | my $wstatus = $?; | ||
| 231 | $f->done( $wstatus ); | ||
| 232 | return $f; | ||
| 233 | } | ||
| 234 | |||
| 235 | push $waitpids_by_pid{$pid}->@*, $f; | ||
| 236 | |||
| 237 | return $f; | ||
| 238 | } | ||
| 239 | |||
| 240 | package Future::IO::Impl::KQueue::_Future { | ||
| 241 | use base qw( Future ); | ||
| 242 | |||
| 243 | sub await ( $self ) | ||
| 244 | { | ||
| 245 | Future::IO::Impl::KQueue->_tick until $self->is_ready; | ||
| 246 | return $self; | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | =head1 AUTHOR | ||
| 251 | |||
| 252 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | 0x55AA; | ||