1 |
=head1 NAME |
2 |
|
3 |
Coro::AIO - truly asynchronous file and directrory I/O |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro::AIO; |
8 |
|
9 |
# can now use any of the aio requests your IO::AIO module supports. |
10 |
|
11 |
# read 1MB of /etc/passwd, without blocking other coroutines |
12 |
my $fh = aio_open "/etc/passwd", O_RDONLY, 0 |
13 |
or die "/etc/passwd: $!"; |
14 |
aio_read $fh, 0, 1_000_000, my $buf, 0 |
15 |
or die "aio_read: $!"; |
16 |
aio_close $fh; |
17 |
|
18 |
=head1 DESCRIPTION |
19 |
|
20 |
This module is an L<AnyEvent> user, you need to make sure that you use and |
21 |
run a supported event loop. |
22 |
|
23 |
This module implements a thin wrapper around L<IO::AIO|IO::AIO>. All of |
24 |
the functions that expect a callback are being wrapped by this module. |
25 |
|
26 |
The API is exactly the same as that of the corresponding IO::AIO routines, |
27 |
except that you have to specify I<all> arguments I<except> the callback |
28 |
argument. Instead the routines return the values normally passed to the |
29 |
callback. Everything else, including C<$!> and perls stat cache, are set |
30 |
as expected after these functions return. |
31 |
|
32 |
You can mix calls to C<IO::AIO> functions with calls to this module. You |
33 |
I<must not>, however, call these routines from within IO::AIO callbacks, |
34 |
as this causes a deadlock. Start a coro inside the callback instead. |
35 |
|
36 |
You also can, but do not need to, call C<IO::AIO::poll_cb>, as this |
37 |
module automatically installs an event watcher for the C<IO::AIO> file |
38 |
descriptor. It uses the L<AnyEvent> module for this, so please refer to |
39 |
its documentation on how it selects an appropriate Event module. |
40 |
|
41 |
The AnyEvent watcher can be disabled by executing C<undef |
42 |
$Coro::AIO::WATCHER>. Please notify the author of when and why you think |
43 |
this was necessary. |
44 |
|
45 |
All other functions exported by default by IO::AIO (e.g. C<aioreq_pri>) |
46 |
will be exported by default by Coro::AIO, too. |
47 |
|
48 |
Functions that can be optionally imported from IO::AIO can be imported |
49 |
from Coro::AIO or can be called directly, e.g. C<Coro::AIO::nreqs>. |
50 |
|
51 |
You cannot specify priorities with C<aioreq_pri>, as this module |
52 |
overwrites the request priority with the current coroutine priority at all |
53 |
times. |
54 |
|
55 |
For your convienience, here are the changed function signatures for most |
56 |
of the requests, for documentation of these functions please have a look |
57 |
at L<IO::AIO|the IO::AIO manual>. Note that requests added by newer |
58 |
versions of L<IO::AIO> will be automatically wrapped as well. |
59 |
|
60 |
=over 4 |
61 |
|
62 |
=cut |
63 |
|
64 |
package Coro::AIO; |
65 |
|
66 |
use strict qw(subs vars); |
67 |
|
68 |
use Coro (); |
69 |
use AnyEvent (); |
70 |
use IO::AIO (); |
71 |
|
72 |
use base Exporter::; |
73 |
|
74 |
our $WATCHER; |
75 |
|
76 |
$WATCHER = AnyEvent::post_detect { |
77 |
if ($AnyEvent::MODEL eq "AnyEvent::Impl::EV") { |
78 |
$WATCHER = EV::io (IO::AIO::poll_fileno, &EV::READ, \&IO::AIO::poll_cb); |
79 |
} else { |
80 |
our $FH; open $FH, "<&=" . IO::AIO::poll_fileno; |
81 |
$WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => \&IO::AIO::poll_cb); |
82 |
} |
83 |
}; |
84 |
|
85 |
our @EXPORT = @IO::AIO::EXPORT; |
86 |
our @EXPORT_OK = @IO::AIO::EXPORT_OK; |
87 |
our $AUTOLOAD; |
88 |
|
89 |
{ |
90 |
my @reqs = @IO::AIO::AIO_REQ ? @IO::AIO::AIO_REQ : @EXPORT; |
91 |
my %reqs = map +($_ => 1), @reqs; |
92 |
|
93 |
eval |
94 |
join "", |
95 |
map "sub $_(" . (prototype "IO::AIO::$_") . ");", |
96 |
grep !$reqs{$_}, |
97 |
@EXPORT, @EXPORT_OK; |
98 |
|
99 |
for my $sub (@reqs) { |
100 |
push @EXPORT, $sub; |
101 |
|
102 |
my $iosub = "IO::AIO::$sub"; |
103 |
my $proto = prototype $iosub; |
104 |
|
105 |
$proto =~ s/;?\$$// or die "$iosub: unable to remove callback slot from prototype"; |
106 |
|
107 |
eval qq{ |
108 |
#line 1 "Coro::AIO::$sub($proto)" |
109 |
sub $sub($proto) { |
110 |
my \$current = \$Coro::current; |
111 |
my \$state; |
112 |
my \@res; |
113 |
|
114 |
push \@_, sub { |
115 |
\$state = _get_state; |
116 |
\@res = \@_; |
117 |
\$current->ready; |
118 |
}; |
119 |
|
120 |
aioreq_pri \$Coro::current->prio; |
121 |
&$iosub; |
122 |
|
123 |
&Coro::schedule; |
124 |
&Coro::schedule while !\$state; |
125 |
|
126 |
_set_state \$state; |
127 |
wantarray ? \@res : \$res[0] |
128 |
} |
129 |
}; |
130 |
die if $@; |
131 |
} |
132 |
} |
133 |
|
134 |
sub AUTOLOAD { |
135 |
(my $func = $AUTOLOAD) =~ s/^.*:://; |
136 |
*$AUTOLOAD = \&{"IO::AIO::$func"}; |
137 |
goto &$AUTOLOAD; |
138 |
} |
139 |
|
140 |
=item $fh = aio_open $pathname, $flags, $mode |
141 |
|
142 |
=item $status = aio_close $fh |
143 |
|
144 |
=item $retval = aio_read $fh,$offset,$length, $data,$dataoffset |
145 |
|
146 |
=item $retval = aio_write $fh,$offset,$length, $data,$dataoffset |
147 |
|
148 |
=item $retval = aio_sendfile $out_fh, $in_fh, $in_offset, $length |
149 |
|
150 |
=item $retval = aio_readahead $fh,$offset,$length |
151 |
|
152 |
=item $status = aio_stat $fh_or_path |
153 |
|
154 |
=item $status = aio_lstat $fh |
155 |
|
156 |
=item $status = aio_unlink $pathname |
157 |
|
158 |
=item $status = aio_rmdir $pathname |
159 |
|
160 |
=item $entries = aio_readdir $pathname |
161 |
|
162 |
=item ($dirs, $nondirs) = aio_scandir $path, $maxreq |
163 |
|
164 |
=item $status = aio_fsync $fh |
165 |
|
166 |
=item $status = aio_fdatasync $fh |
167 |
|
168 |
=item ... = aio_xxx ... |
169 |
|
170 |
Any additional aio requests follow the same scheme: same parameters except |
171 |
you must not specify a callback but instead get the callback arguments as |
172 |
return values. |
173 |
|
174 |
=back |
175 |
|
176 |
=head1 SEE ALSO |
177 |
|
178 |
L<Coro::Socket> and L<Coro::Handle> for non-blocking socket operation. |
179 |
|
180 |
=head1 AUTHOR |
181 |
|
182 |
Marc Lehmann <schmorp@schmorp.de> |
183 |
http://home.schmorp.de/ |
184 |
|
185 |
=cut |
186 |
|
187 |
1 |