… | |
… | |
3 | IO::AIO - Asynchronous Input/Output |
3 | IO::AIO - Asynchronous Input/Output |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use IO::AIO; |
7 | use IO::AIO; |
|
|
8 | |
|
|
9 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
|
|
10 | my ($fh) = @_; |
|
|
11 | ... |
|
|
12 | }; |
|
|
13 | |
|
|
14 | aio_unlink "/tmp/file", sub { }; |
|
|
15 | |
|
|
16 | aio_read $fh, 30000, 1024, $buffer, 0, sub { |
|
|
17 | $_[0] > 0 or die "read error: $!"; |
|
|
18 | }; |
|
|
19 | |
|
|
20 | # Event |
|
|
21 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
22 | poll => 'r', |
|
|
23 | cb => \&IO::AIO::poll_cb); |
|
|
24 | |
|
|
25 | # Glib/Gtk2 |
|
|
26 | add_watch Glib::IO IO::AIO::poll_fileno, |
|
|
27 | \&IO::AIO::poll_cb; |
|
|
28 | |
|
|
29 | # Tk |
|
|
30 | Tk::Event::IO->fileevent (IO::AIO::poll_fileno, "", |
|
|
31 | readable => \&IO::AIO::poll_cb); |
|
|
32 | |
|
|
33 | # Danga::Socket |
|
|
34 | Danga::Socket->AddOtherFds (IO::AIO::poll_fileno => |
|
|
35 | \&IO::AIO::poll_cb); |
|
|
36 | |
8 | |
37 | |
9 | =head1 DESCRIPTION |
38 | =head1 DESCRIPTION |
10 | |
39 | |
11 | This module implements asynchronous I/O using whatever means your |
40 | This module implements asynchronous I/O using whatever means your |
12 | operating system supports. |
41 | operating system supports. |
… | |
… | |
30 | use base 'Exporter'; |
59 | use base 'Exporter'; |
31 | |
60 | |
32 | use Fcntl (); |
61 | use Fcntl (); |
33 | |
62 | |
34 | BEGIN { |
63 | BEGIN { |
35 | $VERSION = 0.2; |
64 | $VERSION = 0.3; |
36 | |
65 | |
37 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink |
66 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink |
38 | aio_fsync aio_fdatasync aio_readahead); |
67 | aio_fsync aio_fdatasync aio_readahead); |
39 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel max_outstanding nreqs); |
68 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel max_outstanding nreqs); |
40 | |
69 | |
… | |
… | |
46 | |
75 | |
47 | =head2 AIO FUNCTIONS |
76 | =head2 AIO FUNCTIONS |
48 | |
77 | |
49 | All the C<aio_*> calls are more or less thin wrappers around the syscall |
78 | All the C<aio_*> calls are more or less thin wrappers around the syscall |
50 | with the same name (sans C<aio_>). The arguments are similar or identical, |
79 | with the same name (sans C<aio_>). The arguments are similar or identical, |
51 | and they all accept an additional C<$callback> argument which must be |
80 | and they all accept an additional (and optional) C<$callback> argument |
52 | a code reference. This code reference will get called with the syscall |
81 | which must be a code reference. This code reference will get called with |
53 | return code (e.g. most syscalls return C<-1> on error, unlike perl, which |
82 | the syscall return code (e.g. most syscalls return C<-1> on error, unlike |
54 | usually delivers "false") as it's sole argument when the given syscall has |
83 | perl, which usually delivers "false") as it's sole argument when the given |
55 | been executed asynchronously. |
84 | syscall has been executed asynchronously. |
56 | |
85 | |
57 | All functions that expect a filehandle will also accept a file descriptor. |
86 | All functions that expect a filehandle will also accept a file descriptor. |
58 | |
87 | |
59 | The filenames you pass to these routines I<must> be absolute. The reason |
88 | The filenames you pass to these routines I<must> be absolute. The reason |
60 | is that at the time the request is being executed, the current working |
89 | is that at the time the request is being executed, the current working |
… | |
… | |
104 | |
133 | |
105 | Example: Read 15 bytes at offset 7 into scalar C<$buffer>, strating at |
134 | Example: Read 15 bytes at offset 7 into scalar C<$buffer>, strating at |
106 | offset C<0> within the scalar: |
135 | offset C<0> within the scalar: |
107 | |
136 | |
108 | aio_read $fh, 7, 15, $buffer, 0, sub { |
137 | aio_read $fh, 7, 15, $buffer, 0, sub { |
109 | $_[0] >= 0 or die "read error: $!"; |
138 | $_[0] > 0 or die "read error: $!"; |
110 | print "read <$buffer>\n"; |
139 | print "read $_[0] bytes: <$buffer>\n"; |
111 | }; |
140 | }; |
112 | |
141 | |
113 | =item aio_readahead $fh,$offset,$length, $callback |
142 | =item aio_readahead $fh,$offset,$length, $callback |
114 | |
143 | |
115 | Asynchronously reads the specified byte range into the page cache, using |
144 | Asynchronously reads the specified byte range into the page cache, using |
… | |
… | |
204 | Example: wait till there are no outstanding requests anymore: |
233 | Example: wait till there are no outstanding requests anymore: |
205 | |
234 | |
206 | IO::AIO::poll_wait, IO::AIO::poll_cb |
235 | IO::AIO::poll_wait, IO::AIO::poll_cb |
207 | while IO::AIO::nreqs; |
236 | while IO::AIO::nreqs; |
208 | |
237 | |
|
|
238 | =item IO::AIO::flush |
|
|
239 | |
|
|
240 | Wait till all outstanding AIO requests have been handled. |
|
|
241 | |
|
|
242 | Strictly equivalent to: |
|
|
243 | |
|
|
244 | IO::AIO::poll_wait, IO::AIO::poll_cb |
|
|
245 | while IO::AIO::nreqs; |
|
|
246 | |
|
|
247 | =item IO::AIO::poll |
|
|
248 | |
|
|
249 | Waits until some requests have been handled. |
|
|
250 | |
|
|
251 | Strictly equivalent to: |
|
|
252 | |
|
|
253 | IO::AIO::poll_wait, IO::AIO::poll_cb |
|
|
254 | if IO::AIO::nreqs; |
|
|
255 | |
209 | =item IO::AIO::min_parallel $nthreads |
256 | =item IO::AIO::min_parallel $nthreads |
210 | |
257 | |
211 | Set the minimum number of AIO threads to C<$nthreads>. The default is |
258 | Set the minimum number of AIO threads to C<$nthreads>. The default is |
212 | C<1>, which means a single asynchronous operation can be done at one time |
259 | C<1>, which means a single asynchronous operation can be done at one time |
213 | (the number of outstanding operations, however, is unlimited). |
260 | (the number of outstanding operations, however, is unlimited). |