… | |
… | |
194 | use strict 'vars'; |
194 | use strict 'vars'; |
195 | |
195 | |
196 | use base 'Exporter'; |
196 | use base 'Exporter'; |
197 | |
197 | |
198 | BEGIN { |
198 | BEGIN { |
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 | |
323 | Asynchronously close a file and call the callback with the result |
323 | Asynchronously close a file and call the callback with the result |
324 | code. |
324 | code. |
325 | |
325 | |
326 | Unfortunately, you can't do this to perl. Perl I<insists> very strongly on |
326 | Unfortunately, you can't do this to perl. Perl I<insists> very strongly on |
327 | closing the file descriptor associated with the filehandle itself. Here is |
327 | closing the file descriptor associated with the filehandle itself. |
328 | what aio_close will try: |
|
|
329 | |
328 | |
330 | 1. dup()licate the fd |
329 | Therefore, C<aio_close> will not close the filehandle - instead it will |
331 | 2. asynchronously close() the duplicated fd |
330 | use 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 | |
336 | The idea is that the first close() flushes stuff to disk that closing an |
333 | Or in other words: the file descriptor will be closed, but it will not be |
337 | fd will flush, so when perl closes the fd, nothing much will need to be |
334 | free for reuse until the perl filehandle is closed. |
338 | flushed. The second async. close() will then flush stuff to disk that |
|
|
339 | closing the last fd to the file will flush. |
|
|
340 | |
|
|
341 | Just 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 | |
|
|
351 | And at least Linux additionally actually flushes stuff on every close, |
|
|
352 | even when the file itself is still open. |
|
|
353 | |
|
|
354 | Sounds enourmously inefficient and complicated? Yes... please show me how |
|
|
355 | to nuke perl's fd out of existence... |
|
|
356 | |
335 | |
357 | =cut |
336 | =cut |
358 | |
|
|
359 | sub 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 | |