… | |
… | |
131 | our $VERSION = '2.0'; |
131 | our $VERSION = '2.0'; |
132 | |
132 | |
133 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
133 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
134 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
134 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
135 | aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move |
135 | aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move |
136 | aio_group aio_nop); |
136 | aio_copy aio_group aio_nop aio_mknod); |
137 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
137 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
138 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
138 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
139 | min_parallel max_parallel nreqs); |
139 | min_parallel max_parallel nreqs nready npending); |
140 | |
140 | |
141 | @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; |
141 | @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; |
142 | |
142 | |
143 | require XSLoader; |
143 | require XSLoader; |
144 | XSLoader::load ("IO::AIO", $VERSION); |
144 | XSLoader::load ("IO::AIO", $VERSION); |
… | |
… | |
175 | environment, d) use Glib::filename_from_unicode on unicode filenames or e) |
175 | environment, d) use Glib::filename_from_unicode on unicode filenames or e) |
176 | use something else. |
176 | use something else. |
177 | |
177 | |
178 | =over 4 |
178 | =over 4 |
179 | |
179 | |
180 | =item aioreq_pri $pri |
180 | =item $prev_pri = aioreq_pri [$pri] |
181 | |
181 | |
182 | Sets the priority for the next aio request. The default priority |
182 | Returns the priority value that would be used for the next request and, if |
|
|
183 | C<$pri> is given, sets the priority for the next aio request. |
|
|
184 | |
183 | is C<0>, the minimum and maximum priorities are C<-4> and C<4>, |
185 | The default priority is C<0>, the minimum and maximum priorities are C<-4> |
184 | respectively. Requests with higher priority will be serviced first. |
186 | and C<4>, respectively. Requests with higher priority will be serviced |
|
|
187 | first. |
185 | |
188 | |
186 | The priority will be reset to C<0> after each call to one of the C<aio_> |
189 | The priority will be reset to C<0> after each call to one of the C<aio_*> |
187 | functions. |
190 | functions. |
188 | |
191 | |
189 | Example: open a file with low priority, then read something from it with |
192 | Example: open a file with low priority, then read something from it with |
190 | higher priority so the read request is serviced before other low priority |
193 | higher priority so the read request is serviced before other low priority |
191 | open requests (potentially spamming the cache): |
194 | open requests (potentially spamming the cache): |
… | |
… | |
261 | |
264 | |
262 | aio_read $fh, 7, 15, $buffer, 0, sub { |
265 | aio_read $fh, 7, 15, $buffer, 0, sub { |
263 | $_[0] > 0 or die "read error: $!"; |
266 | $_[0] > 0 or die "read error: $!"; |
264 | print "read $_[0] bytes: <$buffer>\n"; |
267 | print "read $_[0] bytes: <$buffer>\n"; |
265 | }; |
268 | }; |
266 | |
|
|
267 | =item aio_move $srcpath, $dstpath, $callback->($status) |
|
|
268 | |
|
|
269 | Try to move the I<file> (directories not supported as either source or |
|
|
270 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
|
|
271 | the C<0> (error) or C<-1> ok. |
|
|
272 | |
|
|
273 | This is a composite request that tries to rename(2) the file first. If |
|
|
274 | rename files with C<EXDEV>, it creates the destination file with mode 0200 |
|
|
275 | and copies the contents of the source file into it using C<aio_sendfile>, |
|
|
276 | followed by restoring atime, mtime, access mode and uid/gid, in that |
|
|
277 | order, and unlinking the C<$srcpath>. |
|
|
278 | |
|
|
279 | If an error occurs, the partial destination file will be unlinked, if |
|
|
280 | possible, except when setting atime, mtime, access mode and uid/gid, where |
|
|
281 | errors are being ignored. |
|
|
282 | |
|
|
283 | =cut |
|
|
284 | |
|
|
285 | sub aio_move($$$) { |
|
|
286 | my ($src, $dst, $cb) = @_; |
|
|
287 | |
|
|
288 | my $grp = aio_group $cb; |
|
|
289 | |
|
|
290 | add $grp aio_rename $src, $dst, sub { |
|
|
291 | if ($_[0] && $! == EXDEV) { |
|
|
292 | add $grp aio_open $src, O_RDONLY, 0, sub { |
|
|
293 | if (my $src_fh = $_[0]) { |
|
|
294 | my @stat = stat $src_fh; |
|
|
295 | |
|
|
296 | add $grp aio_open $dst, O_WRONLY, 0200, sub { |
|
|
297 | if (my $dst_fh = $_[0]) { |
|
|
298 | add $grp aio_sendfile $dst_fh, $src_fh, 0, $stat[7], sub { |
|
|
299 | close $src_fh; |
|
|
300 | |
|
|
301 | if ($_[0] == $stat[7]) { |
|
|
302 | utime $stat[8], $stat[9], $dst; |
|
|
303 | chmod $stat[2] & 07777, $dst_fh; |
|
|
304 | chown $stat[4], $stat[5], $dst_fh; |
|
|
305 | close $dst_fh; |
|
|
306 | |
|
|
307 | add $grp aio_unlink $src, sub { |
|
|
308 | $grp->result ($_[0]); |
|
|
309 | }; |
|
|
310 | } else { |
|
|
311 | my $errno = $!; |
|
|
312 | add $grp aio_unlink $dst, sub { |
|
|
313 | $! = $errno; |
|
|
314 | $grp->result (-1); |
|
|
315 | }; |
|
|
316 | } |
|
|
317 | }; |
|
|
318 | } else { |
|
|
319 | $grp->result (-1); |
|
|
320 | } |
|
|
321 | }, |
|
|
322 | |
|
|
323 | } else { |
|
|
324 | $grp->result (-1); |
|
|
325 | } |
|
|
326 | }; |
|
|
327 | } else { |
|
|
328 | $grp->result ($_[0]); |
|
|
329 | } |
|
|
330 | }; |
|
|
331 | |
|
|
332 | $grp |
|
|
333 | } |
|
|
334 | |
269 | |
335 | =item aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) |
270 | =item aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) |
336 | |
271 | |
337 | Tries to copy C<$length> bytes from C<$in_fh> to C<$out_fh>. It starts |
272 | Tries to copy C<$length> bytes from C<$in_fh> to C<$out_fh>. It starts |
338 | reading at byte offset C<$in_offset>, and starts writing at the current |
273 | reading at byte offset C<$in_offset>, and starts writing at the current |
… | |
… | |
394 | =item aio_unlink $pathname, $callback->($status) |
329 | =item aio_unlink $pathname, $callback->($status) |
395 | |
330 | |
396 | Asynchronously unlink (delete) a file and call the callback with the |
331 | Asynchronously unlink (delete) a file and call the callback with the |
397 | result code. |
332 | result code. |
398 | |
333 | |
|
|
334 | =item aio_mknod $path, $mode, $dev, $callback->($status) |
|
|
335 | |
|
|
336 | Asynchronously create a device node (or fifo). See mknod(2). |
|
|
337 | |
|
|
338 | The only portable (POSIX) way of calling this function is: |
|
|
339 | |
|
|
340 | aio_mknod $path, IO::AIO::S_IFIFO | $mode, 0, sub { ... |
|
|
341 | |
399 | =item aio_link $srcpath, $dstpath, $callback->($status) |
342 | =item aio_link $srcpath, $dstpath, $callback->($status) |
400 | |
343 | |
401 | Asynchronously create a new link to the existing object at C<$srcpath> at |
344 | Asynchronously create a new link to the existing object at C<$srcpath> at |
402 | the path C<$dstpath> and call the callback with the result code. |
345 | the path C<$dstpath> and call the callback with the result code. |
403 | |
346 | |
… | |
… | |
422 | directory (i.e. opendir + readdir + closedir). The entries will not be |
365 | directory (i.e. opendir + readdir + closedir). The entries will not be |
423 | sorted, and will B<NOT> include the C<.> and C<..> entries. |
366 | sorted, and will B<NOT> include the C<.> and C<..> entries. |
424 | |
367 | |
425 | The callback a single argument which is either C<undef> or an array-ref |
368 | The callback a single argument which is either C<undef> or an array-ref |
426 | with the filenames. |
369 | with the filenames. |
|
|
370 | |
|
|
371 | =item aio_copy $srcpath, $dstpath, $callback->($status) |
|
|
372 | |
|
|
373 | Try to copy the I<file> (directories not supported as either source or |
|
|
374 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
|
|
375 | the C<0> (error) or C<-1> ok. |
|
|
376 | |
|
|
377 | This is a composite request that it creates the destination file with |
|
|
378 | mode 0200 and copies the contents of the source file into it using |
|
|
379 | C<aio_sendfile>, followed by restoring atime, mtime, access mode and |
|
|
380 | uid/gid, in that order. |
|
|
381 | |
|
|
382 | If an error occurs, the partial destination file will be unlinked, if |
|
|
383 | possible, except when setting atime, mtime, access mode and uid/gid, where |
|
|
384 | errors are being ignored. |
|
|
385 | |
|
|
386 | =cut |
|
|
387 | |
|
|
388 | sub aio_copy($$;$) { |
|
|
389 | my ($src, $dst, $cb) = @_; |
|
|
390 | |
|
|
391 | my $pri = aioreq_pri; |
|
|
392 | my $grp = aio_group $cb; |
|
|
393 | |
|
|
394 | aioreq_pri $pri; |
|
|
395 | add $grp aio_open $src, O_RDONLY, 0, sub { |
|
|
396 | if (my $src_fh = $_[0]) { |
|
|
397 | my @stat = stat $src_fh; |
|
|
398 | |
|
|
399 | aioreq_pri $pri; |
|
|
400 | add $grp aio_open $dst, O_CREAT | O_WRONLY | O_TRUNC, 0200, sub { |
|
|
401 | if (my $dst_fh = $_[0]) { |
|
|
402 | aioreq_pri $pri; |
|
|
403 | add $grp aio_sendfile $dst_fh, $src_fh, 0, $stat[7], sub { |
|
|
404 | if ($_[0] == $stat[7]) { |
|
|
405 | $grp->result (0); |
|
|
406 | close $src_fh; |
|
|
407 | |
|
|
408 | # those should not normally block. should. should. |
|
|
409 | utime $stat[8], $stat[9], $dst; |
|
|
410 | chmod $stat[2] & 07777, $dst_fh; |
|
|
411 | chown $stat[4], $stat[5], $dst_fh; |
|
|
412 | close $dst_fh; |
|
|
413 | } else { |
|
|
414 | $grp->result (-1); |
|
|
415 | close $src_fh; |
|
|
416 | close $dst_fh; |
|
|
417 | |
|
|
418 | aioreq $pri; |
|
|
419 | add $grp aio_unlink $dst; |
|
|
420 | } |
|
|
421 | }; |
|
|
422 | } else { |
|
|
423 | $grp->result (-1); |
|
|
424 | } |
|
|
425 | }, |
|
|
426 | |
|
|
427 | } else { |
|
|
428 | $grp->result (-1); |
|
|
429 | } |
|
|
430 | }; |
|
|
431 | |
|
|
432 | $grp |
|
|
433 | } |
|
|
434 | |
|
|
435 | =item aio_move $srcpath, $dstpath, $callback->($status) |
|
|
436 | |
|
|
437 | Try to move the I<file> (directories not supported as either source or |
|
|
438 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
|
|
439 | the C<0> (error) or C<-1> ok. |
|
|
440 | |
|
|
441 | This is a composite request that tries to rename(2) the file first. If |
|
|
442 | rename files with C<EXDEV>, it copies the file with C<aio_copy> and, if |
|
|
443 | that is successful, unlinking the C<$srcpath>. |
|
|
444 | |
|
|
445 | =cut |
|
|
446 | |
|
|
447 | sub aio_move($$;$) { |
|
|
448 | my ($src, $dst, $cb) = @_; |
|
|
449 | |
|
|
450 | my $pri = aioreq_pri; |
|
|
451 | my $grp = aio_group $cb; |
|
|
452 | |
|
|
453 | aioreq_pri $pri; |
|
|
454 | add $grp aio_rename $src, $dst, sub { |
|
|
455 | if ($_[0] && $! == EXDEV) { |
|
|
456 | aioreq_pri $pri; |
|
|
457 | add $grp aio_copy $src, $dst, sub { |
|
|
458 | $grp->result ($_[0]); |
|
|
459 | |
|
|
460 | if (!$_[0]) { |
|
|
461 | aioreq_pri $pri; |
|
|
462 | add $grp aio_unlink $src; |
|
|
463 | } |
|
|
464 | }; |
|
|
465 | } else { |
|
|
466 | $grp->result ($_[0]); |
|
|
467 | } |
|
|
468 | }; |
|
|
469 | |
|
|
470 | $grp |
|
|
471 | } |
427 | |
472 | |
428 | =item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) |
473 | =item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) |
429 | |
474 | |
430 | Scans a directory (similar to C<aio_readdir>) but additionally tries to |
475 | Scans a directory (similar to C<aio_readdir>) but additionally tries to |
431 | efficiently separate the entries of directory C<$path> into two sets of |
476 | efficiently separate the entries of directory C<$path> into two sets of |
… | |
… | |
433 | recurse into (everything else, including symlinks to directories). |
478 | recurse into (everything else, including symlinks to directories). |
434 | |
479 | |
435 | C<aio_scandir> is a composite request that creates of many sub requests_ |
480 | C<aio_scandir> is a composite request that creates of many sub requests_ |
436 | C<$maxreq> specifies the maximum number of outstanding aio requests that |
481 | C<$maxreq> specifies the maximum number of outstanding aio requests that |
437 | this function generates. If it is C<< <= 0 >>, then a suitable default |
482 | this function generates. If it is C<< <= 0 >>, then a suitable default |
438 | will be chosen (currently 6). |
483 | will be chosen (currently 4). |
439 | |
484 | |
440 | On error, the callback is called without arguments, otherwise it receives |
485 | On error, the callback is called without arguments, otherwise it receives |
441 | two array-refs with path-relative entry names. |
486 | two array-refs with path-relative entry names. |
442 | |
487 | |
443 | Example: |
488 | Example: |
… | |
… | |
480 | =cut |
525 | =cut |
481 | |
526 | |
482 | sub aio_scandir($$$) { |
527 | sub aio_scandir($$$) { |
483 | my ($path, $maxreq, $cb) = @_; |
528 | my ($path, $maxreq, $cb) = @_; |
484 | |
529 | |
|
|
530 | my $pri = aioreq_pri; |
|
|
531 | |
485 | my $grp = aio_group $cb; |
532 | my $grp = aio_group $cb; |
486 | |
533 | |
487 | $maxreq = 6 if $maxreq <= 0; |
534 | $maxreq = 4 if $maxreq <= 0; |
488 | |
535 | |
489 | # stat once |
536 | # stat once |
|
|
537 | aioreq_pri $pri; |
490 | add $grp aio_stat $path, sub { |
538 | add $grp aio_stat $path, sub { |
491 | return $grp->result () if $_[0]; |
539 | return $grp->result () if $_[0]; |
492 | my $now = time; |
540 | my $now = time; |
493 | my $hash1 = join ":", (stat _)[0,1,3,7,9]; |
541 | my $hash1 = join ":", (stat _)[0,1,3,7,9]; |
494 | |
542 | |
495 | # read the directory entries |
543 | # read the directory entries |
|
|
544 | aioreq_pri $pri; |
496 | add $grp aio_readdir $path, sub { |
545 | add $grp aio_readdir $path, sub { |
497 | my $entries = shift |
546 | my $entries = shift |
498 | or return $grp->result (); |
547 | or return $grp->result (); |
499 | |
548 | |
500 | # stat the dir another time |
549 | # stat the dir another time |
|
|
550 | aioreq_pri $pri; |
501 | add $grp aio_stat $path, sub { |
551 | add $grp aio_stat $path, sub { |
502 | my $hash2 = join ":", (stat _)[0,1,3,7,9]; |
552 | my $hash2 = join ":", (stat _)[0,1,3,7,9]; |
503 | |
553 | |
504 | my $ndirs; |
554 | my $ndirs; |
505 | |
555 | |
… | |
… | |
529 | limit $statgrp $maxreq; |
579 | limit $statgrp $maxreq; |
530 | feed $statgrp sub { |
580 | feed $statgrp sub { |
531 | return unless @$entries; |
581 | return unless @$entries; |
532 | my $entry = pop @$entries; |
582 | my $entry = pop @$entries; |
533 | |
583 | |
|
|
584 | aioreq_pri $pri; |
534 | add $statgrp aio_stat "$path/$entry/.", sub { |
585 | add $statgrp aio_stat "$path/$entry/.", sub { |
535 | if ($_[0] < 0) { |
586 | if ($_[0] < 0) { |
536 | push @nondirs, $entry; |
587 | push @nondirs, $entry; |
537 | } else { |
588 | } else { |
538 | # need to check for real directory |
589 | # need to check for real directory |
|
|
590 | aioreq_pri $pri; |
539 | add $statgrp aio_lstat "$path/$entry", sub { |
591 | add $statgrp aio_lstat "$path/$entry", sub { |
540 | if (-d _) { |
592 | if (-d _) { |
541 | push @dirs, $entry; |
593 | push @dirs, $entry; |
542 | |
594 | |
543 | unless (--$ndirs) { |
595 | unless (--$ndirs) { |
… | |
… | |
714 | itself. Useful when you queued a lot of events but got a result early. |
766 | itself. Useful when you queued a lot of events but got a result early. |
715 | |
767 | |
716 | =item $grp->result (...) |
768 | =item $grp->result (...) |
717 | |
769 | |
718 | Set the result value(s) that will be passed to the group callback when all |
770 | Set the result value(s) that will be passed to the group callback when all |
719 | subrequests have finished. By default, no argument will be passed. |
771 | subrequests have finished and set thre groups errno to the current value |
|
|
772 | of errno (just like calling C<errno> without an error number). By default, |
|
|
773 | no argument will be passed and errno is zero. |
|
|
774 | |
|
|
775 | =item $grp->errno ([$errno]) |
|
|
776 | |
|
|
777 | Sets the group errno value to C<$errno>, or the current value of errno |
|
|
778 | when the argument is missing. |
|
|
779 | |
|
|
780 | Every aio request has an associated errno value that is restored when |
|
|
781 | the callback is invoked. This method lets you change this value from its |
|
|
782 | default (0). |
|
|
783 | |
|
|
784 | Calling C<result> will also set errno, so make sure you either set C<$!> |
|
|
785 | before the call to C<result>, or call c<errno> after it. |
720 | |
786 | |
721 | =item feed $grp $callback->($grp) |
787 | =item feed $grp $callback->($grp) |
722 | |
788 | |
723 | Sets a feeder/generator on this group: every group can have an attached |
789 | Sets a feeder/generator on this group: every group can have an attached |
724 | generator that generates requests if idle. The idea behind this is that, |
790 | generator that generates requests if idle. The idea behind this is that, |
… | |
… | |
816 | |
882 | |
817 | See C<nreqs> for an example. |
883 | See C<nreqs> for an example. |
818 | |
884 | |
819 | =item IO::AIO::nreqs |
885 | =item IO::AIO::nreqs |
820 | |
886 | |
821 | Returns the number of requests currently outstanding (i.e. for which their |
887 | Returns the number of requests currently in the ready, execute or pending |
822 | callback has not been invoked yet). |
888 | states (i.e. for which their callback has not been invoked yet). |
823 | |
889 | |
824 | Example: wait till there are no outstanding requests anymore: |
890 | Example: wait till there are no outstanding requests anymore: |
825 | |
891 | |
826 | IO::AIO::poll_wait, IO::AIO::poll_cb |
892 | IO::AIO::poll_wait, IO::AIO::poll_cb |
827 | while IO::AIO::nreqs; |
893 | while IO::AIO::nreqs; |
|
|
894 | |
|
|
895 | =item IO::AIO::nready |
|
|
896 | |
|
|
897 | Returns the number of requests currently in the ready state (not yet |
|
|
898 | executed). |
|
|
899 | |
|
|
900 | =item IO::AIO::npending |
|
|
901 | |
|
|
902 | Returns the number of requests currently in the pending state (executed, |
|
|
903 | but not yet processed by poll_cb). |
828 | |
904 | |
829 | =item IO::AIO::flush |
905 | =item IO::AIO::flush |
830 | |
906 | |
831 | Wait till all outstanding AIO requests have been handled. |
907 | Wait till all outstanding AIO requests have been handled. |
832 | |
908 | |
… | |
… | |
915 | } |
991 | } |
916 | |
992 | |
917 | min_parallel 8; |
993 | min_parallel 8; |
918 | |
994 | |
919 | END { |
995 | END { |
920 | max_parallel 0; |
996 | min_parallel 1; |
921 | } |
997 | flush; |
|
|
998 | }; |
922 | |
999 | |
923 | 1; |
1000 | 1; |
924 | |
1001 | |
925 | =head2 FORK BEHAVIOUR |
1002 | =head2 FORK BEHAVIOUR |
926 | |
1003 | |