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.110 by root, Sun Jul 8 09:09:34 2007 UTC vs.
Revision 1.116 by root, Wed Oct 3 21:27:51 2007 UTC

189use strict 'vars'; 189use strict 'vars';
190 190
191use base 'Exporter'; 191use base 'Exporter';
192 192
193BEGIN { 193BEGIN {
194 our $VERSION = '2.4'; 194 our $VERSION = '2.5';
195 195
196 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat 196 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat
197 aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink 197 aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink
198 aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link 198 aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link
199 aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir 199 aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir
311 311
312 312
313=item aio_close $fh, $callback->($status) 313=item aio_close $fh, $callback->($status)
314 314
315Asynchronously close a file and call the callback with the result 315Asynchronously close a file and call the callback with the result
316code. I<WARNING:> although accepted, you should not pass in a perl 316code.
317filehandle here, as perl will likely close the file descriptor another
318time when the filehandle is destroyed. Normally, you can safely call perls
319C<close> or just let filehandles go out of scope.
320 317
321This is supposed to be a bug in the API, so that might change. It's 318Unlike the other functions operating on files, this function uses the
322therefore best to avoid this function. 319PerlIO layer to close the filehandle. The reason is that the PerlIO API
320insists on closing the underlying fd itself, no matter what, and doesn't
321allow modifications to the fd. Unfortunately, it is not clear that you can
322call PerlIO from different threads (actually, its quite clear that this
323won't work in some cases), so while it likely works perfectly with simple
324file handles (such as the ones created by C<aio_open>) it might fail in
325interesting ways for others.
326
327Having said that, aio_close tries to clean up the filehandle as much as
328possible before handing it to an io thread, and generally does work.
323 329
324 330
325=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 331=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
326 332
327=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 333=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
329Reads or writes C<$length> bytes from the specified C<$fh> and C<$offset> 335Reads or writes C<$length> bytes from the specified C<$fh> and C<$offset>
330into the scalar given by C<$data> and offset C<$dataoffset> and calls the 336into the scalar given by C<$data> and offset C<$dataoffset> and calls the
331callback without the actual number of bytes read (or -1 on error, just 337callback without the actual number of bytes read (or -1 on error, just
332like the syscall). 338like the syscall).
333 339
334If C<$offset> is undefined, then the current file offset will be used (and 340If C<$offset> is undefined, then the current file descriptor offset will
335updated), otherwise the file offset will not be changed by these calls. 341be used (and updated), otherwise the file descriptor offset will not be
342changed by these calls.
336 343
337If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>. 344If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>.
338 345
339If C<$dataoffset> is less than zero, it will be counted from the end of 346If C<$dataoffset> is less than zero, it will be counted from the end of
340C<$data>. 347C<$data>.
341 348
342The C<$data> scalar I<MUST NOT> be modified in any way while the request 349The C<$data> scalar I<MUST NOT> be modified in any way while the request
1206This is a very bad function to use in interactive programs because it 1213This is a very bad function to use in interactive programs because it
1207blocks, and a bad way to reduce concurrency because it is inexact: Better 1214blocks, and a bad way to reduce concurrency because it is inexact: Better
1208use an C<aio_group> together with a feed callback. 1215use an C<aio_group> together with a feed callback.
1209 1216
1210Sets the maximum number of outstanding requests to C<$nreqs>. If you 1217Sets the maximum number of outstanding requests to C<$nreqs>. If you
1211to queue up more than this number of requests, the next call to the 1218do queue up more than this number of requests, the next call to the
1212C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) 1219C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>)
1213function will block until the limit is no longer exceeded. 1220function will block until the limit is no longer exceeded.
1214 1221
1215The default value is very large, so there is no practical limit on the 1222The default value is very large, so there is no practical limit on the
1216number of outstanding requests. 1223number of outstanding requests.
1246but not yet processed by poll_cb). 1253but not yet processed by poll_cb).
1247 1254
1248=back 1255=back
1249 1256
1250=cut 1257=cut
1251
1252# support function to convert a fd into a perl filehandle
1253sub _fd2fh {
1254 return undef if $_[0] < 0;
1255
1256 # try to generate nice filehandles
1257 my $sym = "IO::AIO::fd#$_[0]";
1258 local *$sym;
1259
1260 open *$sym, "+<&=$_[0]" # usually works under any unix
1261 or open *$sym, "<&=$_[0]" # cygwin needs this
1262 or open *$sym, ">&=$_[0]" # or this
1263 or return undef;
1264
1265 *$sym
1266}
1267 1258
1268min_parallel 8; 1259min_parallel 8;
1269 1260
1270END { flush } 1261END { flush }
1271 1262
1295bytes of memory. In addition, stat requests need a stat buffer (possibly 1286bytes of memory. In addition, stat requests need a stat buffer (possibly
1296a few hundred bytes), readdir requires a result buffer and so on. Perl 1287a few hundred bytes), readdir requires a result buffer and so on. Perl
1297scalars and other data passed into aio requests will also be locked and 1288scalars and other data passed into aio requests will also be locked and
1298will consume memory till the request has entered the done state. 1289will consume memory till the request has entered the done state.
1299 1290
1300This is now awfully much, so queuing lots of requests is not usually a 1291This is not awfully much, so queuing lots of requests is not usually a
1301problem. 1292problem.
1302 1293
1303Per-thread usage: 1294Per-thread usage:
1304 1295
1305In the execution phase, some aio requests require more memory for 1296In the execution phase, some aio requests require more memory for

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines