--- IO-AIO/AIO.pm 2006/10/21 23:20:29 1.53 +++ IO-AIO/AIO.pm 2006/10/22 00:53:47 1.56 @@ -17,33 +17,36 @@ $_[0] > 0 or die "read error: $!"; }; - use IO::AIO 2; # version has aio objects + # version 2+ has request and group objects + use IO::AIO 2; my $req = aio_unlink "/tmp/file", sub { }; $req->cancel; # cancel request if still in queue - # AnyEvent + my $grp = aio_group sub { print "all stats done\n" }; + add $grp aio_stat "..." for ...; + + # AnyEvent integration open my $fh, "<&=" . IO::AIO::poll_fileno or die "$!"; my $w = AnyEvent->io (fh => $fh, poll => 'r', cb => sub { IO::AIO::poll_cb }); - # Event + # Event integration Event->io (fd => IO::AIO::poll_fileno, poll => 'r', cb => \&IO::AIO::poll_cb); - # Glib/Gtk2 + # Glib/Gtk2 integration add_watch Glib::IO IO::AIO::poll_fileno, in => sub { IO::AIO::poll_cb; 1 }; - # Tk + # Tk integration Tk::Event::IO->fileevent (IO::AIO::poll_fileno, "", readable => \&IO::AIO::poll_cb); - # Danga::Socket + # Danga::Socket integration Danga::Socket->AddOtherFds (IO::AIO::poll_fileno => \&IO::AIO::poll_cb); - =head1 DESCRIPTION This module implements asynchronous I/O using whatever means your @@ -73,13 +76,16 @@ use base 'Exporter'; BEGIN { - our $VERSION = '1.99'; + our $VERSION = '2.0'; our @EXPORT = 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_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move); + aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move + aio_group); our @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel max_outstanding nreqs); + @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; + require XSLoader; XSLoader::load ("IO::AIO", $VERSION); } @@ -99,9 +105,8 @@ All functions expecting a filehandle keep a copy of the filehandle internally until the request has finished. -All non-composite requests (requests that are not broken down into -multiple requests) return objects of type L that allow -further manipulation of running requests. +All requests return objects of type L that allow further +manipulation of those requests while they are in-flight. The pathnames you pass to these routines I be absolute and encoded in byte form. The reason for the former is that at the time the @@ -198,15 +203,17 @@ sub aio_move($$$) { my ($src, $dst, $cb) = @_; - aio_rename $src, $dst, sub { + my $grp = aio_group; + + add $grp aio_rename $src, $dst, sub { if ($_[0] && $! == EXDEV) { - aio_open $src, O_RDONLY, 0, sub { + add $grp aio_open $src, O_RDONLY, 0, sub { if (my $src_fh = $_[0]) { my @stat = stat $src_fh; - aio_open $dst, O_WRONLY, 0200, sub { + add $grp aio_open $dst, O_WRONLY, 0200, sub { if (my $dst_fh = $_[0]) { - aio_sendfile $dst_fh, $src_fh, 0, $stat[7], sub { + add $grp aio_sendfile $dst_fh, $src_fh, 0, $stat[7], sub { close $src_fh; if ($_[0] == $stat[7]) { @@ -215,12 +222,12 @@ chown $stat[4], $stat[5], $dst_fh; close $dst_fh; - aio_unlink $src, sub { + add $grp aio_unlink $src, sub { $cb->($_[0]); }; } else { my $errno = $!; - aio_unlink $dst, sub { + add $grp aio_unlink $dst, sub { $! = $errno; $cb->(-1); }; @@ -239,6 +246,8 @@ $cb->($_[0]); } }; + + $grp } =item aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) @@ -391,21 +400,23 @@ sub aio_scandir($$$) { my ($path, $maxreq, $cb) = @_; + my $grp = aio_group; + $maxreq = 8 if $maxreq <= 0; # stat once - aio_stat $path, sub { + add $grp aio_stat $path, sub { return $cb->() if $_[0]; my $now = time; my $hash1 = join ":", (stat _)[0,1,3,7,9]; # read the directory entries - aio_readdir $path, sub { + add $grp aio_readdir $path, sub { my $entries = shift or return $cb->(); # stat the dir another time - aio_stat $path, sub { + add $grp aio_stat $path, sub { my $hash2 = join ":", (stat _)[0,1,3,7,9]; my $ndirs; @@ -437,7 +448,7 @@ if ($nreq < $maxreq) { my $ent = pop @$entries; $nreq++; - aio_stat "$path/$ent/.", sub { $statcb->($_[0], $ent) }; + add $grp aio_stat "$path/$ent/.", sub { $statcb->($_[0], $ent) }; } } elsif (!$nreq) { # finished @@ -456,7 +467,7 @@ &$schedcb; } else { # need to check for real directory - aio_lstat "$path/$entry", sub { + add $grp aio_lstat "$path/$entry", sub { $nreq--; if (-d _) { @@ -479,6 +490,8 @@ }; }; }; + + $grp } =item aio_fsync $fh, $callback->($status) @@ -494,6 +507,38 @@ If this call isn't available because your OS lacks it or it couldn't be detected, it will be emulated by calling C instead. +=item aio_group $callback->() + +[EXPERIMENTAL] + +This is a very special aio request: Instead of doing something, it is a +container for other aio requests, which is useful if you want to bundle +many requests into a single, composite, request. + +Returns an object of class L. See its documentation below +for more info. + +Example: + + my $grp = aio_group sub { + print "all stats done\n"; + }; + + add $grp + (aio_stat ...), + (aio_stat ...), + ...; + +=item IO::AIO::aio_sleep $fractional_seconds, $callback->() *NOT EXPORTED* + +Mainly used for debugging and benchmarking, this aio request puts one of +the request workers to sleep for the given time. + +While it is theoretically handy to have simple I/O scheduling requests +like sleep and file handle readable/writable, the overhead this creates +is immense, so do not use this function except to put your application +under artificial I/O pressure. + =back =head2 IO::AIO::REQ CLASS @@ -516,6 +561,60 @@ Cancels the request, if possible. Has the effect of skipping execution when entering the B state and skipping calling the callback when entering the the B state, but will leave the request otherwise +untouched. That means that requests that currently execute will not be +stopped and resources held by the request will not be freed prematurely. + +=back + +=head2 IO::AIO::GRP CLASS + +This class is a subclass of L, so all its methods apply to +objects of this class, too. + +A IO::AIO::GRP object is a special request that can contain multiple other +aio requests. + +You create one by calling the C constructing function with a +callback that will be called when all contained requests have entered the +C state: + + my $grp = aio_group sub { + print "all requests are done\n"; + }; + +You add requests by calling the C method with one or more +C objects: + + $grp->add (aio_unlink "..."); + + add $grp aio_stat "...", sub { ... }; + +This makes it very easy to create composite requests (see the source of +C for an application) that work and feel like simple requests. + +The IO::AIO::GRP objects will be cleaned up during calls to +C, just like any other request. + +They can be canceled like any other request. Canceling will cancel not +just the request itself, but also all requests it contains. + +They can also can also be added to other IO::AIO::GRP objects. + +Their lifetime, simplified, looks like this: when they are empty, they +will finish very quickly. If they contain only requests that are in the +C state, they will also finish. Otherwise they will continue to +exist. + +=over 4 + +=item $grp->add (...) + +=item add $grp ... + +Add one or more +Cancels the request, if possible. Has the effect of skipping execution +when entering the B state and skipping calling the callback when +entering the the B state, but will leave the request otherwise untouched. That means that requests that currently execute will not be stopped and resources held by the request will not be freed prematurely.