| 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 IO::Async::Loop::FutureIO 0.02; | ||
| 7 | |||
| 8 | 1 | ▶ | use v5.14; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 1 | ▶ | use constant API_VERSION => '0.76'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | # We cannot support ->watch_process( 0 => ... ) because Future::IO does not | ||
| 14 | # give us a hook | ||
| 15 | 1 | ▶ | use constant _CAN_WATCH_ALL_PIDS => 0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | 1 | ▶ | use base qw( IO::Async::Loop ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | IO::Async::Loop->VERSION( '0.49' ); | ||
| 19 | |||
| 20 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | |||
| 22 | 0 | ▶ | use Future::IO 0.19; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 23 | use Future::IO 0.19 qw( POLLIN POLLOUT ); | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | C<IO::Async::Loop::FutureIO> - use C<IO::Async> with C<Future::IO> | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | =for highlighter language=perl | ||
| 32 | |||
| 33 | use IO::Async::Loop::FutureIO; | ||
| 34 | |||
| 35 | use Future::IO: | ||
| 36 | Future::IO->load_best_impl; | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This subclass of L<IO::Async::Loop> uses L<Future::IO> to perform its work. | ||
| 41 | |||
| 42 | Currently there are a few features that don't yet work, due to missing support | ||
| 43 | from C<Future::IO> itself. Hopefully a later version of C<Future::IO> will be | ||
| 44 | able to provide these missing pieces, and then this module will be shipped by | ||
| 45 | default in the main C<IO-Async> distribution. | ||
| 46 | |||
| 47 | =head2 Missing Features | ||
| 48 | |||
| 49 | Currently the following things do not work with this module: | ||
| 50 | |||
| 51 | =over 4 | ||
| 52 | |||
| 53 | =item Signals | ||
| 54 | |||
| 55 | The C<watch_signal> and C<unwatch_signal> methods are not currently | ||
| 56 | implemented, because C<Future::IO> does not support a general purpose signal | ||
| 57 | wait ability. Once this is available, these methods can be added. | ||
| 58 | |||
| 59 | =item Watching PID 0 | ||
| 60 | |||
| 61 | Likewise, as C<Future::IO> only supports watching specific PIDs and not a | ||
| 62 | repeating wait for any process, this is not permitted here. | ||
| 63 | |||
| 64 | =item C<is_running> | ||
| 65 | |||
| 66 | The C<is_running> method cannot reliably answer whether C<Future::IO> itself | ||
| 67 | is currently blocked awaiting IO, so it is also not provided. | ||
| 68 | |||
| 69 | =item Metrics | ||
| 70 | |||
| 71 | This module would not be able to provide metrics on the overall operation of | ||
| 72 | C<Future::IO>, so it is not provided. | ||
| 73 | |||
| 74 | =back | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub new | ||
| 79 | { | ||
| 80 | my $class = shift; | ||
| 81 | my $self = $class->SUPER::__new( @_ ); | ||
| 82 | |||
| 83 | $Future::IO::IMPL eq "Future::IO::Impl::IOAsync" and | ||
| 84 | croak "Cannot mutually build Future::IO and IO::Async on each other"; | ||
| 85 | |||
| 86 | return $self; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub _more_f | ||
| 90 | { | ||
| 91 | my $self = shift; | ||
| 92 | my ( $f ) = @_; | ||
| 93 | |||
| 94 | my $loop_f = ( $self->{next_loop_f} //= $f->new ); | ||
| 95 | |||
| 96 | $f->on_ready( sub { | ||
| 97 | $loop_f->done unless $loop_f->is_ready; | ||
| 98 | }); | ||
| 99 | |||
| 100 | return $f; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub loop_once | ||
| 104 | { | ||
| 105 | my $self = shift; | ||
| 106 | my ( $timeout ) = @_; | ||
| 107 | |||
| 108 | my $timeout_f; | ||
| 109 | if( defined $timeout ) { | ||
| 110 | $timeout_f = $self->_more_f( Future::IO->sleep( $timeout ) ); | ||
| 111 | } | ||
| 112 | |||
| 113 | if( my $f = $self->{next_loop_f} ) { | ||
| 114 | undef $self->{next_loop_f}; | ||
| 115 | |||
| 116 | $f->await; | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | die "TODO: loop_once without next_loop_f"; | ||
| 120 | } | ||
| 121 | |||
| 122 | $timeout_f->cancel if $timeout_f; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _poll_for_io | ||
| 126 | { | ||
| 127 | my ( $watch, $handle, $mask, $cb ) = @_; | ||
| 128 | |||
| 129 | my $key = "${mask}_f"; | ||
| 130 | |||
| 131 | $watch->{$key} = Future::IO->poll( $handle, $mask ) | ||
| 132 | ->on_done( sub { | ||
| 133 | $cb->(); | ||
| 134 | |||
| 135 | _poll_for_io( $watch, $handle, $mask, $cb ) | ||
| 136 | if exists $watch->{$key}; | ||
| 137 | } ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub watch_io | ||
| 141 | { | ||
| 142 | my $self = shift; | ||
| 143 | my %params = @_; | ||
| 144 | |||
| 145 | my $handle = $params{handle} or die "Need a handle"; | ||
| 146 | my $watch = $self->{watch_io}{$handle} //= {}; | ||
| 147 | |||
| 148 | if( my $cb = $params{on_read_ready} ) { | ||
| 149 | _poll_for_io( $watch, $handle, POLLIN, $cb ); | ||
| 150 | } | ||
| 151 | if( my $cb = $params{on_write_ready} ) { | ||
| 152 | _poll_for_io( $watch, $handle, POLLOUT, $cb ); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | sub unwatch_io | ||
| 157 | { | ||
| 158 | my $self = shift; | ||
| 159 | my %params = @_; | ||
| 160 | |||
| 161 | my $handle = $params{handle} or die "Need a handle"; | ||
| 162 | my $watch = $self->{watch_io}{$handle} or return; | ||
| 163 | |||
| 164 | if( $params{on_read_ready} ) { | ||
| 165 | my $mask = POLLIN; | ||
| 166 | ( delete $watch->{"${mask}_f"} )->cancel; | ||
| 167 | } | ||
| 168 | if( $params{on_write_ready} ) { | ||
| 169 | my $mask = POLLOUT; | ||
| 170 | ( delete $watch->{"${mask}_f"} )->cancel; | ||
| 171 | } | ||
| 172 | |||
| 173 | keys %$watch or | ||
| 174 | delete $self->{watch_io}{$handle}; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub watch_time | ||
| 178 | { | ||
| 179 | my $self = shift; | ||
| 180 | my %params = @_; | ||
| 181 | |||
| 182 | my $code = $params{code} or croak "Expected 'code' as CODE ref"; | ||
| 183 | my $now = $params{now} // $self->time; | ||
| 184 | my $delay = $params{after} // ( $params{at} - $now ); | ||
| 185 | |||
| 186 | $delay = 0 if $delay < 0; | ||
| 187 | |||
| 188 | return $self->_more_f( | ||
| 189 | Future::IO->sleep( $delay ) | ||
| 190 | ->on_done( sub { $code->() } ) | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub unwatch_time | ||
| 195 | { | ||
| 196 | my $self = shift; | ||
| 197 | my ( $timer ) = @_; | ||
| 198 | |||
| 199 | $timer->cancel; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub watch_idle | ||
| 203 | { | ||
| 204 | my $self = shift; | ||
| 205 | my %params = @_; | ||
| 206 | |||
| 207 | my $when = delete $params{when} or croak "Expected 'when'"; | ||
| 208 | |||
| 209 | my $code = delete $params{code} or croak "Expected 'code' as a CODE ref"; | ||
| 210 | |||
| 211 | $when eq "later" or croak "Expected 'when' to be 'later'"; | ||
| 212 | |||
| 213 | # Just treat it as a zero timer | ||
| 214 | return $self->_more_f( | ||
| 215 | Future::IO->sleep( 0 ) | ||
| 216 | ->on_done( sub { $code->() } ) | ||
| 217 | ); | ||
| 218 | } | ||
| 219 | |||
| 220 | sub unwatch_idle | ||
| 221 | { | ||
| 222 | my $self = shift; | ||
| 223 | my ( $idle ) = @_; | ||
| 224 | |||
| 225 | $idle->cancel; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub watch_signal | ||
| 229 | { | ||
| 230 | croak "Future::IO does not currently support signal watches"; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub unwatch_signal | ||
| 234 | { | ||
| 235 | croak "Future::IO does not currently support signal watches"; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub watch_process | ||
| 239 | { | ||
| 240 | my $self = shift; | ||
| 241 | my ( $pid, $code ) = @_; | ||
| 242 | |||
| 243 | defined $pid or croak "Require a PID for ->watch_process"; | ||
| 244 | $pid or croak "Require a PID for ->watch_process (cannot watch for all processes by PID=0)"; | ||
| 245 | |||
| 246 | my $waitpids = $self->{waitpids} //= {}; | ||
| 247 | |||
| 248 | $waitpids->{$pid} = $self->_more_f( | ||
| 249 | Future::IO->waitpid( $pid ) | ||
| 250 | ->on_done( sub { | ||
| 251 | delete $waitpids->{$pid}; | ||
| 252 | $code->( $pid, $_[0] ); | ||
| 253 | }) | ||
| 254 | ); | ||
| 255 | } | ||
| 256 | |||
| 257 | sub unwatch_process | ||
| 258 | { | ||
| 259 | my $self = shift; | ||
| 260 | my ( $pid ) = @_; | ||
| 261 | |||
| 262 | my $f = delete $self->{waitpids}{$pid}; | ||
| 263 | $f->cancel if $f; | ||
| 264 | } | ||
| 265 | |||
| 266 | =head1 AUTHOR | ||
| 267 | |||
| 268 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 269 | |||
| 270 | =cut | ||
| 271 | |||
| 272 | 0x55AA; | ||