ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.pm
(Generate patch)

Comparing IO-AIO/AIO.pm (file contents):
Revision 1.4 by root, Sun Jul 10 20:57:00 2005 UTC vs.
Revision 1.5 by root, Sun Jul 10 21:04:24 2005 UTC

21remaining functionality would have to be implemented using threads anyway. 21remaining functionality would have to be implemented using threads anyway.
22 22
23Although the module will work with in the presence of other threads, it is 23Although the module will work with in the presence of other threads, it is
24currently not reentrant, so use appropriate locking yourself. 24currently not reentrant, so use appropriate locking yourself.
25 25
26=head2 API NOTES 26=cut
27
28package IO::AIO;
29
30use base 'Exporter';
31
32use Fcntl ();
33
34BEGIN {
35 $VERSION = 0.2;
36
37 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink
38 aio_fsync aio_fdatasync aio_readahead);
39 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel max_outstanding nreqs);
40
41 require XSLoader;
42 XSLoader::load IO::AIO, $VERSION;
43}
44
45=head1 FUNCTIONS
46
47=head2 AIO FUNCTIONS
27 48
28All the C<aio_*> calls are more or less thin wrappers around the syscall 49All the C<aio_*> calls are more or less thin wrappers around the syscall
29with the same name (sans C<aio_>). The arguments are similar or identical, 50with the same name (sans C<aio_>). The arguments are similar or identical,
30and they all accept an additional C<$callback> argument which must be 51and they all accept an additional C<$callback> argument which must be
31a code reference. This code reference will get called with the syscall 52a code reference. This code reference will get called with the syscall
39is that at the time the request is being executed, the current working 60is that at the time the request is being executed, the current working
40directory could have changed. Alternatively, you can make sure that you 61directory could have changed. Alternatively, you can make sure that you
41never change the current working directory. 62never change the current working directory.
42 63
43=over 4 64=over 4
44
45=cut
46
47package IO::AIO;
48
49use base 'Exporter';
50
51use Fcntl ();
52
53BEGIN {
54 $VERSION = 0.2;
55
56 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink
57 aio_fsync aio_fdatasync aio_readahead);
58 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel max_outstanding nreqs);
59
60 require XSLoader;
61 XSLoader::load IO::AIO, $VERSION;
62}
63
64=item IO::AIO::min_parallel $nthreads
65
66Set the minimum number of AIO threads to C<$nthreads>. The default is
67C<1>, which means a single asynchronous operation can be done at one time
68(the number of outstanding operations, however, is unlimited).
69
70It is recommended to keep the number of threads low, as some Linux
71kernel versions will scale negatively with the number of threads (higher
72parallelity => MUCH higher latency). With current Linux 2.6 versions, 4-32
73threads should be fine.
74
75Under normal circumstances you don't need to call this function, as this
76module automatically starts some threads (the exact number might change,
77and is currently 4).
78
79=item IO::AIO::max_parallel $nthreads
80
81Sets the maximum number of AIO threads to C<$nthreads>. If more than
82the specified number of threads are currently running, kill them. This
83function blocks until the limit is reached.
84
85This module automatically runs C<max_parallel 0> at program end, to ensure
86that all threads are killed and that there are no outstanding requests.
87
88Under normal circumstances you don't need to call this function.
89
90=item $oldnreqs = IO::AIO::max_outstanding $nreqs
91
92Sets the maximum number of outstanding requests to C<$nreqs>. If you
93try to queue up more than this number of requests, the caller will block until
94some requests have been handled.
95
96The default is very large, so normally there is no practical limit. If you
97queue up many requests in a loop it it often improves speed if you set
98this to a relatively low number, such as C<100>.
99
100Under normal circumstances you don't need to call this function.
101
102=item $fileno = IO::AIO::poll_fileno
103
104Return the I<request result pipe filehandle>. This filehandle must be
105polled for reading by some mechanism outside this module (e.g. Event
106or select, see below). If the pipe becomes readable you have to call
107C<poll_cb> to check the results.
108
109See C<poll_cb> for an example.
110
111=item IO::AIO::poll_cb
112
113Process all outstanding events on the result pipe. You have to call this
114regularly. Returns the number of events processed. Returns immediately
115when no events are outstanding.
116
117You can use Event to multiplex, e.g.:
118
119 Event->io (fd => IO::AIO::poll_fileno,
120 poll => 'r', async => 1,
121 cb => \&IO::AIO::poll_cb);
122
123=item IO::AIO::poll_wait
124
125Wait till the result filehandle becomes ready for reading (simply does a
126select on the filehandle. This is useful if you want to synchronously wait
127for some requests to finish).
128
129See C<nreqs> for an example.
130
131=item IO::AIO::nreqs
132
133Returns the number of requests currently outstanding.
134
135Example: wait till there are no outstanding requests anymore:
136
137 IO::AIO::poll_wait, IO::AIO::poll_cb
138 while IO::AIO::nreqs;
139 65
140=item aio_open $pathname, $flags, $mode, $callback 66=item aio_open $pathname, $flags, $mode, $callback
141 67
142Asynchronously open or create a file and call the callback with a newly 68Asynchronously open or create a file and call the callback with a newly
143created filehandle for the file. 69created filehandle for the file.
234=item aio_fdatasync $fh, $callback 160=item aio_fdatasync $fh, $callback
235 161
236Asynchronously call fdatasync on the given filehandle and call the 162Asynchronously call fdatasync on the given filehandle and call the
237callback with the fdatasync result code. 163callback with the fdatasync result code.
238 164
165=back
166
167=head2 SUPPORT FUNCTIONS
168
169=over 4
170
171=item $fileno = IO::AIO::poll_fileno
172
173Return the I<request result pipe filehandle>. This filehandle must be
174polled for reading by some mechanism outside this module (e.g. Event
175or select, see below). If the pipe becomes readable you have to call
176C<poll_cb> to check the results.
177
178See C<poll_cb> for an example.
179
180=item IO::AIO::poll_cb
181
182Process all outstanding events on the result pipe. You have to call this
183regularly. Returns the number of events processed. Returns immediately
184when no events are outstanding.
185
186You can use Event to multiplex, e.g.:
187
188 Event->io (fd => IO::AIO::poll_fileno,
189 poll => 'r', async => 1,
190 cb => \&IO::AIO::poll_cb);
191
192=item IO::AIO::poll_wait
193
194Wait till the result filehandle becomes ready for reading (simply does a
195select on the filehandle. This is useful if you want to synchronously wait
196for some requests to finish).
197
198See C<nreqs> for an example.
199
200=item IO::AIO::nreqs
201
202Returns the number of requests currently outstanding.
203
204Example: wait till there are no outstanding requests anymore:
205
206 IO::AIO::poll_wait, IO::AIO::poll_cb
207 while IO::AIO::nreqs;
208
209=item IO::AIO::min_parallel $nthreads
210
211Set the minimum number of AIO threads to C<$nthreads>. The default is
212C<1>, which means a single asynchronous operation can be done at one time
213(the number of outstanding operations, however, is unlimited).
214
215It is recommended to keep the number of threads low, as some Linux
216kernel versions will scale negatively with the number of threads (higher
217parallelity => MUCH higher latency). With current Linux 2.6 versions, 4-32
218threads should be fine.
219
220Under normal circumstances you don't need to call this function, as this
221module automatically starts some threads (the exact number might change,
222and is currently 4).
223
224=item IO::AIO::max_parallel $nthreads
225
226Sets the maximum number of AIO threads to C<$nthreads>. If more than
227the specified number of threads are currently running, kill them. This
228function blocks until the limit is reached.
229
230This module automatically runs C<max_parallel 0> at program end, to ensure
231that all threads are killed and that there are no outstanding requests.
232
233Under normal circumstances you don't need to call this function.
234
235=item $oldnreqs = IO::AIO::max_outstanding $nreqs
236
237Sets the maximum number of outstanding requests to C<$nreqs>. If you
238try to queue up more than this number of requests, the caller will block until
239some requests have been handled.
240
241The default is very large, so normally there is no practical limit. If you
242queue up many requests in a loop it it often improves speed if you set
243this to a relatively low number, such as C<100>.
244
245Under normal circumstances you don't need to call this function.
246
247=back
248
239=cut 249=cut
240 250
241# support function to convert a fd into a perl filehandle 251# support function to convert a fd into a perl filehandle
242sub _fd2fh { 252sub _fd2fh {
243 return undef if $_[0] < 0; 253 return undef if $_[0] < 0;
256 max_parallel 0; 266 max_parallel 0;
257} 267}
258 268
2591; 2691;
260 270
261=back
262
263=head1 BUGS
264
265 - could be optimized to use more semaphores instead of filehandles.
266
267=head1 SEE ALSO 271=head1 SEE ALSO
268 272
269L<Coro>, L<Linux::AIO>. 273L<Coro>, L<Linux::AIO>.
270 274
271=head1 AUTHOR 275=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines