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.111 by root, Mon Aug 13 12:08:13 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 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
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
833 893
834 $grp 894 $grp
835 } 895 }
836} 896}
837 897
898=item aio_sync $callback->($status)
899
900Asynchronously call sync and call the callback when finished.
901
838=item aio_fsync $fh, $callback->($status) 902=item aio_fsync $fh, $callback->($status)
839 903
840Asynchronously call fsync on the given filehandle and call the callback 904Asynchronously call fsync on the given filehandle and call the callback
841with the fsync result code. 905with the fsync result code.
842 906
1206This 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
1207blocks, 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
1208use an C<aio_group> together with a feed callback. 1272use an C<aio_group> together with a feed callback.
1209 1273
1210Sets the maximum number of outstanding requests to C<$nreqs>. If you 1274Sets the maximum number of outstanding requests to C<$nreqs>. If you
1211to 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
1212C<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>)
1213function will block until the limit is no longer exceeded. 1277function will block until the limit is no longer exceeded.
1214 1278
1215The 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
1216number of outstanding requests. 1280number of outstanding requests.
1246but not yet processed by poll_cb). 1310but not yet processed by poll_cb).
1247 1311
1248=back 1312=back
1249 1313
1250=cut 1314=cut
1251
1252# support function to convert a fd into a perl filehandle
1253sub _fd2fh {
1254 return undef if $_[0] < 0;
1255
1256 # try to generate nice filehandles
1257 my $sym = "IO::AIO::fd#$_[0]";
1258 local *$sym;
1259
1260 open *$sym, "+<&=$_[0]" # usually works under any unix
1261 or open *$sym, "<&=$_[0]" # cygwin needs this
1262 or open *$sym, ">&=$_[0]" # or this
1263 or return undef;
1264
1265 *$sym
1266}
1267 1315
1268min_parallel 8; 1316min_parallel 8;
1269 1317
1270END { flush } 1318END { flush }
1271 1319

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines