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.120 by root, Sun Dec 2 21:51:36 2007 UTC vs.
Revision 1.121 by root, Wed Apr 16 16:45:18 2008 UTC

194use strict 'vars'; 194use strict 'vars';
195 195
196use base 'Exporter'; 196use base 'Exporter';
197 197
198BEGIN { 198BEGIN {
199 our $VERSION = '2.6'; 199 our $VERSION = '2.61';
200 200
201 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close 201 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close
202 aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir 202 aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir
203 aio_scandir aio_symlink aio_readlink aio_sync aio_fsync 203 aio_scandir aio_symlink aio_readlink aio_sync aio_fsync
204 aio_fdatasync aio_pathsync aio_readahead 204 aio_fdatasync aio_pathsync aio_readahead
322 322
323Asynchronously close a file and call the callback with the result 323Asynchronously close a file and call the callback with the result
324code. 324code.
325 325
326Unfortunately, you can't do this to perl. Perl I<insists> very strongly on 326Unfortunately, you can't do this to perl. Perl I<insists> very strongly on
327closing the file descriptor associated with the filehandle itself. Here is 327closing the file descriptor associated with the filehandle itself.
328what aio_close will try:
329 328
330 1. dup()licate the fd 329Therefore, C<aio_close> will not close the filehandle - instead it will
331 2. asynchronously close() the duplicated fd 330use dup2 to overwrite the file descriptor with the write-end of a pipe
332 3. dup()licate the fd once more 331(the pipe fd will be created on demand and will be cached).
333 4. let perl close() the filehandle
334 5. asynchronously close the duplicated fd
335 332
336The idea is that the first close() flushes stuff to disk that closing an 333Or in other words: the file descriptor will be closed, but it will not be
337fd will flush, so when perl closes the fd, nothing much will need to be 334free for reuse until the perl filehandle is closed.
338flushed. The second async. close() will then flush stuff to disk that
339closing the last fd to the file will flush.
340
341Just FYI, SuSv3 has this to say on close:
342
343 All outstanding record locks owned by the process on the file
344 associated with the file descriptor shall be removed.
345
346 If fildes refers to a socket, close() shall cause the socket to be
347 destroyed. ... close() shall block for up to the current linger
348 interval until all data is transmitted.
349 [this actually sounds like a specification bug, but who knows]
350
351And at least Linux additionally actually flushes stuff on every close,
352even when the file itself is still open.
353
354Sounds enourmously inefficient and complicated? Yes... please show me how
355to nuke perl's fd out of existence...
356 335
357=cut 336=cut
358
359sub aio_close($;$) {
360 aio_block {
361 my ($fh, $cb) = @_;
362
363 my $pri = aioreq_pri;
364 my $grp = aio_group $cb;
365
366 my $fd = fileno $fh;
367
368 defined $fd or Carp::croak "aio_close called with fd-less filehandle";
369
370 # if the dups fail we will simply get EBADF
371 my $fd2 = _dup $fd;
372 aioreq_pri $pri;
373 add $grp _aio_close $fd2, sub {
374 my $fd2 = _dup $fd;
375 close $fh;
376 aioreq_pri $pri;
377 add $grp _aio_close $fd2, sub {
378 $grp->result ($_[0]);
379 };
380 };
381
382 $grp
383 }
384}
385
386 337
387=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 338=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
388 339
389=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 340=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
390 341

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines