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.56 by root, Sun Oct 22 00:53:47 2006 UTC vs.
Revision 1.58 by root, Sun Oct 22 10:33:19 2006 UTC

182 print "read $_[0] bytes: <$buffer>\n"; 182 print "read $_[0] bytes: <$buffer>\n";
183 }; 183 };
184 184
185=item aio_move $srcpath, $dstpath, $callback->($status) 185=item aio_move $srcpath, $dstpath, $callback->($status)
186 186
187[EXPERIMENTAL due to internal aio_group use]
188
187Try to move the I<file> (directories not supported as either source or 189Try to move the I<file> (directories not supported as either source or
188destination) from C<$srcpath> to C<$dstpath> and call the callback with 190destination) from C<$srcpath> to C<$dstpath> and call the callback with
189the C<0> (error) or C<-1> ok. 191the C<0> (error) or C<-1> ok.
190 192
191This is a composite request that tries to rename(2) the file first. If 193This is a composite request that tries to rename(2) the file first. If
201=cut 203=cut
202 204
203sub aio_move($$$) { 205sub aio_move($$$) {
204 my ($src, $dst, $cb) = @_; 206 my ($src, $dst, $cb) = @_;
205 207
206 my $grp = aio_group; 208 my $grp = aio_group $cb;
207 209
208 add $grp aio_rename $src, $dst, sub { 210 add $grp aio_rename $src, $dst, sub {
209 if ($_[0] && $! == EXDEV) { 211 if ($_[0] && $! == EXDEV) {
210 add $grp aio_open $src, O_RDONLY, 0, sub { 212 add $grp aio_open $src, O_RDONLY, 0, sub {
211 if (my $src_fh = $_[0]) { 213 if (my $src_fh = $_[0]) {
221 chmod $stat[2] & 07777, $dst_fh; 223 chmod $stat[2] & 07777, $dst_fh;
222 chown $stat[4], $stat[5], $dst_fh; 224 chown $stat[4], $stat[5], $dst_fh;
223 close $dst_fh; 225 close $dst_fh;
224 226
225 add $grp aio_unlink $src, sub { 227 add $grp aio_unlink $src, sub {
226 $cb->($_[0]); 228 $grp->result ($_[0]);
227 }; 229 };
228 } else { 230 } else {
229 my $errno = $!; 231 my $errno = $!;
230 add $grp aio_unlink $dst, sub { 232 add $grp aio_unlink $dst, sub {
231 $! = $errno; 233 $! = $errno;
232 $cb->(-1); 234 $grp->result (-1);
233 }; 235 };
234 } 236 }
235 }; 237 };
236 } else { 238 } else {
237 $cb->(-1); 239 $grp->result (-1);
238 } 240 }
239 }, 241 },
240 242
241 } else { 243 } else {
242 $cb->(-1); 244 $grp->result (-1);
243 } 245 }
244 }; 246 };
245 } else { 247 } else {
246 $cb->($_[0]); 248 $grp->result ($_[0]);
247 } 249 }
248 }; 250 };
249 251
250 $grp 252 $grp
251} 253}
343The callback a single argument which is either C<undef> or an array-ref 345The callback a single argument which is either C<undef> or an array-ref
344with the filenames. 346with the filenames.
345 347
346=item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) 348=item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs)
347 349
350[EXPERIMENTAL due to internal aio_group use]
351
348Scans a directory (similar to C<aio_readdir>) but additionally tries to 352Scans a directory (similar to C<aio_readdir>) but additionally tries to
349separate the entries of directory C<$path> into two sets of names, ones 353separate the entries of directory C<$path> into two sets of names, ones
350you can recurse into (directories or links to them), and ones you cannot 354you can recurse into (directories or links to them), and ones you cannot
351recurse into (everything else). 355recurse into (everything else).
352 356
398=cut 402=cut
399 403
400sub aio_scandir($$$) { 404sub aio_scandir($$$) {
401 my ($path, $maxreq, $cb) = @_; 405 my ($path, $maxreq, $cb) = @_;
402 406
403 my $grp = aio_group; 407 my $grp = aio_group $cb;
404 408
405 $maxreq = 8 if $maxreq <= 0; 409 $maxreq = 8 if $maxreq <= 0;
406 410
407 # stat once 411 # stat once
408 add $grp aio_stat $path, sub { 412 add $grp aio_stat $path, sub {
409 return $cb->() if $_[0]; 413 return $grp->result () if $_[0];
410 my $now = time; 414 my $now = time;
411 my $hash1 = join ":", (stat _)[0,1,3,7,9]; 415 my $hash1 = join ":", (stat _)[0,1,3,7,9];
412 416
413 # read the directory entries 417 # read the directory entries
414 add $grp aio_readdir $path, sub { 418 add $grp aio_readdir $path, sub {
415 my $entries = shift 419 my $entries = shift
416 or return $cb->(); 420 or return $grp->result ();
417 421
418 # stat the dir another time 422 # stat the dir another time
419 add $grp aio_stat $path, sub { 423 add $grp aio_stat $path, sub {
420 my $hash2 = join ":", (stat _)[0,1,3,7,9]; 424 my $hash2 = join ":", (stat _)[0,1,3,7,9];
421 425
426 $ndirs = -1; 430 $ndirs = -1;
427 } else { 431 } else {
428 # if nlink == 2, we are finished 432 # if nlink == 2, we are finished
429 # on non-posix-fs's, we rely on nlink < 2 433 # on non-posix-fs's, we rely on nlink < 2
430 $ndirs = (stat _)[3] - 2 434 $ndirs = (stat _)[3] - 2
431 or return $cb->([], $entries); 435 or return $grp->result ([], $entries);
432 } 436 }
433 437
434 # sort into likely dirs and likely nondirs 438 # sort into likely dirs and likely nondirs
435 # dirs == files without ".", short entries first 439 # dirs == files without ".", short entries first
436 $entries = [map $_->[0], 440 $entries = [map $_->[0],
452 } 456 }
453 } elsif (!$nreq) { 457 } elsif (!$nreq) {
454 # finished 458 # finished
455 undef $statcb; 459 undef $statcb;
456 undef $schedcb; 460 undef $schedcb;
457 $cb->(\@dirs, \@nondirs) if $cb; 461 $grp->result (\@dirs, \@nondirs) if $cb;
458 undef $cb; 462 undef $cb;
459 } 463 }
460 }; 464 };
461 $statcb = sub { 465 $statcb = sub {
462 my ($status, $entry) = @_; 466 my ($status, $entry) = @_;
505callback with the fdatasync result code. 509callback with the fdatasync result code.
506 510
507If this call isn't available because your OS lacks it or it couldn't be 511If this call isn't available because your OS lacks it or it couldn't be
508detected, it will be emulated by calling C<fsync> instead. 512detected, it will be emulated by calling C<fsync> instead.
509 513
510=item aio_group $callback->() 514=item aio_group $callback->(...)
511 515
512[EXPERIMENTAL] 516[EXPERIMENTAL]
513 517
514This is a very special aio request: Instead of doing something, it is a 518This is a very special aio request: Instead of doing something, it is a
515container for other aio requests, which is useful if you want to bundle 519container for other aio requests, which is useful if you want to bundle
585You add requests by calling the C<add> method with one or more 589You add requests by calling the C<add> method with one or more
586C<IO::AIO::REQ> objects: 590C<IO::AIO::REQ> objects:
587 591
588 $grp->add (aio_unlink "..."); 592 $grp->add (aio_unlink "...");
589 593
590 add $grp aio_stat "...", sub { ... }; 594 add $grp aio_stat "...", sub {
595 $_[0] or return $grp->result ("error");
596
597 # add another request dynamically, if first succeeded
598 add $grp aio_open "...", sub {
599 $grp->result ("ok");
600 };
601 };
591 602
592This makes it very easy to create composite requests (see the source of 603This makes it very easy to create composite requests (see the source of
593C<aio_move> for an application) that work and feel like simple requests. 604C<aio_move> for an application) that work and feel like simple requests.
594 605
595The IO::AIO::GRP objects will be cleaned up during calls to 606The IO::AIO::GRP objects will be cleaned up during calls to
603Their lifetime, simplified, looks like this: when they are empty, they 614Their lifetime, simplified, looks like this: when they are empty, they
604will finish very quickly. If they contain only requests that are in the 615will finish very quickly. If they contain only requests that are in the
605C<done> state, they will also finish. Otherwise they will continue to 616C<done> state, they will also finish. Otherwise they will continue to
606exist. 617exist.
607 618
619That means after creating a group you have some time to add requests. And
620in the callbacks of those requests, you can add further requests to the
621group. And only when all those requests have finished will the the group
622itself finish.
623
608=over 4 624=over 4
609 625
610=item $grp->add (...) 626=item $grp->add (...)
611 627
612=item add $grp ... 628=item add $grp ...
613 629
614Add one or more 630Add one or more requests to the group. Any type of L<IO::AIO::REQ> can
615Cancels the request, if possible. Has the effect of skipping execution 631be added, including other groups, as long as you do not create circular
616when entering the B<execute> state and skipping calling the callback when 632dependencies.
617entering the the B<result> state, but will leave the request otherwise 633
618untouched. That means that requests that currently execute will not be 634Returns all its arguments.
619stopped and resources held by the request will not be freed prematurely. 635
636=item $grp->result (...)
637
638Set the result value(s) that will be passed to the group callback when all
639subrequests have finished. By default, no argument will be passed.
620 640
621=back 641=back
622 642
623=head2 SUPPORT FUNCTIONS 643=head2 SUPPORT FUNCTIONS
624 644

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines