… | |
… | |
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 | |
186 | package IO::AIO; |
189 | package IO::AIO; |
187 | |
190 | |
|
|
191 | use Carp (); |
|
|
192 | |
188 | no warnings; |
193 | no warnings; |
189 | use strict 'vars'; |
194 | use strict 'vars'; |
190 | |
195 | |
191 | use base 'Exporter'; |
196 | use base 'Exporter'; |
192 | |
197 | |
193 | BEGIN { |
198 | BEGIN { |
194 | our $VERSION = '2.4'; |
199 | our $VERSION = '2.51'; |
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_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 |
… | |
… | |
311 | |
316 | |
312 | |
317 | |
313 | =item aio_close $fh, $callback->($status) |
318 | =item aio_close $fh, $callback->($status) |
314 | |
319 | |
315 | Asynchronously close a file and call the callback with the result |
320 | Asynchronously close a file and call the callback with the result |
316 | code. I<WARNING:> although accepted, you should not pass in a perl |
321 | code. |
317 | filehandle here, as perl will likely close the file descriptor another |
|
|
318 | time when the filehandle is destroyed. Normally, you can safely call perls |
|
|
319 | C<close> or just let filehandles go out of scope. |
|
|
320 | |
322 | |
321 | This is supposed to be a bug in the API, so that might change. It's |
323 | Unfortunately, you can't do this to perl. Perl I<insists> very strongly on |
322 | therefore best to avoid this function. |
324 | closing the file descriptor associated with the filehandle itself. Here is |
|
|
325 | what 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 | |
|
|
333 | The idea is that the first close() flushes stuff to disk that closing an |
|
|
334 | fd will flush, so when perl closes the fd, nothing much will need to be |
|
|
335 | flushed. The second async. close() will then flush stuff to disk that |
|
|
336 | closing the last fd to the file will flush. |
|
|
337 | |
|
|
338 | Just 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 | |
|
|
348 | And at least Linux additionally actually flushes stuff on every close, |
|
|
349 | even when the file itself is still open. |
|
|
350 | |
|
|
351 | Sounds enourmously inefficient and complicated? Yes... please show me how |
|
|
352 | to nuke perl's fd out of existence... |
|
|
353 | |
|
|
354 | =cut |
|
|
355 | |
|
|
356 | sub 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) |
… | |
… | |
329 | Reads or writes C<$length> bytes from the specified C<$fh> and C<$offset> |
388 | Reads or writes C<$length> bytes from the specified C<$fh> and C<$offset> |
330 | into the scalar given by C<$data> and offset C<$dataoffset> and calls the |
389 | into the scalar given by C<$data> and offset C<$dataoffset> and calls the |
331 | callback without the actual number of bytes read (or -1 on error, just |
390 | callback without the actual number of bytes read (or -1 on error, just |
332 | like the syscall). |
391 | like the syscall). |
333 | |
392 | |
334 | If C<$offset> is undefined, then the current file offset will be used (and |
393 | If C<$offset> is undefined, then the current file descriptor offset will |
335 | updated), otherwise the file offset will not be changed by these calls. |
394 | be used (and updated), otherwise the file descriptor offset will not be |
|
|
395 | changed by these calls. |
336 | |
396 | |
337 | If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>. |
397 | If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>. |
338 | |
398 | |
339 | If C<$dataoffset> is less than zero, it will be counted from the end of |
399 | If C<$dataoffset> is less than zero, it will be counted from the end of |
340 | C<$data>. |
400 | C<$data>. |
341 | |
401 | |
342 | The C<$data> scalar I<MUST NOT> be modified in any way while the request |
402 | The C<$data> scalar I<MUST NOT> be modified in any way while the request |
… | |
… | |
1206 | This is a very bad function to use in interactive programs because it |
1266 | This is a very bad function to use in interactive programs because it |
1207 | blocks, and a bad way to reduce concurrency because it is inexact: Better |
1267 | blocks, and a bad way to reduce concurrency because it is inexact: Better |
1208 | use an C<aio_group> together with a feed callback. |
1268 | use an C<aio_group> together with a feed callback. |
1209 | |
1269 | |
1210 | Sets the maximum number of outstanding requests to C<$nreqs>. If you |
1270 | Sets the maximum number of outstanding requests to C<$nreqs>. If you |
1211 | to queue up more than this number of requests, the next call to the |
1271 | do queue up more than this number of requests, the next call to the |
1212 | C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) |
1272 | C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) |
1213 | function will block until the limit is no longer exceeded. |
1273 | function will block until the limit is no longer exceeded. |
1214 | |
1274 | |
1215 | The default value is very large, so there is no practical limit on the |
1275 | The default value is very large, so there is no practical limit on the |
1216 | number of outstanding requests. |
1276 | number of outstanding requests. |
… | |
… | |
1246 | but not yet processed by poll_cb). |
1306 | but not yet processed by poll_cb). |
1247 | |
1307 | |
1248 | =back |
1308 | =back |
1249 | |
1309 | |
1250 | =cut |
1310 | =cut |
1251 | |
|
|
1252 | # support function to convert a fd into a perl filehandle |
|
|
1253 | sub _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 | |
1311 | |
1268 | min_parallel 8; |
1312 | min_parallel 8; |
1269 | |
1313 | |
1270 | END { flush } |
1314 | END { flush } |
1271 | |
1315 | |