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.175 by root, Sun Jan 10 20:37:33 2010 UTC vs.
Revision 1.181 by root, Tue May 4 21:14:01 2010 UTC

4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use IO::AIO; 7 use IO::AIO;
8 8
9 aio_open "/etc/passwd", O_RDONLY, 0, sub { 9 aio_open "/etc/passwd", IO::AIO::O_RDONLY, 0, sub {
10 my $fh = shift 10 my $fh = shift
11 or die "/etc/passwd: $!"; 11 or die "/etc/passwd: $!";
12 ... 12 ...
13 }; 13 };
14 14
76 76
77 # register the IO::AIO callback with EV 77 # register the IO::AIO callback with EV
78 my $aio_w = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; 78 my $aio_w = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb;
79 79
80 # queue the request to open /etc/passwd 80 # queue the request to open /etc/passwd
81 aio_open "/etc/passwd", O_RDONLY, 0, sub { 81 aio_open "/etc/passwd", IO::AIO::O_RDONLY, 0, sub {
82 my $fh = shift 82 my $fh = shift
83 or die "error while opening: $!"; 83 or die "error while opening: $!";
84 84
85 # stat'ing filehandles is generally non-blocking 85 # stat'ing filehandles is generally non-blocking
86 my $size = -s $fh; 86 my $size = -s $fh;
168use common::sense; 168use common::sense;
169 169
170use base 'Exporter'; 170use base 'Exporter';
171 171
172BEGIN { 172BEGIN {
173 our $VERSION = '3.5'; 173 our $VERSION = '3.65';
174 174
175 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close 175 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close
176 aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir aio_readdirx 176 aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir aio_readdirx
177 aio_scandir aio_symlink aio_readlink aio_sync aio_fsync 177 aio_scandir aio_symlink aio_readlink aio_sync aio_fsync
178 aio_fdatasync aio_sync_file_range aio_pathsync aio_readahead 178 aio_fdatasync aio_sync_file_range aio_pathsync aio_readahead
352by the umask in effect then the request is being executed, so better never 352by the umask in effect then the request is being executed, so better never
353change the umask. 353change the umask.
354 354
355Example: 355Example:
356 356
357 aio_open "/etc/passwd", O_RDONLY, 0, sub { 357 aio_open "/etc/passwd", IO::AIO::O_RDONLY, 0, sub {
358 if ($_[0]) { 358 if ($_[0]) {
359 print "open successful, fh is $_[0]\n"; 359 print "open successful, fh is $_[0]\n";
360 ... 360 ...
361 } else { 361 } else {
362 die "open failed: $!\n"; 362 die "open failed: $!\n";
424than one C<aio_sendfile> per C<$out_fh>, as they will interfere with each 424than one C<aio_sendfile> per C<$out_fh>, as they will interfere with each
425other. 425other.
426 426
427This call tries to make use of a native C<sendfile> syscall to provide 427This call tries to make use of a native C<sendfile> syscall to provide
428zero-copy operation. For this to work, C<$out_fh> should refer to a 428zero-copy operation. For this to work, C<$out_fh> should refer to a
429socket, and C<$in_fh> should refer to mmap'able file. 429socket, and C<$in_fh> should refer to an mmap'able file.
430 430
431If a native sendfile cannot be found or it fails with C<ENOSYS>, 431If a native sendfile cannot be found or it fails with C<ENOSYS>,
432C<ENOTSUP>, C<EOPNOTSUPP>, C<EAFNOSUPPORT>, C<EPROTOTYPE> or C<ENOTSOCK>, 432C<ENOTSUP>, C<EOPNOTSUPP>, C<EAFNOSUPPORT>, C<EPROTOTYPE> or C<ENOTSOCK>,
433it will be emulated, so you can call C<aio_sendfile> on any type of 433it will be emulated, so you can call C<aio_sendfile> on any type of
434filehandle regardless of the limitations of the operating system. 434filehandle regardless of the limitations of the operating system.
1084} 1084}
1085 1085
1086=item aio_msync $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) 1086=item aio_msync $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status)
1087 1087
1088This is a rather advanced IO::AIO call, which only works on mmap(2)ed 1088This is a rather advanced IO::AIO call, which only works on mmap(2)ed
1089scalars (see the L<Sys::Mmap> or L<Mmap> modules for details on this, note 1089scalars (see the C<IO::AIO::mmap> function, although it also works on data
1090scalars managed by the L<Sys::Mmap> or L<Mmap> modules, note that the
1090that the scalar must only be modified in-place while an aio operation is 1091scalar must only be modified in-place while an aio operation is pending on
1091pending on it). 1092it).
1092 1093
1093It calls the C<msync> function of your OS, if available, with the memory 1094It calls the C<msync> function of your OS, if available, with the memory
1094area starting at C<$offset> in the string and ending C<$length> bytes 1095area starting at C<$offset> in the string and ending C<$length> bytes
1095later. If C<$length> is negative, counts from the end, and if C<$length> 1096later. If C<$length> is negative, counts from the end, and if C<$length>
1096is C<undef>, then it goes till the end of the string. The flags can be 1097is C<undef>, then it goes till the end of the string. The flags can be
1544C<IO::AIO::FADV_RANDOM>, C<IO::AIO::FADV_NOREUSE>, 1545C<IO::AIO::FADV_RANDOM>, C<IO::AIO::FADV_NOREUSE>,
1545C<IO::AIO::FADV_WILLNEED>, C<IO::AIO::FADV_DONTNEED>. 1546C<IO::AIO::FADV_WILLNEED>, C<IO::AIO::FADV_DONTNEED>.
1546 1547
1547On systems that do not implement C<posix_fadvise>, this function returns 1548On systems that do not implement C<posix_fadvise>, this function returns
1548ENOSYS, otherwise the return value of C<posix_fadvise>. 1549ENOSYS, otherwise the return value of C<posix_fadvise>.
1550
1551=item IO::AIO::mmap $scalar, $length, $prot, $flags, $fh[, $offset]
1552
1553Memory-maps a file (or anonymous memory range) and attaches it to the
1554given C<$scalar>, which will act like a string scalar.
1555
1556The only operations allowed on the scalar are C<substr>/C<vec> that don't
1557change the string length, and most read-only operations such as copying it
1558or searching it with regexes and so on.
1559
1560Anything else is unsafe and will, at best, result in memory leaks.
1561
1562The memory map associated with the C<$scalar> is automatically removed
1563when the C<$scalar> is destroyed, or when the C<IO::AIO::mmap> or
1564C<IO::AIO::munmap> functions are called.
1565
1566This calls the C<mmap>(2) function internally. See your system's manual
1567page for details on the C<$length>, C<$prot> and C<$flags> parameters.
1568
1569The C<$length> must be larger than zero and smaller than the actual
1570filesize.
1571
1572C<$prot> is a combination of C<IO::AIO::PROT_NONE>, C<IO::AIO::PROT_EXEC>,
1573C<IO::AIO::PROT_READ> and/or C<IO::AIO::PROT_WRITE>,
1574
1575C<$flags> can be a combination of C<IO::AIO::MAP_SHARED> or
1576C<IO::AIO::MAP_PRIVATE>, or a number of system-specific flags (when
1577not available, the are defined as 0): C<IO::AIO::MAP_ANONYMOUS>
1578(which is set to C<MAP_ANON> if your system only provides this
1579constant), C<IO::AIO::MAP_HUGETLB>, C<IO::AIO::MAP_LOCKED>,
1580C<IO::AIO::MAP_NORESERVE>, C<IO::AIO::MAP_POPULATE> or
1581C<IO::AIO::MAP_NONBLOCK>
1582
1583If C<$fh> is C<undef>, then a file descriptor of C<-1> is passed.
1584
1585C<$offset> is the offset from the start of the file - it generally must be
1586a multiple of C<IO::AIO::PAGESIZE> and defaults to C<0>.
1587
1588Example:
1589
1590 use Digest::MD5;
1591 use IO::AIO;
1592
1593 open my $fh, "<verybigfile"
1594 or die "$!";
1595
1596 IO::AIO::mmap my $data, -s $fh, IO::AIO::PROT_READ, IO::AIO::MAP_SHARED, $fh
1597 or die "verybigfile: $!";
1598
1599 my $fast_md5 = md5 $data;
1600
1601=item IO::AIO::munmap $scalar
1602
1603Removes a previous mmap and undefines the C<$scalar>.
1549 1604
1550=item IO::AIO::mlockall $flags 1605=item IO::AIO::mlockall $flags
1551 1606
1552Calls the C<mlockall> function with the given C<$flags> (a combination of 1607Calls the C<mlockall> function with the given C<$flags> (a combination of
1553C<IO::AIO::MCL_CURRENT> and C<IO::AIO::MCL__FUTURE>). 1608C<IO::AIO::MCL_CURRENT> and C<IO::AIO::MCL__FUTURE>).

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines