--- IO-AIO/AIO.pm 2007/09/24 18:14:00 1.115 +++ IO-AIO/AIO.pm 2007/12/02 20:54:33 1.119 @@ -28,10 +28,13 @@ my $grp = aio_group sub { print "all stats done\n" }; add $grp aio_stat "..." for ...; - # AnyEvent integration + # AnyEvent integration (EV, Event, Glib, Tk, urxvt, pureperl...) open my $fh, "<&=" . IO::AIO::poll_fileno or die "$!"; my $w = AnyEvent->io (fh => $fh, poll => 'r', cb => sub { IO::AIO::poll_cb }); + # EV integration + my $w = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; + # Event integration Event->io (fd => IO::AIO::poll_fileno, poll => 'r', @@ -185,17 +188,19 @@ package IO::AIO; +use Carp (); + no warnings; use strict 'vars'; use base 'Exporter'; BEGIN { - our $VERSION = '2.41'; + our $VERSION = '2.6'; our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink - aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link + aio_readlink aio_sync aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir aio_chown aio_chmod aio_utime aio_truncate); our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block)); @@ -313,13 +318,67 @@ =item aio_close $fh, $callback->($status) Asynchronously close a file and call the callback with the result -code. I although accepted, you should not pass in a perl -filehandle here, as perl will likely close the file descriptor another -time when the filehandle is destroyed. Normally, you can safely call perls -C or just let filehandles go out of scope. +code. + +Unfortunately, you can't do this to perl. Perl I very strongly on +closing the file descriptor associated with the filehandle itself. Here is +what aio_close will try: + + 1. dup()licate the fd + 2. asynchronously close() the duplicated fd + 3. dup()licate the fd once more + 4. let perl close() the filehandle + 5. asynchronously close the duplicated fd + +The idea is that the first close() flushes stuff to disk that closing an +fd will flush, so when perl closes the fd, nothing much will need to be +flushed. The second async. close() will then flush stuff to disk that +closing the last fd to the file will flush. + +Just FYI, SuSv3 has this to say on close: + + All outstanding record locks owned by the process on the file + associated with the file descriptor shall be removed. + + If fildes refers to a socket, close() shall cause the socket to be + destroyed. ... close() shall block for up to the current linger + interval until all data is transmitted. + [this actually sounds like a specification bug, but who knows] + +And at least Linux additionally actually flushes stuff on every close, +even when the file itself is still open. + +Sounds enourmously inefficient and complicated? Yes... please show me how +to nuke perl's fd out of existence... + +=cut + +sub aio_close($;$) { + aio_block { + my ($fh, $cb) = @_; + + my $pri = aioreq_pri; + my $grp = aio_group $cb; + + my $fd = fileno $fh; + + defined $fd or Carp::croak "aio_close called with fd-less filehandle"; -This is supposed to be a bug in the API, so that might change. It's -therefore best to avoid this function. + # if the dups fail we will simply get EBADF + my $fd2 = _dup $fd; + aioreq_pri $pri; + add $grp _aio_close $fd2, sub { + my $fd2 = _dup $fd; + close $fh; + aioreq_pri $pri; + add $grp _aio_close $fd2, sub { + $grp->result ($_[0]); + }; + }; + + $grp + } +} =item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) @@ -836,6 +895,10 @@ } } +=item aio_sync $callback->($status) + +Asynchronously call sync and call the callback when finished. + =item aio_fsync $fh, $callback->($status) Asynchronously call fsync on the given filehandle and call the callback @@ -1250,22 +1313,6 @@ =cut -# support function to convert a fd into a perl filehandle -sub _fd2fh { - return undef if $_[0] < 0; - - # try to generate nice filehandles - my $sym = "IO::AIO::fd#$_[0]"; - local *$sym; - - open *$sym, "+<&=$_[0]" # usually works under any unix - or open *$sym, "<&=$_[0]" # cygwin needs this - or open *$sym, ">&=$_[0]" # or this - or return undef; - - *$sym -} - min_parallel 8; END { flush }