1 | NAME |
1 | NAME |
2 | IO::AIO - Asynchronous Input/Output |
2 | IO::AIO - Asynchronous Input/Output |
3 | |
3 | |
4 | SYNOPSIS |
4 | SYNOPSIS |
5 | use IO::AIO; |
5 | use IO::AIO; |
|
|
6 | |
|
|
7 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
|
|
8 | my ($fh) = @_; |
|
|
9 | ... |
|
|
10 | }; |
|
|
11 | |
|
|
12 | aio_unlink "/tmp/file", sub { }; |
|
|
13 | |
|
|
14 | aio_read $fh, 30000, 1024, $buffer, 0, sub { |
|
|
15 | $_[0] > 0 or die "read error: $!"; |
|
|
16 | }; |
|
|
17 | |
|
|
18 | # Event |
|
|
19 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
20 | poll => 'r', |
|
|
21 | cb => \&IO::AIO::poll_cb); |
|
|
22 | |
|
|
23 | # Glib/Gtk2 |
|
|
24 | add_watch Glib::IO IO::AIO::poll_fileno, |
|
|
25 | in => sub { IO::AIO::poll_cb, 1 }; |
|
|
26 | |
|
|
27 | # Tk |
|
|
28 | Tk::Event::IO->fileevent (IO::AIO::poll_fileno, "", |
|
|
29 | readable => \&IO::AIO::poll_cb); |
|
|
30 | |
|
|
31 | # Danga::Socket |
|
|
32 | Danga::Socket->AddOtherFds (IO::AIO::poll_fileno => |
|
|
33 | \&IO::AIO::poll_cb); |
6 | |
34 | |
7 | DESCRIPTION |
35 | DESCRIPTION |
8 | This module implements asynchronous I/O using whatever means your |
36 | This module implements asynchronous I/O using whatever means your |
9 | operating system supports. |
37 | operating system supports. |
10 | |
38 | |
… | |
… | |
23 | |
51 | |
24 | FUNCTIONS |
52 | FUNCTIONS |
25 | AIO FUNCTIONS |
53 | AIO FUNCTIONS |
26 | All the "aio_*" calls are more or less thin wrappers around the syscall |
54 | All the "aio_*" calls are more or less thin wrappers around the syscall |
27 | with the same name (sans "aio_"). The arguments are similar or |
55 | with the same name (sans "aio_"). The arguments are similar or |
28 | identical, and they all accept an additional $callback argument which |
56 | identical, and they all accept an additional (and optional) $callback |
29 | must be a code reference. This code reference will get called with the |
57 | argument which must be a code reference. This code reference will get |
30 | syscall return code (e.g. most syscalls return -1 on error, unlike perl, |
58 | called with the syscall return code (e.g. most syscalls return -1 on |
31 | which usually delivers "false") as it's sole argument when the given |
59 | error, unlike perl, which usually delivers "false") as it's sole |
32 | syscall has been executed asynchronously. |
60 | argument when the given syscall has been executed asynchronously. |
33 | |
61 | |
34 | All functions that expect a filehandle will also accept a file |
62 | All functions that expect a filehandle will also accept a file |
35 | descriptor. |
63 | descriptor. |
36 | |
64 | |
37 | The filenames you pass to these routines *must* be absolute. The reason |
65 | The filenames you pass to these routines *must* be absolute. The reason |
… | |
… | |
72 | Reads or writes "length" bytes from the specified "fh" and "offset" |
100 | Reads or writes "length" bytes from the specified "fh" and "offset" |
73 | into the scalar given by "data" and offset "dataoffset" and calls |
101 | into the scalar given by "data" and offset "dataoffset" and calls |
74 | the callback without the actual number of bytes read (or -1 on |
102 | the callback without the actual number of bytes read (or -1 on |
75 | error, just like the syscall). |
103 | error, just like the syscall). |
76 | |
104 | |
77 | Example: Read 15 bytes at offset 7 into scalar $buffer, strating at |
105 | Example: Read 15 bytes at offset 7 into scalar $buffer, starting at |
78 | offset 0 within the scalar: |
106 | offset 0 within the scalar: |
79 | |
107 | |
80 | aio_read $fh, 7, 15, $buffer, 0, sub { |
108 | aio_read $fh, 7, 15, $buffer, 0, sub { |
81 | $_[0] >= 0 or die "read error: $!"; |
109 | $_[0] > 0 or die "read error: $!"; |
82 | print "read <$buffer>\n"; |
110 | print "read $_[0] bytes: <$buffer>\n"; |
83 | }; |
111 | }; |
84 | |
112 | |
85 | aio_readahead $fh,$offset,$length, $callback |
113 | aio_readahead $fh,$offset,$length, $callback |
86 | Asynchronously reads the specified byte range into the page cache, |
114 | Asynchronously reads the specified byte range into the page cache, |
87 | using the "readahead" syscall. If that syscall doesn't exist the |
115 | using the "readahead" syscall. If that syscall doesn't exist (likely |
88 | status will be -1 and $! is set to ENOSYS. |
116 | if your OS isn't Linux) the status will be -1 and $! is set to |
|
|
117 | ENOSYS. |
89 | |
118 | |
90 | readahead() populates the page cache with data from a file so that |
119 | readahead() populates the page cache with data from a file so that |
91 | subsequent reads from that file will not block on disk I/O. The |
120 | subsequent reads from that file will not block on disk I/O. The |
92 | $offset argument specifies the starting point from which data is to |
121 | $offset argument specifies the starting point from which data is to |
93 | be read and $length specifies the number of bytes to be read. I/O is |
122 | be read and $length specifies the number of bytes to be read. I/O is |
… | |
… | |
163 | Example: wait till there are no outstanding requests anymore: |
192 | Example: wait till there are no outstanding requests anymore: |
164 | |
193 | |
165 | IO::AIO::poll_wait, IO::AIO::poll_cb |
194 | IO::AIO::poll_wait, IO::AIO::poll_cb |
166 | while IO::AIO::nreqs; |
195 | while IO::AIO::nreqs; |
167 | |
196 | |
|
|
197 | IO::AIO::flush |
|
|
198 | Wait till all outstanding AIO requests have been handled. |
|
|
199 | |
|
|
200 | Strictly equivalent to: |
|
|
201 | |
|
|
202 | IO::AIO::poll_wait, IO::AIO::poll_cb |
|
|
203 | while IO::AIO::nreqs; |
|
|
204 | |
|
|
205 | IO::AIO::poll |
|
|
206 | Waits until some requests have been handled. |
|
|
207 | |
|
|
208 | Strictly equivalent to: |
|
|
209 | |
|
|
210 | IO::AIO::poll_wait, IO::AIO::poll_cb |
|
|
211 | if IO::AIO::nreqs; |
|
|
212 | |
168 | IO::AIO::min_parallel $nthreads |
213 | IO::AIO::min_parallel $nthreads |
169 | Set the minimum number of AIO threads to $nthreads. The default is |
214 | Set the minimum number of AIO threads to $nthreads. The default is |
170 | 1, which means a single asynchronous operation can be done at one |
215 | 1, which means a single asynchronous operation can be done at one |
171 | time (the number of outstanding operations, however, is unlimited). |
216 | time (the number of outstanding operations, however, is unlimited). |
172 | |
217 | |