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.117 by root, Sat Oct 6 14:05:19 2007 UTC

183 183
184=cut 184=cut
185 185
186package IO::AIO; 186package IO::AIO;
187 187
188use Carp ();
189
188no warnings; 190no warnings;
189use strict 'vars'; 191use strict 'vars';
190 192
191use base 'Exporter'; 193use base 'Exporter';
192 194
193BEGIN { 195BEGIN {
194 our $VERSION = '2.4'; 196 our $VERSION = '2.51';
195 197
196 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat 198 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 199 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 200 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 201 aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir
311 313
312 314
313=item aio_close $fh, $callback->($status) 315=item aio_close $fh, $callback->($status)
314 316
315Asynchronously close a file and call the callback with the result 317Asynchronously close a file and call the callback with the result
316code. I<WARNING:> although accepted, you should not pass in a perl 318code.
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 319
321This is supposed to be a bug in the API, so that might change. It's 320Unfortunately, you can't do this to perl. Perl I<insists> very strongly on
322therefore best to avoid this function. 321closing the file descriptor associated with the filehandle itself. Here is
322what aio_close will try:
323
324 1. dup()licate the fd
325 2. asynchronously close() the duplicated fd
326 3. dup()licate the fd once more
327 4. let perl close() the filehandle
328 5. asynchronously close the duplicated fd
329
330The idea is that the first close() flushes stuff to disk that closing an
331fd will flush, so when perl closes the fd, nothing much will need to be
332flushed. The second async. close() will then flush stuff to disk that
333closing the last fd to the file will flush.
334
335Just FYI, SuSv3 has this to say on close:
336
337 All outstanding record locks owned by the process on the file
338 associated with the file descriptor shall be removed.
339
340 If fildes refers to a socket, close() shall cause the socket to be
341 destroyed. ... close() shall block for up to the current linger
342 interval until all data is transmitted.
343 [this actually sounds like a specification bug, but who knows]
344
345And at least Linux additionally actually flushes stuff on every close,
346even when the file itself is still open.
347
348Sounds enourmously inefficient and complicated? Yes... please show me how
349to nuke perl's fd out of existence...
350
351=cut
352
353sub aio_close($;$) {
354 aio_block {
355 my ($fh, $cb) = @_;
356
357 my $pri = aioreq_pri;
358 my $grp = aio_group $cb;
359
360 my $fd = fileno $fh;
361
362 defined $fd or Carp::croak "aio_close called with fd-less filehandle";
363
364 # if the dups fail we will simply get EBADF
365 my $fd2 = _dup $fd;
366 aioreq_pri $pri;
367 add $grp _aio_close $fd2, sub {
368 my $fd2 = _dup $fd;
369 close $fh;
370 aioreq_pri $pri;
371 add $grp _aio_close $fd2, sub {
372 $grp->result ($_[0]);
373 };
374 };
375
376 $grp
377 }
378}
323 379
324 380
325=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 381=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
326 382
327=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 383=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
329Reads or writes C<$length> bytes from the specified C<$fh> and C<$offset> 385Reads 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 386into 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 387callback without the actual number of bytes read (or -1 on error, just
332like the syscall). 388like the syscall).
333 389
334If C<$offset> is undefined, then the current file offset will be used (and 390If C<$offset> is undefined, then the current file descriptor offset will
335updated), otherwise the file offset will not be changed by these calls. 391be used (and updated), otherwise the file descriptor offset will not be
392changed by these calls.
336 393
337If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>. 394If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>.
338 395
339If C<$dataoffset> is less than zero, it will be counted from the end of 396If C<$dataoffset> is less than zero, it will be counted from the end of
340C<$data>. 397C<$data>.
341 398
342The C<$data> scalar I<MUST NOT> be modified in any way while the request 399The 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 1263This 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 1264blocks, and a bad way to reduce concurrency because it is inexact: Better
1208use an C<aio_group> together with a feed callback. 1265use an C<aio_group> together with a feed callback.
1209 1266
1210Sets the maximum number of outstanding requests to C<$nreqs>. If you 1267Sets 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 1268do 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>) 1269C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>)
1213function will block until the limit is no longer exceeded. 1270function will block until the limit is no longer exceeded.
1214 1271
1215The default value is very large, so there is no practical limit on the 1272The default value is very large, so there is no practical limit on the
1216number of outstanding requests. 1273number of outstanding requests.
1246but not yet processed by poll_cb). 1303but not yet processed by poll_cb).
1247 1304
1248=back 1305=back
1249 1306
1250=cut 1307=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 1308
1268min_parallel 8; 1309min_parallel 8;
1269 1310
1270END { flush } 1311END { flush }
1271 1312
1295bytes of memory. In addition, stat requests need a stat buffer (possibly 1336bytes of memory. In addition, stat requests need a stat buffer (possibly
1296a few hundred bytes), readdir requires a result buffer and so on. Perl 1337a 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 1338scalars and other data passed into aio requests will also be locked and
1298will consume memory till the request has entered the done state. 1339will consume memory till the request has entered the done state.
1299 1340
1300This is now awfully much, so queuing lots of requests is not usually a 1341This is not awfully much, so queuing lots of requests is not usually a
1301problem. 1342problem.
1302 1343
1303Per-thread usage: 1344Per-thread usage:
1304 1345
1305In the execution phase, some aio requests require more memory for 1346In the execution phase, some aio requests require more memory for

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines