| 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, 2019-2026 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | package Future::IO::Impl::IOAsync 0.805; | ||
| 7 | |||
| 8 | use v5.14; | ||
| 9 | use warnings; | ||
| 10 | use base qw( Future::IO::ImplBase ); | ||
| 11 | BEGIN { Future::IO::ImplBase->VERSION( '0.19' ); } | ||
| 12 | |||
| 13 | use Future::IO qw( POLLIN POLLOUT POLLHUP ); | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | C<Future::IO::Impl::IOAsync> - implement C<Future::IO> using C<IO::Async> | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | =for highlighter language=perl | ||
| 22 | |||
| 23 | This module provides an implementation for L<Future::IO> which uses | ||
| 24 | L<IO::Async>. | ||
| 25 | |||
| 26 | There are no additional methods to use in this module; it simply has to be | ||
| 27 | loaded, and will provide the C<Future::IO> implementation methods. | ||
| 28 | |||
| 29 | use Future::IO; | ||
| 30 | use Future::IO::Impl::IOAsync; | ||
| 31 | |||
| 32 | my $f = Future::IO->sleep(5); | ||
| 33 | ... | ||
| 34 | |||
| 35 | =head1 LIMITATIONS | ||
| 36 | |||
| 37 | This module only provides a limited subset of the L<Future::IO/poll> method | ||
| 38 | API. It fully handles C<POLLIN> and C<POLLOUT> conditions, but is not able to | ||
| 39 | report on C<POLLHUP> and C<POLLERR> events. | ||
| 40 | |||
| 41 | When a filehandle is at hangup condition it is reported as only C<POLLIN>, and | ||
| 42 | when at error condition it is reported as only C<POLLOUT>. This I<should> be | ||
| 43 | sufficient for most purposes, and works fine for internally providing | ||
| 44 | asynchronous reading and writing on regular filehandles, but may cause some | ||
| 45 | odd behaviours if you are attempting to detect those conditions directly. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | use IO::Async::Loop; | ||
| 50 | |||
| 51 | __PACKAGE__->APPLY; | ||
| 52 | |||
| 53 | my $loop; | ||
| 54 | |||
| 55 | sub sleep | ||
| 56 | { | ||
| 57 | shift; | ||
| 58 | my ( $secs ) = @_; | ||
| 59 | |||
| 60 | return ( $loop //= IO::Async::Loop->new )->delay_future( after => $secs ); | ||
| 61 | } | ||
| 62 | |||
| 63 | my %futures_read_by_fileno; # {fileno} => [@futures] | ||
| 64 | my %futures_write_by_fileno; # {fileno} => [@futures] | ||
| 65 | |||
| 66 | sub poll | ||
| 67 | { | ||
| 68 | shift; | ||
| 69 | my ( $fh, $events ) = @_; | ||
| 70 | |||
| 71 | my $fileno = $fh->fileno; | ||
| 72 | |||
| 73 | my $f = ( $loop //= IO::Async::Loop->new )->new_future; | ||
| 74 | |||
| 75 | if( $events & (POLLIN|POLLHUP) ) { | ||
| 76 | my $futures = $futures_read_by_fileno{$fileno} //= []; | ||
| 77 | |||
| 78 | my $was = scalar @$futures; | ||
| 79 | push @$futures, $f; | ||
| 80 | |||
| 81 | # We can't watch for on_hangup as well here because most loop | ||
| 82 | # implementations will invoke that *as well as* on_read_ready, but | ||
| 83 | # only afterwards. That makes it difficult to report POLLHUP conditions | ||
| 84 | # properly. | ||
| 85 | $loop->watch_io( | ||
| 86 | handle => $fh, | ||
| 87 | on_read_ready => sub { | ||
| 88 | $futures->[0]->done( POLLIN ); | ||
| 89 | shift @$futures; | ||
| 90 | |||
| 91 | return if scalar @$futures; | ||
| 92 | |||
| 93 | $loop->unwatch_io( | ||
| 94 | handle => $fh, | ||
| 95 | on_read_ready => 1, | ||
| 96 | ); | ||
| 97 | delete $futures_read_by_fileno{$fileno}; | ||
| 98 | }, | ||
| 99 | ) if !$was; | ||
| 100 | } | ||
| 101 | if( $events & POLLOUT ) { | ||
| 102 | my $futures = $futures_write_by_fileno{$fileno} //= []; | ||
| 103 | |||
| 104 | my $was = scalar @$futures; | ||
| 105 | push @$futures, $f; | ||
| 106 | |||
| 107 | $loop->watch_io( | ||
| 108 | handle => $fh, | ||
| 109 | on_write_ready => sub { | ||
| 110 | $futures->[0]->done( POLLOUT ); | ||
| 111 | shift @$futures; | ||
| 112 | |||
| 113 | return if scalar @$futures; | ||
| 114 | |||
| 115 | $loop->unwatch_io( | ||
| 116 | handle => $fh, | ||
| 117 | on_write_ready => 1, | ||
| 118 | ); | ||
| 119 | delete $futures_write_by_fileno{$fileno}; | ||
| 120 | }, | ||
| 121 | ) if !$was; | ||
| 122 | } | ||
| 123 | |||
| 124 | return $f; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub waitpid | ||
| 128 | { | ||
| 129 | shift; | ||
| 130 | my ( $pid ) = @_; | ||
| 131 | |||
| 132 | my $f = ( $loop //= IO::Async::Loop->new )->new_future; | ||
| 133 | |||
| 134 | $loop->watch_process( $pid, sub { | ||
| 135 | my ( undef, $wstatus ) = @_; | ||
| 136 | $f->done( $wstatus ); | ||
| 137 | } ); | ||
| 138 | $f->on_cancel( sub { $loop->unwatch_process( $pid ) } ); | ||
| 139 | |||
| 140 | return $f; | ||
| 141 | } | ||
| 142 | |||
| 143 | =head1 AUTHOR | ||
| 144 | |||
| 145 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | 0x55AA; |