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.116 by root, Wed Oct 3 21:27:51 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.5'; 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 aio_truncate); 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
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. 321code.
317 322
318Unlike the other functions operating on files, this function uses the 323Unfortunately, you can't do this to perl. Perl I<insists> very strongly on
319PerlIO layer to close the filehandle. The reason is that the PerlIO API 324closing the file descriptor associated with the filehandle itself. Here is
320insists on closing the underlying fd itself, no matter what, and doesn't 325what aio_close will try:
321allow modifications to the fd. Unfortunately, it is not clear that you can
322call PerlIO from different threads (actually, its quite clear that this
323won't work in some cases), so while it likely works perfectly with simple
324file handles (such as the ones created by C<aio_open>) it might fail in
325interesting ways for others.
326 326
327Having said that, aio_close tries to clean up the filehandle as much as 327 1. dup()licate the fd
328possible before handing it to an io thread, and generally does work. 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}
329 382
330 383
331=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 384=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
332 385
333=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 386=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
840 893
841 $grp 894 $grp
842 } 895 }
843} 896}
844 897
898=item aio_sync $callback->($status)
899
900Asynchronously call sync and call the callback when finished.
901
845=item aio_fsync $fh, $callback->($status) 902=item aio_fsync $fh, $callback->($status)
846 903
847Asynchronously call fsync on the given filehandle and call the callback 904Asynchronously call fsync on the given filehandle and call the callback
848with the fsync result code. 905with the fsync result code.
849 906

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines