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

Comparing IO-AIO/AIO.pm (file contents):
Revision 1.1 by root, Sun Jul 10 17:07:44 2005 UTC vs.
Revision 1.2 by root, Sun Jul 10 18:16:49 2005 UTC

7 use IO::AIO; 7 use IO::AIO;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module implements asynchronous I/O using whatever means your 11This module implements asynchronous I/O using whatever means your
12operating system supports. Currently, it falls back to Linux::AIO if that 12operating system supports.
13module is available, or uses pthreads to emulato aio functionality.
14 13
15Currently, in this module a number of threads are started that execute 14Currently, a number of threads are started that execute your read/writes
16your read/writes and signal their completion. You don't need thread 15and signal their completion. You don't need thread support in your libc or
17support in your libc or perl, and the threads created by this module will 16perl, and the threads created by this module will not be visible to the
18not be visible to the pthreads library. 17pthreads library. In the future, this module might make use of the native
18aio functions available on many operating systems. However, they are often
19not well-supported (Linux doesn't allow them on normal files currently,
20for example), and they would only support aio_read and aio_write, so the
21remaining functionality would have to be implemented using threads anyway.
19 22
20Although the module will work with in the presence of other threads, it is 23Although the module will work with in the presence of other threads, it is
21not reentrant, so use appropriate locking yourself. 24currently not reentrant, so use appropriate locking yourself.
22 25
23=head2 API NOTES 26=head2 API NOTES
24 27
25All the C<aio_*> calls are more or less thin wrappers around the syscall 28All the C<aio_*> calls are more or less thin wrappers around the syscall
26with the same name (sans C<aio_>). The arguments are similar or identical, 29with the same name (sans C<aio_>). The arguments are similar or identical,
43 46
44package IO::AIO; 47package IO::AIO;
45 48
46use base 'Exporter'; 49use base 'Exporter';
47 50
51use Fcntl ();
52
48BEGIN { 53BEGIN {
49 $VERSION = 0.1; 54 $VERSION = 0.1;
50 55
51 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink 56 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink
52 aio_fsync aio_fdatasync aio_readahead); 57 aio_fsync aio_fdatasync aio_readahead);
118 IO::AIO::poll_wait, IO::AIO::poll_cb 123 IO::AIO::poll_wait, IO::AIO::poll_cb
119 while IO::AIO::nreqs; 124 while IO::AIO::nreqs;
120 125
121=item aio_open $pathname, $flags, $mode, $callback 126=item aio_open $pathname, $flags, $mode, $callback
122 127
123Asynchronously open or create a file and call the callback with the 128Asynchronously open or create a file and call the callback with a newly
124filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this 129created filehandle for the file.
125might change in the future).
126 130
127The pathname passed to C<aio_open> must be absolute. See API NOTES, above, 131The pathname passed to C<aio_open> must be absolute. See API NOTES, above,
128for an explanation. 132for an explanation.
129 133
130The C<$mode> argument is a bitmask. See the C<Fcntl> module for a 134The C<$mode> argument is a bitmask. See the C<Fcntl> module for a
131list. They are the same as used in C<sysopen>. 135list. They are the same as used in C<sysopen>.
132 136
133Example: 137Example:
134 138
135 aio_open "/etc/passwd", O_RDONLY, 0, sub { 139 aio_open "/etc/passwd", O_RDONLY, 0, sub {
136 if ($_[0] >= 0) { 140 if ($_[0]) {
137 open my $fh, "<&$_[0]"; # create a copy for perl
138 aio_close $_[0], sub { }; # close the aio handle
139 print "open successful, fh is $fh\n"; 141 print "open successful, fh is $_[0]\n";
140 ... 142 ...
141 } else { 143 } else {
142 die "open failed: $!\n"; 144 die "open failed: $!\n";
143 } 145 }
144 }; 146 };
145 147
146=item aio_close $fh, $callback 148=item aio_close $fh, $callback
147 149
148Asynchronously close a file and call the callback with the result code. 150Asynchronously close a file and call the callback with the result
151code. I<WARNING:> although accepted, you should not pass in a perl
152filehandle here, as perl will likely close the file descriptor itself when
153the filehandle is destroyed. Normally, you can safely call perls C<close>
154or just let filehandles go out of scope.
149 155
150=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback 156=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback
151 157
152=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback 158=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback
153 159
216Asynchronously call fdatasync on the given filehandle and call the 222Asynchronously call fdatasync on the given filehandle and call the
217callback with the fdatasync result code. 223callback with the fdatasync result code.
218 224
219=cut 225=cut
220 226
227# support function to convert a fd into a perl filehandle
228sub _fd2fh {
229 return undef if $_[0] < 0;
230
231 # try to be perl5.6-compatible
232 local *AIO_FH;
233 open AIO_FH, "+<&=$_[0]"
234 or return undef;
235
236 *AIO_FH
237}
238
221min_parallel 4; 239min_parallel 4;
222 240
223END { 241END {
224 max_parallel 0; 242 max_parallel 0;
225} 243}
228 246
229=back 247=back
230 248
231=head1 BUGS 249=head1 BUGS
232 250
233 - aio_open gives a fd, but all other functions expect a perl filehandle. 251 - could be optimized to use more semaphores instead of filehandles.
234 252
235=head1 SEE ALSO 253=head1 SEE ALSO
236 254
237L<Coro>, L<Linux::AIO>. 255L<Coro>, L<Linux::AIO>.
238 256

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines