… | |
… | |
6 | |
6 | |
7 | use Linux::AIO; |
7 | use Linux::AIO; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
|
|
11 | This module implements asynchroneous i/o using the means available to |
|
|
12 | linux - clone. It does not hook into the POSIX aio_* functions because |
|
|
13 | linux does not yet support these in the kernel. Instead, a number of |
|
|
14 | threads are started that execute your read/writes and signal their |
|
|
15 | completion. |
|
|
16 | |
11 | =over 4 |
17 | =over 4 |
12 | |
18 | |
13 | =cut |
19 | =cut |
14 | |
20 | |
15 | package Linux::AIO; |
21 | package Linux::AIO; |
16 | |
22 | |
17 | use base 'Exporter'; |
23 | use base 'Exporter'; |
18 | |
24 | |
19 | BEGIN { |
25 | BEGIN { |
20 | $VERSION = 0.001; |
26 | $VERSION = 0.011; |
21 | |
27 | |
22 | @EXPORT = qw(aio_read aio_write); |
28 | @EXPORT = qw(aio_read aio_write aio_open aio_close); |
23 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
29 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
24 | |
30 | |
25 | require XSLoader; |
31 | require XSLoader; |
26 | XSLoader::load Linux::AIO, $VERSION; |
32 | XSLoader::load Linux::AIO, $VERSION; |
27 | } |
33 | } |
28 | |
34 | |
29 | =item Linux::AIO::min_parallel($nthreads) |
35 | =item Linux::AIO::min_parallel($nthreads) |
30 | |
36 | |
31 | Set the minimum number of AIO threads to $nthreads. |
37 | Set the minimum number of AIO threads to $nthreads. You I<have> to call |
32 | |
38 | this function with a positive number at leats once, otherwise no threads |
33 | =cut |
39 | will be started and you aio-operations will seem to hang. |
34 | |
|
|
35 | =item aio_read($fh,$offset,$length, $data,$dataoffset,$callback) |
|
|
36 | aio_write($fh,$offset,$length, $data,$dataoffset,$callback) |
|
|
37 | |
|
|
38 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
|
|
39 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
|
|
40 | callback without the actual number of bytes read (or undef on error). |
|
|
41 | |
40 | |
42 | =item $fileno = Linux::AIO::poll_fileno |
41 | =item $fileno = Linux::AIO::poll_fileno |
43 | |
42 | |
44 | Return the request result pipe filehandle. This filehandle must be polled |
43 | Return the request result pipe filehandle. This filehandle must be polled |
45 | for reading. If the pipe becomes readable you have to call C<poll_cb>. |
44 | for reading. If the pipe becomes readable you have to call C<poll_cb>. |
… | |
… | |
47 | =item Linux::AIO::poll_cb |
46 | =item Linux::AIO::poll_cb |
48 | |
47 | |
49 | Process all outstanding events on the result pipe. You have to call this |
48 | Process all outstanding events on the result pipe. You have to call this |
50 | regularly. Returns the number of events processed. |
49 | regularly. Returns the number of events processed. |
51 | |
50 | |
|
|
51 | You can use Event to multiplex, e.g.: |
|
|
52 | |
|
|
53 | Event->io(fd => Linux::AIO::poll_fileno, |
|
|
54 | poll => 'r', async => 1, |
|
|
55 | cb => \&Linux::AIO::poll_cb ); |
|
|
56 | |
|
|
57 | |
52 | =item Linux::AIO::nreqs |
58 | =item Linux::AIO::nreqs |
53 | |
59 | |
54 | Returns the number of requests currently outstanding. |
60 | Returns the number of requests currently outstanding. |
|
|
61 | |
|
|
62 | =item aio_read($fh,$offset,$length, $data,$dataoffset,$callback) |
|
|
63 | |
|
|
64 | =item aio_write($fh,$offset,$length, $data,$dataoffset,$callback) |
|
|
65 | |
|
|
66 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
|
|
67 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
|
|
68 | callback without the actual number of bytes read (or undef on error). |
|
|
69 | |
|
|
70 | =item aio_open($pathname, $flags, $mode, $callback) |
|
|
71 | |
|
|
72 | Asynchronously open or create a file and call the callback with the |
|
|
73 | filedescriptor. |
|
|
74 | |
|
|
75 | =item aio_close($fh, $callback) |
|
|
76 | |
|
|
77 | Asynchronously close a file and call the callback with the result code. |
55 | |
78 | |
56 | =cut |
79 | =cut |
57 | |
80 | |
58 | END { |
81 | END { |
59 | max_parallel 0; |
82 | max_parallel 0; |
… | |
… | |
65 | |
88 | |
66 | =head1 BUGS |
89 | =head1 BUGS |
67 | |
90 | |
68 | This module has not yet been extensively tested. Watch out! |
91 | This module has not yet been extensively tested. Watch out! |
69 | |
92 | |
70 | This module does not use the aio_* posix functions because a) linux does |
93 | - aio_stat/lstat are seriously missing here. |
71 | not have aio, b) the existing aio_* functions use pthreads, which are too |
|
|
72 | buggy for my usage. |
|
|
73 | |
94 | |
74 | =head1 SEE ALSO |
95 | =head1 SEE ALSO |
75 | |
96 | |
76 | L<Coro>. |
97 | L<Coro>. |
77 | |
98 | |