--- IO-AIO/AIO.pm 2006/10/22 00:19:05 1.54 +++ IO-AIO/AIO.pm 2006/10/22 00:49:29 1.55 @@ -73,7 +73,7 @@ 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 @@ -102,9 +102,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 @@ -201,15 +200,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]) { @@ -218,12 +219,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); }; @@ -242,6 +243,8 @@ $cb->($_[0]); } }; + + $grp } =item aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) @@ -394,21 +397,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; @@ -440,7 +445,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 @@ -459,7 +464,7 @@ &$schedcb; } else { # need to check for real directory - aio_lstat "$path/$entry", sub { + add $grp aio_lstat "$path/$entry", sub { $nreq--; if (-d _) { @@ -482,6 +487,8 @@ }; }; }; + + $grp } =item aio_fsync $fh, $callback->($status) @@ -499,6 +506,26 @@ =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 aio_sleep $fractional_seconds, $callback->() *NOT EXPORTED* Mainly used for debugging and benchmarking, this aio request puts one of @@ -526,6 +553,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.