ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/AIO.pm
Revision: 1.7
Committed: Mon Nov 20 20:14:02 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Changes since 1.6: +5 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro::AIO - truly asynchronous file and directrory I/O
4
5 =head1 SYNOPSIS
6
7 use Coro::AIO;
8
9 # can now use any of the aio requests your IO::AIO module supports.
10
11 # read 1MB of /etc/passwd, without blocking other coroutines
12 my $fh = aio_open "/etc/passwd", O_RDONLY, 0
13 or die "/etc/passwd: $!";
14 aio_read $fh, 0, 1_000_000, my $buf, 0
15 or die "aio_read: $!";
16 aio_close $fh;
17
18 =head1 DESCRIPTION
19
20 This module implements a thin wrapper around L<IO::AIO|IO::AIO>. All of
21 the functions that expect a callback are being wrapped by this module.
22
23 The API is exactly the same as that of the corresponding IO::AIO routines,
24 except that you have to specify I<all> arguments I<except> the callback
25 argument. Instead the routines return the values normally passed to the
26 callback. Everything else, including C<$!> and perls stat cache, are set
27 as expected after these functions return.
28
29 You can mix calls to C<IO::AIO> functions with calls to this module. You
30 I<must not>, however, call these routines from within IO::AIO callbacks,
31 as this causes a deadlock. Start a coro inside the callback instead.
32
33 You also can, but do not need to, call C<IO::AIO::poll_cb>, as this
34 module automatically installs an event watcher for the C<IO::AIO> file
35 descriptor. It uses the L<AnyEvent|AnyEvent> module for this, so please
36 refer to its documentation on how it selects an appropriate Event module.
37
38 All other functions exported by default by IO::AIO (e.g. C<aioreq_pri>)
39 will be exported by default by Coro::AIO, too.
40
41 Functions that can be optionally imported from IO::AIO can be imported
42 from Coro::AIO or can be called directly, e.g. C<Coro::AIO::nreqs>.
43
44 For your convienience, here are the changed function signatures for most
45 of the requests, for documentation of these functions please have a look
46 at L<IO::AIO|the IO::AIO manual>.
47
48 The AnyEvent watcher can be disabled by executing C<undef
49 $Coro::AIO::WATCHER>. Please notify the author of when and why you think
50 this was necessary.
51
52 =over 4
53
54 =cut
55
56 package Coro::AIO;
57
58 use strict 'subs';
59
60 use Coro ();
61 use AnyEvent;
62 use IO::AIO ();
63
64 use base Exporter::;
65
66 our $FH; open $FH, "<&=" . IO::AIO::poll_fileno;
67 our $WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => \&IO::AIO::poll_cb);
68
69 our @EXPORT = @IO::AIO::EXPORT;
70 our @EXPORT_OK = @IO::AIO::EXPORT_OK;
71 our $AUTOLOAD;
72
73 {
74 my @reqs = @IO::AIO::AIO_REQ ? @IO::AIO::AIO_REQ : @EXPORT;
75 my %reqs = map +($_ => 1), @reqs;
76
77 eval
78 join "",
79 map "sub $_(" . (prototype "IO::AIO::$_") . ");",
80 grep !$reqs{$_},
81 @EXPORT, @EXPORT_OK;
82
83 for my $sub (@reqs) {
84 push @EXPORT, $sub;
85
86 my $iosub = "IO::AIO::$sub";
87 my $proto = prototype $iosub;
88
89 $proto =~ s/;?\$$// or die "$iosub: unable to remove callback slot from prototype";
90
91 eval qq{
92 #line 1 "Coro::AIO::$sub($proto)"
93 sub $sub($proto) {
94 my \$current = \$Coro::current;
95 my \$state;
96 my \@res;
97
98 push \@_, sub {
99 \$stat = Coro::_aio_get_state;
100 \@res = \@_;
101 \$current->ready;
102 undef \$current;
103 };
104
105 &$iosub;
106
107 Coro::schedule while !\$state;
108
109 Coro::_aio_set_state \$state;
110 wantarray ? \@res : \$res[0]
111 }
112 };
113 die if $@;
114 }
115 }
116
117 sub AUTOLOAD {
118 (my $func = $AUTOLOAD) =~ s/^.*:://;
119 *$AUTOLOAD = \&{"IO::AIO::$func"};
120 goto &$AUTOLOAD;
121 }
122
123 =item $fh = aio_open $pathname, $flags, $mode
124
125 =item $status = aio_close $fh
126
127 =item $retval = aio_read $fh,$offset,$length, $data,$dataoffset
128
129 =item $retval = aio_write $fh,$offset,$length, $data,$dataoffset
130
131 =item $retval = aio_sendfile $out_fh, $in_fh, $in_offset, $length
132
133 =item $retval = aio_readahead $fh,$offset,$length
134
135 =item $status = aio_stat $fh_or_path
136
137 =item $status = aio_lstat $fh
138
139 =item $status = aio_unlink $pathname
140
141 =item $status = aio_rmdir $pathname
142
143 =item $entries = aio_readdir $pathname
144
145 =item ($dirs, $nondirs) = aio_scandir $path, $maxreq
146
147 =item $status = aio_fsync $fh
148
149 =item $status = aio_fdatasync $fh
150
151 =item ... = aio_xxx ...
152
153 Any additional aio requests follow the same scheme: same parameters except
154 you must not specify a callback but instead get the callback arguments as
155 return values.
156
157 =back
158
159 =head1 SEE ALSO
160
161 L<Coro::Socket> and L<Coro::Handle> for non-blocking socket operation.
162
163 =head1 AUTHOR
164
165 Marc Lehmann <schmorp@schmorp.de>
166 http://home.schmorp.de/
167
168 =cut
169
170 1