ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-AIO/AIO.pm
(Generate patch)

Comparing Linux-AIO/AIO.pm (file contents):
Revision 1.3 by root, Tue Aug 14 23:25:39 2001 UTC vs.
Revision 1.23 by root, Wed Jun 29 15:22:36 2005 UTC

6 6
7 use Linux::AIO; 7 use Linux::AIO;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module implements asynchroneous i/o using the means available to 11This module implements asynchronous i/o using the means available to linux
12linux - clone. It does not hook into the POSIX aio_* functions because 12- clone. It does not hook into the POSIX aio_* functions because linux
13linux does not yet support these in the kernel. Instead, a number of 13does not yet support these in the kernel (and even if, it would only allow
14threads are started that execute your read/writes and signal their 14aio_read and write, not open and stat).
15completion. 15
16Instead, in this module a number of (non-posix) threads are started that
17execute your read/writes and signal their completion. You don't need
18thread support in your libc or perl, and the threads created by this
19module will not be visible to the pthreads library.
20
21NOTICE: the threads created by this module will automatically be killed
22when the thread calling min_parallel exits. Make sure you only ever call
23min_parallel from the same thread that loaded this module.
24
25Although the module will work with threads, it is not reentrant, so use
26appropriate locking yourself.
16 27
17=over 4 28=over 4
18 29
19=cut 30=cut
20 31
21package Linux::AIO; 32package Linux::AIO;
22 33
23use base 'Exporter'; 34use base 'Exporter';
24 35
25BEGIN { 36BEGIN {
26 $VERSION = 0.001; 37 $VERSION = 1.41;
27 38
28 @EXPORT = qw(aio_read aio_write); 39 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink);
29 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); 40 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs);
30 41
31 require XSLoader; 42 require XSLoader;
32 XSLoader::load Linux::AIO, $VERSION; 43 XSLoader::load Linux::AIO, $VERSION;
33} 44}
34 45
35=item Linux::AIO::min_parallel($nthreads) 46=item Linux::AIO::min_parallel $nthreads
36 47
37Set the minimum number of AIO threads to $nthreads. You I<have> to call 48Set the minimum number of AIO threads to C<$nthreads>. The default is
38this function with a positive number at leats once, otherwise no threads 49C<1>, which means a single asynchronous operation can be done at one time
39will be started and you aio-operations will seem to hang. 50(the number of outstanding operations, however, is unlimited).
40 51
41=cut 52It is recommended to keep the number of threads low, as many linux
42 53kernel versions will scale negatively with the number of threads (higher
43=item aio_read($fh,$offset,$length, $data,$dataoffset,$callback) 54parallelity => MUCH higher latency).
44aio_write($fh,$offset,$length, $data,$dataoffset,$callback)
45
46Reads or writes C<length> bytes from the specified C<fh> and C<offset>
47into the scalar given by C<data> and offset C<dataoffset> and calls the
48callback without the actual number of bytes read (or undef on error).
49 55
50=item $fileno = Linux::AIO::poll_fileno 56=item $fileno = Linux::AIO::poll_fileno
51 57
52Return the request result pipe filehandle. This filehandle must be polled 58Return the I<request result pipe filehandle>. This filehandle must be
53for reading. If the pipe becomes readable you have to call C<poll_cb>. 59polled for reading by some mechanism outside this module (e.g. Event
60or select, see below). If the pipe becomes readable you have to call
61C<poll_cb> to check the results.
54 62
55=item Linux::AIO::poll_cb 63=item Linux::AIO::poll_cb
56 64
57Process all outstanding events on the result pipe. You have to call this 65Process all outstanding events on the result pipe. You have to call this
58regularly. Returns the number of events processed. 66regularly. Returns the number of events processed. Returns immediately
67when no events are outstanding.
59 68
60You can use Event to multiplex, e.g.: 69You can use Event to multiplex, e.g.:
61 70
62 Event->io(fd => Linux::AIO::poll_fileno, 71 Event->io (fd => Linux::AIO::poll_fileno,
63 poll => 'r', async => 1, 72 poll => 'r', async => 1,
64 cb => \&Linux::AIO::poll_cb ); 73 cb => \&Linux::AIO::poll_cb );
65
66 74
67=item Linux::AIO::nreqs 75=item Linux::AIO::nreqs
68 76
69Returns the number of requests currently outstanding. 77Returns the number of requests currently outstanding.
70 78
79=item aio_open $pathname, $flags, $mode, $callback
80
81Asynchronously open or create a file and call the callback with the
82filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this
83might change in the future).
84
85=item aio_close $fh, $callback
86
87Asynchronously close a file and call the callback with the result code.
88
89=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback
90
91=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback
92
93Reads or writes C<length> bytes from the specified C<fh> and C<offset>
94into the scalar given by C<data> and offset C<dataoffset> and calls the
95callback without the actual number of bytes read (or C<undef> on error).
96
97=item aio_stat $fh_or_path, $callback
98
99=item aio_lstat $fh, $callback
100
101Works like perl's C<stat> or C<lstat> in void context. The callback will
102be called after the stat and the results will be available using C<stat _>
103or C<-s _> etc...
104
105Currently, the stats are always 64-bit-stats, i.e. instead of returning an
106error when stat'ing a large file, the results will be silently truncated
107unless perl itself is compiled with large file support.
108
109=item aio_unlink $pathname, $callback
110
111Asynchronously unlink a file.
112
71=cut 113=cut
114
115min_parallel 1;
72 116
73END { 117END {
74 max_parallel 0; 118 max_parallel 0;
75} 119}
76 120
78 122
79=back 123=back
80 124
81=head1 BUGS 125=head1 BUGS
82 126
83This module has not yet been extensively tested. Watch out! 127This module has been extensively tested in a large and very busy webserver
128for many years now.
129
130 - aio_open gives a fd, but all other functions expect a perl filehandle.
84 131
85=head1 SEE ALSO 132=head1 SEE ALSO
86 133
87L<Coro>. 134L<Coro>.
88 135
89=head1 AUTHOR 136=head1 AUTHOR
90 137
91 Marc Lehmann <pcg@goof.com> 138 Marc Lehmann <schmorp@schmorp.de>
92 http://www.goof.com/pcg/marc/ 139 http://home.schmorp.de/
93 140
94=cut 141=cut
95 142

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines