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.109 by root, Sun Jun 3 09:44:17 2007 UTC vs.
Revision 1.119 by root, Sun Dec 2 20:54:33 2007 UTC

26 $req->cancel; # cancel request if still in queue 26 $req->cancel; # cancel request if still in queue
27 27
28 my $grp = aio_group sub { print "all stats done\n" }; 28 my $grp = aio_group sub { print "all stats done\n" };
29 add $grp aio_stat "..." for ...; 29 add $grp aio_stat "..." for ...;
30 30
31 # AnyEvent integration 31 # AnyEvent integration (EV, Event, Glib, Tk, urxvt, pureperl...)
32 open my $fh, "<&=" . IO::AIO::poll_fileno or die "$!"; 32 open my $fh, "<&=" . IO::AIO::poll_fileno or die "$!";
33 my $w = AnyEvent->io (fh => $fh, poll => 'r', cb => sub { IO::AIO::poll_cb }); 33 my $w = AnyEvent->io (fh => $fh, poll => 'r', cb => sub { IO::AIO::poll_cb });
34
35 # EV integration
36 my $w = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb;
34 37
35 # Event integration 38 # Event integration
36 Event->io (fd => IO::AIO::poll_fileno, 39 Event->io (fd => IO::AIO::poll_fileno,
37 poll => 'r', 40 poll => 'r',
38 cb => \&IO::AIO::poll_cb); 41 cb => \&IO::AIO::poll_cb);
183 186
184=cut 187=cut
185 188
186package IO::AIO; 189package IO::AIO;
187 190
191use Carp ();
192
188no warnings; 193no warnings;
189use strict 'vars'; 194use strict 'vars';
190 195
191use base 'Exporter'; 196use base 'Exporter';
192 197
193BEGIN { 198BEGIN {
194 our $VERSION = '2.4'; 199 our $VERSION = '2.6';
195 200
196 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat 201 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat
197 aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink 202 aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink
198 aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link 203 aio_readlink aio_sync aio_fsync aio_fdatasync aio_readahead aio_rename aio_link
199 aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir 204 aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir
200 aio_chown aio_chmod aio_utime); 205 aio_chown aio_chmod aio_utime aio_truncate);
201 our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block)); 206 our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block));
202 our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush 207 our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush
203 min_parallel max_parallel max_idle 208 min_parallel max_parallel max_idle
204 nreqs nready npending nthreads 209 nreqs nready npending nthreads
205 max_poll_time max_poll_reqs); 210 max_poll_time max_poll_reqs);
311 316
312 317
313=item aio_close $fh, $callback->($status) 318=item aio_close $fh, $callback->($status)
314 319
315Asynchronously close a file and call the callback with the result 320Asynchronously close a file and call the callback with the result
316code. I<WARNING:> although accepted, you should not pass in a perl 321code.
317filehandle here, as perl will likely close the file descriptor another
318time when the filehandle is destroyed. Normally, you can safely call perls
319C<close> or just let filehandles go out of scope.
320 322
321This is supposed to be a bug in the API, so that might change. It's 323Unfortunately, you can't do this to perl. Perl I<insists> very strongly on
322therefore best to avoid this function. 324closing the file descriptor associated with the filehandle itself. Here is
325what aio_close will try:
326
327 1. dup()licate the fd
328 2. asynchronously close() the duplicated fd
329 3. dup()licate the fd once more
330 4. let perl close() the filehandle
331 5. asynchronously close the duplicated fd
332
333The idea is that the first close() flushes stuff to disk that closing an
334fd will flush, so when perl closes the fd, nothing much will need to be
335flushed. The second async. close() will then flush stuff to disk that
336closing the last fd to the file will flush.
337
338Just FYI, SuSv3 has this to say on close:
339
340 All outstanding record locks owned by the process on the file
341 associated with the file descriptor shall be removed.
342
343 If fildes refers to a socket, close() shall cause the socket to be
344 destroyed. ... close() shall block for up to the current linger
345 interval until all data is transmitted.
346 [this actually sounds like a specification bug, but who knows]
347
348And at least Linux additionally actually flushes stuff on every close,
349even when the file itself is still open.
350
351Sounds enourmously inefficient and complicated? Yes... please show me how
352to nuke perl's fd out of existence...
353
354=cut
355
356sub aio_close($;$) {
357 aio_block {
358 my ($fh, $cb) = @_;
359
360 my $pri = aioreq_pri;
361 my $grp = aio_group $cb;
362
363 my $fd = fileno $fh;
364
365 defined $fd or Carp::croak "aio_close called with fd-less filehandle";
366
367 # if the dups fail we will simply get EBADF
368 my $fd2 = _dup $fd;
369 aioreq_pri $pri;
370 add $grp _aio_close $fd2, sub {
371 my $fd2 = _dup $fd;
372 close $fh;
373 aioreq_pri $pri;
374 add $grp _aio_close $fd2, sub {
375 $grp->result ($_[0]);
376 };
377 };
378
379 $grp
380 }
381}
323 382
324 383
325=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 384=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
326 385
327=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 386=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
329Reads or writes C<$length> bytes from the specified C<$fh> and C<$offset> 388Reads or writes C<$length> bytes from the specified C<$fh> and C<$offset>
330into the scalar given by C<$data> and offset C<$dataoffset> and calls the 389into the scalar given by C<$data> and offset C<$dataoffset> and calls the
331callback without the actual number of bytes read (or -1 on error, just 390callback without the actual number of bytes read (or -1 on error, just
332like the syscall). 391like the syscall).
333 392
334If C<$offset> is undefined, then the current file offset will be used (and 393If C<$offset> is undefined, then the current file descriptor offset will
335updated), otherwise the file offset will not be changed by these calls. 394be used (and updated), otherwise the file descriptor offset will not be
395changed by these calls.
336 396
337If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>. 397If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>.
338 398
339If C<$dataoffset> is less than zero, it will be counted from the end of 399If C<$dataoffset> is less than zero, it will be counted from the end of
340C<$data>. 400C<$data>.
341 401
342The C<$data> scalar I<MUST NOT> be modified in any way while the request 402The C<$data> scalar I<MUST NOT> be modified in any way while the request
441 501
442 # same as "chown root path" in the shell: 502 # same as "chown root path" in the shell:
443 aio_chown "path", 0, -1; 503 aio_chown "path", 0, -1;
444 # same as above: 504 # same as above:
445 aio_chown "path", 0, undef; 505 aio_chown "path", 0, undef;
506
507
508=item aio_truncate $fh_or_path, $offset, $callback->($status)
509
510Works like truncate(2) or ftruncate(2).
446 511
447 512
448=item aio_chmod $fh_or_path, $mode, $callback->($status) 513=item aio_chmod $fh_or_path, $mode, $callback->($status)
449 514
450Works like perl's C<chmod> function. 515Works like perl's C<chmod> function.
828 893
829 $grp 894 $grp
830 } 895 }
831} 896}
832 897
898=item aio_sync $callback->($status)
899
900Asynchronously call sync and call the callback when finished.
901
833=item aio_fsync $fh, $callback->($status) 902=item aio_fsync $fh, $callback->($status)
834 903
835Asynchronously call fsync on the given filehandle and call the callback 904Asynchronously call fsync on the given filehandle and call the callback
836with the fsync result code. 905with the fsync result code.
837 906
1201This is a very bad function to use in interactive programs because it 1270This is a very bad function to use in interactive programs because it
1202blocks, and a bad way to reduce concurrency because it is inexact: Better 1271blocks, and a bad way to reduce concurrency because it is inexact: Better
1203use an C<aio_group> together with a feed callback. 1272use an C<aio_group> together with a feed callback.
1204 1273
1205Sets the maximum number of outstanding requests to C<$nreqs>. If you 1274Sets the maximum number of outstanding requests to C<$nreqs>. If you
1206to queue up more than this number of requests, the next call to the 1275do queue up more than this number of requests, the next call to the
1207C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) 1276C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>)
1208function will block until the limit is no longer exceeded. 1277function will block until the limit is no longer exceeded.
1209 1278
1210The default value is very large, so there is no practical limit on the 1279The default value is very large, so there is no practical limit on the
1211number of outstanding requests. 1280number of outstanding requests.
1241but not yet processed by poll_cb). 1310but not yet processed by poll_cb).
1242 1311
1243=back 1312=back
1244 1313
1245=cut 1314=cut
1246
1247# support function to convert a fd into a perl filehandle
1248sub _fd2fh {
1249 return undef if $_[0] < 0;
1250
1251 # try to generate nice filehandles
1252 my $sym = "IO::AIO::fd#$_[0]";
1253 local *$sym;
1254
1255 open *$sym, "+<&=$_[0]" # usually works under any unix
1256 or open *$sym, "<&=$_[0]" # cygwin needs this
1257 or open *$sym, ">&=$_[0]" # or this
1258 or return undef;
1259
1260 *$sym
1261}
1262 1315
1263min_parallel 8; 1316min_parallel 8;
1264 1317
1265END { flush } 1318END { flush }
1266 1319
1290bytes of memory. In addition, stat requests need a stat buffer (possibly 1343bytes of memory. In addition, stat requests need a stat buffer (possibly
1291a few hundred bytes), readdir requires a result buffer and so on. Perl 1344a few hundred bytes), readdir requires a result buffer and so on. Perl
1292scalars and other data passed into aio requests will also be locked and 1345scalars and other data passed into aio requests will also be locked and
1293will consume memory till the request has entered the done state. 1346will consume memory till the request has entered the done state.
1294 1347
1295This is now awfully much, so queuing lots of requests is not usually a 1348This is not awfully much, so queuing lots of requests is not usually a
1296problem. 1349problem.
1297 1350
1298Per-thread usage: 1351Per-thread usage:
1299 1352
1300In the execution phase, some aio requests require more memory for 1353In the execution phase, some aio requests require more memory for

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines