… | |
… | |
186 | |
186 | |
187 | package IO::AIO; |
187 | package IO::AIO; |
188 | |
188 | |
189 | use Carp (); |
189 | use Carp (); |
190 | |
190 | |
191 | no warnings; |
191 | use common::sense; |
192 | use strict 'vars'; |
|
|
193 | |
192 | |
194 | use base 'Exporter'; |
193 | use base 'Exporter'; |
195 | |
194 | |
196 | BEGIN { |
195 | BEGIN { |
197 | our $VERSION = '3.23'; |
196 | our $VERSION = '3.31'; |
198 | |
197 | |
199 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close |
198 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close |
200 | aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir aio_readdirx |
199 | aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir aio_readdirx |
201 | aio_scandir aio_symlink aio_readlink aio_sync aio_fsync |
200 | aio_scandir aio_symlink aio_readlink aio_sync aio_fsync |
202 | aio_fdatasync aio_sync_file_range aio_pathsync aio_readahead |
201 | aio_fdatasync aio_sync_file_range aio_pathsync aio_readahead |
… | |
… | |
206 | |
205 | |
207 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
206 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
208 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
207 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
209 | min_parallel max_parallel max_idle |
208 | min_parallel max_parallel max_idle |
210 | nreqs nready npending nthreads |
209 | nreqs nready npending nthreads |
211 | max_poll_time max_poll_reqs); |
210 | max_poll_time max_poll_reqs |
|
|
211 | sendfile fadvise); |
212 | |
212 | |
213 | push @AIO_REQ, qw(aio_busy); # not exported |
213 | push @AIO_REQ, qw(aio_busy); # not exported |
214 | |
214 | |
215 | @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; |
215 | @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; |
216 | |
216 | |
… | |
… | |
634 | |
634 | |
635 | =item aio_copy $srcpath, $dstpath, $callback->($status) |
635 | =item aio_copy $srcpath, $dstpath, $callback->($status) |
636 | |
636 | |
637 | Try to copy the I<file> (directories not supported as either source or |
637 | Try to copy the I<file> (directories not supported as either source or |
638 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
638 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
639 | the C<0> (error) or C<-1> ok. |
639 | a status of C<0> (ok) or C<-1> (error, see C<$!>). |
640 | |
640 | |
641 | This is a composite request that creates the destination file with |
641 | This is a composite request that creates the destination file with |
642 | mode 0200 and copies the contents of the source file into it using |
642 | mode 0200 and copies the contents of the source file into it using |
643 | C<aio_sendfile>, followed by restoring atime, mtime, access mode and |
643 | C<aio_sendfile>, followed by restoring atime, mtime, access mode and |
644 | uid/gid, in that order. |
644 | uid/gid, in that order. |
… | |
… | |
656 | my $grp = aio_group $cb; |
656 | my $grp = aio_group $cb; |
657 | |
657 | |
658 | aioreq_pri $pri; |
658 | aioreq_pri $pri; |
659 | add $grp aio_open $src, O_RDONLY, 0, sub { |
659 | add $grp aio_open $src, O_RDONLY, 0, sub { |
660 | if (my $src_fh = $_[0]) { |
660 | if (my $src_fh = $_[0]) { |
661 | my @stat = stat $src_fh; # hmm, might bock over nfs? |
661 | my @stat = stat $src_fh; # hmm, might block over nfs? |
662 | |
662 | |
663 | aioreq_pri $pri; |
663 | aioreq_pri $pri; |
664 | add $grp aio_open $dst, O_CREAT | O_WRONLY | O_TRUNC, 0200, sub { |
664 | add $grp aio_open $dst, O_CREAT | O_WRONLY | O_TRUNC, 0200, sub { |
665 | if (my $dst_fh = $_[0]) { |
665 | if (my $dst_fh = $_[0]) { |
666 | aioreq_pri $pri; |
666 | aioreq_pri $pri; |
… | |
… | |
713 | |
713 | |
714 | =item aio_move $srcpath, $dstpath, $callback->($status) |
714 | =item aio_move $srcpath, $dstpath, $callback->($status) |
715 | |
715 | |
716 | Try to move the I<file> (directories not supported as either source or |
716 | Try to move the I<file> (directories not supported as either source or |
717 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
717 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
718 | the C<0> (error) or C<-1> ok. |
718 | a status of C<0> (ok) or C<-1> (error, see C<$!>). |
719 | |
719 | |
720 | This is a composite request that tries to rename(2) the file first; if |
720 | This is a composite request that tries to rename(2) the file first; if |
721 | rename fails with C<EXDEV>, it copies the file with C<aio_copy> and, if |
721 | rename fails with C<EXDEV>, it copies the file with C<aio_copy> and, if |
722 | that is successful, unlinks the C<$srcpath>. |
722 | that is successful, unlinks the C<$srcpath>. |
723 | |
723 | |
… | |
… | |
957 | (E.g. rename). This might not work on all operating systems or have any |
957 | (E.g. rename). This might not work on all operating systems or have any |
958 | specific effect, but usually it makes sure that directory changes get |
958 | specific effect, but usually it makes sure that directory changes get |
959 | written to disc. It works for anything that can be opened for read-only, |
959 | written to disc. It works for anything that can be opened for read-only, |
960 | not just directories. |
960 | not just directories. |
961 | |
961 | |
|
|
962 | Future versions of this function might fall back to other methods when |
|
|
963 | C<fsync> on the directory fails (such as calling C<sync>). |
|
|
964 | |
962 | Passes C<0> when everything went ok, and C<-1> on error. |
965 | Passes C<0> when everything went ok, and C<-1> on error. |
963 | |
966 | |
964 | =cut |
967 | =cut |
965 | |
968 | |
966 | sub aio_pathsync($;$) { |
969 | sub aio_pathsync($;$) { |
… | |
… | |
1130 | =item $grp->cancel_subs |
1133 | =item $grp->cancel_subs |
1131 | |
1134 | |
1132 | Cancel all subrequests and clears any feeder, but not the group request |
1135 | Cancel all subrequests and clears any feeder, but not the group request |
1133 | itself. Useful when you queued a lot of events but got a result early. |
1136 | itself. Useful when you queued a lot of events but got a result early. |
1134 | |
1137 | |
|
|
1138 | The group request will finish normally (you cannot add requests to the |
|
|
1139 | group). |
|
|
1140 | |
1135 | =item $grp->result (...) |
1141 | =item $grp->result (...) |
1136 | |
1142 | |
1137 | Set the result value(s) that will be passed to the group callback when all |
1143 | Set the result value(s) that will be passed to the group callback when all |
1138 | subrequests have finished and set the groups errno to the current value |
1144 | subrequests have finished and set the groups errno to the current value |
1139 | of errno (just like calling C<errno> without an error number). By default, |
1145 | of errno (just like calling C<errno> without an error number). By default, |
… | |
… | |
1391 | |
1397 | |
1392 | =item IO::AIO::npending |
1398 | =item IO::AIO::npending |
1393 | |
1399 | |
1394 | Returns the number of requests currently in the pending state (executed, |
1400 | Returns the number of requests currently in the pending state (executed, |
1395 | but not yet processed by poll_cb). |
1401 | but not yet processed by poll_cb). |
|
|
1402 | |
|
|
1403 | =back |
|
|
1404 | |
|
|
1405 | =head3 MISCELLANEOUS FUNCTIONS |
|
|
1406 | |
|
|
1407 | IO::AIO implements some functions that might be useful, but are not |
|
|
1408 | asynchronous. |
|
|
1409 | |
|
|
1410 | =over 4 |
|
|
1411 | |
|
|
1412 | =item IO::AIO::sendfile $ofh, $ifh, $offset, $count |
|
|
1413 | |
|
|
1414 | Calls the C<eio_sendfile_sync> function, which is like C<aio_sendfile>, |
|
|
1415 | but is blocking (this makes most sense if you know the input data is |
|
|
1416 | likely cached already and the output filehandle is set to non-blocking |
|
|
1417 | operations). |
|
|
1418 | |
|
|
1419 | Returns the number of bytes copied, or C<-1> on error. |
|
|
1420 | |
|
|
1421 | =item IO::AIO::fadvise $fh, $offset, $len, $advice |
|
|
1422 | |
|
|
1423 | Simply calls the C<posix_fadvise> function (see it's |
|
|
1424 | manpage for details). The following advice constants are |
|
|
1425 | avaiable: C<IO::AIO::FADV_NORMAL>, C<IO::AIO::FADV_SEQUENTIAL>, |
|
|
1426 | C<IO::AIO::FADV_RANDOM>, C<IO::AIO::FADV_NOREUSE>, |
|
|
1427 | C<IO::AIO::FADV_WILLNEED>, C<IO::AIO::FADV_DONTNEED>. |
|
|
1428 | |
|
|
1429 | On systems that do not implement C<posix_fadvise>, this function returns |
|
|
1430 | ENOSYS, otherwise the return value of C<posix_fadvise>. |
1396 | |
1431 | |
1397 | =back |
1432 | =back |
1398 | |
1433 | |
1399 | =cut |
1434 | =cut |
1400 | |
1435 | |