… | |
… | |
189 | use strict 'vars'; |
189 | use strict 'vars'; |
190 | |
190 | |
191 | use base 'Exporter'; |
191 | use base 'Exporter'; |
192 | |
192 | |
193 | BEGIN { |
193 | BEGIN { |
194 | our $VERSION = '2.4'; |
194 | our $VERSION = '2.5'; |
195 | |
195 | |
196 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
196 | 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 |
197 | 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 |
198 | 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 |
199 | aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir |
200 | aio_chown aio_chmod aio_utime); |
200 | aio_chown aio_chmod aio_utime aio_truncate); |
201 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block)); |
201 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block)); |
202 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
202 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
203 | min_parallel max_parallel max_idle |
203 | min_parallel max_parallel max_idle |
204 | nreqs nready npending nthreads |
204 | nreqs nready npending nthreads |
205 | max_poll_time max_poll_reqs); |
205 | max_poll_time max_poll_reqs); |
… | |
… | |
311 | |
311 | |
312 | |
312 | |
313 | =item aio_close $fh, $callback->($status) |
313 | =item aio_close $fh, $callback->($status) |
314 | |
314 | |
315 | Asynchronously close a file and call the callback with the result |
315 | Asynchronously close a file and call the callback with the result |
316 | code. I<WARNING:> although accepted, you should not pass in a perl |
316 | 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 | |
317 | |
321 | This is supposed to be a bug in the API, so that might change. It's |
318 | Unlike the other functions operating on files, this function uses the |
322 | therefore best to avoid this function. |
319 | PerlIO layer to close the filehandle. The reason is that the PerlIO API |
|
|
320 | insists on closing the underlying fd itself, no matter what, and doesn't |
|
|
321 | allow modifications to the fd. Unfortunately, it is not clear that you can |
|
|
322 | call PerlIO from different threads (actually, its quite clear that this |
|
|
323 | won't work in some cases), so while it likely works perfectly with simple |
|
|
324 | file handles (such as the ones created by C<aio_open>) it might fail in |
|
|
325 | interesting ways for others. |
|
|
326 | |
|
|
327 | Having said that, aio_close tries to clean up the filehandle as much as |
|
|
328 | possible before handing it to an io thread, and generally does work. |
323 | |
329 | |
324 | |
330 | |
325 | =item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) |
331 | =item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) |
326 | |
332 | |
327 | =item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) |
333 | =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> |
335 | 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 |
336 | 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 |
337 | callback without the actual number of bytes read (or -1 on error, just |
332 | like the syscall). |
338 | like the syscall). |
333 | |
339 | |
334 | If C<$offset> is undefined, then the current file offset will be used (and |
340 | 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. |
341 | be used (and updated), otherwise the file descriptor offset will not be |
|
|
342 | changed by these calls. |
336 | |
343 | |
337 | If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>. |
344 | If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>. |
338 | |
345 | |
339 | If C<$dataoffset> is less than zero, it will be counted from the end of |
346 | If C<$dataoffset> is less than zero, it will be counted from the end of |
340 | C<$data>. |
347 | C<$data>. |
341 | |
348 | |
342 | The C<$data> scalar I<MUST NOT> be modified in any way while the request |
349 | The C<$data> scalar I<MUST NOT> be modified in any way while the request |
… | |
… | |
441 | |
448 | |
442 | # same as "chown root path" in the shell: |
449 | # same as "chown root path" in the shell: |
443 | aio_chown "path", 0, -1; |
450 | aio_chown "path", 0, -1; |
444 | # same as above: |
451 | # same as above: |
445 | aio_chown "path", 0, undef; |
452 | aio_chown "path", 0, undef; |
|
|
453 | |
|
|
454 | |
|
|
455 | =item aio_truncate $fh_or_path, $offset, $callback->($status) |
|
|
456 | |
|
|
457 | Works like truncate(2) or ftruncate(2). |
446 | |
458 | |
447 | |
459 | |
448 | =item aio_chmod $fh_or_path, $mode, $callback->($status) |
460 | =item aio_chmod $fh_or_path, $mode, $callback->($status) |
449 | |
461 | |
450 | Works like perl's C<chmod> function. |
462 | Works like perl's C<chmod> function. |
… | |
… | |
1201 | This is a very bad function to use in interactive programs because it |
1213 | This is a very bad function to use in interactive programs because it |
1202 | blocks, and a bad way to reduce concurrency because it is inexact: Better |
1214 | blocks, and a bad way to reduce concurrency because it is inexact: Better |
1203 | use an C<aio_group> together with a feed callback. |
1215 | use an C<aio_group> together with a feed callback. |
1204 | |
1216 | |
1205 | Sets the maximum number of outstanding requests to C<$nreqs>. If you |
1217 | Sets the maximum number of outstanding requests to C<$nreqs>. If you |
1206 | to queue up more than this number of requests, the next call to the |
1218 | do queue up more than this number of requests, the next call to the |
1207 | C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) |
1219 | C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) |
1208 | function will block until the limit is no longer exceeded. |
1220 | function will block until the limit is no longer exceeded. |
1209 | |
1221 | |
1210 | The default value is very large, so there is no practical limit on the |
1222 | The default value is very large, so there is no practical limit on the |
1211 | number of outstanding requests. |
1223 | number of outstanding requests. |
… | |
… | |
1241 | but not yet processed by poll_cb). |
1253 | but not yet processed by poll_cb). |
1242 | |
1254 | |
1243 | =back |
1255 | =back |
1244 | |
1256 | |
1245 | =cut |
1257 | =cut |
1246 | |
|
|
1247 | # support function to convert a fd into a perl filehandle |
|
|
1248 | sub _fd2fh { |
|
|
1249 | return undef if $_[0] < 0; |
|
|
1250 | |
|
|
1251 | # try to generate nice filehandles |
|
|
1252 | my $sym = "IO::AIO::fd#$_[0]"; |
|
|
1253 | local *$sym; |
|
|
1254 | |
|
|
1255 | open *$sym, "+<&=$_[0]" # usually works under any unix |
|
|
1256 | or open *$sym, "<&=$_[0]" # cygwin needs this |
|
|
1257 | or open *$sym, ">&=$_[0]" # or this |
|
|
1258 | or return undef; |
|
|
1259 | |
|
|
1260 | *$sym |
|
|
1261 | } |
|
|
1262 | |
1258 | |
1263 | min_parallel 8; |
1259 | min_parallel 8; |
1264 | |
1260 | |
1265 | END { flush } |
1261 | END { flush } |
1266 | |
1262 | |
… | |
… | |
1290 | bytes of memory. In addition, stat requests need a stat buffer (possibly |
1286 | bytes of memory. In addition, stat requests need a stat buffer (possibly |
1291 | a few hundred bytes), readdir requires a result buffer and so on. Perl |
1287 | a few hundred bytes), readdir requires a result buffer and so on. Perl |
1292 | scalars and other data passed into aio requests will also be locked and |
1288 | scalars and other data passed into aio requests will also be locked and |
1293 | will consume memory till the request has entered the done state. |
1289 | will consume memory till the request has entered the done state. |
1294 | |
1290 | |
1295 | This is now awfully much, so queuing lots of requests is not usually a |
1291 | This is not awfully much, so queuing lots of requests is not usually a |
1296 | problem. |
1292 | problem. |
1297 | |
1293 | |
1298 | Per-thread usage: |
1294 | Per-thread usage: |
1299 | |
1295 | |
1300 | In the execution phase, some aio requests require more memory for |
1296 | In the execution phase, some aio requests require more memory for |