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 |
10 |
# aio_sendfile aio_read aio_write aio_open aio_close aio_stat aio_lstat |
11 |
# aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink aio_fsync |
12 |
# aio_fdatasync aio_readahead |
13 |
|
14 |
# read 1MB of /etc/passwd, without blocking other coroutines |
15 |
my $fh = aio_open "/etc/passwd", O_RDONLY, 0 |
16 |
or die "/etc/passwd: $!"; |
17 |
aio_read $fh, 0, 1_000_000, my $buf, 0 |
18 |
or die "aio_read: $!"; |
19 |
aio_close $fh; |
20 |
|
21 |
=head1 DESCRIPTION |
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. They also all have a prototype of C<@> currently, but that might |
30 |
change. Everything else, including C<$!> and perls stat cache, are set as |
31 |
expected after these functions return. |
32 |
|
33 |
You can mix calls to C<IO::AIO> functions with calls to this module. You |
34 |
also can, but do not need to, call C<IO::AIO::poll_cb>, as this module |
35 |
automatically installs an event watcher for the C<IO::AIO> file |
36 |
descriptor. It uses the L<AnyEvent|AnyEvent> module for this, so please |
37 |
refer to its documentation on how it selects an appropriate Event module. |
38 |
|
39 |
For your convienience, here are the changed function signatures, for |
40 |
documentation of these functions please have a look at L<IO::AIO|the |
41 |
IO::AIO manual>. |
42 |
|
43 |
=over 4 |
44 |
|
45 |
=cut |
46 |
|
47 |
package Coro::AIO; |
48 |
|
49 |
use strict; |
50 |
|
51 |
use Coro (); |
52 |
use AnyEvent; |
53 |
use IO::AIO (); |
54 |
|
55 |
use base Exporter::; |
56 |
|
57 |
our $FH; open $FH, "<&=" . IO::AIO::poll_fileno; |
58 |
our $WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => sub { IO::AIO::poll_cb }); |
59 |
|
60 |
our @EXPORT; |
61 |
|
62 |
sub wrap($) { |
63 |
my ($sub) = @_; |
64 |
|
65 |
no strict 'refs'; |
66 |
|
67 |
push @EXPORT, $sub; |
68 |
|
69 |
my $iosub = "IO::AIO::$sub"; |
70 |
|
71 |
*$sub = sub { |
72 |
my $current = $Coro::current; |
73 |
my $stat; |
74 |
my @res; |
75 |
|
76 |
$iosub->(@_, sub { |
77 |
$stat = Coro::_aio_get_state; |
78 |
@res = @_; |
79 |
$current->ready; |
80 |
undef $current; |
81 |
}); |
82 |
|
83 |
Coro::schedule while $current; |
84 |
|
85 |
Coro::_aio_set_state $stat; |
86 |
wantarray ? @res : $res[0] |
87 |
}; |
88 |
} |
89 |
|
90 |
wrap $_ for qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
91 |
aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir |
92 |
aio_symlink aio_fsync aio_fdatasync aio_readahead); |
93 |
|
94 |
=item $fh = aio_open $pathname, $flags, $mode |
95 |
|
96 |
=item $status = aio_close $fh |
97 |
|
98 |
=item $retval = aio_read $fh,$offset,$length, $data,$dataoffset |
99 |
|
100 |
=item $retval = aio_write $fh,$offset,$length, $data,$dataoffset |
101 |
|
102 |
=item $retval = aio_sendfile $out_fh, $in_fh, $in_offset, $length |
103 |
|
104 |
=item $retval = aio_readahead $fh,$offset,$length |
105 |
|
106 |
=item $status = aio_stat $fh_or_path |
107 |
|
108 |
=item $status = aio_lstat $fh |
109 |
|
110 |
=item $status = aio_unlink $pathname |
111 |
|
112 |
=item $status = aio_rmdir $pathname |
113 |
|
114 |
=item $entries = aio_readdir $pathname $callback->($entries) |
115 |
|
116 |
=item ($dirs, $nondirs) = aio_scandir $path, $maxreq |
117 |
|
118 |
=item $status = aio_fsync $fh |
119 |
|
120 |
=item $status = aio_fdatasync $fh |
121 |
|
122 |
=back |
123 |
|
124 |
=head1 SEE ALSO |
125 |
|
126 |
L<Coro::Socket> and L<Coro::Handle> for non-blocking file operation. |
127 |
|
128 |
=head1 AUTHOR |
129 |
|
130 |
Marc Lehmann <schmorp@schmorp.de> |
131 |
http://home.schmorp.de/ |
132 |
|
133 |
=cut |
134 |
|
135 |
1 |