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.211 by root, Tue Sep 27 12:10:29 2011 UTC vs.
Revision 1.216 by root, Sun Oct 9 08:24:49 2011 UTC

168use common::sense; 168use common::sense;
169 169
170use base 'Exporter'; 170use base 'Exporter';
171 171
172BEGIN { 172BEGIN {
173 our $VERSION = '4.0'; 173 our $VERSION = '4.1';
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_realpath aio_sync 177 aio_scandir aio_symlink aio_readlink aio_realpath aio_sync
178 aio_fsync aio_syncfs aio_fdatasync aio_sync_file_range aio_fallocate 178 aio_fsync aio_syncfs aio_fdatasync aio_sync_file_range aio_fallocate
233 aio_rmdir $pathname, $callback->($status) 233 aio_rmdir $pathname, $callback->($status)
234 aio_readdir $pathname, $callback->($entries) 234 aio_readdir $pathname, $callback->($entries)
235 aio_readdirx $pathname, $flags, $callback->($entries, $flags) 235 aio_readdirx $pathname, $flags, $callback->($entries, $flags)
236 IO::AIO::READDIR_DENTS IO::AIO::READDIR_DIRS_FIRST 236 IO::AIO::READDIR_DENTS IO::AIO::READDIR_DIRS_FIRST
237 IO::AIO::READDIR_STAT_ORDER IO::AIO::READDIR_FOUND_UNKNOWN 237 IO::AIO::READDIR_STAT_ORDER IO::AIO::READDIR_FOUND_UNKNOWN
238 aio_scandir $pathname, $maxreq, $callback->($dirs, $nondirs)
238 aio_load $pathname, $data, $callback->($status) 239 aio_load $pathname, $data, $callback->($status)
239 aio_copy $srcpath, $dstpath, $callback->($status) 240 aio_copy $srcpath, $dstpath, $callback->($status)
240 aio_move $srcpath, $dstpath, $callback->($status) 241 aio_move $srcpath, $dstpath, $callback->($status)
241 aio_scandir $pathname, $maxreq, $callback->($dirs, $nondirs)
242 aio_rmtree $pathname, $callback->($status) 242 aio_rmtree $pathname, $callback->($status)
243 aio_sync $callback->($status) 243 aio_sync $callback->($status)
244 aio_syncfs $fh, $callback->($status) 244 aio_syncfs $fh, $callback->($status)
245 aio_fsync $fh, $callback->($status) 245 aio_fsync $fh, $callback->($status)
246 aio_fdatasync $fh, $callback->($status) 246 aio_fdatasync $fh, $callback->($status)
281=head2 AIO REQUEST FUNCTIONS 281=head2 AIO REQUEST FUNCTIONS
282 282
283All the C<aio_*> calls are more or less thin wrappers around the syscall 283All the C<aio_*> calls are more or less thin wrappers around the syscall
284with the same name (sans C<aio_>). The arguments are similar or identical, 284with the same name (sans C<aio_>). The arguments are similar or identical,
285and they all accept an additional (and optional) C<$callback> argument 285and they all accept an additional (and optional) C<$callback> argument
286which must be a code reference. This code reference will get called with 286which must be a code reference. This code reference will be called after
287the syscall return code (e.g. most syscalls return C<-1> on error, unlike 287the syscall has been executed in an asynchronous fashion. The results
288perl, which usually delivers "false") as its sole argument after the given 288of the request will be passed as arguments to the callback (and, if an
289syscall has been executed asynchronously. 289error occured, in C<$!>) - for most requests the syscall return code (e.g.
290most syscalls return C<-1> on error, unlike perl, which usually delivers
291"false").
292
293Some requests (such as C<aio_readdir>) pass the actual results and
294communicate failures by passing C<undef>.
290 295
291All functions expecting a filehandle keep a copy of the filehandle 296All functions expecting a filehandle keep a copy of the filehandle
292internally until the request has finished. 297internally until the request has finished.
293 298
294All functions return request objects of type L<IO::AIO::REQ> that allow 299All functions return request objects of type L<IO::AIO::REQ> that allow
295further manipulation of those requests while they are in-flight. 300further manipulation of those requests while they are in-flight.
296 301
297The pathnames you pass to these routines I<should> be absolute. The 302The pathnames you pass to these routines I<should> be absolute. The
298reason for this is that at the time the request is being executed, the 303reason for this is that at the time the request is being executed, the
299current working directory could have changed. Alternatively, you can make 304current working directory could have changed. Alternatively, you can
300sure that you never change the current working directory anywhere in 305make sure that you never change the current working directory anywhere
301the program and then use relative paths. Lastly, you can take advantage 306in the program and then use relative paths. You can also take advantage
302of IO::AIOs working directory abstraction - see the description of the 307of IO::AIOs working directory abstraction, that lets you specify paths
308relative to some previously-opened "working directory object" - see the
303C<IO::AIO::WD> class later in this document. 309description of the C<IO::AIO::WD> class later in this document.
304 310
305To encode pathnames as octets, either make sure you either: a) always pass 311To encode pathnames as octets, either make sure you either: a) always pass
306in filenames you got from outside (command line, readdir etc.) without 312in filenames you got from outside (command line, readdir etc.) without
307tinkering, b) are ASCII or ISO 8859-1, c) use the Encode module and encode 313tinkering, b) are in your native filesystem encoding, c) use the Encode
308your pathnames to the locale (or other) encoding in effect in the user 314module and encode your pathnames to the locale (or other) encoding in
309environment, d) use Glib::filename_from_unicode on unicode filenames or e) 315effect in the user environment, d) use Glib::filename_from_unicode on
310use something else to ensure your scalar has the correct contents. 316unicode filenames or e) use something else to ensure your scalar has the
317correct contents.
311 318
312This works, btw. independent of the internal UTF-8 bit, which IO::AIO 319This works, btw. independent of the internal UTF-8 bit, which IO::AIO
313handles correctly whether it is set or not. 320handles correctly whether it is set or not.
314 321
315=over 4 322=over 4
967 my $grp = aio_group $cb; 974 my $grp = aio_group $cb;
968 975
969 $maxreq = 4 if $maxreq <= 0; 976 $maxreq = 4 if $maxreq <= 0;
970 977
971 # get a wd object 978 # get a wd object
972
973 aioreq_pri $pri; 979 aioreq_pri $pri;
974 add $grp aio_wd $path, sub { 980 add $grp aio_wd $path, sub {
981 $_[0]
982 or return $grp->result ();
983
975 my $wd = [shift, "."]; 984 my $wd = [shift, "."];
976 985
977 # stat once 986 # stat once
978 aioreq_pri $pri; 987 aioreq_pri $pri;
979 add $grp aio_stat $wd, sub { 988 add $grp aio_stat $wd, sub {
1295object. This object stores the canonicalised, absolute version of the 1304object. This object stores the canonicalised, absolute version of the
1296path, and on systems that allow it, also a directory file descriptor. 1305path, and on systems that allow it, also a directory file descriptor.
1297 1306
1298Everywhere where a pathname is accepted by IO::AIO (e.g. in C<aio_stat> 1307Everywhere where a pathname is accepted by IO::AIO (e.g. in C<aio_stat>
1299or C<aio_unlink>), one can specify an array reference with an IO::AIO::WD 1308or C<aio_unlink>), one can specify an array reference with an IO::AIO::WD
1300object and a pathname instead. If the pathname is absolute, the 1309object and a pathname instead (or the IO::AIO::WD object alone, which
1310gets interpreted as C<[$wd, "."]>). If the pathname is absolute, the
1301IO::AIO::WD objetc is ignored, otherwise the pathname is resolved relative 1311IO::AIO::WD object is ignored, otherwise the pathname is resolved relative
1302to that IO::AIO::WD object. 1312to that IO::AIO::WD object.
1303 1313
1304For example, to get a wd object for F</etc> and then stat F<passwd> 1314For example, to get a wd object for F</etc> and then stat F<passwd>
1305inside, you would write: 1315inside, you would write:
1306 1316
1314 aio_stat [$etcdir, "passwd"], sub { 1324 aio_stat [$etcdir, "passwd"], sub {
1315 # yay 1325 # yay
1316 }; 1326 };
1317 }; 1327 };
1318 1328
1319This shows that creating an IO::AIO::WD object is itself a potentially 1329That C<aio_wd> is a request and not a normal function shows that creating
1320blocking operation, which is why it is done asynchronously. 1330an IO::AIO::WD object is itself a potentially blocking operation, which is
1331why it is done asynchronously.
1332
1333To stat the directory obtained with C<aio_wd> above, one could write
1334either of the following three request calls:
1335
1336 aio_lstat "/etc" , sub { ... # pathname as normal string
1337 aio_lstat [$wd, "."], sub { ... # "." relative to $wd (i.e. $wd itself)
1338 aio_lstat $wd , sub { ... # shorthand for the previous
1321 1339
1322As with normal pathnames, IO::AIO keeps a copy of the working directory 1340As with normal pathnames, IO::AIO keeps a copy of the working directory
1323object and the pathname string, so you could write the following without 1341object and the pathname string, so you could write the following without
1324causing any issues due to C<$path> getting reused: 1342causing any issues due to C<$path> getting reused:
1325 1343

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines