… | |
… | |
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 | |
|
|
23 | use base 'Exporter'; |
|
|
24 | |
17 | BEGIN { |
25 | BEGIN { |
18 | $VERSION = 0.001; |
26 | $VERSION = 0.111; |
|
|
27 | |
|
|
28 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat); |
|
|
29 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
19 | |
30 | |
20 | require XSLoader; |
31 | require XSLoader; |
21 | XSLoader::load Linux::AIO, $VERSION; |
32 | XSLoader::load Linux::AIO, $VERSION; |
22 | } |
33 | } |
23 | |
34 | |
24 | use base 'Exporter'; |
|
|
25 | |
|
|
26 | =item Linux::AIO::min_parallel($nthreads) |
35 | =item Linux::AIO::min_parallel($nthreads) |
27 | |
36 | |
28 | Set the minimum number of AIO threads to $nthreads. |
37 | Set the minimum number of AIO threads to $nthreads. You I<have> to call |
|
|
38 | this function with a positive number at leats once, otherwise no threads |
|
|
39 | will be started and you aio-operations will seem to hang. |
29 | |
40 | |
30 | =cut |
41 | =item $fileno = Linux::AIO::poll_fileno |
31 | |
42 | |
|
|
43 | Return the request result pipe filehandle. This filehandle must be polled |
|
|
44 | for reading. If the pipe becomes readable you have to call C<poll_cb>. |
|
|
45 | |
|
|
46 | =item Linux::AIO::poll_cb |
|
|
47 | |
|
|
48 | Process all outstanding events on the result pipe. You have to call this |
|
|
49 | regularly. Returns the number of events processed. |
|
|
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 | |
|
|
58 | =item Linux::AIO::nreqs |
|
|
59 | |
|
|
60 | Returns the number of requests currently outstanding. |
|
|
61 | |
|
|
62 | =item aio_open($pathname, $flags, $mode, $callback) |
|
|
63 | |
|
|
64 | Asynchronously open or create a file and call the callback with the |
|
|
65 | filedescriptor. |
|
|
66 | |
|
|
67 | =item aio_close($fh, $callback) |
|
|
68 | |
|
|
69 | Asynchronously close a file and call the callback with the result code. |
|
|
70 | |
32 | =item Linux::AIO::read($fh,$offset,$length, $data,$dataoffset,$callback) |
71 | =item aio_read($fh,$offset,$length, $data,$dataoffset,$callback) |
|
|
72 | |
33 | Linux::AIO::write($fh,$offset,$length, $data,$dataoffset,$callback) |
73 | =item aio_write($fh,$offset,$length, $data,$dataoffset,$callback) |
34 | |
74 | |
35 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
75 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
36 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
76 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
37 | callback without the actual number of bytes read (or undef on error). |
77 | callback without the actual number of bytes read (or undef on error). |
38 | |
78 | |
|
|
79 | =item aio_stat($fh_or_path,$callback) |
|
|
80 | |
|
|
81 | =item aio_lstat($fh,$callback) |
|
|
82 | |
|
|
83 | Works like perl's C<stat> or C<lstat> in void context, i.e. the callback |
|
|
84 | will be called after the stat and the results will be available using |
|
|
85 | C<stat _> or C<-s _> etc... |
|
|
86 | |
|
|
87 | Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
|
|
88 | error when stat'ing a large file, the results will be silently truncated |
|
|
89 | unless perl itself is compiled with large file support. |
|
|
90 | |
39 | =cut |
91 | =cut |
|
|
92 | |
|
|
93 | END { |
|
|
94 | max_parallel 0; |
|
|
95 | } |
40 | |
96 | |
41 | 1; |
97 | 1; |
42 | |
98 | |
43 | =back |
99 | =back |
44 | |
100 | |
45 | =head1 BUGS |
101 | =head1 BUGS |
46 | |
102 | |
47 | This module has not yet been extensively tested. Watch out! |
103 | This module has not yet been extensively tested. Watch out! |
48 | |
104 | |
49 | This module does not use the aio_* posix functions because a) linux does |
105 | - perl-threads/fork interaction poorly tested. |
50 | not have aio, b) the existing aio_* functions use pthreads, which are too |
106 | - aio_open gives a fd, but all other functions expect a filehandle. |
51 | buggy for my usage. |
|
|
52 | |
107 | |
53 | =head1 SEE ALSO |
108 | =head1 SEE ALSO |
54 | |
109 | |
55 | L<Coro>. |
110 | L<Coro>. |
56 | |
111 | |